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"
30 #include "double-int.h"
37 #include "print-tree.h"
38 #include "stringpool.h"
40 #include "trans-mem.h"
43 #include "c-family/c-pragma.h"
46 #include "diagnostic-core.h"
50 #include "plugin-api.h"
51 #include "hard-reg-set.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-objc.h"
59 #include "tree-pretty-print.h"
61 #include "type-utils.h"
63 #include "gomp-constants.h"
68 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
69 and c-lex.c) and the C++ parser. */
71 static cp_token eof_token
=
73 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
76 /* The various kinds of non integral constant we encounter. */
77 typedef enum non_integral_constant
{
79 /* floating-point literal */
83 /* %<__FUNCTION__%> */
85 /* %<__PRETTY_FUNCTION__%> */
93 /* %<typeid%> operator */
95 /* non-constant compound literals */
103 /* an array reference */
109 /* the address of a label */
123 /* calls to overloaded operators */
127 /* a comma operator */
129 /* a call to a constructor */
131 /* a transaction expression */
133 } non_integral_constant
;
135 /* The various kinds of errors about name-lookup failing. */
136 typedef enum name_lookup_error
{
141 /* is not a class or namespace */
143 /* is not a class, namespace, or enumeration */
147 /* The various kinds of required token */
148 typedef enum required_token
{
150 RT_SEMICOLON
, /* ';' */
151 RT_OPEN_PAREN
, /* '(' */
152 RT_CLOSE_BRACE
, /* '}' */
153 RT_OPEN_BRACE
, /* '{' */
154 RT_CLOSE_SQUARE
, /* ']' */
155 RT_OPEN_SQUARE
, /* '[' */
159 RT_GREATER
, /* '>' */
161 RT_ELLIPSIS
, /* '...' */
165 RT_COLON_SCOPE
, /* ':' or '::' */
166 RT_CLOSE_PAREN
, /* ')' */
167 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
168 RT_PRAGMA_EOL
, /* end of line */
169 RT_NAME
, /* identifier */
171 /* The type is CPP_KEYWORD */
173 RT_DELETE
, /* delete */
174 RT_RETURN
, /* return */
175 RT_WHILE
, /* while */
176 RT_EXTERN
, /* extern */
177 RT_STATIC_ASSERT
, /* static_assert */
178 RT_DECLTYPE
, /* decltype */
179 RT_OPERATOR
, /* operator */
180 RT_CLASS
, /* class */
181 RT_TEMPLATE
, /* template */
182 RT_NAMESPACE
, /* namespace */
183 RT_USING
, /* using */
186 RT_CATCH
, /* catch */
187 RT_THROW
, /* throw */
188 RT_LABEL
, /* __label__ */
189 RT_AT_TRY
, /* @try */
190 RT_AT_SYNCHRONIZED
, /* @synchronized */
191 RT_AT_THROW
, /* @throw */
193 RT_SELECT
, /* selection-statement */
194 RT_INTERATION
, /* iteration-statement */
195 RT_JUMP
, /* jump-statement */
196 RT_CLASS_KEY
, /* class-key */
197 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
198 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
199 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
200 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
205 static cp_lexer
*cp_lexer_new_main
207 static cp_lexer
*cp_lexer_new_from_tokens
208 (cp_token_cache
*tokens
);
209 static void cp_lexer_destroy
211 static int cp_lexer_saving_tokens
213 static cp_token
*cp_lexer_token_at
214 (cp_lexer
*, cp_token_position
);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer
*, cp_token
*);
217 static inline cp_token
*cp_lexer_peek_token
219 static cp_token
*cp_lexer_peek_nth_token
220 (cp_lexer
*, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer
*, enum cpp_ttype
);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer
*, enum cpp_ttype
);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer
*, enum rid
);
227 static cp_token
*cp_lexer_consume_token
229 static void cp_lexer_purge_token
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer
*, cp_token_position
);
233 static void cp_lexer_save_tokens
235 static void cp_lexer_commit_tokens
237 static void cp_lexer_rollback_tokens
239 static void cp_lexer_print_token
240 (FILE *, cp_token
*);
241 static inline bool cp_lexer_debugging_p
243 static void cp_lexer_start_debugging
244 (cp_lexer
*) ATTRIBUTE_UNUSED
;
245 static void cp_lexer_stop_debugging
246 (cp_lexer
*) ATTRIBUTE_UNUSED
;
248 static cp_token_cache
*cp_token_cache_new
249 (cp_token
*, cp_token
*);
251 static void cp_parser_initial_pragma
254 static tree cp_literal_operator_id
257 static void cp_parser_cilk_simd
258 (cp_parser
*, cp_token
*);
259 static tree cp_parser_cilk_for
261 static bool cp_parser_omp_declare_reduction_exprs
263 static tree cp_parser_cilk_simd_vectorlength
264 (cp_parser
*, tree
, bool);
266 /* Manifest constants. */
267 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
268 #define CP_SAVED_TOKEN_STACK 5
272 /* The stream to which debugging output should be written. */
273 static FILE *cp_lexer_debug_stream
;
275 /* Nonzero if we are parsing an unevaluated operand: an operand to
276 sizeof, typeof, or alignof. */
277 int cp_unevaluated_operand
;
279 /* Dump up to NUM tokens in BUFFER to FILE starting with token
280 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
281 first token in BUFFER. If NUM is 0, dump all the tokens. If
282 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
283 highlighted by surrounding it in [[ ]]. */
286 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
287 cp_token
*start_token
, unsigned num
,
288 cp_token
*curr_token
)
290 unsigned i
, nprinted
;
294 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
300 num
= buffer
->length ();
302 if (start_token
== NULL
)
303 start_token
= buffer
->address ();
305 if (start_token
> buffer
->address ())
307 cp_lexer_print_token (file
, &(*buffer
)[0]);
308 fprintf (file
, " ... ");
313 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
315 if (token
== start_token
)
322 if (token
== curr_token
)
323 fprintf (file
, "[[");
325 cp_lexer_print_token (file
, token
);
327 if (token
== curr_token
)
328 fprintf (file
, "]]");
334 case CPP_CLOSE_BRACE
:
344 if (i
== num
&& i
< buffer
->length ())
346 fprintf (file
, " ... ");
347 cp_lexer_print_token (file
, &buffer
->last ());
350 fprintf (file
, "\n");
354 /* Dump all tokens in BUFFER to stderr. */
357 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
359 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
363 debug (vec
<cp_token
, va_gc
> &ref
)
365 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
369 debug (vec
<cp_token
, va_gc
> *ptr
)
374 fprintf (stderr
, "<nil>\n");
378 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
379 description for T. */
382 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
386 fprintf (file
, "%s: ", desc
);
387 print_node_brief (file
, "", t
, 0);
392 /* Dump parser context C to FILE. */
395 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
397 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
398 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
399 print_node_brief (file
, "", c
->object_type
, 0);
400 fprintf (file
, "}\n");
404 /* Print the stack of parsing contexts to FILE starting with FIRST. */
407 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
410 cp_parser_context
*c
;
412 fprintf (file
, "Parsing context stack:\n");
413 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
415 fprintf (file
, "\t#%u: ", i
);
416 cp_debug_print_context (file
, c
);
421 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
424 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
427 fprintf (file
, "%s: true\n", desc
);
431 /* Print an unparsed function entry UF to FILE. */
434 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
437 cp_default_arg_entry
*default_arg_fn
;
440 fprintf (file
, "\tFunctions with default args:\n");
442 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
445 fprintf (file
, "\t\tClass type: ");
446 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
447 fprintf (file
, "\t\tDeclaration: ");
448 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
449 fprintf (file
, "\n");
452 fprintf (file
, "\n\tFunctions with definitions that require "
453 "post-processing\n\t\t");
454 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
456 print_node_brief (file
, "", fn
, 0);
459 fprintf (file
, "\n");
461 fprintf (file
, "\n\tNon-static data members with initializers that require "
462 "post-processing\n\t\t");
463 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
465 print_node_brief (file
, "", fn
, 0);
468 fprintf (file
, "\n");
472 /* Print the stack of unparsed member functions S to FILE. */
475 cp_debug_print_unparsed_queues (FILE *file
,
476 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
479 cp_unparsed_functions_entry
*uf
;
481 fprintf (file
, "Unparsed functions\n");
482 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
484 fprintf (file
, "#%u:\n", i
);
485 cp_debug_print_unparsed_function (file
, uf
);
490 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
491 the given PARSER. If FILE is NULL, the output is printed on stderr. */
494 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
496 cp_token
*next_token
, *first_token
, *start_token
;
501 next_token
= parser
->lexer
->next_token
;
502 first_token
= parser
->lexer
->buffer
->address ();
503 start_token
= (next_token
> first_token
+ window_size
/ 2)
504 ? next_token
- window_size
/ 2
506 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
511 /* Dump debugging information for the given PARSER. If FILE is NULL,
512 the output is printed on stderr. */
515 cp_debug_parser (FILE *file
, cp_parser
*parser
)
517 const size_t window_size
= 20;
519 expanded_location eloc
;
524 fprintf (file
, "Parser state\n\n");
525 fprintf (file
, "Number of tokens: %u\n",
526 vec_safe_length (parser
->lexer
->buffer
));
527 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
528 cp_debug_print_tree_if_set (file
, "Object scope",
529 parser
->object_scope
);
530 cp_debug_print_tree_if_set (file
, "Qualifying scope",
531 parser
->qualifying_scope
);
532 cp_debug_print_context_stack (file
, parser
->context
);
533 cp_debug_print_flag (file
, "Allow GNU extensions",
534 parser
->allow_gnu_extensions_p
);
535 cp_debug_print_flag (file
, "'>' token is greater-than",
536 parser
->greater_than_is_operator_p
);
537 cp_debug_print_flag (file
, "Default args allowed in current "
538 "parameter list", parser
->default_arg_ok_p
);
539 cp_debug_print_flag (file
, "Parsing integral constant-expression",
540 parser
->integral_constant_expression_p
);
541 cp_debug_print_flag (file
, "Allow non-constant expression in current "
542 "constant-expression",
543 parser
->allow_non_integral_constant_expression_p
);
544 cp_debug_print_flag (file
, "Seen non-constant expression",
545 parser
->non_integral_constant_expression_p
);
546 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
548 parser
->local_variables_forbidden_p
);
549 cp_debug_print_flag (file
, "In unbraced linkage specification",
550 parser
->in_unbraced_linkage_specification_p
);
551 cp_debug_print_flag (file
, "Parsing a declarator",
552 parser
->in_declarator_p
);
553 cp_debug_print_flag (file
, "In template argument list",
554 parser
->in_template_argument_list_p
);
555 cp_debug_print_flag (file
, "Parsing an iteration statement",
556 parser
->in_statement
& IN_ITERATION_STMT
);
557 cp_debug_print_flag (file
, "Parsing a switch statement",
558 parser
->in_statement
& IN_SWITCH_STMT
);
559 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
560 parser
->in_statement
& IN_OMP_BLOCK
);
561 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
562 parser
->in_statement
& IN_CILK_SIMD_FOR
);
563 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
564 parser
->in_statement
& IN_OMP_FOR
);
565 cp_debug_print_flag (file
, "Parsing an if statement",
566 parser
->in_statement
& IN_IF_STMT
);
567 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
568 "context", parser
->in_type_id_in_expr_p
);
569 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
570 parser
->implicit_extern_c
);
571 cp_debug_print_flag (file
, "String expressions should be translated "
572 "to execution character set",
573 parser
->translate_strings_p
);
574 cp_debug_print_flag (file
, "Parsing function body outside of a "
575 "local class", parser
->in_function_body
);
576 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
577 parser
->colon_corrects_to_scope_p
);
578 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
579 parser
->colon_doesnt_start_class_def_p
);
580 if (parser
->type_definition_forbidden_message
)
581 fprintf (file
, "Error message for forbidden type definitions: %s\n",
582 parser
->type_definition_forbidden_message
);
583 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
584 fprintf (file
, "Number of class definitions in progress: %u\n",
585 parser
->num_classes_being_defined
);
586 fprintf (file
, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser
->num_template_parameter_lists
);
588 cp_debug_parser_tokens (file
, parser
, window_size
);
589 token
= parser
->lexer
->next_token
;
590 fprintf (file
, "Next token to parse:\n");
591 fprintf (file
, "\tToken: ");
592 cp_lexer_print_token (file
, token
);
593 eloc
= expand_location (token
->location
);
594 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
595 fprintf (file
, "\tLine: %d\n", eloc
.line
);
596 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
600 debug (cp_parser
&ref
)
602 cp_debug_parser (stderr
, &ref
);
606 debug (cp_parser
*ptr
)
611 fprintf (stderr
, "<nil>\n");
614 /* Allocate memory for a new lexer object and return it. */
617 cp_lexer_alloc (void)
621 c_common_no_more_pch ();
623 /* Allocate the memory. */
624 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
626 /* Initially we are not debugging. */
627 lexer
->debugging_p
= false;
629 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
631 /* Create the buffer. */
632 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
642 cp_lexer_new_main (void)
647 /* It's possible that parsing the first pragma will load a PCH file,
648 which is a GC collection point. So we have to do that before
649 allocating any memory. */
650 cp_parser_initial_pragma (&token
);
652 lexer
= cp_lexer_alloc ();
654 /* Put the first token in the buffer. */
655 lexer
->buffer
->quick_push (token
);
657 /* Get the remaining tokens from the preprocessor. */
658 while (token
.type
!= CPP_EOF
)
660 cp_lexer_get_preprocessor_token (lexer
, &token
);
661 vec_safe_push (lexer
->buffer
, token
);
664 lexer
->last_token
= lexer
->buffer
->address ()
665 + lexer
->buffer
->length ()
667 lexer
->next_token
= lexer
->buffer
->length ()
668 ? lexer
->buffer
->address ()
671 /* Subsequent preprocessor diagnostics should use compiler
672 diagnostic functions to get the compiler source location. */
675 gcc_assert (!lexer
->next_token
->purged_p
);
679 /* Create a new lexer whose token stream is primed with the tokens in
680 CACHE. When these tokens are exhausted, no new tokens will be read. */
683 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
685 cp_token
*first
= cache
->first
;
686 cp_token
*last
= cache
->last
;
687 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
689 /* We do not own the buffer. */
690 lexer
->buffer
= NULL
;
691 lexer
->next_token
= first
== last
? &eof_token
: first
;
692 lexer
->last_token
= last
;
694 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
696 /* Initially we are not debugging. */
697 lexer
->debugging_p
= false;
699 gcc_assert (!lexer
->next_token
->purged_p
);
703 /* Frees all resources associated with LEXER. */
706 cp_lexer_destroy (cp_lexer
*lexer
)
708 vec_free (lexer
->buffer
);
709 lexer
->saved_tokens
.release ();
713 /* Returns nonzero if debugging information should be output. */
716 cp_lexer_debugging_p (cp_lexer
*lexer
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 return cp_lexer_token_at (lexer
, tp
);
759 /* nonzero if we are presently saving tokens. */
762 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
764 return lexer
->saved_tokens
.length () != 0;
767 /* Store the next token from the preprocessor in *TOKEN. Return true
768 if we reach EOF. If LEXER is NULL, assume we are handling an
769 initial #pragma pch_preprocess, and thus want the lexer to return
770 processed strings. */
773 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
775 static int is_extern_c
= 0;
777 /* Get a new token from the preprocessor. */
779 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
780 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
781 token
->keyword
= RID_MAX
;
782 token
->pragma_kind
= PRAGMA_NONE
;
783 token
->purged_p
= false;
784 token
->error_reported
= false;
786 /* On some systems, some header files are surrounded by an
787 implicit extern "C" block. Set a flag in the token if it
788 comes from such a header. */
789 is_extern_c
+= pending_lang_change
;
790 pending_lang_change
= 0;
791 token
->implicit_extern_c
= is_extern_c
> 0;
793 /* Check to see if this token is a keyword. */
794 if (token
->type
== CPP_NAME
)
796 if (C_IS_RESERVED_WORD (token
->u
.value
))
798 /* Mark this token as a keyword. */
799 token
->type
= CPP_KEYWORD
;
800 /* Record which keyword. */
801 token
->keyword
= C_RID_CODE (token
->u
.value
);
805 if (warn_cxx0x_compat
806 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
807 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
809 /* Warn about the C++0x keyword (but still treat it as
811 warning (OPT_Wc__0x_compat
,
812 "identifier %qE is a keyword in C++11",
815 /* Clear out the C_RID_CODE so we don't warn about this
816 particular identifier-turned-keyword again. */
817 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
820 token
->keyword
= RID_MAX
;
823 else if (token
->type
== CPP_AT_NAME
)
825 /* This only happens in Objective-C++; it must be a keyword. */
826 token
->type
= CPP_KEYWORD
;
827 switch (C_RID_CODE (token
->u
.value
))
829 /* Replace 'class' with '@class', 'private' with '@private',
830 etc. This prevents confusion with the C++ keyword
831 'class', and makes the tokens consistent with other
832 Objective-C 'AT' keywords. For example '@class' is
833 reported as RID_AT_CLASS which is consistent with
834 '@synchronized', which is reported as
837 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
838 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
839 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
840 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
841 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
842 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
843 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
844 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
847 else if (token
->type
== CPP_PRAGMA
)
849 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
850 token
->pragma_kind
= ((enum pragma_kind
)
851 TREE_INT_CST_LOW (token
->u
.value
));
852 token
->u
.value
= NULL_TREE
;
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if the next token is a keyword for a decl-specifier. */
936 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
940 token
= cp_lexer_peek_token (lexer
);
941 switch (token
->keyword
)
943 /* auto specifier: storage-class-specifier in C++,
944 simple-type-specifier in C++0x. */
946 /* Storage classes. */
952 /* Elaborated type specifiers. */
958 /* Simple type specifiers. */
972 /* GNU extensions. */
975 /* C++0x extensions. */
977 case RID_UNDERLYING_TYPE
:
981 if (token
->keyword
>= RID_FIRST_INT_N
982 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
983 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
989 /* Returns TRUE iff the token T begins a decltype type. */
992 token_is_decltype (cp_token
*t
)
994 return (t
->keyword
== RID_DECLTYPE
995 || t
->type
== CPP_DECLTYPE
);
998 /* Returns TRUE iff the next token begins a decltype type. */
1001 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1003 cp_token
*t
= cp_lexer_peek_token (lexer
);
1004 return token_is_decltype (t
);
1007 /* Return a pointer to the Nth token in the token stream. If N is 1,
1008 then this is precisely equivalent to cp_lexer_peek_token (except
1009 that it is not inline). One would like to disallow that case, but
1010 there is one case (cp_parser_nth_token_starts_template_id) where
1011 the caller passes a variable for N and it might be 1. */
1014 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1018 /* N is 1-based, not zero-based. */
1021 if (cp_lexer_debugging_p (lexer
))
1022 fprintf (cp_lexer_debug_stream
,
1023 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1026 token
= lexer
->next_token
;
1027 gcc_assert (!n
|| token
!= &eof_token
);
1031 if (token
== lexer
->last_token
)
1037 if (!token
->purged_p
)
1041 if (cp_lexer_debugging_p (lexer
))
1043 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1044 putc ('\n', cp_lexer_debug_stream
);
1050 /* Return the next token, and advance the lexer's next_token pointer
1051 to point to the next non-purged token. */
1054 cp_lexer_consume_token (cp_lexer
* lexer
)
1056 cp_token
*token
= lexer
->next_token
;
1058 gcc_assert (token
!= &eof_token
);
1059 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1063 lexer
->next_token
++;
1064 if (lexer
->next_token
== lexer
->last_token
)
1066 lexer
->next_token
= &eof_token
;
1071 while (lexer
->next_token
->purged_p
);
1073 cp_lexer_set_source_position_from_token (token
);
1075 /* Provide debugging output. */
1076 if (cp_lexer_debugging_p (lexer
))
1078 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1079 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1080 putc ('\n', cp_lexer_debug_stream
);
1086 /* Permanently remove the next token from the token stream, and
1087 advance the next_token pointer to refer to the next non-purged
1091 cp_lexer_purge_token (cp_lexer
*lexer
)
1093 cp_token
*tok
= lexer
->next_token
;
1095 gcc_assert (tok
!= &eof_token
);
1096 tok
->purged_p
= true;
1097 tok
->location
= UNKNOWN_LOCATION
;
1098 tok
->u
.value
= NULL_TREE
;
1099 tok
->keyword
= RID_MAX
;
1104 if (tok
== lexer
->last_token
)
1110 while (tok
->purged_p
);
1111 lexer
->next_token
= tok
;
1114 /* Permanently remove all tokens after TOK, up to, but not
1115 including, the token that will be returned next by
1116 cp_lexer_peek_token. */
1119 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1121 cp_token
*peek
= lexer
->next_token
;
1123 if (peek
== &eof_token
)
1124 peek
= lexer
->last_token
;
1126 gcc_assert (tok
< peek
);
1128 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1130 tok
->purged_p
= true;
1131 tok
->location
= UNKNOWN_LOCATION
;
1132 tok
->u
.value
= NULL_TREE
;
1133 tok
->keyword
= RID_MAX
;
1137 /* Begin saving tokens. All tokens consumed after this point will be
1141 cp_lexer_save_tokens (cp_lexer
* lexer
)
1143 /* Provide debugging output. */
1144 if (cp_lexer_debugging_p (lexer
))
1145 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1147 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1150 /* Commit to the portion of the token stream most recently saved. */
1153 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1155 /* Provide debugging output. */
1156 if (cp_lexer_debugging_p (lexer
))
1157 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1159 lexer
->saved_tokens
.pop ();
1162 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1163 to the token stream. Stop saving tokens. */
1166 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1168 /* Provide debugging output. */
1169 if (cp_lexer_debugging_p (lexer
))
1170 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1172 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1175 /* RAII wrapper around the above functions, with sanity checking. Creating
1176 a variable saves tokens, which are committed when the variable is
1177 destroyed unless they are explicitly rolled back by calling the rollback
1180 struct saved_token_sentinel
1185 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1187 len
= lexer
->saved_tokens
.length ();
1188 cp_lexer_save_tokens (lexer
);
1192 cp_lexer_rollback_tokens (lexer
);
1195 ~saved_token_sentinel()
1198 cp_lexer_commit_tokens (lexer
);
1199 gcc_assert (lexer
->saved_tokens
.length () == len
);
1203 /* Print a representation of the TOKEN on the STREAM. */
1206 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1208 /* We don't use cpp_type2name here because the parser defines
1209 a few tokens of its own. */
1210 static const char *const token_names
[] = {
1211 /* cpplib-defined token types */
1212 #define OP(e, s) #e,
1213 #define TK(e, s) #e,
1217 /* C++ parser token types - see "Manifest constants", above. */
1220 "NESTED_NAME_SPECIFIER",
1223 /* For some tokens, print the associated data. */
1224 switch (token
->type
)
1227 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1228 For example, `struct' is mapped to an INTEGER_CST. */
1229 if (!identifier_p (token
->u
.value
))
1231 /* else fall through */
1233 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1240 case CPP_UTF8STRING
:
1241 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1245 print_generic_expr (stream
, token
->u
.value
, 0);
1249 /* If we have a name for the token, print it out. Otherwise, we
1250 simply give the numeric code. */
1251 if (token
->type
< ARRAY_SIZE(token_names
))
1252 fputs (token_names
[token
->type
], stream
);
1254 fprintf (stream
, "[%d]", token
->type
);
1260 debug (cp_token
&ref
)
1262 cp_lexer_print_token (stderr
, &ref
);
1263 fprintf (stderr
, "\n");
1267 debug (cp_token
*ptr
)
1272 fprintf (stderr
, "<nil>\n");
1276 /* Start emitting debugging information. */
1279 cp_lexer_start_debugging (cp_lexer
* lexer
)
1281 lexer
->debugging_p
= true;
1282 cp_lexer_debug_stream
= stderr
;
1285 /* Stop emitting debugging information. */
1288 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1290 lexer
->debugging_p
= false;
1291 cp_lexer_debug_stream
= NULL
;
1294 /* Create a new cp_token_cache, representing a range of tokens. */
1296 static cp_token_cache
*
1297 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1299 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1300 cache
->first
= first
;
1305 /* Diagnose if #pragma omp declare simd isn't followed immediately
1306 by function declaration or definition. */
1309 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1311 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1313 error ("%<#pragma omp declare simd%> not immediately followed by "
1314 "function declaration or definition");
1315 parser
->omp_declare_simd
= NULL
;
1319 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1320 and put that into "omp declare simd" attribute. */
1323 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1325 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1327 if (fndecl
== error_mark_node
)
1329 parser
->omp_declare_simd
= NULL
;
1332 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1334 cp_ensure_no_omp_declare_simd (parser
);
1340 /* Decl-specifiers. */
1342 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1345 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1347 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1352 /* Nothing other than the parser should be creating declarators;
1353 declarators are a semi-syntactic representation of C++ entities.
1354 Other parts of the front end that need to create entities (like
1355 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1357 static cp_declarator
*make_call_declarator
1358 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1359 static cp_declarator
*make_array_declarator
1360 (cp_declarator
*, tree
);
1361 static cp_declarator
*make_pointer_declarator
1362 (cp_cv_quals
, cp_declarator
*, tree
);
1363 static cp_declarator
*make_reference_declarator
1364 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1365 static cp_parameter_declarator
*make_parameter_declarator
1366 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1367 static cp_declarator
*make_ptrmem_declarator
1368 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1370 /* An erroneous declarator. */
1371 static cp_declarator
*cp_error_declarator
;
1373 /* The obstack on which declarators and related data structures are
1375 static struct obstack declarator_obstack
;
1377 /* Alloc BYTES from the declarator memory pool. */
1379 static inline void *
1380 alloc_declarator (size_t bytes
)
1382 return obstack_alloc (&declarator_obstack
, bytes
);
1385 /* Allocate a declarator of the indicated KIND. Clear fields that are
1386 common to all declarators. */
1388 static cp_declarator
*
1389 make_declarator (cp_declarator_kind kind
)
1391 cp_declarator
*declarator
;
1393 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1394 declarator
->kind
= kind
;
1395 declarator
->attributes
= NULL_TREE
;
1396 declarator
->std_attributes
= NULL_TREE
;
1397 declarator
->declarator
= NULL
;
1398 declarator
->parameter_pack_p
= false;
1399 declarator
->id_loc
= UNKNOWN_LOCATION
;
1404 /* Make a declarator for a generalized identifier. If
1405 QUALIFYING_SCOPE is non-NULL, the identifier is
1406 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1407 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1410 static cp_declarator
*
1411 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1412 special_function_kind sfk
)
1414 cp_declarator
*declarator
;
1416 /* It is valid to write:
1418 class C { void f(); };
1422 The standard is not clear about whether `typedef const C D' is
1423 legal; as of 2002-09-15 the committee is considering that
1424 question. EDG 3.0 allows that syntax. Therefore, we do as
1426 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1427 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1429 gcc_assert (identifier_p (unqualified_name
)
1430 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1431 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1433 declarator
= make_declarator (cdk_id
);
1434 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1435 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1436 declarator
->u
.id
.sfk
= sfk
;
1441 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1442 of modifiers such as const or volatile to apply to the pointer
1443 type, represented as identifiers. ATTRIBUTES represent the attributes that
1444 appertain to the pointer or reference. */
1447 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1450 cp_declarator
*declarator
;
1452 declarator
= make_declarator (cdk_pointer
);
1453 declarator
->declarator
= target
;
1454 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1455 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1458 declarator
->id_loc
= target
->id_loc
;
1459 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1460 target
->parameter_pack_p
= false;
1463 declarator
->parameter_pack_p
= false;
1465 declarator
->std_attributes
= attributes
;
1470 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1471 represent the attributes that appertain to the pointer or
1475 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1476 bool rvalue_ref
, tree attributes
)
1478 cp_declarator
*declarator
;
1480 declarator
= make_declarator (cdk_reference
);
1481 declarator
->declarator
= target
;
1482 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1483 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1486 declarator
->id_loc
= target
->id_loc
;
1487 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1488 target
->parameter_pack_p
= false;
1491 declarator
->parameter_pack_p
= false;
1493 declarator
->std_attributes
= attributes
;
1498 /* Like make_pointer_declarator -- but for a pointer to a non-static
1499 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1500 appertain to the pointer or reference. */
1503 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1504 cp_declarator
*pointee
,
1507 cp_declarator
*declarator
;
1509 declarator
= make_declarator (cdk_ptrmem
);
1510 declarator
->declarator
= pointee
;
1511 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1512 declarator
->u
.pointer
.class_type
= class_type
;
1516 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1517 pointee
->parameter_pack_p
= false;
1520 declarator
->parameter_pack_p
= false;
1522 declarator
->std_attributes
= attributes
;
1527 /* Make a declarator for the function given by TARGET, with the
1528 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1529 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1530 indicates what exceptions can be thrown. */
1533 make_call_declarator (cp_declarator
*target
,
1535 cp_cv_quals cv_qualifiers
,
1536 cp_virt_specifiers virt_specifiers
,
1537 cp_ref_qualifier ref_qualifier
,
1538 tree exception_specification
,
1539 tree late_return_type
)
1541 cp_declarator
*declarator
;
1543 declarator
= make_declarator (cdk_function
);
1544 declarator
->declarator
= target
;
1545 declarator
->u
.function
.parameters
= parms
;
1546 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1547 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1548 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1549 declarator
->u
.function
.exception_specification
= exception_specification
;
1550 declarator
->u
.function
.late_return_type
= late_return_type
;
1553 declarator
->id_loc
= target
->id_loc
;
1554 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1555 target
->parameter_pack_p
= false;
1558 declarator
->parameter_pack_p
= false;
1563 /* Make a declarator for an array of BOUNDS elements, each of which is
1564 defined by ELEMENT. */
1567 make_array_declarator (cp_declarator
*element
, tree bounds
)
1569 cp_declarator
*declarator
;
1571 declarator
= make_declarator (cdk_array
);
1572 declarator
->declarator
= element
;
1573 declarator
->u
.array
.bounds
= bounds
;
1576 declarator
->id_loc
= element
->id_loc
;
1577 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1578 element
->parameter_pack_p
= false;
1581 declarator
->parameter_pack_p
= false;
1586 /* Determine whether the declarator we've seen so far can be a
1587 parameter pack, when followed by an ellipsis. */
1589 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1591 /* Search for a declarator name, or any other declarator that goes
1592 after the point where the ellipsis could appear in a parameter
1593 pack. If we find any of these, then this declarator can not be
1594 made into a parameter pack. */
1596 while (declarator
&& !found
)
1598 switch ((int)declarator
->kind
)
1609 declarator
= declarator
->declarator
;
1617 cp_parameter_declarator
*no_parameters
;
1619 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1620 DECLARATOR and DEFAULT_ARGUMENT. */
1622 cp_parameter_declarator
*
1623 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1624 cp_declarator
*declarator
,
1625 tree default_argument
)
1627 cp_parameter_declarator
*parameter
;
1629 parameter
= ((cp_parameter_declarator
*)
1630 alloc_declarator (sizeof (cp_parameter_declarator
)));
1631 parameter
->next
= NULL
;
1632 if (decl_specifiers
)
1633 parameter
->decl_specifiers
= *decl_specifiers
;
1635 clear_decl_specs (¶meter
->decl_specifiers
);
1636 parameter
->declarator
= declarator
;
1637 parameter
->default_argument
= default_argument
;
1638 parameter
->ellipsis_p
= false;
1643 /* Returns true iff DECLARATOR is a declaration for a function. */
1646 function_declarator_p (const cp_declarator
*declarator
)
1650 if (declarator
->kind
== cdk_function
1651 && declarator
->declarator
->kind
== cdk_id
)
1653 if (declarator
->kind
== cdk_id
1654 || declarator
->kind
== cdk_error
)
1656 declarator
= declarator
->declarator
;
1666 A cp_parser parses the token stream as specified by the C++
1667 grammar. Its job is purely parsing, not semantic analysis. For
1668 example, the parser breaks the token stream into declarators,
1669 expressions, statements, and other similar syntactic constructs.
1670 It does not check that the types of the expressions on either side
1671 of an assignment-statement are compatible, or that a function is
1672 not declared with a parameter of type `void'.
1674 The parser invokes routines elsewhere in the compiler to perform
1675 semantic analysis and to build up the abstract syntax tree for the
1678 The parser (and the template instantiation code, which is, in a
1679 way, a close relative of parsing) are the only parts of the
1680 compiler that should be calling push_scope and pop_scope, or
1681 related functions. The parser (and template instantiation code)
1682 keeps track of what scope is presently active; everything else
1683 should simply honor that. (The code that generates static
1684 initializers may also need to set the scope, in order to check
1685 access control correctly when emitting the initializers.)
1690 The parser is of the standard recursive-descent variety. Upcoming
1691 tokens in the token stream are examined in order to determine which
1692 production to use when parsing a non-terminal. Some C++ constructs
1693 require arbitrary look ahead to disambiguate. For example, it is
1694 impossible, in the general case, to tell whether a statement is an
1695 expression or declaration without scanning the entire statement.
1696 Therefore, the parser is capable of "parsing tentatively." When the
1697 parser is not sure what construct comes next, it enters this mode.
1698 Then, while we attempt to parse the construct, the parser queues up
1699 error messages, rather than issuing them immediately, and saves the
1700 tokens it consumes. If the construct is parsed successfully, the
1701 parser "commits", i.e., it issues any queued error messages and
1702 the tokens that were being preserved are permanently discarded.
1703 If, however, the construct is not parsed successfully, the parser
1704 rolls back its state completely so that it can resume parsing using
1705 a different alternative.
1710 The performance of the parser could probably be improved substantially.
1711 We could often eliminate the need to parse tentatively by looking ahead
1712 a little bit. In some places, this approach might not entirely eliminate
1713 the need to parse tentatively, but it might still speed up the average
1716 /* Flags that are passed to some parsing functions. These values can
1717 be bitwise-ored together. */
1722 CP_PARSER_FLAGS_NONE
= 0x0,
1723 /* The construct is optional. If it is not present, then no error
1724 should be issued. */
1725 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1726 /* When parsing a type-specifier, treat user-defined type-names
1727 as non-type identifiers. */
1728 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1729 /* When parsing a type-specifier, do not try to parse a class-specifier
1730 or enum-specifier. */
1731 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1732 /* When parsing a decl-specifier-seq, only allow type-specifier or
1734 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1737 /* This type is used for parameters and variables which hold
1738 combinations of the above flags. */
1739 typedef int cp_parser_flags
;
1741 /* The different kinds of declarators we want to parse. */
1743 typedef enum cp_parser_declarator_kind
1745 /* We want an abstract declarator. */
1746 CP_PARSER_DECLARATOR_ABSTRACT
,
1747 /* We want a named declarator. */
1748 CP_PARSER_DECLARATOR_NAMED
,
1749 /* We don't mind, but the name must be an unqualified-id. */
1750 CP_PARSER_DECLARATOR_EITHER
1751 } cp_parser_declarator_kind
;
1753 /* The precedence values used to parse binary expressions. The minimum value
1754 of PREC must be 1, because zero is reserved to quickly discriminate
1755 binary operators from other tokens. */
1760 PREC_LOGICAL_OR_EXPRESSION
,
1761 PREC_LOGICAL_AND_EXPRESSION
,
1762 PREC_INCLUSIVE_OR_EXPRESSION
,
1763 PREC_EXCLUSIVE_OR_EXPRESSION
,
1764 PREC_AND_EXPRESSION
,
1765 PREC_EQUALITY_EXPRESSION
,
1766 PREC_RELATIONAL_EXPRESSION
,
1767 PREC_SHIFT_EXPRESSION
,
1768 PREC_ADDITIVE_EXPRESSION
,
1769 PREC_MULTIPLICATIVE_EXPRESSION
,
1771 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1774 /* A mapping from a token type to a corresponding tree node type, with a
1775 precedence value. */
1777 typedef struct cp_parser_binary_operations_map_node
1779 /* The token type. */
1780 enum cpp_ttype token_type
;
1781 /* The corresponding tree code. */
1782 enum tree_code tree_type
;
1783 /* The precedence of this operator. */
1784 enum cp_parser_prec prec
;
1785 } cp_parser_binary_operations_map_node
;
1787 typedef struct cp_parser_expression_stack_entry
1789 /* Left hand side of the binary operation we are currently
1792 /* Original tree code for left hand side, if it was a binary
1793 expression itself (used for -Wparentheses). */
1794 enum tree_code lhs_type
;
1795 /* Tree code for the binary operation we are parsing. */
1796 enum tree_code tree_type
;
1797 /* Precedence of the binary operation we are parsing. */
1798 enum cp_parser_prec prec
;
1799 /* Location of the binary operation we are parsing. */
1801 } cp_parser_expression_stack_entry
;
1803 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1804 entries because precedence levels on the stack are monotonically
1806 typedef struct cp_parser_expression_stack_entry
1807 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1811 /* Constructors and destructors. */
1813 static cp_parser_context
*cp_parser_context_new
1814 (cp_parser_context
*);
1816 /* Class variables. */
1818 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1820 /* The operator-precedence table used by cp_parser_binary_expression.
1821 Transformed into an associative array (binops_by_token) by
1824 static const cp_parser_binary_operations_map_node binops
[] = {
1825 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1826 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1828 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1829 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1830 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1832 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1833 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1835 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1836 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1838 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1839 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1840 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1841 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1843 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1844 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1846 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1848 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1850 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1852 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1854 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1857 /* The same as binops, but initialized by cp_parser_new so that
1858 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1860 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1862 /* Constructors and destructors. */
1864 /* Construct a new context. The context below this one on the stack
1865 is given by NEXT. */
1867 static cp_parser_context
*
1868 cp_parser_context_new (cp_parser_context
* next
)
1870 cp_parser_context
*context
;
1872 /* Allocate the storage. */
1873 if (cp_parser_context_free_list
!= NULL
)
1875 /* Pull the first entry from the free list. */
1876 context
= cp_parser_context_free_list
;
1877 cp_parser_context_free_list
= context
->next
;
1878 memset (context
, 0, sizeof (*context
));
1881 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1883 /* No errors have occurred yet in this context. */
1884 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1885 /* If this is not the bottommost context, copy information that we
1886 need from the previous context. */
1889 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1890 expression, then we are parsing one in this context, too. */
1891 context
->object_type
= next
->object_type
;
1892 /* Thread the stack. */
1893 context
->next
= next
;
1899 /* Managing the unparsed function queues. */
1901 #define unparsed_funs_with_default_args \
1902 parser->unparsed_queues->last ().funs_with_default_args
1903 #define unparsed_funs_with_definitions \
1904 parser->unparsed_queues->last ().funs_with_definitions
1905 #define unparsed_nsdmis \
1906 parser->unparsed_queues->last ().nsdmis
1907 #define unparsed_classes \
1908 parser->unparsed_queues->last ().classes
1911 push_unparsed_function_queues (cp_parser
*parser
)
1913 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1914 vec_safe_push (parser
->unparsed_queues
, e
);
1918 pop_unparsed_function_queues (cp_parser
*parser
)
1920 release_tree_vector (unparsed_funs_with_definitions
);
1921 parser
->unparsed_queues
->pop ();
1926 /* Constructors and destructors. */
1928 static cp_parser
*cp_parser_new
1931 /* Routines to parse various constructs.
1933 Those that return `tree' will return the error_mark_node (rather
1934 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1935 Sometimes, they will return an ordinary node if error-recovery was
1936 attempted, even though a parse error occurred. So, to check
1937 whether or not a parse error occurred, you should always use
1938 cp_parser_error_occurred. If the construct is optional (indicated
1939 either by an `_opt' in the name of the function that does the
1940 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1941 the construct is not present. */
1943 /* Lexical conventions [gram.lex] */
1945 static tree cp_parser_identifier
1947 static tree cp_parser_string_literal
1948 (cp_parser
*, bool, bool, bool);
1949 static tree cp_parser_userdef_char_literal
1951 static tree cp_parser_userdef_string_literal
1953 static tree cp_parser_userdef_numeric_literal
1956 /* Basic concepts [gram.basic] */
1958 static bool cp_parser_translation_unit
1961 /* Expressions [gram.expr] */
1963 static tree cp_parser_primary_expression
1964 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1965 static tree cp_parser_id_expression
1966 (cp_parser
*, bool, bool, bool *, bool, bool);
1967 static tree cp_parser_unqualified_id
1968 (cp_parser
*, bool, bool, bool, bool);
1969 static tree cp_parser_nested_name_specifier_opt
1970 (cp_parser
*, bool, bool, bool, bool);
1971 static tree cp_parser_nested_name_specifier
1972 (cp_parser
*, bool, bool, bool, bool);
1973 static tree cp_parser_qualifying_entity
1974 (cp_parser
*, bool, bool, bool, bool, bool);
1975 static tree cp_parser_postfix_expression
1976 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1977 static tree cp_parser_postfix_open_square_expression
1978 (cp_parser
*, tree
, bool, bool);
1979 static tree cp_parser_postfix_dot_deref_expression
1980 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1981 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1982 (cp_parser
*, int, bool, bool, bool *, bool = false);
1983 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1984 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1985 static void cp_parser_pseudo_destructor_name
1986 (cp_parser
*, tree
, tree
*, tree
*);
1987 static tree cp_parser_unary_expression
1988 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
1989 static enum tree_code cp_parser_unary_operator
1991 static tree cp_parser_new_expression
1993 static vec
<tree
, va_gc
> *cp_parser_new_placement
1995 static tree cp_parser_new_type_id
1996 (cp_parser
*, tree
*);
1997 static cp_declarator
*cp_parser_new_declarator_opt
1999 static cp_declarator
*cp_parser_direct_new_declarator
2001 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2003 static tree cp_parser_delete_expression
2005 static tree cp_parser_cast_expression
2006 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2007 static tree cp_parser_binary_expression
2008 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2009 static tree cp_parser_question_colon_clause
2010 (cp_parser
*, tree
);
2011 static tree cp_parser_assignment_expression
2012 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2013 static enum tree_code cp_parser_assignment_operator_opt
2015 static tree cp_parser_expression
2016 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2017 static tree cp_parser_constant_expression
2018 (cp_parser
*, bool = false, bool * = NULL
);
2019 static tree cp_parser_builtin_offsetof
2021 static tree cp_parser_lambda_expression
2023 static void cp_parser_lambda_introducer
2024 (cp_parser
*, tree
);
2025 static bool cp_parser_lambda_declarator_opt
2026 (cp_parser
*, tree
);
2027 static void cp_parser_lambda_body
2028 (cp_parser
*, tree
);
2030 /* Statements [gram.stmt.stmt] */
2032 static void cp_parser_statement
2033 (cp_parser
*, tree
, bool, bool *);
2034 static void cp_parser_label_for_labeled_statement
2035 (cp_parser
*, tree
);
2036 static tree cp_parser_expression_statement
2037 (cp_parser
*, tree
);
2038 static tree cp_parser_compound_statement
2039 (cp_parser
*, tree
, bool, bool);
2040 static void cp_parser_statement_seq_opt
2041 (cp_parser
*, tree
);
2042 static tree cp_parser_selection_statement
2043 (cp_parser
*, bool *);
2044 static tree cp_parser_condition
2046 static tree cp_parser_iteration_statement
2047 (cp_parser
*, bool);
2048 static bool cp_parser_for_init_statement
2049 (cp_parser
*, tree
*decl
);
2050 static tree cp_parser_for
2051 (cp_parser
*, bool);
2052 static tree cp_parser_c_for
2053 (cp_parser
*, tree
, tree
, bool);
2054 static tree cp_parser_range_for
2055 (cp_parser
*, tree
, tree
, tree
, bool);
2056 static void do_range_for_auto_deduction
2058 static tree cp_parser_perform_range_for_lookup
2059 (tree
, tree
*, tree
*);
2060 static tree cp_parser_range_for_member_function
2062 static tree cp_parser_jump_statement
2064 static void cp_parser_declaration_statement
2067 static tree cp_parser_implicitly_scoped_statement
2068 (cp_parser
*, bool *);
2069 static void cp_parser_already_scoped_statement
2072 /* Declarations [gram.dcl.dcl] */
2074 static void cp_parser_declaration_seq_opt
2076 static void cp_parser_declaration
2078 static void cp_parser_block_declaration
2079 (cp_parser
*, bool);
2080 static void cp_parser_simple_declaration
2081 (cp_parser
*, bool, tree
*);
2082 static void cp_parser_decl_specifier_seq
2083 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2084 static tree cp_parser_storage_class_specifier_opt
2086 static tree cp_parser_function_specifier_opt
2087 (cp_parser
*, cp_decl_specifier_seq
*);
2088 static tree cp_parser_type_specifier
2089 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2091 static tree cp_parser_simple_type_specifier
2092 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2093 static tree cp_parser_type_name
2095 static tree cp_parser_nonclass_name
2096 (cp_parser
* parser
);
2097 static tree cp_parser_elaborated_type_specifier
2098 (cp_parser
*, bool, bool);
2099 static tree cp_parser_enum_specifier
2101 static void cp_parser_enumerator_list
2102 (cp_parser
*, tree
);
2103 static void cp_parser_enumerator_definition
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_namespace_name
2107 static void cp_parser_namespace_definition
2109 static void cp_parser_namespace_body
2111 static tree cp_parser_qualified_namespace_specifier
2113 static void cp_parser_namespace_alias_definition
2115 static bool cp_parser_using_declaration
2116 (cp_parser
*, bool);
2117 static void cp_parser_using_directive
2119 static tree cp_parser_alias_declaration
2121 static void cp_parser_asm_definition
2123 static void cp_parser_linkage_specification
2125 static void cp_parser_static_assert
2126 (cp_parser
*, bool);
2127 static tree cp_parser_decltype
2130 /* Declarators [gram.dcl.decl] */
2132 static tree cp_parser_init_declarator
2133 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2134 bool, bool, int, bool *, tree
*, location_t
*);
2135 static cp_declarator
*cp_parser_declarator
2136 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2137 static cp_declarator
*cp_parser_direct_declarator
2138 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2139 static enum tree_code cp_parser_ptr_operator
2140 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2141 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2143 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2145 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2147 static tree cp_parser_late_return_type_opt
2148 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2149 static tree cp_parser_declarator_id
2150 (cp_parser
*, bool);
2151 static tree cp_parser_type_id
2153 static tree cp_parser_template_type_arg
2155 static tree
cp_parser_trailing_type_id (cp_parser
*);
2156 static tree cp_parser_type_id_1
2157 (cp_parser
*, bool, bool);
2158 static void cp_parser_type_specifier_seq
2159 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2160 static tree cp_parser_parameter_declaration_clause
2162 static tree cp_parser_parameter_declaration_list
2163 (cp_parser
*, bool *);
2164 static cp_parameter_declarator
*cp_parser_parameter_declaration
2165 (cp_parser
*, bool, bool *);
2166 static tree cp_parser_default_argument
2167 (cp_parser
*, bool);
2168 static void cp_parser_function_body
2169 (cp_parser
*, bool);
2170 static tree cp_parser_initializer
2171 (cp_parser
*, bool *, bool *);
2172 static tree cp_parser_initializer_clause
2173 (cp_parser
*, bool *);
2174 static tree cp_parser_braced_list
2175 (cp_parser
*, bool*);
2176 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2177 (cp_parser
*, bool *);
2179 static bool cp_parser_ctor_initializer_opt_and_function_body
2180 (cp_parser
*, bool);
2182 static tree cp_parser_late_parsing_omp_declare_simd
2183 (cp_parser
*, tree
);
2185 static tree cp_parser_late_parsing_cilk_simd_fn_info
2186 (cp_parser
*, tree
);
2188 static tree synthesize_implicit_template_parm
2190 static tree finish_fully_implicit_template
2191 (cp_parser
*, tree
);
2193 /* Classes [gram.class] */
2195 static tree cp_parser_class_name
2196 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2197 static tree cp_parser_class_specifier
2199 static tree cp_parser_class_head
2200 (cp_parser
*, bool *);
2201 static enum tag_types cp_parser_class_key
2203 static void cp_parser_type_parameter_key
2204 (cp_parser
* parser
);
2205 static void cp_parser_member_specification_opt
2207 static void cp_parser_member_declaration
2209 static tree cp_parser_pure_specifier
2211 static tree cp_parser_constant_initializer
2214 /* Derived classes [gram.class.derived] */
2216 static tree cp_parser_base_clause
2218 static tree cp_parser_base_specifier
2221 /* Special member functions [gram.special] */
2223 static tree cp_parser_conversion_function_id
2225 static tree cp_parser_conversion_type_id
2227 static cp_declarator
*cp_parser_conversion_declarator_opt
2229 static bool cp_parser_ctor_initializer_opt
2231 static void cp_parser_mem_initializer_list
2233 static tree cp_parser_mem_initializer
2235 static tree cp_parser_mem_initializer_id
2238 /* Overloading [gram.over] */
2240 static tree cp_parser_operator_function_id
2242 static tree cp_parser_operator
2245 /* Templates [gram.temp] */
2247 static void cp_parser_template_declaration
2248 (cp_parser
*, bool);
2249 static tree cp_parser_template_parameter_list
2251 static tree cp_parser_template_parameter
2252 (cp_parser
*, bool *, bool *);
2253 static tree cp_parser_type_parameter
2254 (cp_parser
*, bool *);
2255 static tree cp_parser_template_id
2256 (cp_parser
*, bool, bool, enum tag_types
, bool);
2257 static tree cp_parser_template_name
2258 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2259 static tree cp_parser_template_argument_list
2261 static tree cp_parser_template_argument
2263 static void cp_parser_explicit_instantiation
2265 static void cp_parser_explicit_specialization
2268 /* Exception handling [gram.exception] */
2270 static tree cp_parser_try_block
2272 static bool cp_parser_function_try_block
2274 static void cp_parser_handler_seq
2276 static void cp_parser_handler
2278 static tree cp_parser_exception_declaration
2280 static tree cp_parser_throw_expression
2282 static tree cp_parser_exception_specification_opt
2284 static tree cp_parser_type_id_list
2287 /* GNU Extensions */
2289 static tree cp_parser_asm_specification_opt
2291 static tree cp_parser_asm_operand_list
2293 static tree cp_parser_asm_clobber_list
2295 static tree cp_parser_asm_label_list
2297 static bool cp_next_tokens_can_be_attribute_p
2299 static bool cp_next_tokens_can_be_gnu_attribute_p
2301 static bool cp_next_tokens_can_be_std_attribute_p
2303 static bool cp_nth_tokens_can_be_std_attribute_p
2304 (cp_parser
*, size_t);
2305 static bool cp_nth_tokens_can_be_gnu_attribute_p
2306 (cp_parser
*, size_t);
2307 static bool cp_nth_tokens_can_be_attribute_p
2308 (cp_parser
*, size_t);
2309 static tree cp_parser_attributes_opt
2311 static tree cp_parser_gnu_attributes_opt
2313 static tree cp_parser_gnu_attribute_list
2315 static tree cp_parser_std_attribute
2317 static tree cp_parser_std_attribute_spec
2319 static tree cp_parser_std_attribute_spec_seq
2321 static bool cp_parser_extension_opt
2322 (cp_parser
*, int *);
2323 static void cp_parser_label_declaration
2326 /* Transactional Memory Extensions */
2328 static tree cp_parser_transaction
2329 (cp_parser
*, enum rid
);
2330 static tree cp_parser_transaction_expression
2331 (cp_parser
*, enum rid
);
2332 static bool cp_parser_function_transaction
2333 (cp_parser
*, enum rid
);
2334 static tree cp_parser_transaction_cancel
2337 enum pragma_context
{
2344 static bool cp_parser_pragma
2345 (cp_parser
*, enum pragma_context
);
2347 /* Objective-C++ Productions */
2349 static tree cp_parser_objc_message_receiver
2351 static tree cp_parser_objc_message_args
2353 static tree cp_parser_objc_message_expression
2355 static tree cp_parser_objc_encode_expression
2357 static tree cp_parser_objc_defs_expression
2359 static tree cp_parser_objc_protocol_expression
2361 static tree cp_parser_objc_selector_expression
2363 static tree cp_parser_objc_expression
2365 static bool cp_parser_objc_selector_p
2367 static tree cp_parser_objc_selector
2369 static tree cp_parser_objc_protocol_refs_opt
2371 static void cp_parser_objc_declaration
2372 (cp_parser
*, tree
);
2373 static tree cp_parser_objc_statement
2375 static bool cp_parser_objc_valid_prefix_attributes
2376 (cp_parser
*, tree
*);
2377 static void cp_parser_objc_at_property_declaration
2379 static void cp_parser_objc_at_synthesize_declaration
2381 static void cp_parser_objc_at_dynamic_declaration
2383 static tree cp_parser_objc_struct_declaration
2386 /* Utility Routines */
2388 static tree cp_parser_lookup_name
2389 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2390 static tree cp_parser_lookup_name_simple
2391 (cp_parser
*, tree
, location_t
);
2392 static tree cp_parser_maybe_treat_template_as_class
2394 static bool cp_parser_check_declarator_template_parameters
2395 (cp_parser
*, cp_declarator
*, location_t
);
2396 static bool cp_parser_check_template_parameters
2397 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2398 static tree cp_parser_simple_cast_expression
2400 static tree cp_parser_global_scope_opt
2401 (cp_parser
*, bool);
2402 static bool cp_parser_constructor_declarator_p
2403 (cp_parser
*, bool);
2404 static tree cp_parser_function_definition_from_specifiers_and_declarator
2405 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2406 static tree cp_parser_function_definition_after_declarator
2407 (cp_parser
*, bool);
2408 static void cp_parser_template_declaration_after_export
2409 (cp_parser
*, bool);
2410 static void cp_parser_perform_template_parameter_access_checks
2411 (vec
<deferred_access_check
, va_gc
> *);
2412 static tree cp_parser_single_declaration
2413 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2414 static tree cp_parser_functional_cast
2415 (cp_parser
*, tree
);
2416 static tree cp_parser_save_member_function_body
2417 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2418 static tree cp_parser_save_nsdmi
2420 static tree cp_parser_enclosed_template_argument_list
2422 static void cp_parser_save_default_args
2423 (cp_parser
*, tree
);
2424 static void cp_parser_late_parsing_for_member
2425 (cp_parser
*, tree
);
2426 static tree cp_parser_late_parse_one_default_arg
2427 (cp_parser
*, tree
, tree
, tree
);
2428 static void cp_parser_late_parsing_nsdmi
2429 (cp_parser
*, tree
);
2430 static void cp_parser_late_parsing_default_args
2431 (cp_parser
*, tree
);
2432 static tree cp_parser_sizeof_operand
2433 (cp_parser
*, enum rid
);
2434 static tree cp_parser_trait_expr
2435 (cp_parser
*, enum rid
);
2436 static bool cp_parser_declares_only_class_p
2438 static void cp_parser_set_storage_class
2439 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2440 static void cp_parser_set_decl_spec_type
2441 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2442 static void set_and_check_decl_spec_loc
2443 (cp_decl_specifier_seq
*decl_specs
,
2444 cp_decl_spec ds
, cp_token
*);
2445 static bool cp_parser_friend_p
2446 (const cp_decl_specifier_seq
*);
2447 static void cp_parser_required_error
2448 (cp_parser
*, required_token
, bool);
2449 static cp_token
*cp_parser_require
2450 (cp_parser
*, enum cpp_ttype
, required_token
);
2451 static cp_token
*cp_parser_require_keyword
2452 (cp_parser
*, enum rid
, required_token
);
2453 static bool cp_parser_token_starts_function_definition_p
2455 static bool cp_parser_next_token_starts_class_definition_p
2457 static bool cp_parser_next_token_ends_template_argument_p
2459 static bool cp_parser_nth_token_starts_template_argument_list_p
2460 (cp_parser
*, size_t);
2461 static enum tag_types cp_parser_token_is_class_key
2463 static enum tag_types cp_parser_token_is_type_parameter_key
2465 static void cp_parser_check_class_key
2466 (enum tag_types
, tree type
);
2467 static void cp_parser_check_access_in_redeclaration
2468 (tree type
, location_t location
);
2469 static bool cp_parser_optional_template_keyword
2471 static void cp_parser_pre_parsed_nested_name_specifier
2473 static bool cp_parser_cache_group
2474 (cp_parser
*, enum cpp_ttype
, unsigned);
2475 static tree cp_parser_cache_defarg
2476 (cp_parser
*parser
, bool nsdmi
);
2477 static void cp_parser_parse_tentatively
2479 static void cp_parser_commit_to_tentative_parse
2481 static void cp_parser_commit_to_topmost_tentative_parse
2483 static void cp_parser_abort_tentative_parse
2485 static bool cp_parser_parse_definitely
2487 static inline bool cp_parser_parsing_tentatively
2489 static bool cp_parser_uncommitted_to_tentative_parse_p
2491 static void cp_parser_error
2492 (cp_parser
*, const char *);
2493 static void cp_parser_name_lookup_error
2494 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2495 static bool cp_parser_simulate_error
2497 static bool cp_parser_check_type_definition
2499 static void cp_parser_check_for_definition_in_return_type
2500 (cp_declarator
*, tree
, location_t type_location
);
2501 static void cp_parser_check_for_invalid_template_id
2502 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2503 static bool cp_parser_non_integral_constant_expression
2504 (cp_parser
*, non_integral_constant
);
2505 static void cp_parser_diagnose_invalid_type_name
2506 (cp_parser
*, tree
, location_t
);
2507 static bool cp_parser_parse_and_diagnose_invalid_type_name
2509 static int cp_parser_skip_to_closing_parenthesis
2510 (cp_parser
*, bool, bool, bool);
2511 static void cp_parser_skip_to_end_of_statement
2513 static void cp_parser_consume_semicolon_at_end_of_statement
2515 static void cp_parser_skip_to_end_of_block_or_statement
2517 static bool cp_parser_skip_to_closing_brace
2519 static void cp_parser_skip_to_end_of_template_parameter_list
2521 static void cp_parser_skip_to_pragma_eol
2522 (cp_parser
*, cp_token
*);
2523 static bool cp_parser_error_occurred
2525 static bool cp_parser_allow_gnu_extensions_p
2527 static bool cp_parser_is_pure_string_literal
2529 static bool cp_parser_is_string_literal
2531 static bool cp_parser_is_keyword
2532 (cp_token
*, enum rid
);
2533 static tree cp_parser_make_typename_type
2534 (cp_parser
*, tree
, location_t location
);
2535 static cp_declarator
* cp_parser_make_indirect_declarator
2536 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2537 static bool cp_parser_compound_literal_p
2539 static bool cp_parser_array_designator_p
2541 static bool cp_parser_skip_to_closing_square_bracket
2544 /* Returns nonzero if we are parsing tentatively. */
2547 cp_parser_parsing_tentatively (cp_parser
* parser
)
2549 return parser
->context
->next
!= NULL
;
2552 /* Returns nonzero if TOKEN is a string literal. */
2555 cp_parser_is_pure_string_literal (cp_token
* token
)
2557 return (token
->type
== CPP_STRING
||
2558 token
->type
== CPP_STRING16
||
2559 token
->type
== CPP_STRING32
||
2560 token
->type
== CPP_WSTRING
||
2561 token
->type
== CPP_UTF8STRING
);
2564 /* Returns nonzero if TOKEN is a string literal
2565 of a user-defined string literal. */
2568 cp_parser_is_string_literal (cp_token
* token
)
2570 return (cp_parser_is_pure_string_literal (token
) ||
2571 token
->type
== CPP_STRING_USERDEF
||
2572 token
->type
== CPP_STRING16_USERDEF
||
2573 token
->type
== CPP_STRING32_USERDEF
||
2574 token
->type
== CPP_WSTRING_USERDEF
||
2575 token
->type
== CPP_UTF8STRING_USERDEF
);
2578 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2581 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2583 return token
->keyword
== keyword
;
2586 /* If not parsing tentatively, issue a diagnostic of the form
2587 FILE:LINE: MESSAGE before TOKEN
2588 where TOKEN is the next token in the input stream. MESSAGE
2589 (specified by the caller) is usually of the form "expected
2593 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2595 if (!cp_parser_simulate_error (parser
))
2597 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2598 /* This diagnostic makes more sense if it is tagged to the line
2599 of the token we just peeked at. */
2600 cp_lexer_set_source_position_from_token (token
);
2602 if (token
->type
== CPP_PRAGMA
)
2604 error_at (token
->location
,
2605 "%<#pragma%> is not allowed here");
2606 cp_parser_skip_to_pragma_eol (parser
, token
);
2610 c_parse_error (gmsgid
,
2611 /* Because c_parser_error does not understand
2612 CPP_KEYWORD, keywords are treated like
2614 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2615 token
->u
.value
, token
->flags
);
2619 /* Issue an error about name-lookup failing. NAME is the
2620 IDENTIFIER_NODE DECL is the result of
2621 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2622 the thing that we hoped to find. */
2625 cp_parser_name_lookup_error (cp_parser
* parser
,
2628 name_lookup_error desired
,
2629 location_t location
)
2631 /* If name lookup completely failed, tell the user that NAME was not
2633 if (decl
== error_mark_node
)
2635 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2636 error_at (location
, "%<%E::%E%> has not been declared",
2637 parser
->scope
, name
);
2638 else if (parser
->scope
== global_namespace
)
2639 error_at (location
, "%<::%E%> has not been declared", name
);
2640 else if (parser
->object_scope
2641 && !CLASS_TYPE_P (parser
->object_scope
))
2642 error_at (location
, "request for member %qE in non-class type %qT",
2643 name
, parser
->object_scope
);
2644 else if (parser
->object_scope
)
2645 error_at (location
, "%<%T::%E%> has not been declared",
2646 parser
->object_scope
, name
);
2648 error_at (location
, "%qE has not been declared", name
);
2650 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2655 error_at (location
, "%<%E::%E%> is not a type",
2656 parser
->scope
, name
);
2659 error_at (location
, "%<%E::%E%> is not a class or namespace",
2660 parser
->scope
, name
);
2664 "%<%E::%E%> is not a class, namespace, or enumeration",
2665 parser
->scope
, name
);
2672 else if (parser
->scope
== global_namespace
)
2677 error_at (location
, "%<::%E%> is not a type", name
);
2680 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2684 "%<::%E%> is not a class, namespace, or enumeration",
2696 error_at (location
, "%qE is not a type", name
);
2699 error_at (location
, "%qE is not a class or namespace", name
);
2703 "%qE is not a class, namespace, or enumeration", name
);
2711 /* If we are parsing tentatively, remember that an error has occurred
2712 during this tentative parse. Returns true if the error was
2713 simulated; false if a message should be issued by the caller. */
2716 cp_parser_simulate_error (cp_parser
* parser
)
2718 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2720 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2726 /* This function is called when a type is defined. If type
2727 definitions are forbidden at this point, an error message is
2731 cp_parser_check_type_definition (cp_parser
* parser
)
2733 /* If types are forbidden here, issue a message. */
2734 if (parser
->type_definition_forbidden_message
)
2736 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2737 in the message need to be interpreted. */
2738 error (parser
->type_definition_forbidden_message
);
2744 /* This function is called when the DECLARATOR is processed. The TYPE
2745 was a type defined in the decl-specifiers. If it is invalid to
2746 define a type in the decl-specifiers for DECLARATOR, an error is
2747 issued. TYPE_LOCATION is the location of TYPE and is used
2748 for error reporting. */
2751 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2752 tree type
, location_t type_location
)
2754 /* [dcl.fct] forbids type definitions in return types.
2755 Unfortunately, it's not easy to know whether or not we are
2756 processing a return type until after the fact. */
2758 && (declarator
->kind
== cdk_pointer
2759 || declarator
->kind
== cdk_reference
2760 || declarator
->kind
== cdk_ptrmem
))
2761 declarator
= declarator
->declarator
;
2763 && declarator
->kind
== cdk_function
)
2765 error_at (type_location
,
2766 "new types may not be defined in a return type");
2767 inform (type_location
,
2768 "(perhaps a semicolon is missing after the definition of %qT)",
2773 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2774 "<" in any valid C++ program. If the next token is indeed "<",
2775 issue a message warning the user about what appears to be an
2776 invalid attempt to form a template-id. LOCATION is the location
2777 of the type-specifier (TYPE) */
2780 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2782 enum tag_types tag_type
,
2783 location_t location
)
2785 cp_token_position start
= 0;
2787 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2790 error_at (location
, "%qT is not a template", type
);
2791 else if (identifier_p (type
))
2793 if (tag_type
!= none_type
)
2794 error_at (location
, "%qE is not a class template", type
);
2796 error_at (location
, "%qE is not a template", type
);
2799 error_at (location
, "invalid template-id");
2800 /* Remember the location of the invalid "<". */
2801 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2802 start
= cp_lexer_token_position (parser
->lexer
, true);
2803 /* Consume the "<". */
2804 cp_lexer_consume_token (parser
->lexer
);
2805 /* Parse the template arguments. */
2806 cp_parser_enclosed_template_argument_list (parser
);
2807 /* Permanently remove the invalid template arguments so that
2808 this error message is not issued again. */
2810 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2814 /* If parsing an integral constant-expression, issue an error message
2815 about the fact that THING appeared and return true. Otherwise,
2816 return false. In either case, set
2817 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2820 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2821 non_integral_constant thing
)
2823 parser
->non_integral_constant_expression_p
= true;
2824 if (parser
->integral_constant_expression_p
)
2826 if (!parser
->allow_non_integral_constant_expression_p
)
2828 const char *msg
= NULL
;
2832 error ("floating-point literal "
2833 "cannot appear in a constant-expression");
2836 error ("a cast to a type other than an integral or "
2837 "enumeration type cannot appear in a "
2838 "constant-expression");
2841 error ("%<typeid%> operator "
2842 "cannot appear in a constant-expression");
2845 error ("non-constant compound literals "
2846 "cannot appear in a constant-expression");
2849 error ("a function call "
2850 "cannot appear in a constant-expression");
2853 error ("an increment "
2854 "cannot appear in a constant-expression");
2857 error ("an decrement "
2858 "cannot appear in a constant-expression");
2861 error ("an array reference "
2862 "cannot appear in a constant-expression");
2864 case NIC_ADDR_LABEL
:
2865 error ("the address of a label "
2866 "cannot appear in a constant-expression");
2868 case NIC_OVERLOADED
:
2869 error ("calls to overloaded operators "
2870 "cannot appear in a constant-expression");
2872 case NIC_ASSIGNMENT
:
2873 error ("an assignment cannot appear in a constant-expression");
2876 error ("a comma operator "
2877 "cannot appear in a constant-expression");
2879 case NIC_CONSTRUCTOR
:
2880 error ("a call to a constructor "
2881 "cannot appear in a constant-expression");
2883 case NIC_TRANSACTION
:
2884 error ("a transaction expression "
2885 "cannot appear in a constant-expression");
2891 msg
= "__FUNCTION__";
2893 case NIC_PRETTY_FUNC
:
2894 msg
= "__PRETTY_FUNCTION__";
2914 case NIC_PREINCREMENT
:
2917 case NIC_PREDECREMENT
:
2930 error ("%qs cannot appear in a constant-expression", msg
);
2937 /* Emit a diagnostic for an invalid type name. This function commits
2938 to the current active tentative parse, if any. (Otherwise, the
2939 problematic construct might be encountered again later, resulting
2940 in duplicate error messages.) LOCATION is the location of ID. */
2943 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
2944 location_t location
)
2946 tree decl
, ambiguous_decls
;
2947 cp_parser_commit_to_tentative_parse (parser
);
2948 /* Try to lookup the identifier. */
2949 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
2950 /*is_template=*/false,
2951 /*is_namespace=*/false,
2952 /*check_dependency=*/true,
2953 &ambiguous_decls
, location
);
2954 if (ambiguous_decls
)
2955 /* If the lookup was ambiguous, an error will already have
2958 /* If the lookup found a template-name, it means that the user forgot
2959 to specify an argument list. Emit a useful error message. */
2960 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2962 "invalid use of template-name %qE without an argument list",
2964 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2965 error_at (location
, "invalid use of destructor %qD as a type", id
);
2966 else if (TREE_CODE (decl
) == TYPE_DECL
)
2967 /* Something like 'unsigned A a;' */
2968 error_at (location
, "invalid combination of multiple type-specifiers");
2969 else if (!parser
->scope
)
2971 /* Issue an error message. */
2972 error_at (location
, "%qE does not name a type", id
);
2973 /* If we're in a template class, it's possible that the user was
2974 referring to a type from a base class. For example:
2976 template <typename T> struct A { typedef T X; };
2977 template <typename T> struct B : public A<T> { X x; };
2979 The user should have said "typename A<T>::X". */
2980 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2981 inform (location
, "C++11 %<constexpr%> only available with "
2982 "-std=c++11 or -std=gnu++11");
2983 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
2984 inform (location
, "C++11 %<noexcept%> only available with "
2985 "-std=c++11 or -std=gnu++11");
2986 else if (cxx_dialect
< cxx11
2987 && TREE_CODE (id
) == IDENTIFIER_NODE
2988 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
2989 inform (location
, "C++11 %<thread_local%> only available with "
2990 "-std=c++11 or -std=gnu++11");
2991 else if (processing_template_decl
&& current_class_type
2992 && TYPE_BINFO (current_class_type
))
2996 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3000 tree base_type
= BINFO_TYPE (b
);
3001 if (CLASS_TYPE_P (base_type
)
3002 && dependent_type_p (base_type
))
3005 /* Go from a particular instantiation of the
3006 template (which will have an empty TYPE_FIELDs),
3007 to the main version. */
3008 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3009 for (field
= TYPE_FIELDS (base_type
);
3011 field
= DECL_CHAIN (field
))
3012 if (TREE_CODE (field
) == TYPE_DECL
3013 && DECL_NAME (field
) == id
)
3016 "(perhaps %<typename %T::%E%> was intended)",
3017 BINFO_TYPE (b
), id
);
3026 /* Here we diagnose qualified-ids where the scope is actually correct,
3027 but the identifier does not resolve to a valid type name. */
3028 else if (parser
->scope
!= error_mark_node
)
3030 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3033 error_at (location_of (id
),
3034 "%qE in namespace %qE does not name a template type",
3037 error_at (location_of (id
),
3038 "%qE in namespace %qE does not name a type",
3041 else if (CLASS_TYPE_P (parser
->scope
)
3042 && constructor_name_p (id
, parser
->scope
))
3045 error_at (location
, "%<%T::%E%> names the constructor, not"
3046 " the type", parser
->scope
, id
);
3047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3048 error_at (location
, "and %qT has no template constructors",
3051 else if (TYPE_P (parser
->scope
)
3052 && dependent_scope_p (parser
->scope
))
3053 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3054 "%qT is a dependent scope",
3055 parser
->scope
, id
, parser
->scope
);
3056 else if (TYPE_P (parser
->scope
))
3058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3059 error_at (location_of (id
),
3060 "%qE in %q#T does not name a template type",
3063 error_at (location_of (id
),
3064 "%qE in %q#T does not name a type",
3072 /* Check for a common situation where a type-name should be present,
3073 but is not, and issue a sensible error message. Returns true if an
3074 invalid type-name was detected.
3076 The situation handled by this function are variable declarations of the
3077 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3078 Usually, `ID' should name a type, but if we got here it means that it
3079 does not. We try to emit the best possible error message depending on
3080 how exactly the id-expression looks like. */
3083 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3086 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3088 /* Avoid duplicate error about ambiguous lookup. */
3089 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3091 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3092 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3096 cp_parser_parse_tentatively (parser
);
3097 id
= cp_parser_id_expression (parser
,
3098 /*template_keyword_p=*/false,
3099 /*check_dependency_p=*/true,
3100 /*template_p=*/NULL
,
3101 /*declarator_p=*/true,
3102 /*optional_p=*/false);
3103 /* If the next token is a (, this is a function with no explicit return
3104 type, i.e. constructor, destructor or conversion op. */
3105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3106 || TREE_CODE (id
) == TYPE_DECL
)
3108 cp_parser_abort_tentative_parse (parser
);
3111 if (!cp_parser_parse_definitely (parser
))
3114 /* Emit a diagnostic for the invalid type. */
3115 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3117 /* If we aren't in the middle of a declarator (i.e. in a
3118 parameter-declaration-clause), skip to the end of the declaration;
3119 there's no point in trying to process it. */
3120 if (!parser
->in_declarator_p
)
3121 cp_parser_skip_to_end_of_block_or_statement (parser
);
3125 /* Consume tokens up to, and including, the next non-nested closing `)'.
3126 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3127 are doing error recovery. Returns -1 if OR_COMMA is true and we
3128 found an unnested comma. */
3131 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3136 unsigned paren_depth
= 0;
3137 unsigned brace_depth
= 0;
3138 unsigned square_depth
= 0;
3140 if (recovering
&& !or_comma
3141 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3146 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3148 switch (token
->type
)
3151 case CPP_PRAGMA_EOL
:
3152 /* If we've run out of tokens, then there is no closing `)'. */
3155 /* This is good for lambda expression capture-lists. */
3156 case CPP_OPEN_SQUARE
:
3159 case CPP_CLOSE_SQUARE
:
3160 if (!square_depth
--)
3165 /* This matches the processing in skip_to_end_of_statement. */
3170 case CPP_OPEN_BRACE
:
3173 case CPP_CLOSE_BRACE
:
3179 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3184 case CPP_OPEN_PAREN
:
3189 case CPP_CLOSE_PAREN
:
3190 if (!brace_depth
&& !paren_depth
--)
3193 cp_lexer_consume_token (parser
->lexer
);
3202 /* Consume the token. */
3203 cp_lexer_consume_token (parser
->lexer
);
3207 /* Consume tokens until we reach the end of the current statement.
3208 Normally, that will be just before consuming a `;'. However, if a
3209 non-nested `}' comes first, then we stop before consuming that. */
3212 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3214 unsigned nesting_depth
= 0;
3216 /* Unwind generic function template scope if necessary. */
3217 if (parser
->fully_implicit_function_template_p
)
3218 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3222 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3224 switch (token
->type
)
3227 case CPP_PRAGMA_EOL
:
3228 /* If we've run out of tokens, stop. */
3232 /* If the next token is a `;', we have reached the end of the
3238 case CPP_CLOSE_BRACE
:
3239 /* If this is a non-nested '}', stop before consuming it.
3240 That way, when confronted with something like:
3244 we stop before consuming the closing '}', even though we
3245 have not yet reached a `;'. */
3246 if (nesting_depth
== 0)
3249 /* If it is the closing '}' for a block that we have
3250 scanned, stop -- but only after consuming the token.
3256 we will stop after the body of the erroneously declared
3257 function, but before consuming the following `typedef'
3259 if (--nesting_depth
== 0)
3261 cp_lexer_consume_token (parser
->lexer
);
3265 case CPP_OPEN_BRACE
:
3273 /* Consume the token. */
3274 cp_lexer_consume_token (parser
->lexer
);
3278 /* This function is called at the end of a statement or declaration.
3279 If the next token is a semicolon, it is consumed; otherwise, error
3280 recovery is attempted. */
3283 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3285 /* Look for the trailing `;'. */
3286 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3288 /* If there is additional (erroneous) input, skip to the end of
3290 cp_parser_skip_to_end_of_statement (parser
);
3291 /* If the next token is now a `;', consume it. */
3292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3293 cp_lexer_consume_token (parser
->lexer
);
3297 /* Skip tokens until we have consumed an entire block, or until we
3298 have consumed a non-nested `;'. */
3301 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3303 int nesting_depth
= 0;
3305 /* Unwind generic function template scope if necessary. */
3306 if (parser
->fully_implicit_function_template_p
)
3307 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3309 while (nesting_depth
>= 0)
3311 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3313 switch (token
->type
)
3316 case CPP_PRAGMA_EOL
:
3317 /* If we've run out of tokens, stop. */
3321 /* Stop if this is an unnested ';'. */
3326 case CPP_CLOSE_BRACE
:
3327 /* Stop if this is an unnested '}', or closes the outermost
3330 if (nesting_depth
< 0)
3336 case CPP_OPEN_BRACE
:
3345 /* Consume the token. */
3346 cp_lexer_consume_token (parser
->lexer
);
3350 /* Skip tokens until a non-nested closing curly brace is the next
3351 token, or there are no more tokens. Return true in the first case,
3355 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3357 unsigned nesting_depth
= 0;
3361 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3363 switch (token
->type
)
3366 case CPP_PRAGMA_EOL
:
3367 /* If we've run out of tokens, stop. */
3370 case CPP_CLOSE_BRACE
:
3371 /* If the next token is a non-nested `}', then we have reached
3372 the end of the current block. */
3373 if (nesting_depth
-- == 0)
3377 case CPP_OPEN_BRACE
:
3378 /* If it the next token is a `{', then we are entering a new
3379 block. Consume the entire block. */
3387 /* Consume the token. */
3388 cp_lexer_consume_token (parser
->lexer
);
3392 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3393 parameter is the PRAGMA token, allowing us to purge the entire pragma
3397 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3401 parser
->lexer
->in_pragma
= false;
3404 token
= cp_lexer_consume_token (parser
->lexer
);
3405 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3407 /* Ensure that the pragma is not parsed again. */
3408 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3411 /* Require pragma end of line, resyncing with it as necessary. The
3412 arguments are as for cp_parser_skip_to_pragma_eol. */
3415 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3417 parser
->lexer
->in_pragma
= false;
3418 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3419 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3422 /* This is a simple wrapper around make_typename_type. When the id is
3423 an unresolved identifier node, we can provide a superior diagnostic
3424 using cp_parser_diagnose_invalid_type_name. */
3427 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3428 location_t id_location
)
3431 if (identifier_p (id
))
3433 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3434 /*complain=*/tf_none
);
3435 if (result
== error_mark_node
)
3436 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3439 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3442 /* This is a wrapper around the
3443 make_{pointer,ptrmem,reference}_declarator functions that decides
3444 which one to call based on the CODE and CLASS_TYPE arguments. The
3445 CODE argument should be one of the values returned by
3446 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3447 appertain to the pointer or reference. */
3449 static cp_declarator
*
3450 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3451 cp_cv_quals cv_qualifiers
,
3452 cp_declarator
*target
,
3455 if (code
== ERROR_MARK
)
3456 return cp_error_declarator
;
3458 if (code
== INDIRECT_REF
)
3459 if (class_type
== NULL_TREE
)
3460 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3462 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3463 target
, attributes
);
3464 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3465 return make_reference_declarator (cv_qualifiers
, target
,
3467 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3468 return make_reference_declarator (cv_qualifiers
, target
,
3473 /* Create a new C++ parser. */
3476 cp_parser_new (void)
3482 /* cp_lexer_new_main is called before doing GC allocation because
3483 cp_lexer_new_main might load a PCH file. */
3484 lexer
= cp_lexer_new_main ();
3486 /* Initialize the binops_by_token so that we can get the tree
3487 directly from the token. */
3488 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3489 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3491 parser
= ggc_cleared_alloc
<cp_parser
> ();
3492 parser
->lexer
= lexer
;
3493 parser
->context
= cp_parser_context_new (NULL
);
3495 /* For now, we always accept GNU extensions. */
3496 parser
->allow_gnu_extensions_p
= 1;
3498 /* The `>' token is a greater-than operator, not the end of a
3500 parser
->greater_than_is_operator_p
= true;
3502 parser
->default_arg_ok_p
= true;
3504 /* We are not parsing a constant-expression. */
3505 parser
->integral_constant_expression_p
= false;
3506 parser
->allow_non_integral_constant_expression_p
= false;
3507 parser
->non_integral_constant_expression_p
= false;
3509 /* Local variable names are not forbidden. */
3510 parser
->local_variables_forbidden_p
= false;
3512 /* We are not processing an `extern "C"' declaration. */
3513 parser
->in_unbraced_linkage_specification_p
= false;
3515 /* We are not processing a declarator. */
3516 parser
->in_declarator_p
= false;
3518 /* We are not processing a template-argument-list. */
3519 parser
->in_template_argument_list_p
= false;
3521 /* We are not in an iteration statement. */
3522 parser
->in_statement
= 0;
3524 /* We are not in a switch statement. */
3525 parser
->in_switch_statement_p
= false;
3527 /* We are not parsing a type-id inside an expression. */
3528 parser
->in_type_id_in_expr_p
= false;
3530 /* Declarations aren't implicitly extern "C". */
3531 parser
->implicit_extern_c
= false;
3533 /* String literals should be translated to the execution character set. */
3534 parser
->translate_strings_p
= true;
3536 /* We are not parsing a function body. */
3537 parser
->in_function_body
= false;
3539 /* We can correct until told otherwise. */
3540 parser
->colon_corrects_to_scope_p
= true;
3542 /* The unparsed function queue is empty. */
3543 push_unparsed_function_queues (parser
);
3545 /* There are no classes being defined. */
3546 parser
->num_classes_being_defined
= 0;
3548 /* No template parameters apply. */
3549 parser
->num_template_parameter_lists
= 0;
3551 /* Not declaring an implicit function template. */
3552 parser
->auto_is_implicit_function_template_parm_p
= false;
3553 parser
->fully_implicit_function_template_p
= false;
3554 parser
->implicit_template_parms
= 0;
3555 parser
->implicit_template_scope
= 0;
3560 /* Create a cp_lexer structure which will emit the tokens in CACHE
3561 and push it onto the parser's lexer stack. This is used for delayed
3562 parsing of in-class method bodies and default arguments, and should
3563 not be confused with tentative parsing. */
3565 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3567 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3568 lexer
->next
= parser
->lexer
;
3569 parser
->lexer
= lexer
;
3571 /* Move the current source position to that of the first token in the
3573 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3576 /* Pop the top lexer off the parser stack. This is never used for the
3577 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3579 cp_parser_pop_lexer (cp_parser
*parser
)
3581 cp_lexer
*lexer
= parser
->lexer
;
3582 parser
->lexer
= lexer
->next
;
3583 cp_lexer_destroy (lexer
);
3585 /* Put the current source position back where it was before this
3586 lexer was pushed. */
3587 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3590 /* Lexical conventions [gram.lex] */
3592 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3596 cp_parser_identifier (cp_parser
* parser
)
3600 /* Look for the identifier. */
3601 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3602 /* Return the value. */
3603 return token
? token
->u
.value
: error_mark_node
;
3606 /* Parse a sequence of adjacent string constants. Returns a
3607 TREE_STRING representing the combined, nul-terminated string
3608 constant. If TRANSLATE is true, translate the string to the
3609 execution character set. If WIDE_OK is true, a wide string is
3612 C++98 [lex.string] says that if a narrow string literal token is
3613 adjacent to a wide string literal token, the behavior is undefined.
3614 However, C99 6.4.5p4 says that this results in a wide string literal.
3615 We follow C99 here, for consistency with the C front end.
3617 This code is largely lifted from lex_string() in c-lex.c.
3619 FUTURE: ObjC++ will need to handle @-strings here. */
3621 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3622 bool lookup_udlit
= true)
3626 struct obstack str_ob
;
3627 cpp_string str
, istr
, *strs
;
3629 enum cpp_ttype type
, curr_type
;
3630 int have_suffix_p
= 0;
3632 tree suffix_id
= NULL_TREE
;
3633 bool curr_tok_is_userdef_p
= false;
3635 tok
= cp_lexer_peek_token (parser
->lexer
);
3636 if (!cp_parser_is_string_literal (tok
))
3638 cp_parser_error (parser
, "expected string-literal");
3639 return error_mark_node
;
3642 if (cpp_userdef_string_p (tok
->type
))
3644 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3645 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3646 curr_tok_is_userdef_p
= true;
3650 string_tree
= tok
->u
.value
;
3651 curr_type
= tok
->type
;
3655 /* Try to avoid the overhead of creating and destroying an obstack
3656 for the common case of just one string. */
3657 if (!cp_parser_is_string_literal
3658 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3660 cp_lexer_consume_token (parser
->lexer
);
3662 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3663 str
.len
= TREE_STRING_LENGTH (string_tree
);
3666 if (curr_tok_is_userdef_p
)
3668 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3670 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3673 curr_type
= tok
->type
;
3679 gcc_obstack_init (&str_ob
);
3684 cp_lexer_consume_token (parser
->lexer
);
3686 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3687 str
.len
= TREE_STRING_LENGTH (string_tree
);
3689 if (curr_tok_is_userdef_p
)
3691 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3692 if (have_suffix_p
== 0)
3694 suffix_id
= curr_suffix_id
;
3697 else if (have_suffix_p
== 1
3698 && curr_suffix_id
!= suffix_id
)
3700 error ("inconsistent user-defined literal suffixes"
3701 " %qD and %qD in string literal",
3702 suffix_id
, curr_suffix_id
);
3705 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3708 curr_type
= tok
->type
;
3710 if (type
!= curr_type
)
3712 if (type
== CPP_STRING
)
3714 else if (curr_type
!= CPP_STRING
)
3715 error_at (tok
->location
,
3716 "unsupported non-standard concatenation "
3717 "of string literals");
3720 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3722 tok
= cp_lexer_peek_token (parser
->lexer
);
3723 if (cpp_userdef_string_p (tok
->type
))
3725 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3726 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3727 curr_tok_is_userdef_p
= true;
3731 string_tree
= tok
->u
.value
;
3732 curr_type
= tok
->type
;
3733 curr_tok_is_userdef_p
= false;
3736 while (cp_parser_is_string_literal (tok
));
3738 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3741 if (type
!= CPP_STRING
&& !wide_ok
)
3743 cp_parser_error (parser
, "a wide string is invalid in this context");
3747 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3748 (parse_in
, strs
, count
, &istr
, type
))
3750 value
= build_string (istr
.len
, (const char *)istr
.text
);
3751 free (CONST_CAST (unsigned char *, istr
.text
));
3757 case CPP_UTF8STRING
:
3758 TREE_TYPE (value
) = char_array_type_node
;
3761 TREE_TYPE (value
) = char16_array_type_node
;
3764 TREE_TYPE (value
) = char32_array_type_node
;
3767 TREE_TYPE (value
) = wchar_array_type_node
;
3771 value
= fix_string_type (value
);
3775 tree literal
= build_userdef_literal (suffix_id
, value
,
3776 OT_NONE
, NULL_TREE
);
3778 value
= cp_parser_userdef_string_literal (literal
);
3784 /* cpp_interpret_string has issued an error. */
3785 value
= error_mark_node
;
3788 obstack_free (&str_ob
, 0);
3793 /* Look up a literal operator with the name and the exact arguments. */
3796 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3799 decl
= lookup_name (name
);
3800 if (!decl
|| !is_overloaded_fn (decl
))
3801 return error_mark_node
;
3803 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3807 tree fn
= OVL_CURRENT (fns
);
3808 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3809 if (parmtypes
!= NULL_TREE
)
3811 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3812 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3814 tree tparm
= TREE_VALUE (parmtypes
);
3815 tree targ
= TREE_TYPE ((*args
)[ix
]);
3816 bool ptr
= TYPE_PTR_P (tparm
);
3817 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3818 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3820 || !same_type_p (TREE_TYPE (tparm
),
3825 && ix
== vec_safe_length (args
)
3826 /* May be this should be sufficient_parms_p instead,
3827 depending on how exactly should user-defined literals
3828 work in presence of default arguments on the literal
3829 operator parameters. */
3830 && parmtypes
== void_list_node
)
3835 return error_mark_node
;
3838 /* Parse a user-defined char constant. Returns a call to a user-defined
3839 literal operator taking the character as an argument. */
3842 cp_parser_userdef_char_literal (cp_parser
*parser
)
3844 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3845 tree literal
= token
->u
.value
;
3846 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3847 tree value
= USERDEF_LITERAL_VALUE (literal
);
3848 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3851 /* Build up a call to the user-defined operator */
3852 /* Lookup the name we got back from the id-expression. */
3853 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3854 vec_safe_push (args
, value
);
3855 decl
= lookup_literal_operator (name
, args
);
3856 if (!decl
|| decl
== error_mark_node
)
3858 error ("unable to find character literal operator %qD with %qT argument",
3859 name
, TREE_TYPE (value
));
3860 release_tree_vector (args
);
3861 return error_mark_node
;
3863 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3864 release_tree_vector (args
);
3868 /* A subroutine of cp_parser_userdef_numeric_literal to
3869 create a char... template parameter pack from a string node. */
3872 make_char_string_pack (tree value
)
3875 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3876 const char *str
= TREE_STRING_POINTER (value
);
3877 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3878 tree argvec
= make_tree_vec (1);
3880 /* Fill in CHARVEC with all of the parameters. */
3881 charvec
= make_tree_vec (len
);
3882 for (i
= 0; i
< len
; ++i
)
3883 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3885 /* Build the argument packs. */
3886 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3887 TREE_TYPE (argpack
) = char_type_node
;
3889 TREE_VEC_ELT (argvec
, 0) = argpack
;
3894 /* A subroutine of cp_parser_userdef_numeric_literal to
3895 create a char... template parameter pack from a string node. */
3898 make_string_pack (tree value
)
3901 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3902 const unsigned char *str
3903 = (const unsigned char *) TREE_STRING_POINTER (value
);
3904 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3905 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3906 tree argvec
= make_tree_vec (2);
3908 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3909 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3911 /* First template parm is character type. */
3912 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3914 /* Fill in CHARVEC with all of the parameters. */
3915 charvec
= make_tree_vec (len
);
3916 for (int i
= 0; i
< len
; ++i
)
3917 TREE_VEC_ELT (charvec
, i
)
3918 = double_int_to_tree (str_char_type_node
,
3919 double_int::from_buffer (str
+ i
* sz
, sz
));
3921 /* Build the argument packs. */
3922 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3923 TREE_TYPE (argpack
) = str_char_type_node
;
3925 TREE_VEC_ELT (argvec
, 1) = argpack
;
3930 /* Parse a user-defined numeric constant. returns a call to a user-defined
3931 literal operator. */
3934 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3936 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3937 tree literal
= token
->u
.value
;
3938 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3939 tree value
= USERDEF_LITERAL_VALUE (literal
);
3940 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3941 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3942 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3944 vec
<tree
, va_gc
> *args
;
3946 /* Look for a literal operator taking the exact type of numeric argument
3947 as the literal value. */
3948 args
= make_tree_vector ();
3949 vec_safe_push (args
, value
);
3950 decl
= lookup_literal_operator (name
, args
);
3951 if (decl
&& decl
!= error_mark_node
)
3953 result
= finish_call_expr (decl
, &args
, false, true,
3954 tf_warning_or_error
);
3956 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3958 warning_at (token
->location
, OPT_Woverflow
,
3959 "integer literal exceeds range of %qT type",
3960 long_long_unsigned_type_node
);
3965 warning_at (token
->location
, OPT_Woverflow
,
3966 "floating literal exceeds range of %qT type",
3967 long_double_type_node
);
3968 else if (overflow
< 0)
3969 warning_at (token
->location
, OPT_Woverflow
,
3970 "floating literal truncated to zero");
3973 release_tree_vector (args
);
3976 release_tree_vector (args
);
3978 /* If the numeric argument didn't work, look for a raw literal
3979 operator taking a const char* argument consisting of the number
3980 in string format. */
3981 args
= make_tree_vector ();
3982 vec_safe_push (args
, num_string
);
3983 decl
= lookup_literal_operator (name
, args
);
3984 if (decl
&& decl
!= error_mark_node
)
3986 result
= finish_call_expr (decl
, &args
, false, true,
3987 tf_warning_or_error
);
3988 release_tree_vector (args
);
3991 release_tree_vector (args
);
3993 /* If the raw literal didn't work, look for a non-type template
3994 function with parameter pack char.... Call the function with
3995 template parameter characters representing the number. */
3996 args
= make_tree_vector ();
3997 decl
= lookup_literal_operator (name
, args
);
3998 if (decl
&& decl
!= error_mark_node
)
4000 tree tmpl_args
= make_char_string_pack (num_string
);
4001 decl
= lookup_template_function (decl
, tmpl_args
);
4002 result
= finish_call_expr (decl
, &args
, false, true,
4003 tf_warning_or_error
);
4004 release_tree_vector (args
);
4008 release_tree_vector (args
);
4010 error ("unable to find numeric literal operator %qD", name
);
4011 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4012 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4013 "to enable more built-in suffixes");
4014 return error_mark_node
;
4017 /* Parse a user-defined string constant. Returns a call to a user-defined
4018 literal operator taking a character pointer and the length of the string
4022 cp_parser_userdef_string_literal (tree literal
)
4024 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4025 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4026 tree value
= USERDEF_LITERAL_VALUE (literal
);
4027 int len
= TREE_STRING_LENGTH (value
)
4028 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4030 vec
<tree
, va_gc
> *args
;
4032 /* Build up a call to the user-defined operator. */
4033 /* Lookup the name we got back from the id-expression. */
4034 args
= make_tree_vector ();
4035 vec_safe_push (args
, value
);
4036 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4037 decl
= lookup_literal_operator (name
, args
);
4039 if (decl
&& decl
!= error_mark_node
)
4041 result
= finish_call_expr (decl
, &args
, false, true,
4042 tf_warning_or_error
);
4043 release_tree_vector (args
);
4046 release_tree_vector (args
);
4048 /* Look for a template function with typename parameter CharT
4049 and parameter pack CharT... Call the function with
4050 template parameter characters representing the string. */
4051 args
= make_tree_vector ();
4052 decl
= lookup_literal_operator (name
, args
);
4053 if (decl
&& decl
!= error_mark_node
)
4055 tree tmpl_args
= make_string_pack (value
);
4056 decl
= lookup_template_function (decl
, tmpl_args
);
4057 result
= finish_call_expr (decl
, &args
, false, true,
4058 tf_warning_or_error
);
4059 release_tree_vector (args
);
4062 release_tree_vector (args
);
4064 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4065 name
, TREE_TYPE (value
), size_type_node
);
4066 return error_mark_node
;
4070 /* Basic concepts [gram.basic] */
4072 /* Parse a translation-unit.
4075 declaration-seq [opt]
4077 Returns TRUE if all went well. */
4080 cp_parser_translation_unit (cp_parser
* parser
)
4082 /* The address of the first non-permanent object on the declarator
4084 static void *declarator_obstack_base
;
4088 /* Create the declarator obstack, if necessary. */
4089 if (!cp_error_declarator
)
4091 gcc_obstack_init (&declarator_obstack
);
4092 /* Create the error declarator. */
4093 cp_error_declarator
= make_declarator (cdk_error
);
4094 /* Create the empty parameter list. */
4095 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4096 /* Remember where the base of the declarator obstack lies. */
4097 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4100 cp_parser_declaration_seq_opt (parser
);
4102 /* If there are no tokens left then all went well. */
4103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4105 /* Get rid of the token array; we don't need it any more. */
4106 cp_lexer_destroy (parser
->lexer
);
4107 parser
->lexer
= NULL
;
4109 /* This file might have been a context that's implicitly extern
4110 "C". If so, pop the lang context. (Only relevant for PCH.) */
4111 if (parser
->implicit_extern_c
)
4113 pop_lang_context ();
4114 parser
->implicit_extern_c
= false;
4118 finish_translation_unit ();
4124 cp_parser_error (parser
, "expected declaration");
4128 /* Make sure the declarator obstack was fully cleaned up. */
4129 gcc_assert (obstack_next_free (&declarator_obstack
)
4130 == declarator_obstack_base
);
4132 /* All went well. */
4136 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4137 decltype context. */
4139 static inline tsubst_flags_t
4140 complain_flags (bool decltype_p
)
4142 tsubst_flags_t complain
= tf_warning_or_error
;
4144 complain
|= tf_decltype
;
4148 /* We're about to parse a collection of statements. If we're currently
4149 parsing tentatively, set up a firewall so that any nested
4150 cp_parser_commit_to_tentative_parse won't affect the current context. */
4152 static cp_token_position
4153 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4155 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4158 cp_parser_parse_tentatively (parser
);
4159 cp_parser_commit_to_topmost_tentative_parse (parser
);
4160 return cp_lexer_token_position (parser
->lexer
, false);
4163 /* We've finished parsing the collection of statements. Wrap up the
4164 firewall and replace the relevant tokens with the parsed form. */
4167 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4173 /* Finish the firewall level. */
4174 cp_parser_parse_definitely (parser
);
4175 /* And remember the result of the parse for when we try again. */
4176 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4177 token
->type
= CPP_PREPARSED_EXPR
;
4178 token
->u
.value
= expr
;
4179 token
->keyword
= RID_MAX
;
4180 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4183 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4184 enclosing parentheses. */
4187 cp_parser_statement_expr (cp_parser
*parser
)
4189 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4191 /* Consume the '('. */
4192 cp_lexer_consume_token (parser
->lexer
);
4193 /* Start the statement-expression. */
4194 tree expr
= begin_stmt_expr ();
4195 /* Parse the compound-statement. */
4196 cp_parser_compound_statement (parser
, expr
, false, false);
4198 expr
= finish_stmt_expr (expr
, false);
4199 /* Consume the ')'. */
4200 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4201 cp_parser_skip_to_end_of_statement (parser
);
4203 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4207 /* Expressions [gram.expr] */
4209 /* Parse a primary-expression.
4216 lambda-expression (C++11)
4221 ( compound-statement )
4222 __builtin_va_arg ( assignment-expression , type-id )
4223 __builtin_offsetof ( type-id , offsetof-expression )
4226 __has_nothrow_assign ( type-id )
4227 __has_nothrow_constructor ( type-id )
4228 __has_nothrow_copy ( type-id )
4229 __has_trivial_assign ( type-id )
4230 __has_trivial_constructor ( type-id )
4231 __has_trivial_copy ( type-id )
4232 __has_trivial_destructor ( type-id )
4233 __has_virtual_destructor ( type-id )
4234 __is_abstract ( type-id )
4235 __is_base_of ( type-id , type-id )
4236 __is_class ( type-id )
4237 __is_empty ( type-id )
4238 __is_enum ( type-id )
4239 __is_final ( type-id )
4240 __is_literal_type ( type-id )
4241 __is_pod ( type-id )
4242 __is_polymorphic ( type-id )
4243 __is_std_layout ( type-id )
4244 __is_trivial ( type-id )
4245 __is_union ( type-id )
4247 Objective-C++ Extension:
4255 ADDRESS_P is true iff this expression was immediately preceded by
4256 "&" and therefore might denote a pointer-to-member. CAST_P is true
4257 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4258 true iff this expression is a template argument.
4260 Returns a representation of the expression. Upon return, *IDK
4261 indicates what kind of id-expression (if any) was present. */
4264 cp_parser_primary_expression (cp_parser
*parser
,
4267 bool template_arg_p
,
4271 cp_token
*token
= NULL
;
4273 /* Assume the primary expression is not an id-expression. */
4274 *idk
= CP_ID_KIND_NONE
;
4276 /* Peek at the next token. */
4277 token
= cp_lexer_peek_token (parser
->lexer
);
4278 switch ((int) token
->type
)
4287 user-defined-literal */
4293 case CPP_PREPARSED_EXPR
:
4294 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4295 return cp_parser_userdef_numeric_literal (parser
);
4296 token
= cp_lexer_consume_token (parser
->lexer
);
4297 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4299 error_at (token
->location
,
4300 "fixed-point types not supported in C++");
4301 return error_mark_node
;
4303 /* Floating-point literals are only allowed in an integral
4304 constant expression if they are cast to an integral or
4305 enumeration type. */
4306 if (TREE_CODE (token
->u
.value
) == REAL_CST
4307 && parser
->integral_constant_expression_p
4310 /* CAST_P will be set even in invalid code like "int(2.7 +
4311 ...)". Therefore, we have to check that the next token
4312 is sure to end the cast. */
4315 cp_token
*next_token
;
4317 next_token
= cp_lexer_peek_token (parser
->lexer
);
4318 if (/* The comma at the end of an
4319 enumerator-definition. */
4320 next_token
->type
!= CPP_COMMA
4321 /* The curly brace at the end of an enum-specifier. */
4322 && next_token
->type
!= CPP_CLOSE_BRACE
4323 /* The end of a statement. */
4324 && next_token
->type
!= CPP_SEMICOLON
4325 /* The end of the cast-expression. */
4326 && next_token
->type
!= CPP_CLOSE_PAREN
4327 /* The end of an array bound. */
4328 && next_token
->type
!= CPP_CLOSE_SQUARE
4329 /* The closing ">" in a template-argument-list. */
4330 && (next_token
->type
!= CPP_GREATER
4331 || parser
->greater_than_is_operator_p
)
4332 /* C++0x only: A ">>" treated like two ">" tokens,
4333 in a template-argument-list. */
4334 && (next_token
->type
!= CPP_RSHIFT
4335 || (cxx_dialect
== cxx98
)
4336 || parser
->greater_than_is_operator_p
))
4340 /* If we are within a cast, then the constraint that the
4341 cast is to an integral or enumeration type will be
4342 checked at that point. If we are not within a cast, then
4343 this code is invalid. */
4345 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4347 return token
->u
.value
;
4349 case CPP_CHAR_USERDEF
:
4350 case CPP_CHAR16_USERDEF
:
4351 case CPP_CHAR32_USERDEF
:
4352 case CPP_WCHAR_USERDEF
:
4353 return cp_parser_userdef_char_literal (parser
);
4359 case CPP_UTF8STRING
:
4360 case CPP_STRING_USERDEF
:
4361 case CPP_STRING16_USERDEF
:
4362 case CPP_STRING32_USERDEF
:
4363 case CPP_WSTRING_USERDEF
:
4364 case CPP_UTF8STRING_USERDEF
:
4365 /* ??? Should wide strings be allowed when parser->translate_strings_p
4366 is false (i.e. in attributes)? If not, we can kill the third
4367 argument to cp_parser_string_literal. */
4368 return cp_parser_string_literal (parser
,
4369 parser
->translate_strings_p
,
4372 case CPP_OPEN_PAREN
:
4373 /* If we see `( { ' then we are looking at the beginning of
4374 a GNU statement-expression. */
4375 if (cp_parser_allow_gnu_extensions_p (parser
)
4376 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4378 /* Statement-expressions are not allowed by the standard. */
4379 pedwarn (token
->location
, OPT_Wpedantic
,
4380 "ISO C++ forbids braced-groups within expressions");
4382 /* And they're not allowed outside of a function-body; you
4383 cannot, for example, write:
4385 int i = ({ int j = 3; j + 1; });
4387 at class or namespace scope. */
4388 if (!parser
->in_function_body
4389 || parser
->in_template_argument_list_p
)
4391 error_at (token
->location
,
4392 "statement-expressions are not allowed outside "
4393 "functions nor in template-argument lists");
4394 cp_parser_skip_to_end_of_block_or_statement (parser
);
4395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4396 cp_lexer_consume_token (parser
->lexer
);
4397 return error_mark_node
;
4400 return cp_parser_statement_expr (parser
);
4402 /* Otherwise it's a normal parenthesized expression. */
4405 bool saved_greater_than_is_operator_p
;
4407 /* Consume the `('. */
4408 cp_lexer_consume_token (parser
->lexer
);
4409 /* Within a parenthesized expression, a `>' token is always
4410 the greater-than operator. */
4411 saved_greater_than_is_operator_p
4412 = parser
->greater_than_is_operator_p
;
4413 parser
->greater_than_is_operator_p
= true;
4415 /* Parse the parenthesized expression. */
4416 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4417 /* Let the front end know that this expression was
4418 enclosed in parentheses. This matters in case, for
4419 example, the expression is of the form `A::B', since
4420 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4422 expr
= finish_parenthesized_expr (expr
);
4423 /* DR 705: Wrapping an unqualified name in parentheses
4424 suppresses arg-dependent lookup. We want to pass back
4425 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4426 (c++/37862), but none of the others. */
4427 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4428 *idk
= CP_ID_KIND_NONE
;
4430 /* The `>' token might be the end of a template-id or
4431 template-parameter-list now. */
4432 parser
->greater_than_is_operator_p
4433 = saved_greater_than_is_operator_p
;
4434 /* Consume the `)'. */
4435 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4436 cp_parser_skip_to_end_of_statement (parser
);
4441 case CPP_OPEN_SQUARE
:
4443 if (c_dialect_objc ())
4445 /* We might have an Objective-C++ message. */
4446 cp_parser_parse_tentatively (parser
);
4447 tree msg
= cp_parser_objc_message_expression (parser
);
4448 /* If that works out, we're done ... */
4449 if (cp_parser_parse_definitely (parser
))
4451 /* ... else, fall though to see if it's a lambda. */
4453 tree lam
= cp_parser_lambda_expression (parser
);
4454 /* Don't warn about a failed tentative parse. */
4455 if (cp_parser_error_occurred (parser
))
4456 return error_mark_node
;
4457 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4461 case CPP_OBJC_STRING
:
4462 if (c_dialect_objc ())
4463 /* We have an Objective-C++ string literal. */
4464 return cp_parser_objc_expression (parser
);
4465 cp_parser_error (parser
, "expected primary-expression");
4466 return error_mark_node
;
4469 switch (token
->keyword
)
4471 /* These two are the boolean literals. */
4473 cp_lexer_consume_token (parser
->lexer
);
4474 return boolean_true_node
;
4476 cp_lexer_consume_token (parser
->lexer
);
4477 return boolean_false_node
;
4479 /* The `__null' literal. */
4481 cp_lexer_consume_token (parser
->lexer
);
4484 /* The `nullptr' literal. */
4486 cp_lexer_consume_token (parser
->lexer
);
4487 return nullptr_node
;
4489 /* Recognize the `this' keyword. */
4491 cp_lexer_consume_token (parser
->lexer
);
4492 if (parser
->local_variables_forbidden_p
)
4494 error_at (token
->location
,
4495 "%<this%> may not be used in this context");
4496 return error_mark_node
;
4498 /* Pointers cannot appear in constant-expressions. */
4499 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4500 return error_mark_node
;
4501 return finish_this_expr ();
4503 /* The `operator' keyword can be the beginning of an
4508 case RID_FUNCTION_NAME
:
4509 case RID_PRETTY_FUNCTION_NAME
:
4510 case RID_C99_FUNCTION_NAME
:
4512 non_integral_constant name
;
4514 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4515 __func__ are the names of variables -- but they are
4516 treated specially. Therefore, they are handled here,
4517 rather than relying on the generic id-expression logic
4518 below. Grammatically, these names are id-expressions.
4520 Consume the token. */
4521 token
= cp_lexer_consume_token (parser
->lexer
);
4523 switch (token
->keyword
)
4525 case RID_FUNCTION_NAME
:
4526 name
= NIC_FUNC_NAME
;
4528 case RID_PRETTY_FUNCTION_NAME
:
4529 name
= NIC_PRETTY_FUNC
;
4531 case RID_C99_FUNCTION_NAME
:
4532 name
= NIC_C99_FUNC
;
4538 if (cp_parser_non_integral_constant_expression (parser
, name
))
4539 return error_mark_node
;
4541 /* Look up the name. */
4542 return finish_fname (token
->u
.value
);
4549 source_location type_location
;
4551 /* The `__builtin_va_arg' construct is used to handle
4552 `va_arg'. Consume the `__builtin_va_arg' token. */
4553 cp_lexer_consume_token (parser
->lexer
);
4554 /* Look for the opening `('. */
4555 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4556 /* Now, parse the assignment-expression. */
4557 expression
= cp_parser_assignment_expression (parser
);
4558 /* Look for the `,'. */
4559 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4560 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4561 /* Parse the type-id. */
4562 type
= cp_parser_type_id (parser
);
4563 /* Look for the closing `)'. */
4564 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4565 /* Using `va_arg' in a constant-expression is not
4567 if (cp_parser_non_integral_constant_expression (parser
,
4569 return error_mark_node
;
4570 return build_x_va_arg (type_location
, expression
, type
);
4574 return cp_parser_builtin_offsetof (parser
);
4576 case RID_HAS_NOTHROW_ASSIGN
:
4577 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4578 case RID_HAS_NOTHROW_COPY
:
4579 case RID_HAS_TRIVIAL_ASSIGN
:
4580 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4581 case RID_HAS_TRIVIAL_COPY
:
4582 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4583 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4584 case RID_IS_ABSTRACT
:
4585 case RID_IS_BASE_OF
:
4590 case RID_IS_LITERAL_TYPE
:
4592 case RID_IS_POLYMORPHIC
:
4593 case RID_IS_STD_LAYOUT
:
4594 case RID_IS_TRIVIAL
:
4595 case RID_IS_TRIVIALLY_ASSIGNABLE
:
4596 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
4597 case RID_IS_TRIVIALLY_COPYABLE
:
4599 return cp_parser_trait_expr (parser
, token
->keyword
);
4601 /* Objective-C++ expressions. */
4603 case RID_AT_PROTOCOL
:
4604 case RID_AT_SELECTOR
:
4605 return cp_parser_objc_expression (parser
);
4608 if (parser
->in_function_body
4609 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4612 error_at (token
->location
,
4613 "a template declaration cannot appear at block scope");
4614 cp_parser_skip_to_end_of_block_or_statement (parser
);
4615 return error_mark_node
;
4618 cp_parser_error (parser
, "expected primary-expression");
4619 return error_mark_node
;
4622 /* An id-expression can start with either an identifier, a
4623 `::' as the beginning of a qualified-id, or the "operator"
4627 case CPP_TEMPLATE_ID
:
4628 case CPP_NESTED_NAME_SPECIFIER
:
4632 const char *error_msg
;
4635 cp_token
*id_expr_token
;
4638 /* Parse the id-expression. */
4640 = cp_parser_id_expression (parser
,
4641 /*template_keyword_p=*/false,
4642 /*check_dependency_p=*/true,
4644 /*declarator_p=*/false,
4645 /*optional_p=*/false);
4646 if (id_expression
== error_mark_node
)
4647 return error_mark_node
;
4648 id_expr_token
= token
;
4649 token
= cp_lexer_peek_token (parser
->lexer
);
4650 done
= (token
->type
!= CPP_OPEN_SQUARE
4651 && token
->type
!= CPP_OPEN_PAREN
4652 && token
->type
!= CPP_DOT
4653 && token
->type
!= CPP_DEREF
4654 && token
->type
!= CPP_PLUS_PLUS
4655 && token
->type
!= CPP_MINUS_MINUS
);
4656 /* If we have a template-id, then no further lookup is
4657 required. If the template-id was for a template-class, we
4658 will sometimes have a TYPE_DECL at this point. */
4659 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4660 || TREE_CODE (id_expression
) == TYPE_DECL
)
4661 decl
= id_expression
;
4662 /* Look up the name. */
4665 tree ambiguous_decls
;
4667 /* If we already know that this lookup is ambiguous, then
4668 we've already issued an error message; there's no reason
4670 if (id_expr_token
->type
== CPP_NAME
4671 && id_expr_token
->error_reported
)
4673 cp_parser_simulate_error (parser
);
4674 return error_mark_node
;
4677 decl
= cp_parser_lookup_name (parser
, id_expression
,
4680 /*is_namespace=*/false,
4681 /*check_dependency=*/true,
4683 id_expr_token
->location
);
4684 /* If the lookup was ambiguous, an error will already have
4686 if (ambiguous_decls
)
4687 return error_mark_node
;
4689 /* In Objective-C++, we may have an Objective-C 2.0
4690 dot-syntax for classes here. */
4691 if (c_dialect_objc ()
4692 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4693 && TREE_CODE (decl
) == TYPE_DECL
4694 && objc_is_class_name (decl
))
4697 cp_lexer_consume_token (parser
->lexer
);
4698 component
= cp_parser_identifier (parser
);
4699 if (component
== error_mark_node
)
4700 return error_mark_node
;
4702 return objc_build_class_component_ref (id_expression
, component
);
4705 /* In Objective-C++, an instance variable (ivar) may be preferred
4706 to whatever cp_parser_lookup_name() found. */
4707 decl
= objc_lookup_ivar (decl
, id_expression
);
4709 /* If name lookup gives us a SCOPE_REF, then the
4710 qualifying scope was dependent. */
4711 if (TREE_CODE (decl
) == SCOPE_REF
)
4713 /* At this point, we do not know if DECL is a valid
4714 integral constant expression. We assume that it is
4715 in fact such an expression, so that code like:
4717 template <int N> struct A {
4721 is accepted. At template-instantiation time, we
4722 will check that B<N>::i is actually a constant. */
4725 /* Check to see if DECL is a local variable in a context
4726 where that is forbidden. */
4727 if (parser
->local_variables_forbidden_p
4728 && local_variable_p (decl
))
4730 /* It might be that we only found DECL because we are
4731 trying to be generous with pre-ISO scoping rules.
4732 For example, consider:
4736 for (int i = 0; i < 10; ++i) {}
4737 extern void f(int j = i);
4740 Here, name look up will originally find the out
4741 of scope `i'. We need to issue a warning message,
4742 but then use the global `i'. */
4743 decl
= check_for_out_of_scope_variable (decl
);
4744 if (local_variable_p (decl
))
4746 error_at (id_expr_token
->location
,
4747 "local variable %qD may not appear in this context",
4749 return error_mark_node
;
4754 decl
= (finish_id_expression
4755 (id_expression
, decl
, parser
->scope
,
4757 parser
->integral_constant_expression_p
,
4758 parser
->allow_non_integral_constant_expression_p
,
4759 &parser
->non_integral_constant_expression_p
,
4760 template_p
, done
, address_p
,
4763 id_expr_token
->location
));
4765 cp_parser_error (parser
, error_msg
);
4769 /* Anything else is an error. */
4771 cp_parser_error (parser
, "expected primary-expression");
4772 return error_mark_node
;
4777 cp_parser_primary_expression (cp_parser
*parser
,
4780 bool template_arg_p
,
4783 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4784 /*decltype*/false, idk
);
4787 /* Parse an id-expression.
4794 :: [opt] nested-name-specifier template [opt] unqualified-id
4796 :: operator-function-id
4799 Return a representation of the unqualified portion of the
4800 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4801 a `::' or nested-name-specifier.
4803 Often, if the id-expression was a qualified-id, the caller will
4804 want to make a SCOPE_REF to represent the qualified-id. This
4805 function does not do this in order to avoid wastefully creating
4806 SCOPE_REFs when they are not required.
4808 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4811 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4812 uninstantiated templates.
4814 If *TEMPLATE_P is non-NULL, it is set to true iff the
4815 `template' keyword is used to explicitly indicate that the entity
4816 named is a template.
4818 If DECLARATOR_P is true, the id-expression is appearing as part of
4819 a declarator, rather than as part of an expression. */
4822 cp_parser_id_expression (cp_parser
*parser
,
4823 bool template_keyword_p
,
4824 bool check_dependency_p
,
4829 bool global_scope_p
;
4830 bool nested_name_specifier_p
;
4832 /* Assume the `template' keyword was not used. */
4834 *template_p
= template_keyword_p
;
4836 /* Look for the optional `::' operator. */
4838 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4840 /* Look for the optional nested-name-specifier. */
4841 nested_name_specifier_p
4842 = (cp_parser_nested_name_specifier_opt (parser
,
4843 /*typename_keyword_p=*/false,
4848 /* If there is a nested-name-specifier, then we are looking at
4849 the first qualified-id production. */
4850 if (nested_name_specifier_p
)
4853 tree saved_object_scope
;
4854 tree saved_qualifying_scope
;
4855 tree unqualified_id
;
4858 /* See if the next token is the `template' keyword. */
4860 template_p
= &is_template
;
4861 *template_p
= cp_parser_optional_template_keyword (parser
);
4862 /* Name lookup we do during the processing of the
4863 unqualified-id might obliterate SCOPE. */
4864 saved_scope
= parser
->scope
;
4865 saved_object_scope
= parser
->object_scope
;
4866 saved_qualifying_scope
= parser
->qualifying_scope
;
4867 /* Process the final unqualified-id. */
4868 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4871 /*optional_p=*/false);
4872 /* Restore the SAVED_SCOPE for our caller. */
4873 parser
->scope
= saved_scope
;
4874 parser
->object_scope
= saved_object_scope
;
4875 parser
->qualifying_scope
= saved_qualifying_scope
;
4877 return unqualified_id
;
4879 /* Otherwise, if we are in global scope, then we are looking at one
4880 of the other qualified-id productions. */
4881 else if (global_scope_p
)
4886 /* Peek at the next token. */
4887 token
= cp_lexer_peek_token (parser
->lexer
);
4889 /* If it's an identifier, and the next token is not a "<", then
4890 we can avoid the template-id case. This is an optimization
4891 for this common case. */
4892 if (token
->type
== CPP_NAME
4893 && !cp_parser_nth_token_starts_template_argument_list_p
4895 return cp_parser_identifier (parser
);
4897 cp_parser_parse_tentatively (parser
);
4898 /* Try a template-id. */
4899 id
= cp_parser_template_id (parser
,
4900 /*template_keyword_p=*/false,
4901 /*check_dependency_p=*/true,
4904 /* If that worked, we're done. */
4905 if (cp_parser_parse_definitely (parser
))
4908 /* Peek at the next token. (Changes in the token buffer may
4909 have invalidated the pointer obtained above.) */
4910 token
= cp_lexer_peek_token (parser
->lexer
);
4912 switch (token
->type
)
4915 return cp_parser_identifier (parser
);
4918 if (token
->keyword
== RID_OPERATOR
)
4919 return cp_parser_operator_function_id (parser
);
4923 cp_parser_error (parser
, "expected id-expression");
4924 return error_mark_node
;
4928 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4929 /*check_dependency_p=*/true,
4934 /* Parse an unqualified-id.
4938 operator-function-id
4939 conversion-function-id
4943 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4944 keyword, in a construct like `A::template ...'.
4946 Returns a representation of unqualified-id. For the `identifier'
4947 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4948 production a BIT_NOT_EXPR is returned; the operand of the
4949 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4950 other productions, see the documentation accompanying the
4951 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4952 names are looked up in uninstantiated templates. If DECLARATOR_P
4953 is true, the unqualified-id is appearing as part of a declarator,
4954 rather than as part of an expression. */
4957 cp_parser_unqualified_id (cp_parser
* parser
,
4958 bool template_keyword_p
,
4959 bool check_dependency_p
,
4965 /* Peek at the next token. */
4966 token
= cp_lexer_peek_token (parser
->lexer
);
4968 switch ((int) token
->type
)
4974 /* We don't know yet whether or not this will be a
4976 cp_parser_parse_tentatively (parser
);
4977 /* Try a template-id. */
4978 id
= cp_parser_template_id (parser
, template_keyword_p
,
4982 /* If it worked, we're done. */
4983 if (cp_parser_parse_definitely (parser
))
4985 /* Otherwise, it's an ordinary identifier. */
4986 return cp_parser_identifier (parser
);
4989 case CPP_TEMPLATE_ID
:
4990 return cp_parser_template_id (parser
, template_keyword_p
,
4998 tree qualifying_scope
;
5003 /* Consume the `~' token. */
5004 cp_lexer_consume_token (parser
->lexer
);
5005 /* Parse the class-name. The standard, as written, seems to
5008 template <typename T> struct S { ~S (); };
5009 template <typename T> S<T>::~S() {}
5011 is invalid, since `~' must be followed by a class-name, but
5012 `S<T>' is dependent, and so not known to be a class.
5013 That's not right; we need to look in uninstantiated
5014 templates. A further complication arises from:
5016 template <typename T> void f(T t) {
5020 Here, it is not possible to look up `T' in the scope of `T'
5021 itself. We must look in both the current scope, and the
5022 scope of the containing complete expression.
5024 Yet another issue is:
5033 The standard does not seem to say that the `S' in `~S'
5034 should refer to the type `S' and not the data member
5037 /* DR 244 says that we look up the name after the "~" in the
5038 same scope as we looked up the qualifying name. That idea
5039 isn't fully worked out; it's more complicated than that. */
5040 scope
= parser
->scope
;
5041 object_scope
= parser
->object_scope
;
5042 qualifying_scope
= parser
->qualifying_scope
;
5044 /* Check for invalid scopes. */
5045 if (scope
== error_mark_node
)
5047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5048 cp_lexer_consume_token (parser
->lexer
);
5049 return error_mark_node
;
5051 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5054 error_at (token
->location
,
5055 "scope %qT before %<~%> is not a class-name",
5057 cp_parser_simulate_error (parser
);
5058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5059 cp_lexer_consume_token (parser
->lexer
);
5060 return error_mark_node
;
5062 gcc_assert (!scope
|| TYPE_P (scope
));
5064 /* If the name is of the form "X::~X" it's OK even if X is a
5066 token
= cp_lexer_peek_token (parser
->lexer
);
5068 && token
->type
== CPP_NAME
5069 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5071 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5072 || (CLASS_TYPE_P (scope
)
5073 && constructor_name_p (token
->u
.value
, scope
))))
5075 cp_lexer_consume_token (parser
->lexer
);
5076 return build_nt (BIT_NOT_EXPR
, scope
);
5079 /* ~auto means the destructor of whatever the object is. */
5080 if (cp_parser_is_keyword (token
, RID_AUTO
))
5082 if (cxx_dialect
< cxx14
)
5083 pedwarn (input_location
, 0,
5084 "%<~auto%> only available with "
5085 "-std=c++14 or -std=gnu++14");
5086 cp_lexer_consume_token (parser
->lexer
);
5087 return build_nt (BIT_NOT_EXPR
, make_auto ());
5090 /* If there was an explicit qualification (S::~T), first look
5091 in the scope given by the qualification (i.e., S).
5093 Note: in the calls to cp_parser_class_name below we pass
5094 typename_type so that lookup finds the injected-class-name
5095 rather than the constructor. */
5097 type_decl
= NULL_TREE
;
5100 cp_parser_parse_tentatively (parser
);
5101 type_decl
= cp_parser_class_name (parser
,
5102 /*typename_keyword_p=*/false,
5103 /*template_keyword_p=*/false,
5105 /*check_dependency=*/false,
5106 /*class_head_p=*/false,
5108 if (cp_parser_parse_definitely (parser
))
5111 /* In "N::S::~S", look in "N" as well. */
5112 if (!done
&& scope
&& qualifying_scope
)
5114 cp_parser_parse_tentatively (parser
);
5115 parser
->scope
= qualifying_scope
;
5116 parser
->object_scope
= NULL_TREE
;
5117 parser
->qualifying_scope
= NULL_TREE
;
5119 = cp_parser_class_name (parser
,
5120 /*typename_keyword_p=*/false,
5121 /*template_keyword_p=*/false,
5123 /*check_dependency=*/false,
5124 /*class_head_p=*/false,
5126 if (cp_parser_parse_definitely (parser
))
5129 /* In "p->S::~T", look in the scope given by "*p" as well. */
5130 else if (!done
&& object_scope
)
5132 cp_parser_parse_tentatively (parser
);
5133 parser
->scope
= object_scope
;
5134 parser
->object_scope
= NULL_TREE
;
5135 parser
->qualifying_scope
= NULL_TREE
;
5137 = cp_parser_class_name (parser
,
5138 /*typename_keyword_p=*/false,
5139 /*template_keyword_p=*/false,
5141 /*check_dependency=*/false,
5142 /*class_head_p=*/false,
5144 if (cp_parser_parse_definitely (parser
))
5147 /* Look in the surrounding context. */
5150 parser
->scope
= NULL_TREE
;
5151 parser
->object_scope
= NULL_TREE
;
5152 parser
->qualifying_scope
= NULL_TREE
;
5153 if (processing_template_decl
)
5154 cp_parser_parse_tentatively (parser
);
5156 = cp_parser_class_name (parser
,
5157 /*typename_keyword_p=*/false,
5158 /*template_keyword_p=*/false,
5160 /*check_dependency=*/false,
5161 /*class_head_p=*/false,
5163 if (processing_template_decl
5164 && ! cp_parser_parse_definitely (parser
))
5166 /* We couldn't find a type with this name, so just accept
5167 it and check for a match at instantiation time. */
5168 type_decl
= cp_parser_identifier (parser
);
5169 if (type_decl
!= error_mark_node
)
5170 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5174 /* If an error occurred, assume that the name of the
5175 destructor is the same as the name of the qualifying
5176 class. That allows us to keep parsing after running
5177 into ill-formed destructor names. */
5178 if (type_decl
== error_mark_node
&& scope
)
5179 return build_nt (BIT_NOT_EXPR
, scope
);
5180 else if (type_decl
== error_mark_node
)
5181 return error_mark_node
;
5183 /* Check that destructor name and scope match. */
5184 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5186 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5187 error_at (token
->location
,
5188 "declaration of %<~%T%> as member of %qT",
5190 cp_parser_simulate_error (parser
);
5191 return error_mark_node
;
5196 A typedef-name that names a class shall not be used as the
5197 identifier in the declarator for a destructor declaration. */
5199 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5200 && !DECL_SELF_REFERENCE_P (type_decl
)
5201 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5202 error_at (token
->location
,
5203 "typedef-name %qD used as destructor declarator",
5206 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5210 if (token
->keyword
== RID_OPERATOR
)
5214 /* This could be a template-id, so we try that first. */
5215 cp_parser_parse_tentatively (parser
);
5216 /* Try a template-id. */
5217 id
= cp_parser_template_id (parser
, template_keyword_p
,
5218 /*check_dependency_p=*/true,
5221 /* If that worked, we're done. */
5222 if (cp_parser_parse_definitely (parser
))
5224 /* We still don't know whether we're looking at an
5225 operator-function-id or a conversion-function-id. */
5226 cp_parser_parse_tentatively (parser
);
5227 /* Try an operator-function-id. */
5228 id
= cp_parser_operator_function_id (parser
);
5229 /* If that didn't work, try a conversion-function-id. */
5230 if (!cp_parser_parse_definitely (parser
))
5231 id
= cp_parser_conversion_function_id (parser
);
5232 else if (UDLIT_OPER_P (id
))
5235 const char *name
= UDLIT_OP_SUFFIX (id
);
5236 if (name
[0] != '_' && !in_system_header_at (input_location
)
5238 warning (0, "literal operator suffixes not preceded by %<_%>"
5239 " are reserved for future standardization");
5249 cp_parser_error (parser
, "expected unqualified-id");
5250 return error_mark_node
;
5254 /* Parse an (optional) nested-name-specifier.
5256 nested-name-specifier: [C++98]
5257 class-or-namespace-name :: nested-name-specifier [opt]
5258 class-or-namespace-name :: template nested-name-specifier [opt]
5260 nested-name-specifier: [C++0x]
5263 nested-name-specifier identifier ::
5264 nested-name-specifier template [opt] simple-template-id ::
5266 PARSER->SCOPE should be set appropriately before this function is
5267 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5268 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5271 Sets PARSER->SCOPE to the class (TYPE) or namespace
5272 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5273 it unchanged if there is no nested-name-specifier. Returns the new
5274 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5276 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5277 part of a declaration and/or decl-specifier. */
5280 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5281 bool typename_keyword_p
,
5282 bool check_dependency_p
,
5284 bool is_declaration
)
5286 bool success
= false;
5287 cp_token_position start
= 0;
5290 /* Remember where the nested-name-specifier starts. */
5291 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5293 start
= cp_lexer_token_position (parser
->lexer
, false);
5294 push_deferring_access_checks (dk_deferred
);
5301 tree saved_qualifying_scope
;
5302 bool template_keyword_p
;
5304 /* Spot cases that cannot be the beginning of a
5305 nested-name-specifier. */
5306 token
= cp_lexer_peek_token (parser
->lexer
);
5308 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5309 the already parsed nested-name-specifier. */
5310 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5312 /* Grab the nested-name-specifier and continue the loop. */
5313 cp_parser_pre_parsed_nested_name_specifier (parser
);
5314 /* If we originally encountered this nested-name-specifier
5315 with IS_DECLARATION set to false, we will not have
5316 resolved TYPENAME_TYPEs, so we must do so here. */
5318 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5320 new_scope
= resolve_typename_type (parser
->scope
,
5321 /*only_current_p=*/false);
5322 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5323 parser
->scope
= new_scope
;
5329 /* Spot cases that cannot be the beginning of a
5330 nested-name-specifier. On the second and subsequent times
5331 through the loop, we look for the `template' keyword. */
5332 if (success
&& token
->keyword
== RID_TEMPLATE
)
5334 /* A template-id can start a nested-name-specifier. */
5335 else if (token
->type
== CPP_TEMPLATE_ID
)
5337 /* DR 743: decltype can be used in a nested-name-specifier. */
5338 else if (token_is_decltype (token
))
5342 /* If the next token is not an identifier, then it is
5343 definitely not a type-name or namespace-name. */
5344 if (token
->type
!= CPP_NAME
)
5346 /* If the following token is neither a `<' (to begin a
5347 template-id), nor a `::', then we are not looking at a
5348 nested-name-specifier. */
5349 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5351 if (token
->type
== CPP_COLON
5352 && parser
->colon_corrects_to_scope_p
5353 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5355 error_at (token
->location
,
5356 "found %<:%> in nested-name-specifier, expected %<::%>");
5357 token
->type
= CPP_SCOPE
;
5360 if (token
->type
!= CPP_SCOPE
5361 && !cp_parser_nth_token_starts_template_argument_list_p
5366 /* The nested-name-specifier is optional, so we parse
5368 cp_parser_parse_tentatively (parser
);
5370 /* Look for the optional `template' keyword, if this isn't the
5371 first time through the loop. */
5373 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5375 template_keyword_p
= false;
5377 /* Save the old scope since the name lookup we are about to do
5378 might destroy it. */
5379 old_scope
= parser
->scope
;
5380 saved_qualifying_scope
= parser
->qualifying_scope
;
5381 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5382 look up names in "X<T>::I" in order to determine that "Y" is
5383 a template. So, if we have a typename at this point, we make
5384 an effort to look through it. */
5386 && !typename_keyword_p
5388 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5389 parser
->scope
= resolve_typename_type (parser
->scope
,
5390 /*only_current_p=*/false);
5391 /* Parse the qualifying entity. */
5393 = cp_parser_qualifying_entity (parser
,
5399 /* Look for the `::' token. */
5400 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5402 /* If we found what we wanted, we keep going; otherwise, we're
5404 if (!cp_parser_parse_definitely (parser
))
5406 bool error_p
= false;
5408 /* Restore the OLD_SCOPE since it was valid before the
5409 failed attempt at finding the last
5410 class-or-namespace-name. */
5411 parser
->scope
= old_scope
;
5412 parser
->qualifying_scope
= saved_qualifying_scope
;
5414 /* If the next token is a decltype, and the one after that is a
5415 `::', then the decltype has failed to resolve to a class or
5416 enumeration type. Give this error even when parsing
5417 tentatively since it can't possibly be valid--and we're going
5418 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5419 won't get another chance.*/
5420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5421 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5424 token
= cp_lexer_consume_token (parser
->lexer
);
5425 error_at (token
->location
, "decltype evaluates to %qT, "
5426 "which is not a class or enumeration type",
5428 parser
->scope
= error_mark_node
;
5432 cp_lexer_consume_token (parser
->lexer
);
5435 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
5436 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
5438 /* If we have a non-type template-id followed by ::, it can't
5439 possibly be valid. */
5440 token
= cp_lexer_peek_token (parser
->lexer
);
5441 tree tid
= token
->u
.tree_check_value
->value
;
5442 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
5443 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
5445 tree tmpl
= NULL_TREE
;
5446 if (is_overloaded_fn (tid
))
5448 tree fns
= get_fns (tid
);
5449 if (!OVL_CHAIN (fns
))
5450 tmpl
= OVL_CURRENT (fns
);
5451 error_at (token
->location
, "function template-id %qD "
5452 "in nested-name-specifier", tid
);
5456 /* Variable template. */
5457 tmpl
= TREE_OPERAND (tid
, 0);
5458 gcc_assert (variable_template_p (tmpl
));
5459 error_at (token
->location
, "variable template-id %qD "
5460 "in nested-name-specifier", tid
);
5463 inform (DECL_SOURCE_LOCATION (tmpl
),
5464 "%qD declared here", tmpl
);
5466 parser
->scope
= error_mark_node
;
5470 cp_lexer_consume_token (parser
->lexer
);
5471 cp_lexer_consume_token (parser
->lexer
);
5475 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5477 /* If the next token is an identifier, and the one after
5478 that is a `::', then any valid interpretation would have
5479 found a class-or-namespace-name. */
5480 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5481 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5483 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5486 token
= cp_lexer_consume_token (parser
->lexer
);
5489 if (!token
->error_reported
)
5492 tree ambiguous_decls
;
5494 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5496 /*is_template=*/false,
5497 /*is_namespace=*/false,
5498 /*check_dependency=*/true,
5501 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5502 error_at (token
->location
,
5503 "%qD used without template parameters",
5505 else if (ambiguous_decls
)
5507 // cp_parser_lookup_name has the same diagnostic,
5508 // thus make sure to emit it at most once.
5509 if (cp_parser_uncommitted_to_tentative_parse_p
5512 error_at (token
->location
,
5513 "reference to %qD is ambiguous",
5515 print_candidates (ambiguous_decls
);
5517 decl
= error_mark_node
;
5521 if (cxx_dialect
!= cxx98
)
5522 cp_parser_name_lookup_error
5523 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5526 cp_parser_name_lookup_error
5527 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5531 parser
->scope
= error_mark_node
;
5533 /* Treat this as a successful nested-name-specifier
5538 If the name found is not a class-name (clause
5539 _class_) or namespace-name (_namespace.def_), the
5540 program is ill-formed. */
5543 cp_lexer_consume_token (parser
->lexer
);
5547 /* We've found one valid nested-name-specifier. */
5549 /* Name lookup always gives us a DECL. */
5550 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5551 new_scope
= TREE_TYPE (new_scope
);
5552 /* Uses of "template" must be followed by actual templates. */
5553 if (template_keyword_p
5554 && !(CLASS_TYPE_P (new_scope
)
5555 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5556 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5557 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5558 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5559 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5560 == TEMPLATE_ID_EXPR
)))
5561 permerror (input_location
, TYPE_P (new_scope
)
5562 ? G_("%qT is not a template")
5563 : G_("%qD is not a template"),
5565 /* If it is a class scope, try to complete it; we are about to
5566 be looking up names inside the class. */
5567 if (TYPE_P (new_scope
)
5568 /* Since checking types for dependency can be expensive,
5569 avoid doing it if the type is already complete. */
5570 && !COMPLETE_TYPE_P (new_scope
)
5571 /* Do not try to complete dependent types. */
5572 && !dependent_type_p (new_scope
))
5574 new_scope
= complete_type (new_scope
);
5575 /* If it is a typedef to current class, use the current
5576 class instead, as the typedef won't have any names inside
5578 if (!COMPLETE_TYPE_P (new_scope
)
5579 && currently_open_class (new_scope
))
5580 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5582 /* Make sure we look in the right scope the next time through
5584 parser
->scope
= new_scope
;
5587 /* If parsing tentatively, replace the sequence of tokens that makes
5588 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5589 token. That way, should we re-parse the token stream, we will
5590 not have to repeat the effort required to do the parse, nor will
5591 we issue duplicate error messages. */
5592 if (success
&& start
)
5596 token
= cp_lexer_token_at (parser
->lexer
, start
);
5597 /* Reset the contents of the START token. */
5598 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5599 /* Retrieve any deferred checks. Do not pop this access checks yet
5600 so the memory will not be reclaimed during token replacing below. */
5601 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
5602 token
->u
.tree_check_value
->value
= parser
->scope
;
5603 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5604 token
->u
.tree_check_value
->qualifying_scope
=
5605 parser
->qualifying_scope
;
5606 token
->keyword
= RID_MAX
;
5608 /* Purge all subsequent tokens. */
5609 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5613 pop_to_parent_deferring_access_checks ();
5615 return success
? parser
->scope
: NULL_TREE
;
5618 /* Parse a nested-name-specifier. See
5619 cp_parser_nested_name_specifier_opt for details. This function
5620 behaves identically, except that it will an issue an error if no
5621 nested-name-specifier is present. */
5624 cp_parser_nested_name_specifier (cp_parser
*parser
,
5625 bool typename_keyword_p
,
5626 bool check_dependency_p
,
5628 bool is_declaration
)
5632 /* Look for the nested-name-specifier. */
5633 scope
= cp_parser_nested_name_specifier_opt (parser
,
5638 /* If it was not present, issue an error message. */
5641 cp_parser_error (parser
, "expected nested-name-specifier");
5642 parser
->scope
= NULL_TREE
;
5648 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5649 this is either a class-name or a namespace-name (which corresponds
5650 to the class-or-namespace-name production in the grammar). For
5651 C++0x, it can also be a type-name that refers to an enumeration
5652 type or a simple-template-id.
5654 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5655 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5656 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5657 TYPE_P is TRUE iff the next name should be taken as a class-name,
5658 even the same name is declared to be another entity in the same
5661 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5662 specified by the class-or-namespace-name. If neither is found the
5663 ERROR_MARK_NODE is returned. */
5666 cp_parser_qualifying_entity (cp_parser
*parser
,
5667 bool typename_keyword_p
,
5668 bool template_keyword_p
,
5669 bool check_dependency_p
,
5671 bool is_declaration
)
5674 tree saved_qualifying_scope
;
5675 tree saved_object_scope
;
5678 bool successful_parse_p
;
5680 /* DR 743: decltype can appear in a nested-name-specifier. */
5681 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5683 scope
= cp_parser_decltype (parser
);
5684 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5685 && !MAYBE_CLASS_TYPE_P (scope
))
5687 cp_parser_simulate_error (parser
);
5688 return error_mark_node
;
5690 if (TYPE_NAME (scope
))
5691 scope
= TYPE_NAME (scope
);
5695 /* Before we try to parse the class-name, we must save away the
5696 current PARSER->SCOPE since cp_parser_class_name will destroy
5698 saved_scope
= parser
->scope
;
5699 saved_qualifying_scope
= parser
->qualifying_scope
;
5700 saved_object_scope
= parser
->object_scope
;
5701 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5702 there is no need to look for a namespace-name. */
5703 only_class_p
= template_keyword_p
5704 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5706 cp_parser_parse_tentatively (parser
);
5707 scope
= cp_parser_class_name (parser
,
5710 type_p
? class_type
: none_type
,
5712 /*class_head_p=*/false,
5714 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5715 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5717 && cxx_dialect
!= cxx98
5718 && !successful_parse_p
)
5720 /* Restore the saved scope. */
5721 parser
->scope
= saved_scope
;
5722 parser
->qualifying_scope
= saved_qualifying_scope
;
5723 parser
->object_scope
= saved_object_scope
;
5725 /* Parse tentatively. */
5726 cp_parser_parse_tentatively (parser
);
5728 /* Parse a type-name */
5729 scope
= cp_parser_type_name (parser
);
5731 /* "If the name found does not designate a namespace or a class,
5732 enumeration, or dependent type, the program is ill-formed."
5734 We cover classes and dependent types above and namespaces below,
5735 so this code is only looking for enums. */
5736 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5737 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5738 cp_parser_simulate_error (parser
);
5740 successful_parse_p
= cp_parser_parse_definitely (parser
);
5742 /* If that didn't work, try for a namespace-name. */
5743 if (!only_class_p
&& !successful_parse_p
)
5745 /* Restore the saved scope. */
5746 parser
->scope
= saved_scope
;
5747 parser
->qualifying_scope
= saved_qualifying_scope
;
5748 parser
->object_scope
= saved_object_scope
;
5749 /* If we are not looking at an identifier followed by the scope
5750 resolution operator, then this is not part of a
5751 nested-name-specifier. (Note that this function is only used
5752 to parse the components of a nested-name-specifier.) */
5753 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5754 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5755 return error_mark_node
;
5756 scope
= cp_parser_namespace_name (parser
);
5762 /* Return true if we are looking at a compound-literal, false otherwise. */
5765 cp_parser_compound_literal_p (cp_parser
*parser
)
5767 /* Consume the `('. */
5768 cp_lexer_consume_token (parser
->lexer
);
5770 cp_lexer_save_tokens (parser
->lexer
);
5772 /* Skip tokens until the next token is a closing parenthesis.
5773 If we find the closing `)', and the next token is a `{', then
5774 we are looking at a compound-literal. */
5775 bool compound_literal_p
5776 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5777 /*consume_paren=*/true)
5778 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5780 /* Roll back the tokens we skipped. */
5781 cp_lexer_rollback_tokens (parser
->lexer
);
5783 return compound_literal_p
;
5786 /* Parse a postfix-expression.
5790 postfix-expression [ expression ]
5791 postfix-expression ( expression-list [opt] )
5792 simple-type-specifier ( expression-list [opt] )
5793 typename :: [opt] nested-name-specifier identifier
5794 ( expression-list [opt] )
5795 typename :: [opt] nested-name-specifier template [opt] template-id
5796 ( expression-list [opt] )
5797 postfix-expression . template [opt] id-expression
5798 postfix-expression -> template [opt] id-expression
5799 postfix-expression . pseudo-destructor-name
5800 postfix-expression -> pseudo-destructor-name
5801 postfix-expression ++
5802 postfix-expression --
5803 dynamic_cast < type-id > ( expression )
5804 static_cast < type-id > ( expression )
5805 reinterpret_cast < type-id > ( expression )
5806 const_cast < type-id > ( expression )
5807 typeid ( expression )
5813 ( type-id ) { initializer-list , [opt] }
5815 This extension is a GNU version of the C99 compound-literal
5816 construct. (The C99 grammar uses `type-name' instead of `type-id',
5817 but they are essentially the same concept.)
5819 If ADDRESS_P is true, the postfix expression is the operand of the
5820 `&' operator. CAST_P is true if this expression is the target of a
5823 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5824 class member access expressions [expr.ref].
5826 Returns a representation of the expression. */
5829 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5830 bool member_access_only_p
, bool decltype_p
,
5831 cp_id_kind
* pidk_return
)
5836 cp_id_kind idk
= CP_ID_KIND_NONE
;
5837 tree postfix_expression
= NULL_TREE
;
5838 bool is_member_access
= false;
5839 int saved_in_statement
= -1;
5841 /* Peek at the next token. */
5842 token
= cp_lexer_peek_token (parser
->lexer
);
5843 loc
= token
->location
;
5844 /* Some of the productions are determined by keywords. */
5845 keyword
= token
->keyword
;
5855 const char *saved_message
;
5856 bool saved_in_type_id_in_expr_p
;
5858 /* All of these can be handled in the same way from the point
5859 of view of parsing. Begin by consuming the token
5860 identifying the cast. */
5861 cp_lexer_consume_token (parser
->lexer
);
5863 /* New types cannot be defined in the cast. */
5864 saved_message
= parser
->type_definition_forbidden_message
;
5865 parser
->type_definition_forbidden_message
5866 = G_("types may not be defined in casts");
5868 /* Look for the opening `<'. */
5869 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5870 /* Parse the type to which we are casting. */
5871 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5872 parser
->in_type_id_in_expr_p
= true;
5873 type
= cp_parser_type_id (parser
);
5874 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5875 /* Look for the closing `>'. */
5876 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5877 /* Restore the old message. */
5878 parser
->type_definition_forbidden_message
= saved_message
;
5880 bool saved_greater_than_is_operator_p
5881 = parser
->greater_than_is_operator_p
;
5882 parser
->greater_than_is_operator_p
= true;
5884 /* And the expression which is being cast. */
5885 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5886 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
5887 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5889 parser
->greater_than_is_operator_p
5890 = saved_greater_than_is_operator_p
;
5892 /* Only type conversions to integral or enumeration types
5893 can be used in constant-expressions. */
5894 if (!cast_valid_in_integral_constant_expression_p (type
)
5895 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5896 return error_mark_node
;
5902 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5906 = build_static_cast (type
, expression
, tf_warning_or_error
);
5910 = build_reinterpret_cast (type
, expression
,
5911 tf_warning_or_error
);
5915 = build_const_cast (type
, expression
, tf_warning_or_error
);
5926 const char *saved_message
;
5927 bool saved_in_type_id_in_expr_p
;
5929 /* Consume the `typeid' token. */
5930 cp_lexer_consume_token (parser
->lexer
);
5931 /* Look for the `(' token. */
5932 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5933 /* Types cannot be defined in a `typeid' expression. */
5934 saved_message
= parser
->type_definition_forbidden_message
;
5935 parser
->type_definition_forbidden_message
5936 = G_("types may not be defined in a %<typeid%> expression");
5937 /* We can't be sure yet whether we're looking at a type-id or an
5939 cp_parser_parse_tentatively (parser
);
5940 /* Try a type-id first. */
5941 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5942 parser
->in_type_id_in_expr_p
= true;
5943 type
= cp_parser_type_id (parser
);
5944 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5945 /* Look for the `)' token. Otherwise, we can't be sure that
5946 we're not looking at an expression: consider `typeid (int
5947 (3))', for example. */
5948 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5949 /* If all went well, simply lookup the type-id. */
5950 if (cp_parser_parse_definitely (parser
))
5951 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5952 /* Otherwise, fall back to the expression variant. */
5957 /* Look for an expression. */
5958 expression
= cp_parser_expression (parser
, & idk
);
5959 /* Compute its typeid. */
5960 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5961 /* Look for the `)' token. */
5962 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5964 /* Restore the saved message. */
5965 parser
->type_definition_forbidden_message
= saved_message
;
5966 /* `typeid' may not appear in an integral constant expression. */
5967 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5968 return error_mark_node
;
5975 /* The syntax permitted here is the same permitted for an
5976 elaborated-type-specifier. */
5977 type
= cp_parser_elaborated_type_specifier (parser
,
5978 /*is_friend=*/false,
5979 /*is_declaration=*/false);
5980 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5984 case RID_CILK_SPAWN
:
5986 cp_lexer_consume_token (parser
->lexer
);
5987 token
= cp_lexer_peek_token (parser
->lexer
);
5988 if (token
->type
== CPP_SEMICOLON
)
5990 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
5992 postfix_expression
= error_mark_node
;
5995 else if (!current_function_decl
)
5997 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
5998 "inside a function");
5999 postfix_expression
= error_mark_node
;
6004 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6005 saved_in_statement
= parser
->in_statement
;
6006 parser
->in_statement
|= IN_CILK_SPAWN
;
6008 cfun
->calls_cilk_spawn
= 1;
6009 postfix_expression
=
6010 cp_parser_postfix_expression (parser
, false, false,
6011 false, false, &idk
);
6014 error_at (token
->location
, "-fcilkplus must be enabled to use"
6015 " %<_Cilk_spawn%>");
6016 cfun
->calls_cilk_spawn
= 0;
6018 else if (saved_in_statement
& IN_CILK_SPAWN
)
6020 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6021 "are not permitted");
6022 postfix_expression
= error_mark_node
;
6023 cfun
->calls_cilk_spawn
= 0;
6027 postfix_expression
= build_cilk_spawn (token
->location
,
6028 postfix_expression
);
6029 if (postfix_expression
!= error_mark_node
)
6030 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6031 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6036 case RID_BUILTIN_SHUFFLE
:
6038 vec
<tree
, va_gc
> *vec
;
6042 cp_lexer_consume_token (parser
->lexer
);
6043 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6044 /*cast_p=*/false, /*allow_expansion_p=*/true,
6045 /*non_constant_p=*/NULL
);
6047 return error_mark_node
;
6049 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6052 if (vec
->length () == 2)
6053 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
6054 tf_warning_or_error
);
6055 else if (vec
->length () == 3)
6056 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6057 tf_warning_or_error
);
6060 error_at (loc
, "wrong number of arguments to "
6061 "%<__builtin_shuffle%>");
6062 return error_mark_node
;
6071 /* If the next thing is a simple-type-specifier, we may be
6072 looking at a functional cast. We could also be looking at
6073 an id-expression. So, we try the functional cast, and if
6074 that doesn't work we fall back to the primary-expression. */
6075 cp_parser_parse_tentatively (parser
);
6076 /* Look for the simple-type-specifier. */
6077 type
= cp_parser_simple_type_specifier (parser
,
6078 /*decl_specs=*/NULL
,
6079 CP_PARSER_FLAGS_NONE
);
6080 /* Parse the cast itself. */
6081 if (!cp_parser_error_occurred (parser
))
6083 = cp_parser_functional_cast (parser
, type
);
6084 /* If that worked, we're done. */
6085 if (cp_parser_parse_definitely (parser
))
6088 /* If the functional-cast didn't work out, try a
6089 compound-literal. */
6090 if (cp_parser_allow_gnu_extensions_p (parser
)
6091 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6093 tree initializer
= NULL_TREE
;
6095 cp_parser_parse_tentatively (parser
);
6097 /* Avoid calling cp_parser_type_id pointlessly, see comment
6098 in cp_parser_cast_expression about c++/29234. */
6099 if (!cp_parser_compound_literal_p (parser
))
6100 cp_parser_simulate_error (parser
);
6103 /* Parse the type. */
6104 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6105 parser
->in_type_id_in_expr_p
= true;
6106 type
= cp_parser_type_id (parser
);
6107 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6108 /* Look for the `)'. */
6109 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6112 /* If things aren't going well, there's no need to
6114 if (!cp_parser_error_occurred (parser
))
6116 bool non_constant_p
;
6117 /* Parse the brace-enclosed initializer list. */
6118 initializer
= cp_parser_braced_list (parser
,
6121 /* If that worked, we're definitely looking at a
6122 compound-literal expression. */
6123 if (cp_parser_parse_definitely (parser
))
6125 /* Warn the user that a compound literal is not
6126 allowed in standard C++. */
6127 pedwarn (input_location
, OPT_Wpedantic
,
6128 "ISO C++ forbids compound-literals");
6129 /* For simplicity, we disallow compound literals in
6130 constant-expressions. We could
6131 allow compound literals of integer type, whose
6132 initializer was a constant, in constant
6133 expressions. Permitting that usage, as a further
6134 extension, would not change the meaning of any
6135 currently accepted programs. (Of course, as
6136 compound literals are not part of ISO C++, the
6137 standard has nothing to say.) */
6138 if (cp_parser_non_integral_constant_expression (parser
,
6141 postfix_expression
= error_mark_node
;
6144 /* Form the representation of the compound-literal. */
6146 = finish_compound_literal (type
, initializer
,
6147 tf_warning_or_error
);
6152 /* It must be a primary-expression. */
6154 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6155 /*template_arg_p=*/false,
6162 /* Note that we don't need to worry about calling build_cplus_new on a
6163 class-valued CALL_EXPR in decltype when it isn't the end of the
6164 postfix-expression; unary_complex_lvalue will take care of that for
6167 /* Keep looping until the postfix-expression is complete. */
6170 if (idk
== CP_ID_KIND_UNQUALIFIED
6171 && identifier_p (postfix_expression
)
6172 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6173 /* It is not a Koenig lookup function call. */
6175 = unqualified_name_lookup_error (postfix_expression
);
6177 /* Peek at the next token. */
6178 token
= cp_lexer_peek_token (parser
->lexer
);
6180 switch (token
->type
)
6182 case CPP_OPEN_SQUARE
:
6183 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6185 cp_parser_error (parser
,
6186 "two consecutive %<[%> shall "
6187 "only introduce an attribute");
6188 return error_mark_node
;
6191 = cp_parser_postfix_open_square_expression (parser
,
6195 idk
= CP_ID_KIND_NONE
;
6196 is_member_access
= false;
6199 case CPP_OPEN_PAREN
:
6200 /* postfix-expression ( expression-list [opt] ) */
6203 bool is_builtin_constant_p
;
6204 bool saved_integral_constant_expression_p
= false;
6205 bool saved_non_integral_constant_expression_p
= false;
6206 tsubst_flags_t complain
= complain_flags (decltype_p
);
6207 vec
<tree
, va_gc
> *args
;
6209 is_member_access
= false;
6211 is_builtin_constant_p
6212 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6213 if (is_builtin_constant_p
)
6215 /* The whole point of __builtin_constant_p is to allow
6216 non-constant expressions to appear as arguments. */
6217 saved_integral_constant_expression_p
6218 = parser
->integral_constant_expression_p
;
6219 saved_non_integral_constant_expression_p
6220 = parser
->non_integral_constant_expression_p
;
6221 parser
->integral_constant_expression_p
= false;
6223 args
= (cp_parser_parenthesized_expression_list
6225 /*cast_p=*/false, /*allow_expansion_p=*/true,
6226 /*non_constant_p=*/NULL
,
6227 /*want_literal_zero_p=*/warn_memset_transposed_args
));
6228 if (is_builtin_constant_p
)
6230 parser
->integral_constant_expression_p
6231 = saved_integral_constant_expression_p
;
6232 parser
->non_integral_constant_expression_p
6233 = saved_non_integral_constant_expression_p
;
6238 postfix_expression
= error_mark_node
;
6242 /* Function calls are not permitted in
6243 constant-expressions. */
6244 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6245 && cp_parser_non_integral_constant_expression (parser
,
6248 postfix_expression
= error_mark_node
;
6249 release_tree_vector (args
);
6254 if (idk
== CP_ID_KIND_UNQUALIFIED
6255 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6257 if (identifier_p (postfix_expression
))
6259 if (!args
->is_empty ())
6262 if (!any_type_dependent_arguments_p (args
))
6264 = perform_koenig_lookup (postfix_expression
, args
,
6269 = unqualified_fn_lookup_error (postfix_expression
);
6271 /* We do not perform argument-dependent lookup if
6272 normal lookup finds a non-function, in accordance
6273 with the expected resolution of DR 218. */
6274 else if (!args
->is_empty ()
6275 && is_overloaded_fn (postfix_expression
))
6277 tree fn
= get_first_fn (postfix_expression
);
6278 fn
= STRIP_TEMPLATE (fn
);
6280 /* Do not do argument dependent lookup if regular
6281 lookup finds a member function or a block-scope
6282 function declaration. [basic.lookup.argdep]/3 */
6283 if (!DECL_FUNCTION_MEMBER_P (fn
)
6284 && !DECL_LOCAL_FUNCTION_P (fn
))
6287 if (!any_type_dependent_arguments_p (args
))
6289 = perform_koenig_lookup (postfix_expression
, args
,
6295 if (warn_memset_transposed_args
)
6297 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6298 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6299 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6300 && vec_safe_length (args
) == 3
6301 && integer_zerop ((*args
)[2])
6302 && LITERAL_ZERO_P ((*args
)[2])
6303 && !(integer_zerop ((*args
)[1])
6304 && LITERAL_ZERO_P ((*args
)[1])))
6305 warning (OPT_Wmemset_transposed_args
,
6306 "%<memset%> used with constant zero length "
6307 "parameter; this could be due to transposed "
6310 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6311 to avoid leaking those into folder and middle-end. */
6314 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6315 if (TREE_CODE (arg
) == INTEGER_CST
&& LITERAL_ZERO_P (arg
))
6316 (*args
)[i
] = build_int_cst (TREE_TYPE (arg
), 0);
6319 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6321 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6322 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6324 if (processing_template_decl
6325 && (type_dependent_expression_p (instance
)
6326 || (!BASELINK_P (fn
)
6327 && TREE_CODE (fn
) != FIELD_DECL
)
6328 || type_dependent_expression_p (fn
)
6329 || any_type_dependent_arguments_p (args
)))
6332 = build_nt_call_vec (postfix_expression
, args
);
6333 release_tree_vector (args
);
6337 if (BASELINK_P (fn
))
6340 = (build_new_method_call
6341 (instance
, fn
, &args
, NULL_TREE
,
6342 (idk
== CP_ID_KIND_QUALIFIED
6343 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6350 = finish_call_expr (postfix_expression
, &args
,
6351 /*disallow_virtual=*/false,
6355 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6356 || TREE_CODE (postfix_expression
) == MEMBER_REF
6357 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6358 postfix_expression
= (build_offset_ref_call_from_tree
6359 (postfix_expression
, &args
,
6361 else if (idk
== CP_ID_KIND_QUALIFIED
)
6362 /* A call to a static class member, or a namespace-scope
6365 = finish_call_expr (postfix_expression
, &args
,
6366 /*disallow_virtual=*/true,
6370 /* All other function calls. */
6372 = finish_call_expr (postfix_expression
, &args
,
6373 /*disallow_virtual=*/false,
6377 protected_set_expr_location (postfix_expression
, token
->location
);
6379 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6380 idk
= CP_ID_KIND_NONE
;
6382 release_tree_vector (args
);
6388 /* postfix-expression . template [opt] id-expression
6389 postfix-expression . pseudo-destructor-name
6390 postfix-expression -> template [opt] id-expression
6391 postfix-expression -> pseudo-destructor-name */
6393 /* Consume the `.' or `->' operator. */
6394 cp_lexer_consume_token (parser
->lexer
);
6397 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6401 is_member_access
= true;
6405 /* postfix-expression ++ */
6406 /* Consume the `++' token. */
6407 cp_lexer_consume_token (parser
->lexer
);
6408 /* Generate a representation for the complete expression. */
6410 = finish_increment_expr (postfix_expression
,
6411 POSTINCREMENT_EXPR
);
6412 /* Increments may not appear in constant-expressions. */
6413 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6414 postfix_expression
= error_mark_node
;
6415 idk
= CP_ID_KIND_NONE
;
6416 is_member_access
= false;
6419 case CPP_MINUS_MINUS
:
6420 /* postfix-expression -- */
6421 /* Consume the `--' token. */
6422 cp_lexer_consume_token (parser
->lexer
);
6423 /* Generate a representation for the complete expression. */
6425 = finish_increment_expr (postfix_expression
,
6426 POSTDECREMENT_EXPR
);
6427 /* Decrements may not appear in constant-expressions. */
6428 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6429 postfix_expression
= error_mark_node
;
6430 idk
= CP_ID_KIND_NONE
;
6431 is_member_access
= false;
6435 if (pidk_return
!= NULL
)
6436 * pidk_return
= idk
;
6437 if (member_access_only_p
)
6438 return is_member_access
? postfix_expression
: error_mark_node
;
6440 return postfix_expression
;
6444 /* We should never get here. */
6446 return error_mark_node
;
6449 /* This function parses Cilk Plus array notations. If a normal array expr. is
6450 parsed then the array index is passed back to the caller through *INIT_INDEX
6451 and the function returns a NULL_TREE. If array notation expr. is parsed,
6452 then *INIT_INDEX is ignored by the caller and the function returns
6453 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6457 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6460 cp_token
*token
= NULL
;
6461 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6462 if (!array_value
|| array_value
== error_mark_node
)
6464 cp_parser_skip_to_end_of_statement (parser
);
6465 return error_mark_node
;
6468 array_type
= TREE_TYPE (array_value
);
6470 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6471 parser
->colon_corrects_to_scope_p
= false;
6472 token
= cp_lexer_peek_token (parser
->lexer
);
6476 cp_parser_error (parser
, "expected %<:%> or numeral");
6477 return error_mark_node
;
6479 else if (token
->type
== CPP_COLON
)
6481 /* Consume the ':'. */
6482 cp_lexer_consume_token (parser
->lexer
);
6484 /* If we are here, then we have a case like this A[:]. */
6485 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6487 cp_parser_error (parser
, "expected %<]%>");
6488 cp_parser_skip_to_end_of_statement (parser
);
6489 return error_mark_node
;
6491 *init_index
= NULL_TREE
;
6493 length_index
= NULL_TREE
;
6497 /* If we are here, then there are three valid possibilities:
6499 2. ARRAY [ EXP : EXP ]
6500 3. ARRAY [ EXP : EXP : EXP ] */
6502 *init_index
= cp_parser_expression (parser
);
6503 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6505 /* This indicates that we have a normal array expression. */
6506 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6510 /* Consume the ':'. */
6511 cp_lexer_consume_token (parser
->lexer
);
6512 length_index
= cp_parser_expression (parser
);
6513 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6515 cp_lexer_consume_token (parser
->lexer
);
6516 stride
= cp_parser_expression (parser
);
6519 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6521 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6522 || stride
== error_mark_node
|| array_type
== error_mark_node
)
6524 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6525 cp_lexer_consume_token (parser
->lexer
);
6526 return error_mark_node
;
6528 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6530 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6531 length_index
, stride
, array_type
);
6535 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6536 by cp_parser_builtin_offsetof. We're looking for
6538 postfix-expression [ expression ]
6539 postfix-expression [ braced-init-list ] (C++11)
6541 FOR_OFFSETOF is set if we're being called in that context, which
6542 changes how we deal with integer constant expressions. */
6545 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6546 tree postfix_expression
,
6550 tree index
= NULL_TREE
;
6551 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6552 bool saved_greater_than_is_operator_p
;
6554 /* Consume the `[' token. */
6555 cp_lexer_consume_token (parser
->lexer
);
6557 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6558 parser
->greater_than_is_operator_p
= true;
6560 /* Parse the index expression. */
6561 /* ??? For offsetof, there is a question of what to allow here. If
6562 offsetof is not being used in an integral constant expression context,
6563 then we *could* get the right answer by computing the value at runtime.
6564 If we are in an integral constant expression context, then we might
6565 could accept any constant expression; hard to say without analysis.
6566 Rather than open the barn door too wide right away, allow only integer
6567 constant expressions here. */
6569 index
= cp_parser_constant_expression (parser
);
6572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6574 bool expr_nonconst_p
;
6575 cp_lexer_set_source_position (parser
->lexer
);
6576 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6577 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6579 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6581 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6582 "braced list index is not allowed with array "
6584 cp_parser_skip_to_end_of_statement (parser
);
6585 return error_mark_node
;
6588 else if (flag_cilkplus
)
6590 /* Here are have these two options:
6591 ARRAY[EXP : EXP] - Array notation expr with default
6593 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6595 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6596 postfix_expression
);
6601 index
= cp_parser_expression (parser
);
6604 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6606 /* Look for the closing `]'. */
6607 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6609 /* Build the ARRAY_REF. */
6610 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6613 /* When not doing offsetof, array references are not permitted in
6614 constant-expressions. */
6616 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6617 postfix_expression
= error_mark_node
;
6619 return postfix_expression
;
6622 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6623 by cp_parser_builtin_offsetof. We're looking for
6625 postfix-expression . template [opt] id-expression
6626 postfix-expression . pseudo-destructor-name
6627 postfix-expression -> template [opt] id-expression
6628 postfix-expression -> pseudo-destructor-name
6630 FOR_OFFSETOF is set if we're being called in that context. That sorta
6631 limits what of the above we'll actually accept, but nevermind.
6632 TOKEN_TYPE is the "." or "->" token, which will already have been
6633 removed from the stream. */
6636 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6637 enum cpp_ttype token_type
,
6638 tree postfix_expression
,
6639 bool for_offsetof
, cp_id_kind
*idk
,
6640 location_t location
)
6644 bool pseudo_destructor_p
;
6645 tree scope
= NULL_TREE
;
6647 /* If this is a `->' operator, dereference the pointer. */
6648 if (token_type
== CPP_DEREF
)
6649 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6650 tf_warning_or_error
);
6651 /* Check to see whether or not the expression is type-dependent. */
6652 dependent_p
= type_dependent_expression_p (postfix_expression
);
6653 /* The identifier following the `->' or `.' is not qualified. */
6654 parser
->scope
= NULL_TREE
;
6655 parser
->qualifying_scope
= NULL_TREE
;
6656 parser
->object_scope
= NULL_TREE
;
6657 *idk
= CP_ID_KIND_NONE
;
6659 /* Enter the scope corresponding to the type of the object
6660 given by the POSTFIX_EXPRESSION. */
6661 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6663 scope
= TREE_TYPE (postfix_expression
);
6664 /* According to the standard, no expression should ever have
6665 reference type. Unfortunately, we do not currently match
6666 the standard in this respect in that our internal representation
6667 of an expression may have reference type even when the standard
6668 says it does not. Therefore, we have to manually obtain the
6669 underlying type here. */
6670 scope
= non_reference (scope
);
6671 /* The type of the POSTFIX_EXPRESSION must be complete. */
6672 if (scope
== unknown_type_node
)
6674 error_at (location
, "%qE does not have class type",
6675 postfix_expression
);
6678 /* Unlike the object expression in other contexts, *this is not
6679 required to be of complete type for purposes of class member
6680 access (5.2.5) outside the member function body. */
6681 else if (postfix_expression
!= current_class_ref
6682 && !(processing_template_decl
&& scope
== current_class_type
))
6683 scope
= complete_type_or_else (scope
, NULL_TREE
);
6684 /* Let the name lookup machinery know that we are processing a
6685 class member access expression. */
6686 parser
->context
->object_type
= scope
;
6687 /* If something went wrong, we want to be able to discern that case,
6688 as opposed to the case where there was no SCOPE due to the type
6689 of expression being dependent. */
6691 scope
= error_mark_node
;
6692 /* If the SCOPE was erroneous, make the various semantic analysis
6693 functions exit quickly -- and without issuing additional error
6695 if (scope
== error_mark_node
)
6696 postfix_expression
= error_mark_node
;
6699 /* Assume this expression is not a pseudo-destructor access. */
6700 pseudo_destructor_p
= false;
6702 /* If the SCOPE is a scalar type, then, if this is a valid program,
6703 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6704 is type dependent, it can be pseudo-destructor-name or something else.
6705 Try to parse it as pseudo-destructor-name first. */
6706 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6711 cp_parser_parse_tentatively (parser
);
6712 /* Parse the pseudo-destructor-name. */
6714 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6717 && (cp_parser_error_occurred (parser
)
6718 || !SCALAR_TYPE_P (type
)))
6719 cp_parser_abort_tentative_parse (parser
);
6720 else if (cp_parser_parse_definitely (parser
))
6722 pseudo_destructor_p
= true;
6724 = finish_pseudo_destructor_expr (postfix_expression
,
6729 if (!pseudo_destructor_p
)
6731 /* If the SCOPE is not a scalar type, we are looking at an
6732 ordinary class member access expression, rather than a
6733 pseudo-destructor-name. */
6735 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6736 /* Parse the id-expression. */
6737 name
= (cp_parser_id_expression
6739 cp_parser_optional_template_keyword (parser
),
6740 /*check_dependency_p=*/true,
6742 /*declarator_p=*/false,
6743 /*optional_p=*/false));
6744 /* In general, build a SCOPE_REF if the member name is qualified.
6745 However, if the name was not dependent and has already been
6746 resolved; there is no need to build the SCOPE_REF. For example;
6748 struct X { void f(); };
6749 template <typename T> void f(T* t) { t->X::f(); }
6751 Even though "t" is dependent, "X::f" is not and has been resolved
6752 to a BASELINK; there is no need to include scope information. */
6754 /* But we do need to remember that there was an explicit scope for
6755 virtual function calls. */
6757 *idk
= CP_ID_KIND_QUALIFIED
;
6759 /* If the name is a template-id that names a type, we will get a
6760 TYPE_DECL here. That is invalid code. */
6761 if (TREE_CODE (name
) == TYPE_DECL
)
6763 error_at (token
->location
, "invalid use of %qD", name
);
6764 postfix_expression
= error_mark_node
;
6768 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6770 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6772 error_at (token
->location
, "%<%D::%D%> is not a class member",
6773 parser
->scope
, name
);
6774 postfix_expression
= error_mark_node
;
6777 name
= build_qualified_name (/*type=*/NULL_TREE
,
6781 parser
->scope
= NULL_TREE
;
6782 parser
->qualifying_scope
= NULL_TREE
;
6783 parser
->object_scope
= NULL_TREE
;
6785 if (parser
->scope
&& name
&& BASELINK_P (name
))
6786 adjust_result_of_qualified_name_lookup
6787 (name
, parser
->scope
, scope
);
6789 = finish_class_member_access_expr (postfix_expression
, name
,
6791 tf_warning_or_error
);
6795 /* We no longer need to look up names in the scope of the object on
6796 the left-hand side of the `.' or `->' operator. */
6797 parser
->context
->object_type
= NULL_TREE
;
6799 /* Outside of offsetof, these operators may not appear in
6800 constant-expressions. */
6802 && (cp_parser_non_integral_constant_expression
6803 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6804 postfix_expression
= error_mark_node
;
6806 return postfix_expression
;
6809 /* Cache of LITERAL_ZERO_P constants. */
6811 static GTY(()) tree literal_zeros
[itk_none
];
6813 /* Parse a parenthesized expression-list.
6816 assignment-expression
6817 expression-list, assignment-expression
6822 identifier, expression-list
6824 CAST_P is true if this expression is the target of a cast.
6826 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6829 Returns a vector of trees. Each element is a representation of an
6830 assignment-expression. NULL is returned if the ( and or ) are
6831 missing. An empty, but allocated, vector is returned on no
6832 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6833 if we are parsing an attribute list for an attribute that wants a
6834 plain identifier argument, normal_attr for an attribute that wants
6835 an expression, or non_attr if we aren't parsing an attribute list. If
6836 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6837 not all of the expressions in the list were constant.
6838 WANT_LITERAL_ZERO_P is true if the caller is interested in
6839 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6840 immediately, this can be removed. */
6842 static vec
<tree
, va_gc
> *
6843 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6844 int is_attribute_list
,
6846 bool allow_expansion_p
,
6847 bool *non_constant_p
,
6848 bool want_literal_zero_p
)
6850 vec
<tree
, va_gc
> *expression_list
;
6851 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6852 tree identifier
= NULL_TREE
;
6853 bool saved_greater_than_is_operator_p
;
6855 /* Assume all the expressions will be constant. */
6857 *non_constant_p
= false;
6859 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6862 expression_list
= make_tree_vector ();
6864 /* Within a parenthesized expression, a `>' token is always
6865 the greater-than operator. */
6866 saved_greater_than_is_operator_p
6867 = parser
->greater_than_is_operator_p
;
6868 parser
->greater_than_is_operator_p
= true;
6870 /* Consume expressions until there are no more. */
6871 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6876 /* At the beginning of attribute lists, check to see if the
6877 next token is an identifier. */
6878 if (is_attribute_list
== id_attr
6879 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6883 /* Consume the identifier. */
6884 token
= cp_lexer_consume_token (parser
->lexer
);
6885 /* Save the identifier. */
6886 identifier
= token
->u
.value
;
6890 bool expr_non_constant_p
;
6892 /* Parse the next assignment-expression. */
6893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6895 /* A braced-init-list. */
6896 cp_lexer_set_source_position (parser
->lexer
);
6897 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6898 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6899 if (non_constant_p
&& expr_non_constant_p
)
6900 *non_constant_p
= true;
6902 else if (non_constant_p
)
6904 expr
= (cp_parser_constant_expression
6905 (parser
, /*allow_non_constant_p=*/true,
6906 &expr_non_constant_p
));
6907 if (expr_non_constant_p
)
6908 *non_constant_p
= true;
6913 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
6921 /* If a parameter is literal zero alone, remember it
6922 for -Wmemset-transposed-args warning. */
6923 if (integer_zerop (tok
->u
.value
)
6924 && !TREE_OVERFLOW (tok
->u
.value
)
6925 && want_literal_zero_p
6926 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6928 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6929 == CPP_CLOSE_PAREN
))
6932 for (i
= 0; i
< itk_none
; ++i
)
6933 if (TREE_TYPE (tok
->u
.value
) == integer_types
[i
])
6935 if (i
< itk_none
&& literal_zeros
[i
])
6936 expr
= literal_zeros
[i
];
6939 expr
= copy_node (tok
->u
.value
);
6940 LITERAL_ZERO_P (expr
) = 1;
6942 literal_zeros
[i
] = expr
;
6944 /* Consume the 0 token (or '\0', 0LL etc.). */
6945 cp_lexer_consume_token (parser
->lexer
);
6951 if (expr
== NULL_TREE
)
6952 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
6957 expr
= instantiate_non_dependent_expr (expr
);
6959 /* If we have an ellipsis, then this is an expression
6961 if (allow_expansion_p
6962 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6964 /* Consume the `...'. */
6965 cp_lexer_consume_token (parser
->lexer
);
6967 /* Build the argument pack. */
6968 expr
= make_pack_expansion (expr
);
6971 /* Add it to the list. We add error_mark_node
6972 expressions to the list, so that we can still tell if
6973 the correct form for a parenthesized expression-list
6974 is found. That gives better errors. */
6975 vec_safe_push (expression_list
, expr
);
6977 if (expr
== error_mark_node
)
6981 /* After the first item, attribute lists look the same as
6982 expression lists. */
6983 is_attribute_list
= non_attr
;
6986 /* If the next token isn't a `,', then we are done. */
6987 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6990 /* Otherwise, consume the `,' and keep going. */
6991 cp_lexer_consume_token (parser
->lexer
);
6994 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6999 /* We try and resync to an unnested comma, as that will give the
7000 user better diagnostics. */
7001 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7002 /*recovering=*/true,
7004 /*consume_paren=*/true);
7009 parser
->greater_than_is_operator_p
7010 = saved_greater_than_is_operator_p
;
7015 parser
->greater_than_is_operator_p
7016 = saved_greater_than_is_operator_p
;
7019 vec_safe_insert (expression_list
, 0, identifier
);
7021 return expression_list
;
7024 /* Parse a pseudo-destructor-name.
7026 pseudo-destructor-name:
7027 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7028 :: [opt] nested-name-specifier template template-id :: ~ type-name
7029 :: [opt] nested-name-specifier [opt] ~ type-name
7031 If either of the first two productions is used, sets *SCOPE to the
7032 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7033 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7034 or ERROR_MARK_NODE if the parse fails. */
7037 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7042 bool nested_name_specifier_p
;
7045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7046 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7047 && !type_dependent_expression_p (object
))
7049 if (cxx_dialect
< cxx14
)
7050 pedwarn (input_location
, 0,
7051 "%<~auto%> only available with "
7052 "-std=c++14 or -std=gnu++14");
7053 cp_lexer_consume_token (parser
->lexer
);
7054 cp_lexer_consume_token (parser
->lexer
);
7056 *type
= TREE_TYPE (object
);
7060 /* Assume that things will not work out. */
7061 *type
= error_mark_node
;
7063 /* Look for the optional `::' operator. */
7064 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7065 /* Look for the optional nested-name-specifier. */
7066 nested_name_specifier_p
7067 = (cp_parser_nested_name_specifier_opt (parser
,
7068 /*typename_keyword_p=*/false,
7069 /*check_dependency_p=*/true,
7071 /*is_declaration=*/false)
7073 /* Now, if we saw a nested-name-specifier, we might be doing the
7074 second production. */
7075 if (nested_name_specifier_p
7076 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7078 /* Consume the `template' keyword. */
7079 cp_lexer_consume_token (parser
->lexer
);
7080 /* Parse the template-id. */
7081 cp_parser_template_id (parser
,
7082 /*template_keyword_p=*/true,
7083 /*check_dependency_p=*/false,
7085 /*is_declaration=*/true);
7086 /* Look for the `::' token. */
7087 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7089 /* If the next token is not a `~', then there might be some
7090 additional qualification. */
7091 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7093 /* At this point, we're looking for "type-name :: ~". The type-name
7094 must not be a class-name, since this is a pseudo-destructor. So,
7095 it must be either an enum-name, or a typedef-name -- both of which
7096 are just identifiers. So, we peek ahead to check that the "::"
7097 and "~" tokens are present; if they are not, then we can avoid
7098 calling type_name. */
7099 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7100 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7101 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7103 cp_parser_error (parser
, "non-scalar type");
7107 /* Look for the type-name. */
7108 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7109 if (*scope
== error_mark_node
)
7112 /* Look for the `::' token. */
7113 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7118 /* Look for the `~'. */
7119 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7121 /* Once we see the ~, this has to be a pseudo-destructor. */
7122 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7123 cp_parser_commit_to_topmost_tentative_parse (parser
);
7125 /* Look for the type-name again. We are not responsible for
7126 checking that it matches the first type-name. */
7127 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7130 /* Parse a unary-expression.
7136 unary-operator cast-expression
7137 sizeof unary-expression
7139 alignof ( type-id ) [C++0x]
7146 __extension__ cast-expression
7147 __alignof__ unary-expression
7148 __alignof__ ( type-id )
7149 alignof unary-expression [C++0x]
7150 __real__ cast-expression
7151 __imag__ cast-expression
7153 sizeof ( type-id ) { initializer-list , [opt] }
7154 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7155 __alignof__ ( type-id ) { initializer-list , [opt] }
7157 ADDRESS_P is true iff the unary-expression is appearing as the
7158 operand of the `&' operator. CAST_P is true if this expression is
7159 the target of a cast.
7161 Returns a representation of the expression. */
7164 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7165 bool address_p
, bool cast_p
, bool decltype_p
)
7168 enum tree_code unary_operator
;
7170 /* Peek at the next token. */
7171 token
= cp_lexer_peek_token (parser
->lexer
);
7172 /* Some keywords give away the kind of expression. */
7173 if (token
->type
== CPP_KEYWORD
)
7175 enum rid keyword
= token
->keyword
;
7184 location_t first_loc
;
7186 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7187 /* Consume the token. */
7188 cp_lexer_consume_token (parser
->lexer
);
7189 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7190 /* Parse the operand. */
7191 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7193 if (TYPE_P (operand
))
7194 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7197 /* ISO C++ defines alignof only with types, not with
7198 expressions. So pedwarn if alignof is used with a non-
7199 type expression. However, __alignof__ is ok. */
7200 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7201 pedwarn (token
->location
, OPT_Wpedantic
,
7202 "ISO C++ does not allow %<alignof%> "
7205 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7207 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7208 SIZEOF_EXPR with the original operand. */
7209 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7211 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7213 if (!processing_template_decl
&& TYPE_P (operand
))
7215 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7216 build1 (NOP_EXPR
, operand
,
7218 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7221 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7222 TREE_SIDE_EFFECTS (ret
) = 0;
7223 TREE_READONLY (ret
) = 1;
7225 SET_EXPR_LOCATION (ret
, first_loc
);
7231 return cp_parser_new_expression (parser
);
7234 return cp_parser_delete_expression (parser
);
7238 /* The saved value of the PEDANTIC flag. */
7242 /* Save away the PEDANTIC flag. */
7243 cp_parser_extension_opt (parser
, &saved_pedantic
);
7244 /* Parse the cast-expression. */
7245 expr
= cp_parser_simple_cast_expression (parser
);
7246 /* Restore the PEDANTIC flag. */
7247 pedantic
= saved_pedantic
;
7257 /* Consume the `__real__' or `__imag__' token. */
7258 cp_lexer_consume_token (parser
->lexer
);
7259 /* Parse the cast-expression. */
7260 expression
= cp_parser_simple_cast_expression (parser
);
7261 /* Create the complete representation. */
7262 return build_x_unary_op (token
->location
,
7263 (keyword
== RID_REALPART
7264 ? REALPART_EXPR
: IMAGPART_EXPR
),
7266 tf_warning_or_error
);
7270 case RID_TRANSACTION_ATOMIC
:
7271 case RID_TRANSACTION_RELAXED
:
7272 return cp_parser_transaction_expression (parser
, keyword
);
7277 const char *saved_message
;
7278 bool saved_integral_constant_expression_p
;
7279 bool saved_non_integral_constant_expression_p
;
7280 bool saved_greater_than_is_operator_p
;
7282 cp_lexer_consume_token (parser
->lexer
);
7283 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7285 saved_message
= parser
->type_definition_forbidden_message
;
7286 parser
->type_definition_forbidden_message
7287 = G_("types may not be defined in %<noexcept%> expressions");
7289 saved_integral_constant_expression_p
7290 = parser
->integral_constant_expression_p
;
7291 saved_non_integral_constant_expression_p
7292 = parser
->non_integral_constant_expression_p
;
7293 parser
->integral_constant_expression_p
= false;
7295 saved_greater_than_is_operator_p
7296 = parser
->greater_than_is_operator_p
;
7297 parser
->greater_than_is_operator_p
= true;
7299 ++cp_unevaluated_operand
;
7300 ++c_inhibit_evaluation_warnings
;
7301 ++cp_noexcept_operand
;
7302 expr
= cp_parser_expression (parser
);
7303 --cp_noexcept_operand
;
7304 --c_inhibit_evaluation_warnings
;
7305 --cp_unevaluated_operand
;
7307 parser
->greater_than_is_operator_p
7308 = saved_greater_than_is_operator_p
;
7310 parser
->integral_constant_expression_p
7311 = saved_integral_constant_expression_p
;
7312 parser
->non_integral_constant_expression_p
7313 = saved_non_integral_constant_expression_p
;
7315 parser
->type_definition_forbidden_message
= saved_message
;
7317 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7318 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7326 /* Look for the `:: new' and `:: delete', which also signal the
7327 beginning of a new-expression, or delete-expression,
7328 respectively. If the next token is `::', then it might be one of
7330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7334 /* See if the token after the `::' is one of the keywords in
7335 which we're interested. */
7336 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7337 /* If it's `new', we have a new-expression. */
7338 if (keyword
== RID_NEW
)
7339 return cp_parser_new_expression (parser
);
7340 /* Similarly, for `delete'. */
7341 else if (keyword
== RID_DELETE
)
7342 return cp_parser_delete_expression (parser
);
7345 /* Look for a unary operator. */
7346 unary_operator
= cp_parser_unary_operator (token
);
7347 /* The `++' and `--' operators can be handled similarly, even though
7348 they are not technically unary-operators in the grammar. */
7349 if (unary_operator
== ERROR_MARK
)
7351 if (token
->type
== CPP_PLUS_PLUS
)
7352 unary_operator
= PREINCREMENT_EXPR
;
7353 else if (token
->type
== CPP_MINUS_MINUS
)
7354 unary_operator
= PREDECREMENT_EXPR
;
7355 /* Handle the GNU address-of-label extension. */
7356 else if (cp_parser_allow_gnu_extensions_p (parser
)
7357 && token
->type
== CPP_AND_AND
)
7361 location_t loc
= token
->location
;
7363 /* Consume the '&&' token. */
7364 cp_lexer_consume_token (parser
->lexer
);
7365 /* Look for the identifier. */
7366 identifier
= cp_parser_identifier (parser
);
7367 /* Create an expression representing the address. */
7368 expression
= finish_label_address_expr (identifier
, loc
);
7369 if (cp_parser_non_integral_constant_expression (parser
,
7371 expression
= error_mark_node
;
7375 if (unary_operator
!= ERROR_MARK
)
7377 tree cast_expression
;
7378 tree expression
= error_mark_node
;
7379 non_integral_constant non_constant_p
= NIC_NONE
;
7380 location_t loc
= token
->location
;
7381 tsubst_flags_t complain
= complain_flags (decltype_p
);
7383 /* Consume the operator token. */
7384 token
= cp_lexer_consume_token (parser
->lexer
);
7385 /* Parse the cast-expression. */
7387 = cp_parser_cast_expression (parser
,
7388 unary_operator
== ADDR_EXPR
,
7392 /* Now, build an appropriate representation. */
7393 switch (unary_operator
)
7396 non_constant_p
= NIC_STAR
;
7397 expression
= build_x_indirect_ref (loc
, cast_expression
,
7403 non_constant_p
= NIC_ADDR
;
7406 expression
= build_x_unary_op (loc
, unary_operator
,
7411 case PREINCREMENT_EXPR
:
7412 case PREDECREMENT_EXPR
:
7413 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7414 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7416 case UNARY_PLUS_EXPR
:
7418 case TRUTH_NOT_EXPR
:
7419 expression
= finish_unary_op_expr (loc
, unary_operator
,
7420 cast_expression
, complain
);
7427 if (non_constant_p
!= NIC_NONE
7428 && cp_parser_non_integral_constant_expression (parser
,
7430 expression
= error_mark_node
;
7435 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7436 /*member_access_only_p=*/false,
7441 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7442 unary-operator, the corresponding tree code is returned. */
7444 static enum tree_code
7445 cp_parser_unary_operator (cp_token
* token
)
7447 switch (token
->type
)
7450 return INDIRECT_REF
;
7456 return UNARY_PLUS_EXPR
;
7462 return TRUTH_NOT_EXPR
;
7465 return BIT_NOT_EXPR
;
7472 /* Parse a new-expression.
7475 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7476 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7478 Returns a representation of the expression. */
7481 cp_parser_new_expression (cp_parser
* parser
)
7483 bool global_scope_p
;
7484 vec
<tree
, va_gc
> *placement
;
7486 vec
<tree
, va_gc
> *initializer
;
7487 tree nelts
= NULL_TREE
;
7490 /* Look for the optional `::' operator. */
7492 = (cp_parser_global_scope_opt (parser
,
7493 /*current_scope_valid_p=*/false)
7495 /* Look for the `new' operator. */
7496 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7497 /* There's no easy way to tell a new-placement from the
7498 `( type-id )' construct. */
7499 cp_parser_parse_tentatively (parser
);
7500 /* Look for a new-placement. */
7501 placement
= cp_parser_new_placement (parser
);
7502 /* If that didn't work out, there's no new-placement. */
7503 if (!cp_parser_parse_definitely (parser
))
7505 if (placement
!= NULL
)
7506 release_tree_vector (placement
);
7510 /* If the next token is a `(', then we have a parenthesized
7512 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7515 const char *saved_message
= parser
->type_definition_forbidden_message
;
7517 /* Consume the `('. */
7518 cp_lexer_consume_token (parser
->lexer
);
7520 /* Parse the type-id. */
7521 parser
->type_definition_forbidden_message
7522 = G_("types may not be defined in a new-expression");
7523 type
= cp_parser_type_id (parser
);
7524 parser
->type_definition_forbidden_message
= saved_message
;
7526 /* Look for the closing `)'. */
7527 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7528 token
= cp_lexer_peek_token (parser
->lexer
);
7529 /* There should not be a direct-new-declarator in this production,
7530 but GCC used to allowed this, so we check and emit a sensible error
7531 message for this case. */
7532 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7534 error_at (token
->location
,
7535 "array bound forbidden after parenthesized type-id");
7536 inform (token
->location
,
7537 "try removing the parentheses around the type-id");
7538 cp_parser_direct_new_declarator (parser
);
7541 /* Otherwise, there must be a new-type-id. */
7543 type
= cp_parser_new_type_id (parser
, &nelts
);
7545 /* If the next token is a `(' or '{', then we have a new-initializer. */
7546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7547 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7548 initializer
= cp_parser_new_initializer (parser
);
7552 /* A new-expression may not appear in an integral constant
7554 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7555 ret
= error_mark_node
;
7558 /* Create a representation of the new-expression. */
7559 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7560 tf_warning_or_error
);
7563 if (placement
!= NULL
)
7564 release_tree_vector (placement
);
7565 if (initializer
!= NULL
)
7566 release_tree_vector (initializer
);
7571 /* Parse a new-placement.
7576 Returns the same representation as for an expression-list. */
7578 static vec
<tree
, va_gc
> *
7579 cp_parser_new_placement (cp_parser
* parser
)
7581 vec
<tree
, va_gc
> *expression_list
;
7583 /* Parse the expression-list. */
7584 expression_list
= (cp_parser_parenthesized_expression_list
7585 (parser
, non_attr
, /*cast_p=*/false,
7586 /*allow_expansion_p=*/true,
7587 /*non_constant_p=*/NULL
));
7589 return expression_list
;
7592 /* Parse a new-type-id.
7595 type-specifier-seq new-declarator [opt]
7597 Returns the TYPE allocated. If the new-type-id indicates an array
7598 type, *NELTS is set to the number of elements in the last array
7599 bound; the TYPE will not include the last array bound. */
7602 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7604 cp_decl_specifier_seq type_specifier_seq
;
7605 cp_declarator
*new_declarator
;
7606 cp_declarator
*declarator
;
7607 cp_declarator
*outer_declarator
;
7608 const char *saved_message
;
7610 /* The type-specifier sequence must not contain type definitions.
7611 (It cannot contain declarations of new types either, but if they
7612 are not definitions we will catch that because they are not
7614 saved_message
= parser
->type_definition_forbidden_message
;
7615 parser
->type_definition_forbidden_message
7616 = G_("types may not be defined in a new-type-id");
7617 /* Parse the type-specifier-seq. */
7618 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7619 /*is_trailing_return=*/false,
7620 &type_specifier_seq
);
7621 /* Restore the old message. */
7622 parser
->type_definition_forbidden_message
= saved_message
;
7624 if (type_specifier_seq
.type
== error_mark_node
)
7625 return error_mark_node
;
7627 /* Parse the new-declarator. */
7628 new_declarator
= cp_parser_new_declarator_opt (parser
);
7630 /* Determine the number of elements in the last array dimension, if
7633 /* Skip down to the last array dimension. */
7634 declarator
= new_declarator
;
7635 outer_declarator
= NULL
;
7636 while (declarator
&& (declarator
->kind
== cdk_pointer
7637 || declarator
->kind
== cdk_ptrmem
))
7639 outer_declarator
= declarator
;
7640 declarator
= declarator
->declarator
;
7643 && declarator
->kind
== cdk_array
7644 && declarator
->declarator
7645 && declarator
->declarator
->kind
== cdk_array
)
7647 outer_declarator
= declarator
;
7648 declarator
= declarator
->declarator
;
7651 if (declarator
&& declarator
->kind
== cdk_array
)
7653 *nelts
= declarator
->u
.array
.bounds
;
7654 if (*nelts
== error_mark_node
)
7655 *nelts
= integer_one_node
;
7657 if (outer_declarator
)
7658 outer_declarator
->declarator
= declarator
->declarator
;
7660 new_declarator
= NULL
;
7663 return groktypename (&type_specifier_seq
, new_declarator
, false);
7666 /* Parse an (optional) new-declarator.
7669 ptr-operator new-declarator [opt]
7670 direct-new-declarator
7672 Returns the declarator. */
7674 static cp_declarator
*
7675 cp_parser_new_declarator_opt (cp_parser
* parser
)
7677 enum tree_code code
;
7678 tree type
, std_attributes
= NULL_TREE
;
7679 cp_cv_quals cv_quals
;
7681 /* We don't know if there's a ptr-operator next, or not. */
7682 cp_parser_parse_tentatively (parser
);
7683 /* Look for a ptr-operator. */
7684 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7685 /* If that worked, look for more new-declarators. */
7686 if (cp_parser_parse_definitely (parser
))
7688 cp_declarator
*declarator
;
7690 /* Parse another optional declarator. */
7691 declarator
= cp_parser_new_declarator_opt (parser
);
7693 declarator
= cp_parser_make_indirect_declarator
7694 (code
, type
, cv_quals
, declarator
, std_attributes
);
7699 /* If the next token is a `[', there is a direct-new-declarator. */
7700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7701 return cp_parser_direct_new_declarator (parser
);
7706 /* Parse a direct-new-declarator.
7708 direct-new-declarator:
7710 direct-new-declarator [constant-expression]
7714 static cp_declarator
*
7715 cp_parser_direct_new_declarator (cp_parser
* parser
)
7717 cp_declarator
*declarator
= NULL
;
7724 /* Look for the opening `['. */
7725 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7727 token
= cp_lexer_peek_token (parser
->lexer
);
7728 expression
= cp_parser_expression (parser
);
7729 /* The standard requires that the expression have integral
7730 type. DR 74 adds enumeration types. We believe that the
7731 real intent is that these expressions be handled like the
7732 expression in a `switch' condition, which also allows
7733 classes with a single conversion to integral or
7734 enumeration type. */
7735 if (!processing_template_decl
)
7738 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7743 error_at (token
->location
,
7744 "expression in new-declarator must have integral "
7745 "or enumeration type");
7746 expression
= error_mark_node
;
7750 /* Look for the closing `]'. */
7751 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7753 /* Add this bound to the declarator. */
7754 declarator
= make_array_declarator (declarator
, expression
);
7756 /* If the next token is not a `[', then there are no more
7758 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7765 /* Parse a new-initializer.
7768 ( expression-list [opt] )
7771 Returns a representation of the expression-list. */
7773 static vec
<tree
, va_gc
> *
7774 cp_parser_new_initializer (cp_parser
* parser
)
7776 vec
<tree
, va_gc
> *expression_list
;
7778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7781 bool expr_non_constant_p
;
7782 cp_lexer_set_source_position (parser
->lexer
);
7783 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7784 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7785 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7786 expression_list
= make_tree_vector_single (t
);
7789 expression_list
= (cp_parser_parenthesized_expression_list
7790 (parser
, non_attr
, /*cast_p=*/false,
7791 /*allow_expansion_p=*/true,
7792 /*non_constant_p=*/NULL
));
7794 return expression_list
;
7797 /* Parse a delete-expression.
7800 :: [opt] delete cast-expression
7801 :: [opt] delete [ ] cast-expression
7803 Returns a representation of the expression. */
7806 cp_parser_delete_expression (cp_parser
* parser
)
7808 bool global_scope_p
;
7812 /* Look for the optional `::' operator. */
7814 = (cp_parser_global_scope_opt (parser
,
7815 /*current_scope_valid_p=*/false)
7817 /* Look for the `delete' keyword. */
7818 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7819 /* See if the array syntax is in use. */
7820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7822 /* Consume the `[' token. */
7823 cp_lexer_consume_token (parser
->lexer
);
7824 /* Look for the `]' token. */
7825 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7826 /* Remember that this is the `[]' construct. */
7832 /* Parse the cast-expression. */
7833 expression
= cp_parser_simple_cast_expression (parser
);
7835 /* A delete-expression may not appear in an integral constant
7837 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7838 return error_mark_node
;
7840 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7841 tf_warning_or_error
);
7844 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7845 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7849 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7851 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7852 switch (token
->type
)
7858 case CPP_CLOSE_SQUARE
:
7859 case CPP_CLOSE_PAREN
:
7860 case CPP_CLOSE_BRACE
:
7861 case CPP_OPEN_BRACE
:
7865 case CPP_DEREF_STAR
:
7873 case CPP_GREATER_EQ
:
7894 case CPP_OPEN_PAREN
:
7895 /* In ((type ()) () the last () isn't a valid cast-expression,
7896 so the whole must be parsed as postfix-expression. */
7897 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7900 case CPP_OPEN_SQUARE
:
7901 /* '[' may start a primary-expression in obj-c++ and in C++11,
7902 as a lambda-expression, eg, '(void)[]{}'. */
7903 if (cxx_dialect
>= cxx11
)
7905 return c_dialect_objc ();
7908 case CPP_MINUS_MINUS
:
7909 /* '++' and '--' may or may not start a cast-expression:
7911 struct T { void operator++(int); };
7912 void f() { (T())++; }
7925 /* Parse a cast-expression.
7929 ( type-id ) cast-expression
7931 ADDRESS_P is true iff the unary-expression is appearing as the
7932 operand of the `&' operator. CAST_P is true if this expression is
7933 the target of a cast.
7935 Returns a representation of the expression. */
7938 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7939 bool decltype_p
, cp_id_kind
* pidk
)
7941 /* If it's a `(', then we might be looking at a cast. */
7942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7944 tree type
= NULL_TREE
;
7945 tree expr
= NULL_TREE
;
7946 int cast_expression
= 0;
7947 const char *saved_message
;
7949 /* There's no way to know yet whether or not this is a cast.
7950 For example, `(int (3))' is a unary-expression, while `(int)
7951 3' is a cast. So, we resort to parsing tentatively. */
7952 cp_parser_parse_tentatively (parser
);
7953 /* Types may not be defined in a cast. */
7954 saved_message
= parser
->type_definition_forbidden_message
;
7955 parser
->type_definition_forbidden_message
7956 = G_("types may not be defined in casts");
7957 /* Consume the `('. */
7958 cp_lexer_consume_token (parser
->lexer
);
7959 /* A very tricky bit is that `(struct S) { 3 }' is a
7960 compound-literal (which we permit in C++ as an extension).
7961 But, that construct is not a cast-expression -- it is a
7962 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7963 is legal; if the compound-literal were a cast-expression,
7964 you'd need an extra set of parentheses.) But, if we parse
7965 the type-id, and it happens to be a class-specifier, then we
7966 will commit to the parse at that point, because we cannot
7967 undo the action that is done when creating a new class. So,
7968 then we cannot back up and do a postfix-expression.
7970 Another tricky case is the following (c++/29234):
7972 struct S { void operator () (); };
7979 As a type-id we parse the parenthesized S()() as a function
7980 returning a function, groktypename complains and we cannot
7981 back up in this case either.
7983 Therefore, we scan ahead to the closing `)', and check to see
7984 if the tokens after the `)' can start a cast-expression. Otherwise
7985 we are dealing with an unary-expression, a postfix-expression
7988 Yet another tricky case, in C++11, is the following (c++/54891):
7992 The issue is that usually, besides the case of lambda-expressions,
7993 the parenthesized type-id cannot be followed by '[', and, eg, we
7994 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7995 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7996 we don't commit, we try a cast-expression, then an unary-expression.
7998 Save tokens so that we can put them back. */
7999 cp_lexer_save_tokens (parser
->lexer
);
8001 /* We may be looking at a cast-expression. */
8002 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8003 /*consume_paren=*/true))
8005 = cp_parser_tokens_start_cast_expression (parser
);
8007 /* Roll back the tokens we skipped. */
8008 cp_lexer_rollback_tokens (parser
->lexer
);
8009 /* If we aren't looking at a cast-expression, simulate an error so
8010 that the call to cp_parser_error_occurred below returns true. */
8011 if (!cast_expression
)
8012 cp_parser_simulate_error (parser
);
8015 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8016 parser
->in_type_id_in_expr_p
= true;
8017 /* Look for the type-id. */
8018 type
= cp_parser_type_id (parser
);
8019 /* Look for the closing `)'. */
8020 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8021 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8024 /* Restore the saved message. */
8025 parser
->type_definition_forbidden_message
= saved_message
;
8027 /* At this point this can only be either a cast or a
8028 parenthesized ctor such as `(T ())' that looks like a cast to
8029 function returning T. */
8030 if (!cp_parser_error_occurred (parser
))
8032 /* Only commit if the cast-expression doesn't start with
8033 '++', '--', or '[' in C++11. */
8034 if (cast_expression
> 0)
8035 cp_parser_commit_to_topmost_tentative_parse (parser
);
8037 expr
= cp_parser_cast_expression (parser
,
8038 /*address_p=*/false,
8040 /*decltype_p=*/false,
8043 if (cp_parser_parse_definitely (parser
))
8045 /* Warn about old-style casts, if so requested. */
8046 if (warn_old_style_cast
8047 && !in_system_header_at (input_location
)
8048 && !VOID_TYPE_P (type
)
8049 && current_lang_name
!= lang_name_c
)
8050 warning (OPT_Wold_style_cast
, "use of old-style cast");
8052 /* Only type conversions to integral or enumeration types
8053 can be used in constant-expressions. */
8054 if (!cast_valid_in_integral_constant_expression_p (type
)
8055 && cp_parser_non_integral_constant_expression (parser
,
8057 return error_mark_node
;
8059 /* Perform the cast. */
8060 expr
= build_c_cast (input_location
, type
, expr
);
8065 cp_parser_abort_tentative_parse (parser
);
8068 /* If we get here, then it's not a cast, so it must be a
8069 unary-expression. */
8070 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8071 cast_p
, decltype_p
);
8074 /* Parse a binary expression of the general form:
8078 pm-expression .* cast-expression
8079 pm-expression ->* cast-expression
8081 multiplicative-expression:
8083 multiplicative-expression * pm-expression
8084 multiplicative-expression / pm-expression
8085 multiplicative-expression % pm-expression
8087 additive-expression:
8088 multiplicative-expression
8089 additive-expression + multiplicative-expression
8090 additive-expression - multiplicative-expression
8094 shift-expression << additive-expression
8095 shift-expression >> additive-expression
8097 relational-expression:
8099 relational-expression < shift-expression
8100 relational-expression > shift-expression
8101 relational-expression <= shift-expression
8102 relational-expression >= shift-expression
8106 relational-expression:
8107 relational-expression <? shift-expression
8108 relational-expression >? shift-expression
8110 equality-expression:
8111 relational-expression
8112 equality-expression == relational-expression
8113 equality-expression != relational-expression
8117 and-expression & equality-expression
8119 exclusive-or-expression:
8121 exclusive-or-expression ^ and-expression
8123 inclusive-or-expression:
8124 exclusive-or-expression
8125 inclusive-or-expression | exclusive-or-expression
8127 logical-and-expression:
8128 inclusive-or-expression
8129 logical-and-expression && inclusive-or-expression
8131 logical-or-expression:
8132 logical-and-expression
8133 logical-or-expression || logical-and-expression
8135 All these are implemented with a single function like:
8138 simple-cast-expression
8139 binary-expression <token> binary-expression
8141 CAST_P is true if this expression is the target of a cast.
8143 The binops_by_token map is used to get the tree codes for each <token> type.
8144 binary-expressions are associated according to a precedence table. */
8146 #define TOKEN_PRECEDENCE(token) \
8147 (((token->type == CPP_GREATER \
8148 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8149 && !parser->greater_than_is_operator_p) \
8150 ? PREC_NOT_OPERATOR \
8151 : binops_by_token[token->type].prec)
8154 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8155 bool no_toplevel_fold_p
,
8157 enum cp_parser_prec prec
,
8160 cp_parser_expression_stack stack
;
8161 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8162 cp_parser_expression_stack_entry current
;
8165 enum tree_code rhs_type
;
8166 enum cp_parser_prec new_prec
, lookahead_prec
;
8169 /* Parse the first expression. */
8170 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8171 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8172 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8173 cast_p
, decltype_p
, pidk
);
8174 current
.prec
= prec
;
8176 if (cp_parser_error_occurred (parser
))
8177 return error_mark_node
;
8181 /* Get an operator token. */
8182 token
= cp_lexer_peek_token (parser
->lexer
);
8184 if (warn_cxx0x_compat
8185 && token
->type
== CPP_RSHIFT
8186 && !parser
->greater_than_is_operator_p
)
8188 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
8189 "%<>>%> operator is treated"
8190 " as two right angle brackets in C++11"))
8191 inform (token
->location
,
8192 "suggest parentheses around %<>>%> expression");
8195 new_prec
= TOKEN_PRECEDENCE (token
);
8197 /* Popping an entry off the stack means we completed a subexpression:
8198 - either we found a token which is not an operator (`>' where it is not
8199 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8200 will happen repeatedly;
8201 - or, we found an operator which has lower priority. This is the case
8202 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8204 if (new_prec
<= current
.prec
)
8213 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8214 current
.loc
= token
->location
;
8216 /* We used the operator token. */
8217 cp_lexer_consume_token (parser
->lexer
);
8219 /* For "false && x" or "true || x", x will never be executed;
8220 disable warnings while evaluating it. */
8221 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8222 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
8223 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8224 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
8226 /* Extract another operand. It may be the RHS of this expression
8227 or the LHS of a new, higher priority expression. */
8228 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8229 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8230 rhs
= cp_parser_simple_cast_expression (parser
);
8232 /* Get another operator token. Look up its precedence to avoid
8233 building a useless (immediately popped) stack entry for common
8234 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8235 token
= cp_lexer_peek_token (parser
->lexer
);
8236 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8237 if (lookahead_prec
> new_prec
)
8239 /* ... and prepare to parse the RHS of the new, higher priority
8240 expression. Since precedence levels on the stack are
8241 monotonically increasing, we do not have to care about
8246 current
.lhs_type
= rhs_type
;
8247 current
.prec
= new_prec
;
8248 new_prec
= lookahead_prec
;
8252 lookahead_prec
= new_prec
;
8253 /* If the stack is not empty, we have parsed into LHS the right side
8254 (`4' in the example above) of an expression we had suspended.
8255 We can use the information on the stack to recover the LHS (`3')
8256 from the stack together with the tree code (`MULT_EXPR'), and
8257 the precedence of the higher level subexpression
8258 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8259 which will be used to actually build the additive expression. */
8261 rhs_type
= current
.lhs_type
;
8266 /* Undo the disabling of warnings done above. */
8267 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8268 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
8269 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8270 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
8272 if (warn_logical_not_paren
8273 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
8274 && current
.lhs_type
== TRUTH_NOT_EXPR
8275 /* Avoid warning for !!x == y. */
8276 && (TREE_CODE (current
.lhs
) != NE_EXPR
8277 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
8278 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
8279 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
8280 /* Avoid warning for !b == y where b is boolean. */
8281 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
8282 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
8284 /* Avoid warning for !!b == y where b is boolean. */
8285 && (!DECL_P (current
.lhs
)
8286 || TREE_TYPE (current
.lhs
) == NULL_TREE
8287 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
8288 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
8289 maybe_constant_value (rhs
));
8292 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8293 ERROR_MARK for everything that is not a binary expression.
8294 This makes warn_about_parentheses miss some warnings that
8295 involve unary operators. For unary expressions we should
8296 pass the correct tree_code unless the unary expression was
8297 surrounded by parentheses.
8299 if (no_toplevel_fold_p
8300 && lookahead_prec
<= current
.prec
8302 current
.lhs
= build2 (current
.tree_type
,
8303 TREE_CODE_CLASS (current
.tree_type
)
8305 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8308 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8309 current
.lhs
, current
.lhs_type
,
8310 rhs
, rhs_type
, &overload
,
8311 complain_flags (decltype_p
));
8312 current
.lhs_type
= current
.tree_type
;
8313 if (EXPR_P (current
.lhs
))
8314 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
8316 /* If the binary operator required the use of an overloaded operator,
8317 then this expression cannot be an integral constant-expression.
8318 An overloaded operator can be used even if both operands are
8319 otherwise permissible in an integral constant-expression if at
8320 least one of the operands is of enumeration type. */
8323 && cp_parser_non_integral_constant_expression (parser
,
8325 return error_mark_node
;
8332 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8333 bool no_toplevel_fold_p
,
8334 enum cp_parser_prec prec
,
8337 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8338 /*decltype*/false, prec
, pidk
);
8341 /* Parse the `? expression : assignment-expression' part of a
8342 conditional-expression. The LOGICAL_OR_EXPR is the
8343 logical-or-expression that started the conditional-expression.
8344 Returns a representation of the entire conditional-expression.
8346 This routine is used by cp_parser_assignment_expression.
8348 ? expression : assignment-expression
8352 ? : assignment-expression */
8355 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8358 tree assignment_expr
;
8359 struct cp_token
*token
;
8360 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8362 /* Consume the `?' token. */
8363 cp_lexer_consume_token (parser
->lexer
);
8364 token
= cp_lexer_peek_token (parser
->lexer
);
8365 if (cp_parser_allow_gnu_extensions_p (parser
)
8366 && token
->type
== CPP_COLON
)
8368 pedwarn (token
->location
, OPT_Wpedantic
,
8369 "ISO C++ does not allow ?: with omitted middle operand");
8370 /* Implicit true clause. */
8372 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8373 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8377 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8378 parser
->colon_corrects_to_scope_p
= false;
8379 /* Parse the expression. */
8380 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8381 expr
= cp_parser_expression (parser
);
8382 c_inhibit_evaluation_warnings
+=
8383 ((logical_or_expr
== truthvalue_true_node
)
8384 - (logical_or_expr
== truthvalue_false_node
));
8385 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8388 /* The next token should be a `:'. */
8389 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8390 /* Parse the assignment-expression. */
8391 assignment_expr
= cp_parser_assignment_expression (parser
);
8392 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8394 /* Build the conditional-expression. */
8395 return build_x_conditional_expr (loc
, logical_or_expr
,
8398 tf_warning_or_error
);
8401 /* Parse an assignment-expression.
8403 assignment-expression:
8404 conditional-expression
8405 logical-or-expression assignment-operator assignment_expression
8408 CAST_P is true if this expression is the target of a cast.
8409 DECLTYPE_P is true if this expression is the operand of decltype.
8411 Returns a representation for the expression. */
8414 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8415 bool cast_p
, bool decltype_p
)
8419 /* If the next token is the `throw' keyword, then we're looking at
8420 a throw-expression. */
8421 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8422 expr
= cp_parser_throw_expression (parser
);
8423 /* Otherwise, it must be that we are looking at a
8424 logical-or-expression. */
8427 /* Parse the binary expressions (logical-or-expression). */
8428 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8430 PREC_NOT_OPERATOR
, pidk
);
8431 /* If the next token is a `?' then we're actually looking at a
8432 conditional-expression. */
8433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8434 return cp_parser_question_colon_clause (parser
, expr
);
8437 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8439 /* If it's an assignment-operator, we're using the second
8441 enum tree_code assignment_operator
8442 = cp_parser_assignment_operator_opt (parser
);
8443 if (assignment_operator
!= ERROR_MARK
)
8445 bool non_constant_p
;
8446 location_t saved_input_location
;
8448 /* Parse the right-hand side of the assignment. */
8449 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8451 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8452 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8454 /* An assignment may not appear in a
8455 constant-expression. */
8456 if (cp_parser_non_integral_constant_expression (parser
,
8458 return error_mark_node
;
8459 /* Build the assignment expression. Its default
8460 location is the location of the '=' token. */
8461 saved_input_location
= input_location
;
8462 input_location
= loc
;
8463 expr
= build_x_modify_expr (loc
, expr
,
8464 assignment_operator
,
8466 complain_flags (decltype_p
));
8467 input_location
= saved_input_location
;
8475 /* Parse an (optional) assignment-operator.
8477 assignment-operator: one of
8478 = *= /= %= += -= >>= <<= &= ^= |=
8482 assignment-operator: one of
8485 If the next token is an assignment operator, the corresponding tree
8486 code is returned, and the token is consumed. For example, for
8487 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8488 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8489 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8490 operator, ERROR_MARK is returned. */
8492 static enum tree_code
8493 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8498 /* Peek at the next token. */
8499 token
= cp_lexer_peek_token (parser
->lexer
);
8501 switch (token
->type
)
8512 op
= TRUNC_DIV_EXPR
;
8516 op
= TRUNC_MOD_EXPR
;
8548 /* Nothing else is an assignment operator. */
8552 /* If it was an assignment operator, consume it. */
8553 if (op
!= ERROR_MARK
)
8554 cp_lexer_consume_token (parser
->lexer
);
8559 /* Parse an expression.
8562 assignment-expression
8563 expression , assignment-expression
8565 CAST_P is true if this expression is the target of a cast.
8566 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8567 except possibly parenthesized or on the RHS of a comma (N3276).
8569 Returns a representation of the expression. */
8572 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8573 bool cast_p
, bool decltype_p
)
8575 tree expression
= NULL_TREE
;
8576 location_t loc
= UNKNOWN_LOCATION
;
8580 tree assignment_expression
;
8582 /* Parse the next assignment-expression. */
8583 assignment_expression
8584 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
8586 /* We don't create a temporary for a call that is the immediate operand
8587 of decltype or on the RHS of a comma. But when we see a comma, we
8588 need to create a temporary for a call on the LHS. */
8589 if (decltype_p
&& !processing_template_decl
8590 && TREE_CODE (assignment_expression
) == CALL_EXPR
8591 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8592 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8593 assignment_expression
8594 = build_cplus_new (TREE_TYPE (assignment_expression
),
8595 assignment_expression
, tf_warning_or_error
);
8597 /* If this is the first assignment-expression, we can just
8600 expression
= assignment_expression
;
8602 expression
= build_x_compound_expr (loc
, expression
,
8603 assignment_expression
,
8604 complain_flags (decltype_p
));
8605 /* If the next token is not a comma, then we are done with the
8607 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8609 /* Consume the `,'. */
8610 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8611 cp_lexer_consume_token (parser
->lexer
);
8612 /* A comma operator cannot appear in a constant-expression. */
8613 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8614 expression
= error_mark_node
;
8620 /* Parse a constant-expression.
8622 constant-expression:
8623 conditional-expression
8625 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8626 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8627 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8628 is false, NON_CONSTANT_P should be NULL. */
8631 cp_parser_constant_expression (cp_parser
* parser
,
8632 bool allow_non_constant_p
,
8633 bool *non_constant_p
)
8635 bool saved_integral_constant_expression_p
;
8636 bool saved_allow_non_integral_constant_expression_p
;
8637 bool saved_non_integral_constant_expression_p
;
8640 /* It might seem that we could simply parse the
8641 conditional-expression, and then check to see if it were
8642 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8643 one that the compiler can figure out is constant, possibly after
8644 doing some simplifications or optimizations. The standard has a
8645 precise definition of constant-expression, and we must honor
8646 that, even though it is somewhat more restrictive.
8652 is not a legal declaration, because `(2, 3)' is not a
8653 constant-expression. The `,' operator is forbidden in a
8654 constant-expression. However, GCC's constant-folding machinery
8655 will fold this operation to an INTEGER_CST for `3'. */
8657 /* Save the old settings. */
8658 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8659 saved_allow_non_integral_constant_expression_p
8660 = parser
->allow_non_integral_constant_expression_p
;
8661 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8662 /* We are now parsing a constant-expression. */
8663 parser
->integral_constant_expression_p
= true;
8664 parser
->allow_non_integral_constant_expression_p
8665 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8666 parser
->non_integral_constant_expression_p
= false;
8667 /* Although the grammar says "conditional-expression", we parse an
8668 "assignment-expression", which also permits "throw-expression"
8669 and the use of assignment operators. In the case that
8670 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8671 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8672 actually essential that we look for an assignment-expression.
8673 For example, cp_parser_initializer_clauses uses this function to
8674 determine whether a particular assignment-expression is in fact
8676 expression
= cp_parser_assignment_expression (parser
);
8677 /* Restore the old settings. */
8678 parser
->integral_constant_expression_p
8679 = saved_integral_constant_expression_p
;
8680 parser
->allow_non_integral_constant_expression_p
8681 = saved_allow_non_integral_constant_expression_p
;
8682 if (cxx_dialect
>= cxx11
)
8684 /* Require an rvalue constant expression here; that's what our
8685 callers expect. Reference constant expressions are handled
8686 separately in e.g. cp_parser_template_argument. */
8687 bool is_const
= potential_rvalue_constant_expression (expression
);
8688 parser
->non_integral_constant_expression_p
= !is_const
;
8689 if (!is_const
&& !allow_non_constant_p
)
8690 require_potential_rvalue_constant_expression (expression
);
8692 if (allow_non_constant_p
)
8693 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8694 parser
->non_integral_constant_expression_p
8695 = saved_non_integral_constant_expression_p
;
8700 /* Parse __builtin_offsetof.
8702 offsetof-expression:
8703 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8705 offsetof-member-designator:
8707 | offsetof-member-designator "." id-expression
8708 | offsetof-member-designator "[" expression "]"
8709 | offsetof-member-designator "->" id-expression */
8712 cp_parser_builtin_offsetof (cp_parser
*parser
)
8714 int save_ice_p
, save_non_ice_p
;
8719 /* We're about to accept non-integral-constant things, but will
8720 definitely yield an integral constant expression. Save and
8721 restore these values around our local parsing. */
8722 save_ice_p
= parser
->integral_constant_expression_p
;
8723 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8725 /* Consume the "__builtin_offsetof" token. */
8726 cp_lexer_consume_token (parser
->lexer
);
8727 /* Consume the opening `('. */
8728 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8729 /* Parse the type-id. */
8730 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8731 type
= cp_parser_type_id (parser
);
8732 /* Look for the `,'. */
8733 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8734 token
= cp_lexer_peek_token (parser
->lexer
);
8736 /* Build the (type *)null that begins the traditional offsetof macro. */
8737 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8738 tf_warning_or_error
);
8740 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8741 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8742 true, &dummy
, token
->location
);
8745 token
= cp_lexer_peek_token (parser
->lexer
);
8746 switch (token
->type
)
8748 case CPP_OPEN_SQUARE
:
8749 /* offsetof-member-designator "[" expression "]" */
8750 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8755 /* offsetof-member-designator "->" identifier */
8756 expr
= grok_array_decl (token
->location
, expr
,
8757 integer_zero_node
, false);
8761 /* offsetof-member-designator "." identifier */
8762 cp_lexer_consume_token (parser
->lexer
);
8763 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8768 case CPP_CLOSE_PAREN
:
8769 /* Consume the ")" token. */
8770 cp_lexer_consume_token (parser
->lexer
);
8774 /* Error. We know the following require will fail, but
8775 that gives the proper error message. */
8776 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8777 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8778 expr
= error_mark_node
;
8784 expr
= finish_offsetof (expr
, loc
);
8787 parser
->integral_constant_expression_p
= save_ice_p
;
8788 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8793 /* Parse a trait expression.
8795 Returns a representation of the expression, the underlying type
8796 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8799 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8802 tree type1
, type2
= NULL_TREE
;
8803 bool binary
= false;
8804 bool variadic
= false;
8808 case RID_HAS_NOTHROW_ASSIGN
:
8809 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8811 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8812 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8814 case RID_HAS_NOTHROW_COPY
:
8815 kind
= CPTK_HAS_NOTHROW_COPY
;
8817 case RID_HAS_TRIVIAL_ASSIGN
:
8818 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8820 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8821 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8823 case RID_HAS_TRIVIAL_COPY
:
8824 kind
= CPTK_HAS_TRIVIAL_COPY
;
8826 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8827 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8829 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8830 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8832 case RID_IS_ABSTRACT
:
8833 kind
= CPTK_IS_ABSTRACT
;
8835 case RID_IS_BASE_OF
:
8836 kind
= CPTK_IS_BASE_OF
;
8840 kind
= CPTK_IS_CLASS
;
8843 kind
= CPTK_IS_EMPTY
;
8846 kind
= CPTK_IS_ENUM
;
8849 kind
= CPTK_IS_FINAL
;
8851 case RID_IS_LITERAL_TYPE
:
8852 kind
= CPTK_IS_LITERAL_TYPE
;
8857 case RID_IS_POLYMORPHIC
:
8858 kind
= CPTK_IS_POLYMORPHIC
;
8860 case RID_IS_STD_LAYOUT
:
8861 kind
= CPTK_IS_STD_LAYOUT
;
8863 case RID_IS_TRIVIAL
:
8864 kind
= CPTK_IS_TRIVIAL
;
8866 case RID_IS_TRIVIALLY_ASSIGNABLE
:
8867 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
8870 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
8871 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
8874 case RID_IS_TRIVIALLY_COPYABLE
:
8875 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
8878 kind
= CPTK_IS_UNION
;
8880 case RID_UNDERLYING_TYPE
:
8881 kind
= CPTK_UNDERLYING_TYPE
;
8886 case RID_DIRECT_BASES
:
8887 kind
= CPTK_DIRECT_BASES
;
8893 /* Consume the token. */
8894 cp_lexer_consume_token (parser
->lexer
);
8896 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8898 type1
= cp_parser_type_id (parser
);
8900 if (type1
== error_mark_node
)
8901 return error_mark_node
;
8905 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8907 type2
= cp_parser_type_id (parser
);
8909 if (type2
== error_mark_node
)
8910 return error_mark_node
;
8914 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8916 cp_lexer_consume_token (parser
->lexer
);
8917 tree elt
= cp_parser_type_id (parser
);
8918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8920 cp_lexer_consume_token (parser
->lexer
);
8921 elt
= make_pack_expansion (elt
);
8923 if (elt
== error_mark_node
)
8924 return error_mark_node
;
8925 type2
= tree_cons (NULL_TREE
, elt
, type2
);
8929 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8931 /* Complete the trait expression, which may mean either processing
8932 the trait expr now or saving it for template instantiation. */
8935 case CPTK_UNDERLYING_TYPE
:
8936 return finish_underlying_type (type1
);
8938 return finish_bases (type1
, false);
8939 case CPTK_DIRECT_BASES
:
8940 return finish_bases (type1
, true);
8942 return finish_trait_expr (kind
, type1
, type2
);
8946 /* Lambdas that appear in variable initializer or default argument scope
8947 get that in their mangling, so we need to record it. We might as well
8948 use the count for function and namespace scopes as well. */
8949 static GTY(()) tree lambda_scope
;
8950 static GTY(()) int lambda_count
;
8951 typedef struct GTY(()) tree_int
8956 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8959 start_lambda_scope (tree decl
)
8963 /* Once we're inside a function, we ignore other scopes and just push
8964 the function again so that popping works properly. */
8965 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8966 decl
= current_function_decl
;
8967 ti
.t
= lambda_scope
;
8968 ti
.i
= lambda_count
;
8969 vec_safe_push (lambda_scope_stack
, ti
);
8970 if (lambda_scope
!= decl
)
8972 /* Don't reset the count if we're still in the same function. */
8973 lambda_scope
= decl
;
8979 record_lambda_scope (tree lambda
)
8981 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8982 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8986 finish_lambda_scope (void)
8988 tree_int
*p
= &lambda_scope_stack
->last ();
8989 if (lambda_scope
!= p
->t
)
8991 lambda_scope
= p
->t
;
8992 lambda_count
= p
->i
;
8994 lambda_scope_stack
->pop ();
8997 /* Parse a lambda expression.
9000 lambda-introducer lambda-declarator [opt] compound-statement
9002 Returns a representation of the expression. */
9005 cp_parser_lambda_expression (cp_parser
* parser
)
9007 tree lambda_expr
= build_lambda_expr ();
9010 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9011 cp_token_position start
= 0;
9013 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
9015 if (cp_unevaluated_operand
)
9017 if (!token
->error_reported
)
9019 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9020 "lambda-expression in unevaluated context");
9021 token
->error_reported
= true;
9025 else if (parser
->in_template_argument_list_p
)
9027 if (!token
->error_reported
)
9029 error_at (token
->location
, "lambda-expression in template-argument");
9030 token
->error_reported
= true;
9035 /* We may be in the middle of deferred access check. Disable
9037 push_deferring_access_checks (dk_no_deferred
);
9039 cp_parser_lambda_introducer (parser
, lambda_expr
);
9041 type
= begin_lambda_type (lambda_expr
);
9042 if (type
== error_mark_node
)
9043 return error_mark_node
;
9045 record_lambda_scope (lambda_expr
);
9047 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9048 determine_visibility (TYPE_NAME (type
));
9050 /* Now that we've started the type, add the capture fields for any
9051 explicit captures. */
9052 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9055 /* Inside the class, surrounding template-parameter-lists do not apply. */
9056 unsigned int saved_num_template_parameter_lists
9057 = parser
->num_template_parameter_lists
;
9058 unsigned char in_statement
= parser
->in_statement
;
9059 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9060 bool fully_implicit_function_template_p
9061 = parser
->fully_implicit_function_template_p
;
9062 tree implicit_template_parms
= parser
->implicit_template_parms
;
9063 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9064 bool auto_is_implicit_function_template_parm_p
9065 = parser
->auto_is_implicit_function_template_parm_p
;
9067 parser
->num_template_parameter_lists
= 0;
9068 parser
->in_statement
= 0;
9069 parser
->in_switch_statement_p
= false;
9070 parser
->fully_implicit_function_template_p
= false;
9071 parser
->implicit_template_parms
= 0;
9072 parser
->implicit_template_scope
= 0;
9073 parser
->auto_is_implicit_function_template_parm_p
= false;
9075 /* By virtue of defining a local class, a lambda expression has access to
9076 the private variables of enclosing classes. */
9078 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9082 if (!cp_parser_error_occurred (parser
)
9083 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9084 && cp_parser_start_tentative_firewall (parser
))
9086 cp_parser_lambda_body (parser
, lambda_expr
);
9088 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9090 if (cp_parser_skip_to_closing_brace (parser
))
9091 cp_lexer_consume_token (parser
->lexer
);
9094 /* The capture list was built up in reverse order; fix that now. */
9095 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9096 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9099 maybe_add_lambda_conv_op (type
);
9101 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9103 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9104 parser
->in_statement
= in_statement
;
9105 parser
->in_switch_statement_p
= in_switch_statement_p
;
9106 parser
->fully_implicit_function_template_p
9107 = fully_implicit_function_template_p
;
9108 parser
->implicit_template_parms
= implicit_template_parms
;
9109 parser
->implicit_template_scope
= implicit_template_scope
;
9110 parser
->auto_is_implicit_function_template_parm_p
9111 = auto_is_implicit_function_template_parm_p
;
9114 pop_deferring_access_checks ();
9116 /* This field is only used during parsing of the lambda. */
9117 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9119 /* This lambda shouldn't have any proxies left at this point. */
9120 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9121 /* And now that we're done, push proxies for an enclosing lambda. */
9122 insert_pending_capture_proxies ();
9125 lambda_expr
= build_lambda_object (lambda_expr
);
9127 lambda_expr
= error_mark_node
;
9129 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9134 /* Parse the beginning of a lambda expression.
9137 [ lambda-capture [opt] ]
9139 LAMBDA_EXPR is the current representation of the lambda expression. */
9142 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9144 /* Need commas after the first capture. */
9147 /* Eat the leading `['. */
9148 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9150 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9151 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9152 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9153 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9154 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9155 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9157 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9159 cp_lexer_consume_token (parser
->lexer
);
9163 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9165 cp_token
* capture_token
;
9167 tree capture_init_expr
;
9168 cp_id_kind idk
= CP_ID_KIND_NONE
;
9169 bool explicit_init_p
= false;
9171 enum capture_kind_type
9176 enum capture_kind_type capture_kind
= BY_COPY
;
9178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9180 error ("expected end of capture-list");
9187 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9189 /* Possibly capture `this'. */
9190 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9192 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9193 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9194 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9195 "with by-copy capture default");
9196 cp_lexer_consume_token (parser
->lexer
);
9197 add_capture (lambda_expr
,
9198 /*id=*/this_identifier
,
9199 /*initializer=*/finish_this_expr(),
9200 /*by_reference_p=*/false,
9205 /* Remember whether we want to capture as a reference or not. */
9206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9208 capture_kind
= BY_REFERENCE
;
9209 cp_lexer_consume_token (parser
->lexer
);
9212 /* Get the identifier. */
9213 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9214 capture_id
= cp_parser_identifier (parser
);
9216 if (capture_id
== error_mark_node
)
9217 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9218 delimiters, but I modified this to stop on unnested ']' as well. It
9219 was already changed to stop on unnested '}', so the
9220 "closing_parenthesis" name is no more misleading with my change. */
9222 cp_parser_skip_to_closing_parenthesis (parser
,
9223 /*recovering=*/true,
9225 /*consume_paren=*/true);
9229 /* Find the initializer for this capture. */
9230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9231 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9232 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9234 bool direct
, non_constant
;
9235 /* An explicit initializer exists. */
9236 if (cxx_dialect
< cxx14
)
9237 pedwarn (input_location
, 0,
9238 "lambda capture initializers "
9239 "only available with -std=c++14 or -std=gnu++14");
9240 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9242 explicit_init_p
= true;
9243 if (capture_init_expr
== NULL_TREE
)
9245 error ("empty initializer for lambda init-capture");
9246 capture_init_expr
= error_mark_node
;
9251 const char* error_msg
;
9253 /* Turn the identifier into an id-expression. */
9255 = cp_parser_lookup_name_simple (parser
, capture_id
,
9256 capture_token
->location
);
9258 if (capture_init_expr
== error_mark_node
)
9260 unqualified_name_lookup_error (capture_id
);
9263 else if (DECL_P (capture_init_expr
)
9264 && (!VAR_P (capture_init_expr
)
9265 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9267 error_at (capture_token
->location
,
9268 "capture of non-variable %qD ",
9270 inform (0, "%q+#D declared here", capture_init_expr
);
9273 if (VAR_P (capture_init_expr
)
9274 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9276 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9277 "%qD with non-automatic storage duration",
9279 inform (0, "%q+#D declared here", capture_init_expr
);
9284 = finish_id_expression
9289 /*integral_constant_expression_p=*/false,
9290 /*allow_non_integral_constant_expression_p=*/false,
9291 /*non_integral_constant_expression_p=*/NULL
,
9292 /*template_p=*/false,
9294 /*address_p=*/false,
9295 /*template_arg_p=*/false,
9297 capture_token
->location
);
9299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9301 cp_lexer_consume_token (parser
->lexer
);
9302 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9305 check_for_bare_parameter_packs (capture_init_expr
);
9308 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9309 && !explicit_init_p
)
9311 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9312 && capture_kind
== BY_COPY
)
9313 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9314 "of %qD redundant with by-copy capture default",
9316 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9317 && capture_kind
== BY_REFERENCE
)
9318 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9319 "capture of %qD redundant with by-reference capture "
9320 "default", capture_id
);
9323 add_capture (lambda_expr
,
9326 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9330 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9333 /* Parse the (optional) middle of a lambda expression.
9336 < template-parameter-list [opt] >
9337 ( parameter-declaration-clause [opt] )
9338 attribute-specifier [opt]
9340 exception-specification [opt]
9341 lambda-return-type-clause [opt]
9343 LAMBDA_EXPR is the current representation of the lambda expression. */
9346 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9348 /* 5.1.1.4 of the standard says:
9349 If a lambda-expression does not include a lambda-declarator, it is as if
9350 the lambda-declarator were ().
9351 This means an empty parameter list, no attributes, and no exception
9353 tree param_list
= void_list_node
;
9354 tree attributes
= NULL_TREE
;
9355 tree exception_spec
= NULL_TREE
;
9356 tree template_param_list
= NULL_TREE
;
9358 /* The template-parameter-list is optional, but must begin with
9359 an opening angle if present. */
9360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9362 if (cxx_dialect
< cxx14
)
9363 pedwarn (parser
->lexer
->next_token
->location
, 0,
9364 "lambda templates are only available with "
9365 "-std=c++14 or -std=gnu++14");
9367 cp_lexer_consume_token (parser
->lexer
);
9369 template_param_list
= cp_parser_template_parameter_list (parser
);
9371 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9373 /* We just processed one more parameter list. */
9374 ++parser
->num_template_parameter_lists
;
9377 /* The parameter-declaration-clause is optional (unless
9378 template-parameter-list was given), but must begin with an
9379 opening parenthesis if present. */
9380 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9382 cp_lexer_consume_token (parser
->lexer
);
9384 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9386 /* Parse parameters. */
9387 param_list
= cp_parser_parameter_declaration_clause (parser
);
9389 /* Default arguments shall not be specified in the
9390 parameter-declaration-clause of a lambda-declarator. */
9391 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9392 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
9393 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9394 "default argument specified for lambda parameter");
9396 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9398 attributes
= cp_parser_attributes_opt (parser
);
9400 /* Parse optional `mutable' keyword. */
9401 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9403 cp_lexer_consume_token (parser
->lexer
);
9404 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9407 /* Parse optional exception specification. */
9408 exception_spec
= cp_parser_exception_specification_opt (parser
);
9410 /* Parse optional trailing return type. */
9411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9413 cp_lexer_consume_token (parser
->lexer
);
9414 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9415 = cp_parser_trailing_type_id (parser
);
9418 /* The function parameters must be in scope all the way until after the
9419 trailing-return-type in case of decltype. */
9420 pop_bindings_and_leave_scope ();
9422 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9423 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9425 /* Create the function call operator.
9427 Messing with declarators like this is no uglier than building up the
9428 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9431 cp_decl_specifier_seq return_type_specs
;
9432 cp_declarator
* declarator
;
9437 clear_decl_specs (&return_type_specs
);
9438 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9439 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9441 /* Maybe we will deduce the return type later. */
9442 return_type_specs
.type
= make_auto ();
9444 p
= obstack_alloc (&declarator_obstack
, 0);
9446 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9449 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9450 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9451 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9452 VIRT_SPEC_UNSPECIFIED
,
9455 /*late_return_type=*/NULL_TREE
);
9456 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9458 fco
= grokmethod (&return_type_specs
,
9461 if (fco
!= error_mark_node
)
9463 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9464 DECL_ARTIFICIAL (fco
) = 1;
9465 /* Give the object parameter a different name. */
9466 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9467 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9468 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
9470 if (template_param_list
)
9472 fco
= finish_member_template_decl (fco
);
9473 finish_template_decl (template_param_list
);
9474 --parser
->num_template_parameter_lists
;
9476 else if (parser
->fully_implicit_function_template_p
)
9477 fco
= finish_fully_implicit_template (parser
, fco
);
9479 finish_member_declaration (fco
);
9481 obstack_free (&declarator_obstack
, p
);
9483 return (fco
!= error_mark_node
);
9487 /* Parse the body of a lambda expression, which is simply
9491 but which requires special handling.
9492 LAMBDA_EXPR is the current representation of the lambda expression. */
9495 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9497 bool nested
= (current_function_decl
!= NULL_TREE
);
9498 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9500 push_function_context ();
9502 /* Still increment function_depth so that we don't GC in the
9503 middle of an expression. */
9505 /* Clear this in case we're in the middle of a default argument. */
9506 parser
->local_variables_forbidden_p
= false;
9508 /* Finish the function call operator
9510 + late_parsing_for_member
9511 + function_definition_after_declarator
9512 + ctor_initializer_opt_and_function_body */
9514 tree fco
= lambda_function (lambda_expr
);
9520 /* Let the front end know that we are going to be defining this
9522 start_preparsed_function (fco
,
9524 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9526 start_lambda_scope (fco
);
9527 body
= begin_function_body ();
9529 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9532 /* Push the proxies for any explicit captures. */
9533 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9534 cap
= TREE_CHAIN (cap
))
9535 build_capture_proxy (TREE_PURPOSE (cap
));
9537 compound_stmt
= begin_compound_stmt (0);
9539 /* 5.1.1.4 of the standard says:
9540 If a lambda-expression does not include a trailing-return-type, it
9541 is as if the trailing-return-type denotes the following type:
9542 * if the compound-statement is of the form
9543 { return attribute-specifier [opt] expression ; }
9544 the type of the returned expression after lvalue-to-rvalue
9545 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9546 (_conv.array_ 4.2), and function-to-pointer conversion
9548 * otherwise, void. */
9550 /* In a lambda that has neither a lambda-return-type-clause
9551 nor a deducible form, errors should be reported for return statements
9552 in the body. Since we used void as the placeholder return type, parsing
9553 the body as usual will give such desired behavior. */
9554 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9555 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9556 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9558 tree expr
= NULL_TREE
;
9559 cp_id_kind idk
= CP_ID_KIND_NONE
;
9561 /* Parse tentatively in case there's more after the initial return
9563 cp_parser_parse_tentatively (parser
);
9565 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9567 expr
= cp_parser_expression (parser
, &idk
);
9569 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9570 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9572 if (cp_parser_parse_definitely (parser
))
9574 if (!processing_template_decl
)
9575 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9577 /* Will get error here if type not deduced yet. */
9578 finish_return_stmt (expr
);
9586 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9587 cp_parser_label_declaration (parser
);
9588 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9589 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9592 finish_compound_stmt (compound_stmt
);
9595 finish_function_body (body
);
9596 finish_lambda_scope ();
9598 /* Finish the function and generate code for it if necessary. */
9599 tree fn
= finish_function (/*inline*/2);
9601 /* Only expand if the call op is not a template. */
9602 if (!DECL_TEMPLATE_INFO (fco
))
9603 expand_or_defer_fn (fn
);
9606 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9608 pop_function_context();
9613 /* Statements [gram.stmt.stmt] */
9615 /* Parse a statement.
9619 expression-statement
9624 declaration-statement
9631 attribute-specifier-seq (opt) expression-statement
9632 attribute-specifier-seq (opt) compound-statement
9633 attribute-specifier-seq (opt) selection-statement
9634 attribute-specifier-seq (opt) iteration-statement
9635 attribute-specifier-seq (opt) jump-statement
9636 declaration-statement
9637 attribute-specifier-seq (opt) try-block
9644 IN_COMPOUND is true when the statement is nested inside a
9645 cp_parser_compound_statement; this matters for certain pragmas.
9647 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9648 is a (possibly labeled) if statement which is not enclosed in braces
9649 and has an else clause. This is used to implement -Wparentheses. */
9652 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9653 bool in_compound
, bool *if_p
)
9655 tree statement
, std_attrs
= NULL_TREE
;
9657 location_t statement_location
, attrs_location
;
9662 /* There is no statement yet. */
9663 statement
= NULL_TREE
;
9665 saved_token_sentinel
saved_tokens (parser
->lexer
);
9666 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9667 if (c_dialect_objc ())
9668 /* In obj-c++, seeing '[[' might be the either the beginning of
9669 c++11 attributes, or a nested objc-message-expression. So
9670 let's parse the c++11 attributes tentatively. */
9671 cp_parser_parse_tentatively (parser
);
9672 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9673 if (c_dialect_objc ())
9675 if (!cp_parser_parse_definitely (parser
))
9676 std_attrs
= NULL_TREE
;
9679 /* Peek at the next token. */
9680 token
= cp_lexer_peek_token (parser
->lexer
);
9681 /* Remember the location of the first token in the statement. */
9682 statement_location
= token
->location
;
9683 /* If this is a keyword, then that will often determine what kind of
9684 statement we have. */
9685 if (token
->type
== CPP_KEYWORD
)
9687 enum rid keyword
= token
->keyword
;
9693 /* Looks like a labeled-statement with a case label.
9694 Parse the label, and then use tail recursion to parse
9696 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9701 statement
= cp_parser_selection_statement (parser
, if_p
);
9707 statement
= cp_parser_iteration_statement (parser
, false);
9713 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
9714 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9715 cp_lexer_consume_token (parser
->lexer
);
9716 statement
= error_mark_node
;
9719 statement
= cp_parser_cilk_for (parser
, integer_zero_node
);
9726 statement
= cp_parser_jump_statement (parser
);
9730 cp_lexer_consume_token (parser
->lexer
);
9733 tree sync_expr
= build_cilk_sync ();
9734 SET_EXPR_LOCATION (sync_expr
,
9736 statement
= finish_expr_stmt (sync_expr
);
9740 error_at (token
->location
, "-fcilkplus must be enabled to use"
9742 statement
= error_mark_node
;
9744 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9747 /* Objective-C++ exception-handling constructs. */
9750 case RID_AT_FINALLY
:
9751 case RID_AT_SYNCHRONIZED
:
9753 statement
= cp_parser_objc_statement (parser
);
9757 statement
= cp_parser_try_block (parser
);
9761 /* This must be a namespace alias definition. */
9762 cp_parser_declaration_statement (parser
);
9765 case RID_TRANSACTION_ATOMIC
:
9766 case RID_TRANSACTION_RELAXED
:
9767 statement
= cp_parser_transaction (parser
, keyword
);
9769 case RID_TRANSACTION_CANCEL
:
9770 statement
= cp_parser_transaction_cancel (parser
);
9774 /* It might be a keyword like `int' that can start a
9775 declaration-statement. */
9779 else if (token
->type
== CPP_NAME
)
9781 /* If the next token is a `:', then we are looking at a
9782 labeled-statement. */
9783 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9784 if (token
->type
== CPP_COLON
)
9786 /* Looks like a labeled-statement with an ordinary label.
9787 Parse the label, and then use tail recursion to parse
9790 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9794 /* Anything that starts with a `{' must be a compound-statement. */
9795 else if (token
->type
== CPP_OPEN_BRACE
)
9796 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9797 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9798 a statement all its own. */
9799 else if (token
->type
== CPP_PRAGMA
)
9801 /* Only certain OpenMP pragmas are attached to statements, and thus
9802 are considered statements themselves. All others are not. In
9803 the context of a compound, accept the pragma as a "statement" and
9804 return so that we can check for a close brace. Otherwise we
9805 require a real statement and must go back and read one. */
9807 cp_parser_pragma (parser
, pragma_compound
);
9808 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9812 else if (token
->type
== CPP_EOF
)
9814 cp_parser_error (parser
, "expected statement");
9818 /* Everything else must be a declaration-statement or an
9819 expression-statement. Try for the declaration-statement
9820 first, unless we are looking at a `;', in which case we know that
9821 we have an expression-statement. */
9824 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9826 if (std_attrs
!= NULL_TREE
)
9828 /* Attributes should be parsed as part of the the
9829 declaration, so let's un-parse them. */
9830 saved_tokens
.rollback();
9831 std_attrs
= NULL_TREE
;
9834 cp_parser_parse_tentatively (parser
);
9835 /* Try to parse the declaration-statement. */
9836 cp_parser_declaration_statement (parser
);
9837 /* If that worked, we're done. */
9838 if (cp_parser_parse_definitely (parser
))
9841 /* Look for an expression-statement instead. */
9842 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9845 /* Set the line number for the statement. */
9846 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9847 SET_EXPR_LOCATION (statement
, statement_location
);
9849 /* Note that for now, we don't do anything with c++11 statements
9850 parsed at this level. */
9851 if (std_attrs
!= NULL_TREE
)
9852 warning_at (attrs_location
,
9854 "attributes at the beginning of statement are ignored");
9857 /* Parse the label for a labeled-statement, i.e.
9860 case constant-expression :
9864 case constant-expression ... constant-expression : statement
9866 When a label is parsed without errors, the label is added to the
9867 parse tree by the finish_* functions, so this function doesn't
9868 have to return the label. */
9871 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9874 tree label
= NULL_TREE
;
9875 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9877 /* The next token should be an identifier. */
9878 token
= cp_lexer_peek_token (parser
->lexer
);
9879 if (token
->type
!= CPP_NAME
9880 && token
->type
!= CPP_KEYWORD
)
9882 cp_parser_error (parser
, "expected labeled-statement");
9886 parser
->colon_corrects_to_scope_p
= false;
9887 switch (token
->keyword
)
9894 /* Consume the `case' token. */
9895 cp_lexer_consume_token (parser
->lexer
);
9896 /* Parse the constant-expression. */
9897 expr
= cp_parser_constant_expression (parser
);
9898 if (check_for_bare_parameter_packs (expr
))
9899 expr
= error_mark_node
;
9901 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9902 if (ellipsis
->type
== CPP_ELLIPSIS
)
9904 /* Consume the `...' token. */
9905 cp_lexer_consume_token (parser
->lexer
);
9906 expr_hi
= cp_parser_constant_expression (parser
);
9907 if (check_for_bare_parameter_packs (expr_hi
))
9908 expr_hi
= error_mark_node
;
9910 /* We don't need to emit warnings here, as the common code
9911 will do this for us. */
9914 expr_hi
= NULL_TREE
;
9916 if (parser
->in_switch_statement_p
)
9917 finish_case_label (token
->location
, expr
, expr_hi
);
9919 error_at (token
->location
,
9920 "case label %qE not within a switch statement",
9926 /* Consume the `default' token. */
9927 cp_lexer_consume_token (parser
->lexer
);
9929 if (parser
->in_switch_statement_p
)
9930 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9932 error_at (token
->location
, "case label not within a switch statement");
9936 /* Anything else must be an ordinary label. */
9937 label
= finish_label_stmt (cp_parser_identifier (parser
));
9941 /* Require the `:' token. */
9942 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9944 /* An ordinary label may optionally be followed by attributes.
9945 However, this is only permitted if the attributes are then
9946 followed by a semicolon. This is because, for backward
9947 compatibility, when parsing
9948 lab: __attribute__ ((unused)) int i;
9949 we want the attribute to attach to "i", not "lab". */
9950 if (label
!= NULL_TREE
9951 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9954 cp_parser_parse_tentatively (parser
);
9955 attrs
= cp_parser_gnu_attributes_opt (parser
);
9956 if (attrs
== NULL_TREE
9957 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9958 cp_parser_abort_tentative_parse (parser
);
9959 else if (!cp_parser_parse_definitely (parser
))
9962 attributes
= chainon (attributes
, attrs
);
9965 if (attributes
!= NULL_TREE
)
9966 cplus_decl_attributes (&label
, attributes
, 0);
9968 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9971 /* Parse an expression-statement.
9973 expression-statement:
9976 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9977 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9978 indicates whether this expression-statement is part of an
9979 expression statement. */
9982 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9984 tree statement
= NULL_TREE
;
9985 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9987 /* If the next token is a ';', then there is no expression
9989 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9991 statement
= cp_parser_expression (parser
);
9992 if (statement
== error_mark_node
9993 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9995 cp_parser_skip_to_end_of_block_or_statement (parser
);
9996 return error_mark_node
;
10000 /* Give a helpful message for "A<T>::type t;" and the like. */
10001 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
10002 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10004 if (TREE_CODE (statement
) == SCOPE_REF
)
10005 error_at (token
->location
, "need %<typename%> before %qE because "
10006 "%qT is a dependent scope",
10007 statement
, TREE_OPERAND (statement
, 0));
10008 else if (is_overloaded_fn (statement
)
10009 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
10012 tree fn
= get_first_fn (statement
);
10013 error_at (token
->location
,
10014 "%<%T::%D%> names the constructor, not the type",
10015 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10019 /* Consume the final `;'. */
10020 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10022 if (in_statement_expr
10023 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10024 /* This is the final expression statement of a statement
10026 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10027 else if (statement
)
10028 statement
= finish_expr_stmt (statement
);
10033 /* Parse a compound-statement.
10035 compound-statement:
10036 { statement-seq [opt] }
10040 compound-statement:
10041 { label-declaration-seq [opt] statement-seq [opt] }
10043 label-declaration-seq:
10045 label-declaration-seq label-declaration
10047 Returns a tree representing the statement. */
10050 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10051 bool in_try
, bool function_body
)
10053 tree compound_stmt
;
10055 /* Consume the `{'. */
10056 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10057 return error_mark_node
;
10058 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10059 && !function_body
&& cxx_dialect
< cxx14
)
10060 pedwarn (input_location
, OPT_Wpedantic
,
10061 "compound-statement in constexpr function");
10062 /* Begin the compound-statement. */
10063 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
10064 /* If the next keyword is `__label__' we have a label declaration. */
10065 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10066 cp_parser_label_declaration (parser
);
10067 /* Parse an (optional) statement-seq. */
10068 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10069 /* Finish the compound-statement. */
10070 finish_compound_stmt (compound_stmt
);
10071 /* Consume the `}'. */
10072 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10074 return compound_stmt
;
10077 /* Parse an (optional) statement-seq.
10081 statement-seq [opt] statement */
10084 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10086 /* Scan statements until there aren't any more. */
10089 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10091 /* If we are looking at a `}', then we have run out of
10092 statements; the same is true if we have reached the end
10093 of file, or have stumbled upon a stray '@end'. */
10094 if (token
->type
== CPP_CLOSE_BRACE
10095 || token
->type
== CPP_EOF
10096 || token
->type
== CPP_PRAGMA_EOL
10097 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10100 /* If we are in a compound statement and find 'else' then
10101 something went wrong. */
10102 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10104 if (parser
->in_statement
& IN_IF_STMT
)
10108 token
= cp_lexer_consume_token (parser
->lexer
);
10109 error_at (token
->location
, "%<else%> without a previous %<if%>");
10113 /* Parse the statement. */
10114 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10118 /* Parse a selection-statement.
10120 selection-statement:
10121 if ( condition ) statement
10122 if ( condition ) statement else statement
10123 switch ( condition ) statement
10125 Returns the new IF_STMT or SWITCH_STMT.
10127 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10128 is a (possibly labeled) if statement which is not enclosed in
10129 braces and has an else clause. This is used to implement
10133 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
10141 /* Peek at the next token. */
10142 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10144 /* See what kind of keyword it is. */
10145 keyword
= token
->keyword
;
10154 /* Look for the `('. */
10155 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10157 cp_parser_skip_to_end_of_statement (parser
);
10158 return error_mark_node
;
10161 /* Begin the selection-statement. */
10162 if (keyword
== RID_IF
)
10163 statement
= begin_if_stmt ();
10165 statement
= begin_switch_stmt ();
10167 /* Parse the condition. */
10168 condition
= cp_parser_condition (parser
);
10169 /* Look for the `)'. */
10170 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10171 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10172 /*consume_paren=*/true);
10174 if (keyword
== RID_IF
)
10177 unsigned char in_statement
;
10179 /* Add the condition. */
10180 finish_if_stmt_cond (condition
, statement
);
10182 /* Parse the then-clause. */
10183 in_statement
= parser
->in_statement
;
10184 parser
->in_statement
|= IN_IF_STMT
;
10185 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10187 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10188 add_stmt (build_empty_stmt (loc
));
10189 cp_lexer_consume_token (parser
->lexer
);
10190 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
10191 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
10192 "empty body in an %<if%> statement");
10196 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
10197 parser
->in_statement
= in_statement
;
10199 finish_then_clause (statement
);
10201 /* If the next token is `else', parse the else-clause. */
10202 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10205 /* Consume the `else' keyword. */
10206 cp_lexer_consume_token (parser
->lexer
);
10207 begin_else_clause (statement
);
10208 /* Parse the else-clause. */
10209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10212 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10214 OPT_Wempty_body
, "suggest braces around "
10215 "empty body in an %<else%> statement");
10216 add_stmt (build_empty_stmt (loc
));
10217 cp_lexer_consume_token (parser
->lexer
);
10220 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10222 finish_else_clause (statement
);
10224 /* If we are currently parsing a then-clause, then
10225 IF_P will not be NULL. We set it to true to
10226 indicate that this if statement has an else clause.
10227 This may trigger the Wparentheses warning below
10228 when we get back up to the parent if statement. */
10234 /* This if statement does not have an else clause. If
10235 NESTED_IF is true, then the then-clause is an if
10236 statement which does have an else clause. We warn
10237 about the potential ambiguity. */
10239 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
10240 "suggest explicit braces to avoid ambiguous"
10244 /* Now we're all done with the if-statement. */
10245 finish_if_stmt (statement
);
10249 bool in_switch_statement_p
;
10250 unsigned char in_statement
;
10252 /* Add the condition. */
10253 finish_switch_cond (condition
, statement
);
10255 /* Parse the body of the switch-statement. */
10256 in_switch_statement_p
= parser
->in_switch_statement_p
;
10257 in_statement
= parser
->in_statement
;
10258 parser
->in_switch_statement_p
= true;
10259 parser
->in_statement
|= IN_SWITCH_STMT
;
10260 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10261 parser
->in_switch_statement_p
= in_switch_statement_p
;
10262 parser
->in_statement
= in_statement
;
10264 /* Now we're all done with the switch-statement. */
10265 finish_switch_stmt (statement
);
10273 cp_parser_error (parser
, "expected selection-statement");
10274 return error_mark_node
;
10278 /* Parse a condition.
10282 type-specifier-seq declarator = initializer-clause
10283 type-specifier-seq declarator braced-init-list
10288 type-specifier-seq declarator asm-specification [opt]
10289 attributes [opt] = assignment-expression
10291 Returns the expression that should be tested. */
10294 cp_parser_condition (cp_parser
* parser
)
10296 cp_decl_specifier_seq type_specifiers
;
10297 const char *saved_message
;
10298 int declares_class_or_enum
;
10300 /* Try the declaration first. */
10301 cp_parser_parse_tentatively (parser
);
10302 /* New types are not allowed in the type-specifier-seq for a
10304 saved_message
= parser
->type_definition_forbidden_message
;
10305 parser
->type_definition_forbidden_message
10306 = G_("types may not be defined in conditions");
10307 /* Parse the type-specifier-seq. */
10308 cp_parser_decl_specifier_seq (parser
,
10309 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
10311 &declares_class_or_enum
);
10312 /* Restore the saved message. */
10313 parser
->type_definition_forbidden_message
= saved_message
;
10314 /* If all is well, we might be looking at a declaration. */
10315 if (!cp_parser_error_occurred (parser
))
10318 tree asm_specification
;
10320 cp_declarator
*declarator
;
10321 tree initializer
= NULL_TREE
;
10323 /* Parse the declarator. */
10324 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10325 /*ctor_dtor_or_conv_p=*/NULL
,
10326 /*parenthesized_p=*/NULL
,
10327 /*member_p=*/false,
10328 /*friend_p=*/false);
10329 /* Parse the attributes. */
10330 attributes
= cp_parser_attributes_opt (parser
);
10331 /* Parse the asm-specification. */
10332 asm_specification
= cp_parser_asm_specification_opt (parser
);
10333 /* If the next token is not an `=' or '{', then we might still be
10334 looking at an expression. For example:
10338 looks like a decl-specifier-seq and a declarator -- but then
10339 there is no `=', so this is an expression. */
10340 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10341 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10342 cp_parser_simulate_error (parser
);
10344 /* If we did see an `=' or '{', then we are looking at a declaration
10346 if (cp_parser_parse_definitely (parser
))
10349 bool non_constant_p
;
10350 bool flags
= LOOKUP_ONLYCONVERTING
;
10352 /* Create the declaration. */
10353 decl
= start_decl (declarator
, &type_specifiers
,
10354 /*initialized_p=*/true,
10355 attributes
, /*prefix_attributes=*/NULL_TREE
,
10358 /* Parse the initializer. */
10359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10361 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10362 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10367 /* Consume the `='. */
10368 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10369 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10371 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10372 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10374 /* Process the initializer. */
10375 cp_finish_decl (decl
,
10376 initializer
, !non_constant_p
,
10381 pop_scope (pushed_scope
);
10383 return convert_from_reference (decl
);
10386 /* If we didn't even get past the declarator successfully, we are
10387 definitely not looking at a declaration. */
10389 cp_parser_abort_tentative_parse (parser
);
10391 /* Otherwise, we are looking at an expression. */
10392 return cp_parser_expression (parser
);
10395 /* Parses a for-statement or range-for-statement until the closing ')',
10399 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10401 tree init
, scope
, decl
;
10404 /* Begin the for-statement. */
10405 scope
= begin_for_scope (&init
);
10407 /* Parse the initialization. */
10408 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10411 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10413 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10417 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10419 /* Normal for loop */
10420 tree condition
= NULL_TREE
;
10421 tree expression
= NULL_TREE
;
10424 stmt
= begin_for_stmt (scope
, init
);
10425 /* The for-init-statement has already been parsed in
10426 cp_parser_for_init_statement, so no work is needed here. */
10427 finish_for_init_stmt (stmt
);
10429 /* If there's a condition, process it. */
10430 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10431 condition
= cp_parser_condition (parser
);
10434 cp_parser_error (parser
, "missing loop condition in loop with "
10435 "%<GCC ivdep%> pragma");
10436 condition
= error_mark_node
;
10438 finish_for_cond (condition
, stmt
, ivdep
);
10439 /* Look for the `;'. */
10440 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10442 /* If there's an expression, process it. */
10443 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10444 expression
= cp_parser_expression (parser
);
10445 finish_for_expr (expression
, stmt
);
10450 /* Tries to parse a range-based for-statement:
10453 decl-specifier-seq declarator : expression
10455 The decl-specifier-seq declarator and the `:' are already parsed by
10456 cp_parser_for_init_statement. If processing_template_decl it returns a
10457 newly created RANGE_FOR_STMT; if not, it is converted to a
10458 regular FOR_STMT. */
10461 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10464 tree stmt
, range_expr
;
10466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10468 bool expr_non_constant_p
;
10469 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10472 range_expr
= cp_parser_expression (parser
);
10474 /* If in template, STMT is converted to a normal for-statement
10475 at instantiation. If not, it is done just ahead. */
10476 if (processing_template_decl
)
10478 if (check_for_bare_parameter_packs (range_expr
))
10479 range_expr
= error_mark_node
;
10480 stmt
= begin_range_for_stmt (scope
, init
);
10482 RANGE_FOR_IVDEP (stmt
) = 1;
10483 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10484 if (!type_dependent_expression_p (range_expr
)
10485 /* do_auto_deduction doesn't mess with template init-lists. */
10486 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10487 do_range_for_auto_deduction (range_decl
, range_expr
);
10491 stmt
= begin_for_stmt (scope
, init
);
10492 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10497 /* Subroutine of cp_convert_range_for: given the initializer expression,
10498 builds up the range temporary. */
10501 build_range_temp (tree range_expr
)
10503 tree range_type
, range_temp
;
10505 /* Find out the type deduced by the declaration
10506 `auto &&__range = range_expr'. */
10507 range_type
= cp_build_reference_type (make_auto (), true);
10508 range_type
= do_auto_deduction (range_type
, range_expr
,
10509 type_uses_auto (range_type
));
10511 /* Create the __range variable. */
10512 range_temp
= build_decl (input_location
, VAR_DECL
,
10513 get_identifier ("__for_range"), range_type
);
10514 TREE_USED (range_temp
) = 1;
10515 DECL_ARTIFICIAL (range_temp
) = 1;
10520 /* Used by cp_parser_range_for in template context: we aren't going to
10521 do a full conversion yet, but we still need to resolve auto in the
10522 type of the for-range-declaration if present. This is basically
10523 a shortcut version of cp_convert_range_for. */
10526 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10528 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10531 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10532 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10533 iter_type
= (cp_parser_perform_range_for_lookup
10534 (range_temp
, &begin_dummy
, &end_dummy
));
10537 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10539 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10540 tf_warning_or_error
);
10541 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10542 iter_decl
, auto_node
);
10547 /* Converts a range-based for-statement into a normal
10548 for-statement, as per the definition.
10550 for (RANGE_DECL : RANGE_EXPR)
10553 should be equivalent to:
10556 auto &&__range = RANGE_EXPR;
10557 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10561 RANGE_DECL = *__begin;
10566 If RANGE_EXPR is an array:
10567 BEGIN_EXPR = __range
10568 END_EXPR = __range + ARRAY_SIZE(__range)
10569 Else if RANGE_EXPR has a member 'begin' or 'end':
10570 BEGIN_EXPR = __range.begin()
10571 END_EXPR = __range.end()
10573 BEGIN_EXPR = begin(__range)
10574 END_EXPR = end(__range);
10576 If __range has a member 'begin' but not 'end', or vice versa, we must
10577 still use the second alternative (it will surely fail, however).
10578 When calling begin()/end() in the third alternative we must use
10579 argument dependent lookup, but always considering 'std' as an associated
10583 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10587 tree iter_type
, begin_expr
, end_expr
;
10588 tree condition
, expression
;
10590 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10591 /* If an error happened previously do nothing or else a lot of
10592 unhelpful errors would be issued. */
10593 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10598 if (TREE_CODE (range_expr
) == VAR_DECL
10599 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10600 /* Can't bind a reference to an array of runtime bound. */
10601 range_temp
= range_expr
;
10604 range_temp
= build_range_temp (range_expr
);
10605 pushdecl (range_temp
);
10606 cp_finish_decl (range_temp
, range_expr
,
10607 /*is_constant_init*/false, NULL_TREE
,
10608 LOOKUP_ONLYCONVERTING
);
10609 range_temp
= convert_from_reference (range_temp
);
10611 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10612 &begin_expr
, &end_expr
);
10615 /* The new for initialization statement. */
10616 begin
= build_decl (input_location
, VAR_DECL
,
10617 get_identifier ("__for_begin"), iter_type
);
10618 TREE_USED (begin
) = 1;
10619 DECL_ARTIFICIAL (begin
) = 1;
10621 cp_finish_decl (begin
, begin_expr
,
10622 /*is_constant_init*/false, NULL_TREE
,
10623 LOOKUP_ONLYCONVERTING
);
10625 end
= build_decl (input_location
, VAR_DECL
,
10626 get_identifier ("__for_end"), iter_type
);
10627 TREE_USED (end
) = 1;
10628 DECL_ARTIFICIAL (end
) = 1;
10630 cp_finish_decl (end
, end_expr
,
10631 /*is_constant_init*/false, NULL_TREE
,
10632 LOOKUP_ONLYCONVERTING
);
10634 finish_for_init_stmt (statement
);
10636 /* The new for condition. */
10637 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10640 NULL
, tf_warning_or_error
);
10641 finish_for_cond (condition
, statement
, ivdep
);
10643 /* The new increment expression. */
10644 expression
= finish_unary_op_expr (input_location
,
10645 PREINCREMENT_EXPR
, begin
,
10646 tf_warning_or_error
);
10647 finish_for_expr (expression
, statement
);
10649 /* The declaration is initialized with *__begin inside the loop body. */
10650 cp_finish_decl (range_decl
,
10651 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10652 tf_warning_or_error
),
10653 /*is_constant_init*/false, NULL_TREE
,
10654 LOOKUP_ONLYCONVERTING
);
10659 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10660 We need to solve both at the same time because the method used
10661 depends on the existence of members begin or end.
10662 Returns the type deduced for the iterator expression. */
10665 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10667 if (error_operand_p (range
))
10669 *begin
= *end
= error_mark_node
;
10670 return error_mark_node
;
10673 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10675 error ("range-based %<for%> expression of type %qT "
10676 "has incomplete type", TREE_TYPE (range
));
10677 *begin
= *end
= error_mark_node
;
10678 return error_mark_node
;
10680 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10682 /* If RANGE is an array, we will use pointer arithmetic. */
10684 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10686 array_type_nelts_top (TREE_TYPE (range
)),
10688 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10692 /* If it is not an array, we must do a bit of magic. */
10693 tree id_begin
, id_end
;
10694 tree member_begin
, member_end
;
10696 *begin
= *end
= error_mark_node
;
10698 id_begin
= get_identifier ("begin");
10699 id_end
= get_identifier ("end");
10700 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10701 /*protect=*/2, /*want_type=*/false,
10702 tf_warning_or_error
);
10703 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10704 /*protect=*/2, /*want_type=*/false,
10705 tf_warning_or_error
);
10707 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10709 /* Use the member functions. */
10710 if (member_begin
!= NULL_TREE
)
10711 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10713 error ("range-based %<for%> expression of type %qT has an "
10714 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10716 if (member_end
!= NULL_TREE
)
10717 *end
= cp_parser_range_for_member_function (range
, id_end
);
10719 error ("range-based %<for%> expression of type %qT has a "
10720 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10724 /* Use global functions with ADL. */
10725 vec
<tree
, va_gc
> *vec
;
10726 vec
= make_tree_vector ();
10728 vec_safe_push (vec
, range
);
10730 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10731 tf_warning_or_error
);
10732 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10733 tf_warning_or_error
);
10734 member_end
= perform_koenig_lookup (id_end
, vec
,
10735 tf_warning_or_error
);
10736 *end
= finish_call_expr (member_end
, &vec
, false, true,
10737 tf_warning_or_error
);
10739 release_tree_vector (vec
);
10742 /* Last common checks. */
10743 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10745 /* If one of the expressions is an error do no more checks. */
10746 *begin
= *end
= error_mark_node
;
10747 return error_mark_node
;
10749 else if (type_dependent_expression_p (*begin
)
10750 || type_dependent_expression_p (*end
))
10751 /* Can happen, when, eg, in a template context, Koenig lookup
10752 can't resolve begin/end (c++/58503). */
10756 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10757 /* The unqualified type of the __begin and __end temporaries should
10758 be the same, as required by the multiple auto declaration. */
10759 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10760 error ("inconsistent begin/end types in range-based %<for%> "
10761 "statement: %qT and %qT",
10762 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10768 /* Helper function for cp_parser_perform_range_for_lookup.
10769 Builds a tree for RANGE.IDENTIFIER(). */
10772 cp_parser_range_for_member_function (tree range
, tree identifier
)
10775 vec
<tree
, va_gc
> *vec
;
10777 member
= finish_class_member_access_expr (range
, identifier
,
10778 false, tf_warning_or_error
);
10779 if (member
== error_mark_node
)
10780 return error_mark_node
;
10782 vec
= make_tree_vector ();
10783 res
= finish_call_expr (member
, &vec
,
10784 /*disallow_virtual=*/false,
10785 /*koenig_p=*/false,
10786 tf_warning_or_error
);
10787 release_tree_vector (vec
);
10791 /* Parse an iteration-statement.
10793 iteration-statement:
10794 while ( condition ) statement
10795 do statement while ( expression ) ;
10796 for ( for-init-statement condition [opt] ; expression [opt] )
10799 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10802 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10807 unsigned char in_statement
;
10809 /* Peek at the next token. */
10810 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10812 return error_mark_node
;
10814 /* Remember whether or not we are already within an iteration
10816 in_statement
= parser
->in_statement
;
10818 /* See what kind of keyword it is. */
10819 keyword
= token
->keyword
;
10826 /* Begin the while-statement. */
10827 statement
= begin_while_stmt ();
10828 /* Look for the `('. */
10829 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10830 /* Parse the condition. */
10831 condition
= cp_parser_condition (parser
);
10832 finish_while_stmt_cond (condition
, statement
, ivdep
);
10833 /* Look for the `)'. */
10834 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10835 /* Parse the dependent statement. */
10836 parser
->in_statement
= IN_ITERATION_STMT
;
10837 cp_parser_already_scoped_statement (parser
);
10838 parser
->in_statement
= in_statement
;
10839 /* We're done with the while-statement. */
10840 finish_while_stmt (statement
);
10848 /* Begin the do-statement. */
10849 statement
= begin_do_stmt ();
10850 /* Parse the body of the do-statement. */
10851 parser
->in_statement
= IN_ITERATION_STMT
;
10852 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10853 parser
->in_statement
= in_statement
;
10854 finish_do_body (statement
);
10855 /* Look for the `while' keyword. */
10856 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10857 /* Look for the `('. */
10858 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10859 /* Parse the expression. */
10860 expression
= cp_parser_expression (parser
);
10861 /* We're done with the do-statement. */
10862 finish_do_stmt (expression
, statement
, ivdep
);
10863 /* Look for the `)'. */
10864 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10865 /* Look for the `;'. */
10866 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10872 /* Look for the `('. */
10873 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10875 statement
= cp_parser_for (parser
, ivdep
);
10877 /* Look for the `)'. */
10878 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10880 /* Parse the body of the for-statement. */
10881 parser
->in_statement
= IN_ITERATION_STMT
;
10882 cp_parser_already_scoped_statement (parser
);
10883 parser
->in_statement
= in_statement
;
10885 /* We're done with the for-statement. */
10886 finish_for_stmt (statement
);
10891 cp_parser_error (parser
, "expected iteration-statement");
10892 statement
= error_mark_node
;
10899 /* Parse a for-init-statement or the declarator of a range-based-for.
10900 Returns true if a range-based-for declaration is seen.
10902 for-init-statement:
10903 expression-statement
10904 simple-declaration */
10907 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10909 /* If the next token is a `;', then we have an empty
10910 expression-statement. Grammatically, this is also a
10911 simple-declaration, but an invalid one, because it does not
10912 declare anything. Therefore, if we did not handle this case
10913 specially, we would issue an error message about an invalid
10915 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10917 bool is_range_for
= false;
10918 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10920 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
10921 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
10923 /* N3994 -- for (id : init) ... */
10924 if (cxx_dialect
< cxx1z
)
10925 pedwarn (input_location
, 0, "range-based for loop without a "
10926 "type-specifier only available with "
10927 "-std=c++1z or -std=gnu++1z");
10928 tree name
= cp_parser_identifier (parser
);
10929 tree type
= cp_build_reference_type (make_auto (), /*rval*/true);
10930 *decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
10932 cp_lexer_consume_token (parser
->lexer
);
10936 /* A colon is used in range-based for. */
10937 parser
->colon_corrects_to_scope_p
= false;
10939 /* We're going to speculatively look for a declaration, falling back
10940 to an expression, if necessary. */
10941 cp_parser_parse_tentatively (parser
);
10942 /* Parse the declaration. */
10943 cp_parser_simple_declaration (parser
,
10944 /*function_definition_allowed_p=*/false,
10946 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10949 /* It is a range-for, consume the ':' */
10950 cp_lexer_consume_token (parser
->lexer
);
10951 is_range_for
= true;
10952 if (cxx_dialect
< cxx11
)
10954 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
10955 "range-based %<for%> loops only available with "
10956 "-std=c++11 or -std=gnu++11");
10957 *decl
= error_mark_node
;
10961 /* The ';' is not consumed yet because we told
10962 cp_parser_simple_declaration not to. */
10963 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10965 if (cp_parser_parse_definitely (parser
))
10966 return is_range_for
;
10967 /* If the tentative parse failed, then we shall need to look for an
10968 expression-statement. */
10970 /* If we are here, it is an expression-statement. */
10971 cp_parser_expression_statement (parser
, NULL_TREE
);
10975 /* Parse a jump-statement.
10980 return expression [opt] ;
10981 return braced-init-list ;
10987 goto * expression ;
10989 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10992 cp_parser_jump_statement (cp_parser
* parser
)
10994 tree statement
= error_mark_node
;
10997 unsigned char in_statement
;
10999 /* Peek at the next token. */
11000 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
11002 return error_mark_node
;
11004 /* See what kind of keyword it is. */
11005 keyword
= token
->keyword
;
11009 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
11010 switch (in_statement
)
11013 error_at (token
->location
, "break statement not within loop or switch");
11016 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
11017 || in_statement
== IN_ITERATION_STMT
);
11018 statement
= finish_break_stmt ();
11019 if (in_statement
== IN_ITERATION_STMT
)
11020 break_maybe_infinite_loop ();
11023 error_at (token
->location
, "invalid exit from OpenMP structured block");
11026 error_at (token
->location
, "break statement used with OpenMP for loop");
11028 case IN_CILK_SIMD_FOR
:
11029 error_at (token
->location
, "break statement used with Cilk Plus for loop");
11032 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11036 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
11039 error_at (token
->location
, "continue statement not within a loop");
11041 case IN_CILK_SIMD_FOR
:
11042 error_at (token
->location
,
11043 "continue statement within %<#pragma simd%> loop body");
11044 /* Fall through. */
11045 case IN_ITERATION_STMT
:
11047 statement
= finish_continue_stmt ();
11050 error_at (token
->location
, "invalid exit from OpenMP structured block");
11053 gcc_unreachable ();
11055 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11061 bool expr_non_constant_p
;
11063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11065 cp_lexer_set_source_position (parser
->lexer
);
11066 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11067 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11069 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11070 expr
= cp_parser_expression (parser
);
11072 /* If the next token is a `;', then there is no
11075 /* Build the return-statement. */
11076 statement
= finish_return_stmt (expr
);
11077 /* Look for the final `;'. */
11078 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11083 if (parser
->in_function_body
11084 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11086 error ("%<goto%> in %<constexpr%> function");
11087 cp_function_chain
->invalid_constexpr
= true;
11090 /* Create the goto-statement. */
11091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11093 /* Issue a warning about this use of a GNU extension. */
11094 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11095 /* Consume the '*' token. */
11096 cp_lexer_consume_token (parser
->lexer
);
11097 /* Parse the dependent expression. */
11098 finish_goto_stmt (cp_parser_expression (parser
));
11101 finish_goto_stmt (cp_parser_identifier (parser
));
11102 /* Look for the final `;'. */
11103 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11107 cp_parser_error (parser
, "expected jump-statement");
11114 /* Parse a declaration-statement.
11116 declaration-statement:
11117 block-declaration */
11120 cp_parser_declaration_statement (cp_parser
* parser
)
11124 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11125 p
= obstack_alloc (&declarator_obstack
, 0);
11127 /* Parse the block-declaration. */
11128 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11130 /* Free any declarators allocated. */
11131 obstack_free (&declarator_obstack
, p
);
11134 /* Some dependent statements (like `if (cond) statement'), are
11135 implicitly in their own scope. In other words, if the statement is
11136 a single statement (as opposed to a compound-statement), it is
11137 none-the-less treated as if it were enclosed in braces. Any
11138 declarations appearing in the dependent statement are out of scope
11139 after control passes that point. This function parses a statement,
11140 but ensures that is in its own scope, even if it is not a
11141 compound-statement.
11143 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11144 is a (possibly labeled) if statement which is not enclosed in
11145 braces and has an else clause. This is used to implement
11148 Returns the new statement. */
11151 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
11158 /* Mark if () ; with a special NOP_EXPR. */
11159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11161 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11162 cp_lexer_consume_token (parser
->lexer
);
11163 statement
= add_stmt (build_empty_stmt (loc
));
11165 /* if a compound is opened, we simply parse the statement directly. */
11166 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11167 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
11168 /* If the token is not a `{', then we must take special action. */
11171 /* Create a compound-statement. */
11172 statement
= begin_compound_stmt (0);
11173 /* Parse the dependent-statement. */
11174 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
11175 /* Finish the dummy compound-statement. */
11176 finish_compound_stmt (statement
);
11179 /* Return the statement. */
11183 /* For some dependent statements (like `while (cond) statement'), we
11184 have already created a scope. Therefore, even if the dependent
11185 statement is a compound-statement, we do not want to create another
11189 cp_parser_already_scoped_statement (cp_parser
* parser
)
11191 /* If the token is a `{', then we must take special action. */
11192 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11193 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
11196 /* Avoid calling cp_parser_compound_statement, so that we
11197 don't create a new scope. Do everything else by hand. */
11198 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11199 /* If the next keyword is `__label__' we have a label declaration. */
11200 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11201 cp_parser_label_declaration (parser
);
11202 /* Parse an (optional) statement-seq. */
11203 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11204 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11208 /* Declarations [gram.dcl.dcl] */
11210 /* Parse an optional declaration-sequence.
11214 declaration-seq declaration */
11217 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11223 token
= cp_lexer_peek_token (parser
->lexer
);
11225 if (token
->type
== CPP_CLOSE_BRACE
11226 || token
->type
== CPP_EOF
11227 || token
->type
== CPP_PRAGMA_EOL
)
11230 if (token
->type
== CPP_SEMICOLON
)
11232 /* A declaration consisting of a single semicolon is
11233 invalid. Allow it unless we're being pedantic. */
11234 cp_lexer_consume_token (parser
->lexer
);
11235 if (!in_system_header_at (input_location
))
11236 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11240 /* If we're entering or exiting a region that's implicitly
11241 extern "C", modify the lang context appropriately. */
11242 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11244 push_lang_context (lang_name_c
);
11245 parser
->implicit_extern_c
= true;
11247 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
11249 pop_lang_context ();
11250 parser
->implicit_extern_c
= false;
11253 if (token
->type
== CPP_PRAGMA
)
11255 /* A top-level declaration can consist solely of a #pragma.
11256 A nested declaration cannot, so this is done here and not
11257 in cp_parser_declaration. (A #pragma at block scope is
11258 handled in cp_parser_statement.) */
11259 cp_parser_pragma (parser
, pragma_external
);
11263 /* Parse the declaration itself. */
11264 cp_parser_declaration (parser
);
11268 /* Parse a declaration.
11272 function-definition
11273 template-declaration
11274 explicit-instantiation
11275 explicit-specialization
11276 linkage-specification
11277 namespace-definition
11282 __extension__ declaration */
11285 cp_parser_declaration (cp_parser
* parser
)
11289 int saved_pedantic
;
11291 tree attributes
= NULL_TREE
;
11293 /* Check for the `__extension__' keyword. */
11294 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11296 /* Parse the qualified declaration. */
11297 cp_parser_declaration (parser
);
11298 /* Restore the PEDANTIC flag. */
11299 pedantic
= saved_pedantic
;
11304 /* Try to figure out what kind of declaration is present. */
11305 token1
= *cp_lexer_peek_token (parser
->lexer
);
11307 if (token1
.type
!= CPP_EOF
)
11308 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
11311 token2
.type
= CPP_EOF
;
11312 token2
.keyword
= RID_MAX
;
11315 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11316 p
= obstack_alloc (&declarator_obstack
, 0);
11318 /* If the next token is `extern' and the following token is a string
11319 literal, then we have a linkage specification. */
11320 if (token1
.keyword
== RID_EXTERN
11321 && cp_parser_is_pure_string_literal (&token2
))
11322 cp_parser_linkage_specification (parser
);
11323 /* If the next token is `template', then we have either a template
11324 declaration, an explicit instantiation, or an explicit
11326 else if (token1
.keyword
== RID_TEMPLATE
)
11328 /* `template <>' indicates a template specialization. */
11329 if (token2
.type
== CPP_LESS
11330 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
11331 cp_parser_explicit_specialization (parser
);
11332 /* `template <' indicates a template declaration. */
11333 else if (token2
.type
== CPP_LESS
)
11334 cp_parser_template_declaration (parser
, /*member_p=*/false);
11335 /* Anything else must be an explicit instantiation. */
11337 cp_parser_explicit_instantiation (parser
);
11339 /* If the next token is `export', then we have a template
11341 else if (token1
.keyword
== RID_EXPORT
)
11342 cp_parser_template_declaration (parser
, /*member_p=*/false);
11343 /* If the next token is `extern', 'static' or 'inline' and the one
11344 after that is `template', we have a GNU extended explicit
11345 instantiation directive. */
11346 else if (cp_parser_allow_gnu_extensions_p (parser
)
11347 && (token1
.keyword
== RID_EXTERN
11348 || token1
.keyword
== RID_STATIC
11349 || token1
.keyword
== RID_INLINE
)
11350 && token2
.keyword
== RID_TEMPLATE
)
11351 cp_parser_explicit_instantiation (parser
);
11352 /* If the next token is `namespace', check for a named or unnamed
11353 namespace definition. */
11354 else if (token1
.keyword
== RID_NAMESPACE
11355 && (/* A named namespace definition. */
11356 (token2
.type
== CPP_NAME
11357 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11359 /* An unnamed namespace definition. */
11360 || token2
.type
== CPP_OPEN_BRACE
11361 || token2
.keyword
== RID_ATTRIBUTE
))
11362 cp_parser_namespace_definition (parser
);
11363 /* An inline (associated) namespace definition. */
11364 else if (token1
.keyword
== RID_INLINE
11365 && token2
.keyword
== RID_NAMESPACE
)
11366 cp_parser_namespace_definition (parser
);
11367 /* Objective-C++ declaration/definition. */
11368 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11369 cp_parser_objc_declaration (parser
, NULL_TREE
);
11370 else if (c_dialect_objc ()
11371 && token1
.keyword
== RID_ATTRIBUTE
11372 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11373 cp_parser_objc_declaration (parser
, attributes
);
11374 /* We must have either a block declaration or a function
11377 /* Try to parse a block-declaration, or a function-definition. */
11378 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11380 /* Free any declarators allocated. */
11381 obstack_free (&declarator_obstack
, p
);
11384 /* Parse a block-declaration.
11389 namespace-alias-definition
11396 __extension__ block-declaration
11401 static_assert-declaration
11403 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11404 part of a declaration-statement. */
11407 cp_parser_block_declaration (cp_parser
*parser
,
11411 int saved_pedantic
;
11413 /* Check for the `__extension__' keyword. */
11414 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11416 /* Parse the qualified declaration. */
11417 cp_parser_block_declaration (parser
, statement_p
);
11418 /* Restore the PEDANTIC flag. */
11419 pedantic
= saved_pedantic
;
11424 /* Peek at the next token to figure out which kind of declaration is
11426 token1
= cp_lexer_peek_token (parser
->lexer
);
11428 /* If the next keyword is `asm', we have an asm-definition. */
11429 if (token1
->keyword
== RID_ASM
)
11432 cp_parser_commit_to_tentative_parse (parser
);
11433 cp_parser_asm_definition (parser
);
11435 /* If the next keyword is `namespace', we have a
11436 namespace-alias-definition. */
11437 else if (token1
->keyword
== RID_NAMESPACE
)
11438 cp_parser_namespace_alias_definition (parser
);
11439 /* If the next keyword is `using', we have a
11440 using-declaration, a using-directive, or an alias-declaration. */
11441 else if (token1
->keyword
== RID_USING
)
11446 cp_parser_commit_to_tentative_parse (parser
);
11447 /* If the token after `using' is `namespace', then we have a
11448 using-directive. */
11449 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11450 if (token2
->keyword
== RID_NAMESPACE
)
11451 cp_parser_using_directive (parser
);
11452 /* If the second token after 'using' is '=', then we have an
11453 alias-declaration. */
11454 else if (cxx_dialect
>= cxx11
11455 && token2
->type
== CPP_NAME
11456 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11457 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11458 cp_parser_alias_declaration (parser
);
11459 /* Otherwise, it's a using-declaration. */
11461 cp_parser_using_declaration (parser
,
11462 /*access_declaration_p=*/false);
11464 /* If the next keyword is `__label__' we have a misplaced label
11466 else if (token1
->keyword
== RID_LABEL
)
11468 cp_lexer_consume_token (parser
->lexer
);
11469 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11470 cp_parser_skip_to_end_of_statement (parser
);
11471 /* If the next token is now a `;', consume it. */
11472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11473 cp_lexer_consume_token (parser
->lexer
);
11475 /* If the next token is `static_assert' we have a static assertion. */
11476 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11477 cp_parser_static_assert (parser
, /*member_p=*/false);
11478 /* Anything else must be a simple-declaration. */
11480 cp_parser_simple_declaration (parser
, !statement_p
,
11481 /*maybe_range_for_decl*/NULL
);
11484 /* Parse a simple-declaration.
11486 simple-declaration:
11487 decl-specifier-seq [opt] init-declarator-list [opt] ;
11489 init-declarator-list:
11491 init-declarator-list , init-declarator
11493 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11494 function-definition as a simple-declaration.
11496 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11497 parsed declaration if it is an uninitialized single declarator not followed
11498 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11499 if present, will not be consumed. */
11502 cp_parser_simple_declaration (cp_parser
* parser
,
11503 bool function_definition_allowed_p
,
11504 tree
*maybe_range_for_decl
)
11506 cp_decl_specifier_seq decl_specifiers
;
11507 int declares_class_or_enum
;
11508 bool saw_declarator
;
11509 location_t comma_loc
= UNKNOWN_LOCATION
;
11510 location_t init_loc
= UNKNOWN_LOCATION
;
11512 if (maybe_range_for_decl
)
11513 *maybe_range_for_decl
= NULL_TREE
;
11515 /* Defer access checks until we know what is being declared; the
11516 checks for names appearing in the decl-specifier-seq should be
11517 done as if we were in the scope of the thing being declared. */
11518 push_deferring_access_checks (dk_deferred
);
11520 /* Parse the decl-specifier-seq. We have to keep track of whether
11521 or not the decl-specifier-seq declares a named class or
11522 enumeration type, since that is the only case in which the
11523 init-declarator-list is allowed to be empty.
11527 In a simple-declaration, the optional init-declarator-list can be
11528 omitted only when declaring a class or enumeration, that is when
11529 the decl-specifier-seq contains either a class-specifier, an
11530 elaborated-type-specifier, or an enum-specifier. */
11531 cp_parser_decl_specifier_seq (parser
,
11532 CP_PARSER_FLAGS_OPTIONAL
,
11534 &declares_class_or_enum
);
11535 /* We no longer need to defer access checks. */
11536 stop_deferring_access_checks ();
11538 /* In a block scope, a valid declaration must always have a
11539 decl-specifier-seq. By not trying to parse declarators, we can
11540 resolve the declaration/expression ambiguity more quickly. */
11541 if (!function_definition_allowed_p
11542 && !decl_specifiers
.any_specifiers_p
)
11544 cp_parser_error (parser
, "expected declaration");
11548 /* If the next two tokens are both identifiers, the code is
11549 erroneous. The usual cause of this situation is code like:
11553 where "T" should name a type -- but does not. */
11554 if (!decl_specifiers
.any_type_specifiers_p
11555 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11557 /* If parsing tentatively, we should commit; we really are
11558 looking at a declaration. */
11559 cp_parser_commit_to_tentative_parse (parser
);
11564 /* If we have seen at least one decl-specifier, and the next token
11565 is not a parenthesis, then we must be looking at a declaration.
11566 (After "int (" we might be looking at a functional cast.) */
11567 if (decl_specifiers
.any_specifiers_p
11568 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11569 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11570 && !cp_parser_error_occurred (parser
))
11571 cp_parser_commit_to_tentative_parse (parser
);
11573 /* Keep going until we hit the `;' at the end of the simple
11575 saw_declarator
= false;
11576 while (cp_lexer_next_token_is_not (parser
->lexer
,
11580 bool function_definition_p
;
11583 if (saw_declarator
)
11585 /* If we are processing next declarator, comma is expected */
11586 token
= cp_lexer_peek_token (parser
->lexer
);
11587 gcc_assert (token
->type
== CPP_COMMA
);
11588 cp_lexer_consume_token (parser
->lexer
);
11589 if (maybe_range_for_decl
)
11591 *maybe_range_for_decl
= error_mark_node
;
11592 if (comma_loc
== UNKNOWN_LOCATION
)
11593 comma_loc
= token
->location
;
11597 saw_declarator
= true;
11599 /* Parse the init-declarator. */
11600 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11602 function_definition_allowed_p
,
11603 /*member_p=*/false,
11604 declares_class_or_enum
,
11605 &function_definition_p
,
11606 maybe_range_for_decl
,
11608 /* If an error occurred while parsing tentatively, exit quickly.
11609 (That usually happens when in the body of a function; each
11610 statement is treated as a declaration-statement until proven
11612 if (cp_parser_error_occurred (parser
))
11614 /* Handle function definitions specially. */
11615 if (function_definition_p
)
11617 /* If the next token is a `,', then we are probably
11618 processing something like:
11622 which is erroneous. */
11623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11625 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11626 error_at (token
->location
,
11628 " declarations and function-definitions is forbidden");
11630 /* Otherwise, we're done with the list of declarators. */
11633 pop_deferring_access_checks ();
11637 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11638 *maybe_range_for_decl
= decl
;
11639 /* The next token should be either a `,' or a `;'. */
11640 token
= cp_lexer_peek_token (parser
->lexer
);
11641 /* If it's a `,', there are more declarators to come. */
11642 if (token
->type
== CPP_COMMA
)
11643 /* will be consumed next time around */;
11644 /* If it's a `;', we are done. */
11645 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11647 /* Anything else is an error. */
11650 /* If we have already issued an error message we don't need
11651 to issue another one. */
11652 if (decl
!= error_mark_node
11653 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11654 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11655 /* Skip tokens until we reach the end of the statement. */
11656 cp_parser_skip_to_end_of_statement (parser
);
11657 /* If the next token is now a `;', consume it. */
11658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11659 cp_lexer_consume_token (parser
->lexer
);
11662 /* After the first time around, a function-definition is not
11663 allowed -- even if it was OK at first. For example:
11668 function_definition_allowed_p
= false;
11671 /* Issue an error message if no declarators are present, and the
11672 decl-specifier-seq does not itself declare a class or
11673 enumeration: [dcl.dcl]/3. */
11674 if (!saw_declarator
)
11676 if (cp_parser_declares_only_class_p (parser
))
11678 if (!declares_class_or_enum
11679 && decl_specifiers
.type
11680 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11681 /* Ensure an error is issued anyway when finish_decltype_type,
11682 called via cp_parser_decl_specifier_seq, returns a class or
11683 an enumeration (c++/51786). */
11684 decl_specifiers
.type
= NULL_TREE
;
11685 shadow_tag (&decl_specifiers
);
11687 /* Perform any deferred access checks. */
11688 perform_deferred_access_checks (tf_warning_or_error
);
11691 /* Consume the `;'. */
11692 if (!maybe_range_for_decl
)
11693 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11694 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11696 if (init_loc
!= UNKNOWN_LOCATION
)
11697 error_at (init_loc
, "initializer in range-based %<for%> loop");
11698 if (comma_loc
!= UNKNOWN_LOCATION
)
11699 error_at (comma_loc
,
11700 "multiple declarations in range-based %<for%> loop");
11704 pop_deferring_access_checks ();
11707 /* Parse a decl-specifier-seq.
11709 decl-specifier-seq:
11710 decl-specifier-seq [opt] decl-specifier
11711 decl-specifier attribute-specifier-seq [opt] (C++11)
11714 storage-class-specifier
11725 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11727 The parser flags FLAGS is used to control type-specifier parsing.
11729 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11732 1: one of the decl-specifiers is an elaborated-type-specifier
11733 (i.e., a type declaration)
11734 2: one of the decl-specifiers is an enum-specifier or a
11735 class-specifier (i.e., a type definition)
11740 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11741 cp_parser_flags flags
,
11742 cp_decl_specifier_seq
*decl_specs
,
11743 int* declares_class_or_enum
)
11745 bool constructor_possible_p
= !parser
->in_declarator_p
;
11746 bool found_decl_spec
= false;
11747 cp_token
*start_token
= NULL
;
11750 /* Clear DECL_SPECS. */
11751 clear_decl_specs (decl_specs
);
11753 /* Assume no class or enumeration type is declared. */
11754 *declares_class_or_enum
= 0;
11756 /* Keep reading specifiers until there are no more to read. */
11759 bool constructor_p
;
11763 /* Peek at the next token. */
11764 token
= cp_lexer_peek_token (parser
->lexer
);
11766 /* Save the first token of the decl spec list for error
11769 start_token
= token
;
11770 /* Handle attributes. */
11771 if (cp_next_tokens_can_be_attribute_p (parser
))
11773 /* Parse the attributes. */
11774 tree attrs
= cp_parser_attributes_opt (parser
);
11776 /* In a sequence of declaration specifiers, c++11 attributes
11777 appertain to the type that precede them. In that case
11780 The attribute-specifier-seq affects the type only for
11781 the declaration it appears in, not other declarations
11782 involving the same type.
11784 But for now let's force the user to position the
11785 attribute either at the beginning of the declaration or
11786 after the declarator-id, which would clearly mean that it
11787 applies to the declarator. */
11788 if (cxx11_attribute_p (attrs
))
11790 if (!found_decl_spec
)
11791 /* The c++11 attribute is at the beginning of the
11792 declaration. It appertains to the entity being
11796 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11798 /* This is an attribute following a
11799 class-specifier. */
11800 if (decl_specs
->type_definition_p
)
11801 warn_misplaced_attr_for_class_type (token
->location
,
11807 decl_specs
->std_attributes
11808 = chainon (decl_specs
->std_attributes
,
11810 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11811 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11817 decl_specs
->attributes
11818 = chainon (decl_specs
->attributes
,
11820 if (decl_specs
->locations
[ds_attribute
] == 0)
11821 decl_specs
->locations
[ds_attribute
] = token
->location
;
11824 /* Assume we will find a decl-specifier keyword. */
11825 found_decl_spec
= true;
11826 /* If the next token is an appropriate keyword, we can simply
11827 add it to the list. */
11828 switch (token
->keyword
)
11834 if (!at_class_scope_p ())
11836 error_at (token
->location
, "%<friend%> used outside of class");
11837 cp_lexer_purge_token (parser
->lexer
);
11842 /* Consume the token. */
11843 cp_lexer_consume_token (parser
->lexer
);
11847 case RID_CONSTEXPR
:
11849 cp_lexer_consume_token (parser
->lexer
);
11852 /* function-specifier:
11859 cp_parser_function_specifier_opt (parser
, decl_specs
);
11866 /* Consume the token. */
11867 cp_lexer_consume_token (parser
->lexer
);
11868 /* A constructor declarator cannot appear in a typedef. */
11869 constructor_possible_p
= false;
11870 /* The "typedef" keyword can only occur in a declaration; we
11871 may as well commit at this point. */
11872 cp_parser_commit_to_tentative_parse (parser
);
11874 if (decl_specs
->storage_class
!= sc_none
)
11875 decl_specs
->conflicting_specifiers_p
= true;
11878 /* storage-class-specifier:
11888 if (cxx_dialect
== cxx98
)
11890 /* Consume the token. */
11891 cp_lexer_consume_token (parser
->lexer
);
11893 /* Complain about `auto' as a storage specifier, if
11894 we're complaining about C++0x compatibility. */
11895 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11896 " changes meaning in C++11; please remove it");
11898 /* Set the storage class anyway. */
11899 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11903 /* C++0x auto type-specifier. */
11904 found_decl_spec
= false;
11911 /* Consume the token. */
11912 cp_lexer_consume_token (parser
->lexer
);
11913 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11917 /* Consume the token. */
11919 cp_lexer_consume_token (parser
->lexer
);
11923 /* We did not yet find a decl-specifier yet. */
11924 found_decl_spec
= false;
11928 if (found_decl_spec
11929 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11930 && token
->keyword
!= RID_CONSTEXPR
)
11931 error ("decl-specifier invalid in condition");
11934 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11936 /* Constructors are a special case. The `S' in `S()' is not a
11937 decl-specifier; it is the beginning of the declarator. */
11939 = (!found_decl_spec
11940 && constructor_possible_p
11941 && (cp_parser_constructor_declarator_p
11942 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11944 /* If we don't have a DECL_SPEC yet, then we must be looking at
11945 a type-specifier. */
11946 if (!found_decl_spec
&& !constructor_p
)
11948 int decl_spec_declares_class_or_enum
;
11949 bool is_cv_qualifier
;
11953 = cp_parser_type_specifier (parser
, flags
,
11955 /*is_declaration=*/true,
11956 &decl_spec_declares_class_or_enum
,
11958 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11960 /* If this type-specifier referenced a user-defined type
11961 (a typedef, class-name, etc.), then we can't allow any
11962 more such type-specifiers henceforth.
11966 The longest sequence of decl-specifiers that could
11967 possibly be a type name is taken as the
11968 decl-specifier-seq of a declaration. The sequence shall
11969 be self-consistent as described below.
11973 As a general rule, at most one type-specifier is allowed
11974 in the complete decl-specifier-seq of a declaration. The
11975 only exceptions are the following:
11977 -- const or volatile can be combined with any other
11980 -- signed or unsigned can be combined with char, long,
11988 void g (const int Pc);
11990 Here, Pc is *not* part of the decl-specifier seq; it's
11991 the declarator. Therefore, once we see a type-specifier
11992 (other than a cv-qualifier), we forbid any additional
11993 user-defined types. We *do* still allow things like `int
11994 int' to be considered a decl-specifier-seq, and issue the
11995 error message later. */
11996 if (type_spec
&& !is_cv_qualifier
)
11997 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11998 /* A constructor declarator cannot follow a type-specifier. */
12001 constructor_possible_p
= false;
12002 found_decl_spec
= true;
12003 if (!is_cv_qualifier
)
12004 decl_specs
->any_type_specifiers_p
= true;
12008 /* If we still do not have a DECL_SPEC, then there are no more
12009 decl-specifiers. */
12010 if (!found_decl_spec
)
12013 decl_specs
->any_specifiers_p
= true;
12014 /* After we see one decl-specifier, further decl-specifiers are
12015 always optional. */
12016 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
12019 /* Don't allow a friend specifier with a class definition. */
12020 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
12021 && (*declares_class_or_enum
& 2))
12022 error_at (decl_specs
->locations
[ds_friend
],
12023 "class definition may not be declared a friend");
12026 /* Parse an (optional) storage-class-specifier.
12028 storage-class-specifier:
12037 storage-class-specifier:
12040 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12043 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
12045 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
12048 if (cxx_dialect
!= cxx98
)
12050 /* Fall through for C++98. */
12057 /* Consume the token. */
12058 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12065 /* Parse an (optional) function-specifier.
12067 function-specifier:
12072 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12073 Updates DECL_SPECS, if it is non-NULL. */
12076 cp_parser_function_specifier_opt (cp_parser
* parser
,
12077 cp_decl_specifier_seq
*decl_specs
)
12079 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12080 switch (token
->keyword
)
12083 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12087 /* 14.5.2.3 [temp.mem]
12089 A member function template shall not be virtual. */
12090 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12091 error_at (token
->location
, "templates may not be %<virtual%>");
12093 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12097 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12104 /* Consume the token. */
12105 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12108 /* Parse a linkage-specification.
12110 linkage-specification:
12111 extern string-literal { declaration-seq [opt] }
12112 extern string-literal declaration */
12115 cp_parser_linkage_specification (cp_parser
* parser
)
12119 /* Look for the `extern' keyword. */
12120 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12122 /* Look for the string-literal. */
12123 linkage
= cp_parser_string_literal (parser
, false, false);
12125 /* Transform the literal into an identifier. If the literal is a
12126 wide-character string, or contains embedded NULs, then we can't
12127 handle it as the user wants. */
12128 if (strlen (TREE_STRING_POINTER (linkage
))
12129 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12131 cp_parser_error (parser
, "invalid linkage-specification");
12132 /* Assume C++ linkage. */
12133 linkage
= lang_name_cplusplus
;
12136 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12138 /* We're now using the new linkage. */
12139 push_lang_context (linkage
);
12141 /* If the next token is a `{', then we're using the first
12143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12145 cp_ensure_no_omp_declare_simd (parser
);
12147 /* Consume the `{' token. */
12148 cp_lexer_consume_token (parser
->lexer
);
12149 /* Parse the declarations. */
12150 cp_parser_declaration_seq_opt (parser
);
12151 /* Look for the closing `}'. */
12152 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12154 /* Otherwise, there's just one declaration. */
12157 bool saved_in_unbraced_linkage_specification_p
;
12159 saved_in_unbraced_linkage_specification_p
12160 = parser
->in_unbraced_linkage_specification_p
;
12161 parser
->in_unbraced_linkage_specification_p
= true;
12162 cp_parser_declaration (parser
);
12163 parser
->in_unbraced_linkage_specification_p
12164 = saved_in_unbraced_linkage_specification_p
;
12167 /* We're done with the linkage-specification. */
12168 pop_lang_context ();
12171 /* Parse a static_assert-declaration.
12173 static_assert-declaration:
12174 static_assert ( constant-expression , string-literal ) ;
12176 If MEMBER_P, this static_assert is a class member. */
12179 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12184 location_t saved_loc
;
12187 /* Peek at the `static_assert' token so we can keep track of exactly
12188 where the static assertion started. */
12189 token
= cp_lexer_peek_token (parser
->lexer
);
12190 saved_loc
= token
->location
;
12192 /* Look for the `static_assert' keyword. */
12193 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12197 /* We know we are in a static assertion; commit to any tentative
12199 if (cp_parser_parsing_tentatively (parser
))
12200 cp_parser_commit_to_tentative_parse (parser
);
12202 /* Parse the `(' starting the static assertion condition. */
12203 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
12205 /* Parse the constant-expression. Allow a non-constant expression
12206 here in order to give better diagnostics in finish_static_assert. */
12208 cp_parser_constant_expression (parser
,
12209 /*allow_non_constant_p=*/true,
12210 /*non_constant_p=*/&dummy
);
12212 /* Parse the separating `,'. */
12213 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
12215 /* Parse the string-literal message. */
12216 message
= cp_parser_string_literal (parser
,
12217 /*translate=*/false,
12220 /* A `)' completes the static assertion. */
12221 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12222 cp_parser_skip_to_closing_parenthesis (parser
,
12223 /*recovering=*/true,
12224 /*or_comma=*/false,
12225 /*consume_paren=*/true);
12227 /* A semicolon terminates the declaration. */
12228 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12230 /* Complete the static assertion, which may mean either processing
12231 the static assert now or saving it for template instantiation. */
12232 finish_static_assert (condition
, message
, saved_loc
, member_p
);
12235 /* Parse the expression in decltype ( expression ). */
12238 cp_parser_decltype_expr (cp_parser
*parser
,
12239 bool &id_expression_or_member_access_p
)
12241 cp_token
*id_expr_start_token
;
12244 /* First, try parsing an id-expression. */
12245 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
12246 cp_parser_parse_tentatively (parser
);
12247 expr
= cp_parser_id_expression (parser
,
12248 /*template_keyword_p=*/false,
12249 /*check_dependency_p=*/true,
12250 /*template_p=*/NULL
,
12251 /*declarator_p=*/false,
12252 /*optional_p=*/false);
12254 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
12256 bool non_integral_constant_expression_p
= false;
12257 tree id_expression
= expr
;
12259 const char *error_msg
;
12261 if (identifier_p (expr
))
12262 /* Lookup the name we got back from the id-expression. */
12263 expr
= cp_parser_lookup_name_simple (parser
, expr
,
12264 id_expr_start_token
->location
);
12267 && expr
!= error_mark_node
12268 && TREE_CODE (expr
) != TYPE_DECL
12269 && (TREE_CODE (expr
) != BIT_NOT_EXPR
12270 || !TYPE_P (TREE_OPERAND (expr
, 0)))
12271 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12273 /* Complete lookup of the id-expression. */
12274 expr
= (finish_id_expression
12275 (id_expression
, expr
, parser
->scope
, &idk
,
12276 /*integral_constant_expression_p=*/false,
12277 /*allow_non_integral_constant_expression_p=*/true,
12278 &non_integral_constant_expression_p
,
12279 /*template_p=*/false,
12281 /*address_p=*/false,
12282 /*template_arg_p=*/false,
12284 id_expr_start_token
->location
));
12286 if (expr
== error_mark_node
)
12287 /* We found an id-expression, but it was something that we
12288 should not have found. This is an error, not something
12289 we can recover from, so note that we found an
12290 id-expression and we'll recover as gracefully as
12292 id_expression_or_member_access_p
= true;
12296 && expr
!= error_mark_node
12297 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12298 /* We have an id-expression. */
12299 id_expression_or_member_access_p
= true;
12302 if (!id_expression_or_member_access_p
)
12304 /* Abort the id-expression parse. */
12305 cp_parser_abort_tentative_parse (parser
);
12307 /* Parsing tentatively, again. */
12308 cp_parser_parse_tentatively (parser
);
12310 /* Parse a class member access. */
12311 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
12312 /*cast_p=*/false, /*decltype*/true,
12313 /*member_access_only_p=*/true, NULL
);
12316 && expr
!= error_mark_node
12317 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12318 /* We have an id-expression. */
12319 id_expression_or_member_access_p
= true;
12322 if (id_expression_or_member_access_p
)
12323 /* We have parsed the complete id-expression or member access. */
12324 cp_parser_parse_definitely (parser
);
12327 /* Abort our attempt to parse an id-expression or member access
12329 cp_parser_abort_tentative_parse (parser
);
12331 /* Parse a full expression. */
12332 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
12333 /*decltype_p=*/true);
12339 /* Parse a `decltype' type. Returns the type.
12341 simple-type-specifier:
12342 decltype ( expression )
12344 decltype ( auto ) */
12347 cp_parser_decltype (cp_parser
*parser
)
12350 bool id_expression_or_member_access_p
= false;
12351 const char *saved_message
;
12352 bool saved_integral_constant_expression_p
;
12353 bool saved_non_integral_constant_expression_p
;
12354 bool saved_greater_than_is_operator_p
;
12355 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12357 if (start_token
->type
== CPP_DECLTYPE
)
12359 /* Already parsed. */
12360 cp_lexer_consume_token (parser
->lexer
);
12361 return start_token
->u
.value
;
12364 /* Look for the `decltype' token. */
12365 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
12366 return error_mark_node
;
12368 /* Parse the opening `('. */
12369 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12370 return error_mark_node
;
12372 /* decltype (auto) */
12373 if (cxx_dialect
>= cxx14
12374 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12376 cp_lexer_consume_token (parser
->lexer
);
12377 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12378 return error_mark_node
;
12379 expr
= make_decltype_auto ();
12380 AUTO_IS_DECLTYPE (expr
) = true;
12384 /* Types cannot be defined in a `decltype' expression. Save away the
12386 saved_message
= parser
->type_definition_forbidden_message
;
12388 /* And create the new one. */
12389 parser
->type_definition_forbidden_message
12390 = G_("types may not be defined in %<decltype%> expressions");
12392 /* The restrictions on constant-expressions do not apply inside
12393 decltype expressions. */
12394 saved_integral_constant_expression_p
12395 = parser
->integral_constant_expression_p
;
12396 saved_non_integral_constant_expression_p
12397 = parser
->non_integral_constant_expression_p
;
12398 parser
->integral_constant_expression_p
= false;
12400 /* Within a parenthesized expression, a `>' token is always
12401 the greater-than operator. */
12402 saved_greater_than_is_operator_p
12403 = parser
->greater_than_is_operator_p
;
12404 parser
->greater_than_is_operator_p
= true;
12406 /* Do not actually evaluate the expression. */
12407 ++cp_unevaluated_operand
;
12409 /* Do not warn about problems with the expression. */
12410 ++c_inhibit_evaluation_warnings
;
12412 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12414 /* Go back to evaluating expressions. */
12415 --cp_unevaluated_operand
;
12416 --c_inhibit_evaluation_warnings
;
12418 /* The `>' token might be the end of a template-id or
12419 template-parameter-list now. */
12420 parser
->greater_than_is_operator_p
12421 = saved_greater_than_is_operator_p
;
12423 /* Restore the old message and the integral constant expression
12425 parser
->type_definition_forbidden_message
= saved_message
;
12426 parser
->integral_constant_expression_p
12427 = saved_integral_constant_expression_p
;
12428 parser
->non_integral_constant_expression_p
12429 = saved_non_integral_constant_expression_p
;
12431 /* Parse to the closing `)'. */
12432 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12434 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12435 /*consume_paren=*/true);
12436 return error_mark_node
;
12439 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12440 tf_warning_or_error
);
12443 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12445 start_token
->type
= CPP_DECLTYPE
;
12446 start_token
->u
.value
= expr
;
12447 start_token
->keyword
= RID_MAX
;
12448 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12453 /* Special member functions [gram.special] */
12455 /* Parse a conversion-function-id.
12457 conversion-function-id:
12458 operator conversion-type-id
12460 Returns an IDENTIFIER_NODE representing the operator. */
12463 cp_parser_conversion_function_id (cp_parser
* parser
)
12467 tree saved_qualifying_scope
;
12468 tree saved_object_scope
;
12469 tree pushed_scope
= NULL_TREE
;
12471 /* Look for the `operator' token. */
12472 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12473 return error_mark_node
;
12474 /* When we parse the conversion-type-id, the current scope will be
12475 reset. However, we need that information in able to look up the
12476 conversion function later, so we save it here. */
12477 saved_scope
= parser
->scope
;
12478 saved_qualifying_scope
= parser
->qualifying_scope
;
12479 saved_object_scope
= parser
->object_scope
;
12480 /* We must enter the scope of the class so that the names of
12481 entities declared within the class are available in the
12482 conversion-type-id. For example, consider:
12489 S::operator I() { ... }
12491 In order to see that `I' is a type-name in the definition, we
12492 must be in the scope of `S'. */
12494 pushed_scope
= push_scope (saved_scope
);
12495 /* Parse the conversion-type-id. */
12496 type
= cp_parser_conversion_type_id (parser
);
12497 /* Leave the scope of the class, if any. */
12499 pop_scope (pushed_scope
);
12500 /* Restore the saved scope. */
12501 parser
->scope
= saved_scope
;
12502 parser
->qualifying_scope
= saved_qualifying_scope
;
12503 parser
->object_scope
= saved_object_scope
;
12504 /* If the TYPE is invalid, indicate failure. */
12505 if (type
== error_mark_node
)
12506 return error_mark_node
;
12507 return mangle_conv_op_name_for_type (type
);
12510 /* Parse a conversion-type-id:
12512 conversion-type-id:
12513 type-specifier-seq conversion-declarator [opt]
12515 Returns the TYPE specified. */
12518 cp_parser_conversion_type_id (cp_parser
* parser
)
12521 cp_decl_specifier_seq type_specifiers
;
12522 cp_declarator
*declarator
;
12523 tree type_specified
;
12524 const char *saved_message
;
12526 /* Parse the attributes. */
12527 attributes
= cp_parser_attributes_opt (parser
);
12529 saved_message
= parser
->type_definition_forbidden_message
;
12530 parser
->type_definition_forbidden_message
12531 = G_("types may not be defined in a conversion-type-id");
12533 /* Parse the type-specifiers. */
12534 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12535 /*is_trailing_return=*/false,
12538 parser
->type_definition_forbidden_message
= saved_message
;
12540 /* If that didn't work, stop. */
12541 if (type_specifiers
.type
== error_mark_node
)
12542 return error_mark_node
;
12543 /* Parse the conversion-declarator. */
12544 declarator
= cp_parser_conversion_declarator_opt (parser
);
12546 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12547 /*initialized=*/0, &attributes
);
12549 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12551 /* Don't give this error when parsing tentatively. This happens to
12552 work because we always parse this definitively once. */
12553 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12554 && type_uses_auto (type_specified
))
12556 if (cxx_dialect
< cxx14
)
12558 error ("invalid use of %<auto%> in conversion operator");
12559 return error_mark_node
;
12561 else if (template_parm_scope_p ())
12562 warning (0, "use of %<auto%> in member template "
12563 "conversion operator can never be deduced");
12566 return type_specified
;
12569 /* Parse an (optional) conversion-declarator.
12571 conversion-declarator:
12572 ptr-operator conversion-declarator [opt]
12576 static cp_declarator
*
12577 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12579 enum tree_code code
;
12580 tree class_type
, std_attributes
= NULL_TREE
;
12581 cp_cv_quals cv_quals
;
12583 /* We don't know if there's a ptr-operator next, or not. */
12584 cp_parser_parse_tentatively (parser
);
12585 /* Try the ptr-operator. */
12586 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12588 /* If it worked, look for more conversion-declarators. */
12589 if (cp_parser_parse_definitely (parser
))
12591 cp_declarator
*declarator
;
12593 /* Parse another optional declarator. */
12594 declarator
= cp_parser_conversion_declarator_opt (parser
);
12596 declarator
= cp_parser_make_indirect_declarator
12597 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12605 /* Parse an (optional) ctor-initializer.
12608 : mem-initializer-list
12610 Returns TRUE iff the ctor-initializer was actually present. */
12613 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12615 /* If the next token is not a `:', then there is no
12616 ctor-initializer. */
12617 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12619 /* Do default initialization of any bases and members. */
12620 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12621 finish_mem_initializers (NULL_TREE
);
12626 /* Consume the `:' token. */
12627 cp_lexer_consume_token (parser
->lexer
);
12628 /* And the mem-initializer-list. */
12629 cp_parser_mem_initializer_list (parser
);
12634 /* Parse a mem-initializer-list.
12636 mem-initializer-list:
12637 mem-initializer ... [opt]
12638 mem-initializer ... [opt] , mem-initializer-list */
12641 cp_parser_mem_initializer_list (cp_parser
* parser
)
12643 tree mem_initializer_list
= NULL_TREE
;
12644 tree target_ctor
= error_mark_node
;
12645 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12647 /* Let the semantic analysis code know that we are starting the
12648 mem-initializer-list. */
12649 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12650 error_at (token
->location
,
12651 "only constructors take member initializers");
12653 /* Loop through the list. */
12656 tree mem_initializer
;
12658 token
= cp_lexer_peek_token (parser
->lexer
);
12659 /* Parse the mem-initializer. */
12660 mem_initializer
= cp_parser_mem_initializer (parser
);
12661 /* If the next token is a `...', we're expanding member initializers. */
12662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12664 /* Consume the `...'. */
12665 cp_lexer_consume_token (parser
->lexer
);
12667 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12668 can be expanded but members cannot. */
12669 if (mem_initializer
!= error_mark_node
12670 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12672 error_at (token
->location
,
12673 "cannot expand initializer for member %<%D%>",
12674 TREE_PURPOSE (mem_initializer
));
12675 mem_initializer
= error_mark_node
;
12678 /* Construct the pack expansion type. */
12679 if (mem_initializer
!= error_mark_node
)
12680 mem_initializer
= make_pack_expansion (mem_initializer
);
12682 if (target_ctor
!= error_mark_node
12683 && mem_initializer
!= error_mark_node
)
12685 error ("mem-initializer for %qD follows constructor delegation",
12686 TREE_PURPOSE (mem_initializer
));
12687 mem_initializer
= error_mark_node
;
12689 /* Look for a target constructor. */
12690 if (mem_initializer
!= error_mark_node
12691 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12692 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12694 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12695 if (mem_initializer_list
)
12697 error ("constructor delegation follows mem-initializer for %qD",
12698 TREE_PURPOSE (mem_initializer_list
));
12699 mem_initializer
= error_mark_node
;
12701 target_ctor
= mem_initializer
;
12703 /* Add it to the list, unless it was erroneous. */
12704 if (mem_initializer
!= error_mark_node
)
12706 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12707 mem_initializer_list
= mem_initializer
;
12709 /* If the next token is not a `,', we're done. */
12710 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12712 /* Consume the `,' token. */
12713 cp_lexer_consume_token (parser
->lexer
);
12716 /* Perform semantic analysis. */
12717 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12718 finish_mem_initializers (mem_initializer_list
);
12721 /* Parse a mem-initializer.
12724 mem-initializer-id ( expression-list [opt] )
12725 mem-initializer-id braced-init-list
12730 ( expression-list [opt] )
12732 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12733 class) or FIELD_DECL (for a non-static data member) to initialize;
12734 the TREE_VALUE is the expression-list. An empty initialization
12735 list is represented by void_list_node. */
12738 cp_parser_mem_initializer (cp_parser
* parser
)
12740 tree mem_initializer_id
;
12741 tree expression_list
;
12743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12745 /* Find out what is being initialized. */
12746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12748 permerror (token
->location
,
12749 "anachronistic old-style base class initializer");
12750 mem_initializer_id
= NULL_TREE
;
12754 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12755 if (mem_initializer_id
== error_mark_node
)
12756 return mem_initializer_id
;
12758 member
= expand_member_init (mem_initializer_id
);
12759 if (member
&& !DECL_P (member
))
12760 in_base_initializer
= 1;
12762 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12764 bool expr_non_constant_p
;
12765 cp_lexer_set_source_position (parser
->lexer
);
12766 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12767 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12768 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12769 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12773 vec
<tree
, va_gc
> *vec
;
12774 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12776 /*allow_expansion_p=*/true,
12777 /*non_constant_p=*/NULL
);
12779 return error_mark_node
;
12780 expression_list
= build_tree_list_vec (vec
);
12781 release_tree_vector (vec
);
12784 if (expression_list
== error_mark_node
)
12785 return error_mark_node
;
12786 if (!expression_list
)
12787 expression_list
= void_type_node
;
12789 in_base_initializer
= 0;
12791 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12794 /* Parse a mem-initializer-id.
12796 mem-initializer-id:
12797 :: [opt] nested-name-specifier [opt] class-name
12800 Returns a TYPE indicating the class to be initializer for the first
12801 production. Returns an IDENTIFIER_NODE indicating the data member
12802 to be initialized for the second production. */
12805 cp_parser_mem_initializer_id (cp_parser
* parser
)
12807 bool global_scope_p
;
12808 bool nested_name_specifier_p
;
12809 bool template_p
= false;
12812 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12814 /* `typename' is not allowed in this context ([temp.res]). */
12815 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12817 error_at (token
->location
,
12818 "keyword %<typename%> not allowed in this context (a qualified "
12819 "member initializer is implicitly a type)");
12820 cp_lexer_consume_token (parser
->lexer
);
12822 /* Look for the optional `::' operator. */
12824 = (cp_parser_global_scope_opt (parser
,
12825 /*current_scope_valid_p=*/false)
12827 /* Look for the optional nested-name-specifier. The simplest way to
12832 The keyword `typename' is not permitted in a base-specifier or
12833 mem-initializer; in these contexts a qualified name that
12834 depends on a template-parameter is implicitly assumed to be a
12837 is to assume that we have seen the `typename' keyword at this
12839 nested_name_specifier_p
12840 = (cp_parser_nested_name_specifier_opt (parser
,
12841 /*typename_keyword_p=*/true,
12842 /*check_dependency_p=*/true,
12844 /*is_declaration=*/true)
12846 if (nested_name_specifier_p
)
12847 template_p
= cp_parser_optional_template_keyword (parser
);
12848 /* If there is a `::' operator or a nested-name-specifier, then we
12849 are definitely looking for a class-name. */
12850 if (global_scope_p
|| nested_name_specifier_p
)
12851 return cp_parser_class_name (parser
,
12852 /*typename_keyword_p=*/true,
12853 /*template_keyword_p=*/template_p
,
12855 /*check_dependency_p=*/true,
12856 /*class_head_p=*/false,
12857 /*is_declaration=*/true);
12858 /* Otherwise, we could also be looking for an ordinary identifier. */
12859 cp_parser_parse_tentatively (parser
);
12860 /* Try a class-name. */
12861 id
= cp_parser_class_name (parser
,
12862 /*typename_keyword_p=*/true,
12863 /*template_keyword_p=*/false,
12865 /*check_dependency_p=*/true,
12866 /*class_head_p=*/false,
12867 /*is_declaration=*/true);
12868 /* If we found one, we're done. */
12869 if (cp_parser_parse_definitely (parser
))
12871 /* Otherwise, look for an ordinary identifier. */
12872 return cp_parser_identifier (parser
);
12875 /* Overloading [gram.over] */
12877 /* Parse an operator-function-id.
12879 operator-function-id:
12882 Returns an IDENTIFIER_NODE for the operator which is a
12883 human-readable spelling of the identifier, e.g., `operator +'. */
12886 cp_parser_operator_function_id (cp_parser
* parser
)
12888 /* Look for the `operator' keyword. */
12889 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12890 return error_mark_node
;
12891 /* And then the name of the operator itself. */
12892 return cp_parser_operator (parser
);
12895 /* Return an identifier node for a user-defined literal operator.
12896 The suffix identifier is chained to the operator name identifier. */
12899 cp_literal_operator_id (const char* name
)
12902 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12903 + strlen (name
) + 10);
12904 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12905 identifier
= get_identifier (buffer
);
12910 /* Parse an operator.
12913 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12914 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12915 || ++ -- , ->* -> () []
12922 Returns an IDENTIFIER_NODE for the operator which is a
12923 human-readable spelling of the identifier, e.g., `operator +'. */
12926 cp_parser_operator (cp_parser
* parser
)
12928 tree id
= NULL_TREE
;
12932 /* Peek at the next token. */
12933 token
= cp_lexer_peek_token (parser
->lexer
);
12934 /* Figure out which operator we have. */
12935 switch (token
->type
)
12941 /* The keyword should be either `new' or `delete'. */
12942 if (token
->keyword
== RID_NEW
)
12944 else if (token
->keyword
== RID_DELETE
)
12949 /* Consume the `new' or `delete' token. */
12950 cp_lexer_consume_token (parser
->lexer
);
12952 /* Peek at the next token. */
12953 token
= cp_lexer_peek_token (parser
->lexer
);
12954 /* If it's a `[' token then this is the array variant of the
12956 if (token
->type
== CPP_OPEN_SQUARE
)
12958 /* Consume the `[' token. */
12959 cp_lexer_consume_token (parser
->lexer
);
12960 /* Look for the `]' token. */
12961 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12962 id
= ansi_opname (op
== NEW_EXPR
12963 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12965 /* Otherwise, we have the non-array variant. */
12967 id
= ansi_opname (op
);
12973 id
= ansi_opname (PLUS_EXPR
);
12977 id
= ansi_opname (MINUS_EXPR
);
12981 id
= ansi_opname (MULT_EXPR
);
12985 id
= ansi_opname (TRUNC_DIV_EXPR
);
12989 id
= ansi_opname (TRUNC_MOD_EXPR
);
12993 id
= ansi_opname (BIT_XOR_EXPR
);
12997 id
= ansi_opname (BIT_AND_EXPR
);
13001 id
= ansi_opname (BIT_IOR_EXPR
);
13005 id
= ansi_opname (BIT_NOT_EXPR
);
13009 id
= ansi_opname (TRUTH_NOT_EXPR
);
13013 id
= ansi_assopname (NOP_EXPR
);
13017 id
= ansi_opname (LT_EXPR
);
13021 id
= ansi_opname (GT_EXPR
);
13025 id
= ansi_assopname (PLUS_EXPR
);
13029 id
= ansi_assopname (MINUS_EXPR
);
13033 id
= ansi_assopname (MULT_EXPR
);
13037 id
= ansi_assopname (TRUNC_DIV_EXPR
);
13041 id
= ansi_assopname (TRUNC_MOD_EXPR
);
13045 id
= ansi_assopname (BIT_XOR_EXPR
);
13049 id
= ansi_assopname (BIT_AND_EXPR
);
13053 id
= ansi_assopname (BIT_IOR_EXPR
);
13057 id
= ansi_opname (LSHIFT_EXPR
);
13061 id
= ansi_opname (RSHIFT_EXPR
);
13064 case CPP_LSHIFT_EQ
:
13065 id
= ansi_assopname (LSHIFT_EXPR
);
13068 case CPP_RSHIFT_EQ
:
13069 id
= ansi_assopname (RSHIFT_EXPR
);
13073 id
= ansi_opname (EQ_EXPR
);
13077 id
= ansi_opname (NE_EXPR
);
13081 id
= ansi_opname (LE_EXPR
);
13084 case CPP_GREATER_EQ
:
13085 id
= ansi_opname (GE_EXPR
);
13089 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13093 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13096 case CPP_PLUS_PLUS
:
13097 id
= ansi_opname (POSTINCREMENT_EXPR
);
13100 case CPP_MINUS_MINUS
:
13101 id
= ansi_opname (PREDECREMENT_EXPR
);
13105 id
= ansi_opname (COMPOUND_EXPR
);
13108 case CPP_DEREF_STAR
:
13109 id
= ansi_opname (MEMBER_REF
);
13113 id
= ansi_opname (COMPONENT_REF
);
13116 case CPP_OPEN_PAREN
:
13117 /* Consume the `('. */
13118 cp_lexer_consume_token (parser
->lexer
);
13119 /* Look for the matching `)'. */
13120 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13121 return ansi_opname (CALL_EXPR
);
13123 case CPP_OPEN_SQUARE
:
13124 /* Consume the `['. */
13125 cp_lexer_consume_token (parser
->lexer
);
13126 /* Look for the matching `]'. */
13127 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13128 return ansi_opname (ARRAY_REF
);
13130 case CPP_UTF8STRING
:
13131 case CPP_UTF8STRING_USERDEF
:
13137 case CPP_STRING_USERDEF
:
13138 case CPP_WSTRING_USERDEF
:
13139 case CPP_STRING16_USERDEF
:
13140 case CPP_STRING32_USERDEF
:
13142 tree str
, string_tree
;
13145 if (cxx_dialect
== cxx98
)
13146 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13148 /* Consume the string. */
13149 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13150 /*wide_ok=*/true, /*lookup_udlit=*/false);
13151 if (str
== error_mark_node
)
13152 return error_mark_node
;
13153 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13155 string_tree
= USERDEF_LITERAL_VALUE (str
);
13156 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13161 /* Look for the suffix identifier. */
13162 token
= cp_lexer_peek_token (parser
->lexer
);
13163 if (token
->type
== CPP_NAME
)
13164 id
= cp_parser_identifier (parser
);
13165 else if (token
->type
== CPP_KEYWORD
)
13167 error ("unexpected keyword;"
13168 " remove space between quotes and suffix identifier");
13169 return error_mark_node
;
13173 error ("expected suffix identifier");
13174 return error_mark_node
;
13177 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13178 (TREE_TYPE (TREE_TYPE (string_tree
))));
13179 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
13182 error ("expected empty string after %<operator%> keyword");
13183 return error_mark_node
;
13185 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
13188 error ("invalid encoding prefix in literal operator");
13189 return error_mark_node
;
13191 if (id
!= error_mark_node
)
13193 const char *name
= IDENTIFIER_POINTER (id
);
13194 id
= cp_literal_operator_id (name
);
13200 /* Anything else is an error. */
13204 /* If we have selected an identifier, we need to consume the
13207 cp_lexer_consume_token (parser
->lexer
);
13208 /* Otherwise, no valid operator name was present. */
13211 cp_parser_error (parser
, "expected operator");
13212 id
= error_mark_node
;
13218 /* Parse a template-declaration.
13220 template-declaration:
13221 export [opt] template < template-parameter-list > declaration
13223 If MEMBER_P is TRUE, this template-declaration occurs within a
13226 The grammar rule given by the standard isn't correct. What
13227 is really meant is:
13229 template-declaration:
13230 export [opt] template-parameter-list-seq
13231 decl-specifier-seq [opt] init-declarator [opt] ;
13232 export [opt] template-parameter-list-seq
13233 function-definition
13235 template-parameter-list-seq:
13236 template-parameter-list-seq [opt]
13237 template < template-parameter-list > */
13240 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
13242 /* Check for `export'. */
13243 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
13245 /* Consume the `export' token. */
13246 cp_lexer_consume_token (parser
->lexer
);
13247 /* Warn that we do not support `export'. */
13248 warning (0, "keyword %<export%> not implemented, and will be ignored");
13251 cp_parser_template_declaration_after_export (parser
, member_p
);
13254 /* Parse a template-parameter-list.
13256 template-parameter-list:
13258 template-parameter-list , template-parameter
13260 Returns a TREE_LIST. Each node represents a template parameter.
13261 The nodes are connected via their TREE_CHAINs. */
13264 cp_parser_template_parameter_list (cp_parser
* parser
)
13266 tree parameter_list
= NULL_TREE
;
13268 begin_template_parm_list ();
13270 /* The loop below parses the template parms. We first need to know
13271 the total number of template parms to be able to compute proper
13272 canonical types of each dependent type. So after the loop, when
13273 we know the total number of template parms,
13274 end_template_parm_list computes the proper canonical types and
13275 fixes up the dependent types accordingly. */
13280 bool is_parameter_pack
;
13281 location_t parm_loc
;
13283 /* Parse the template-parameter. */
13284 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13285 parameter
= cp_parser_template_parameter (parser
,
13287 &is_parameter_pack
);
13288 /* Add it to the list. */
13289 if (parameter
!= error_mark_node
)
13290 parameter_list
= process_template_parm (parameter_list
,
13294 is_parameter_pack
);
13297 tree err_parm
= build_tree_list (parameter
, parameter
);
13298 parameter_list
= chainon (parameter_list
, err_parm
);
13301 /* If the next token is not a `,', we're done. */
13302 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13304 /* Otherwise, consume the `,' token. */
13305 cp_lexer_consume_token (parser
->lexer
);
13308 return end_template_parm_list (parameter_list
);
13311 /* Parse a template-parameter.
13313 template-parameter:
13315 parameter-declaration
13317 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13318 the parameter. The TREE_PURPOSE is the default value, if any.
13319 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13320 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13321 set to true iff this parameter is a parameter pack. */
13324 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
13325 bool *is_parameter_pack
)
13328 cp_parameter_declarator
*parameter_declarator
;
13329 cp_declarator
*id_declarator
;
13332 /* Assume it is a type parameter or a template parameter. */
13333 *is_non_type
= false;
13334 /* Assume it not a parameter pack. */
13335 *is_parameter_pack
= false;
13336 /* Peek at the next token. */
13337 token
= cp_lexer_peek_token (parser
->lexer
);
13338 /* If it is `class' or `template', we have a type-parameter. */
13339 if (token
->keyword
== RID_TEMPLATE
)
13340 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13341 /* If it is `class' or `typename' we do not know yet whether it is a
13342 type parameter or a non-type parameter. Consider:
13344 template <typename T, typename T::X X> ...
13348 template <class C, class D*> ...
13350 Here, the first parameter is a type parameter, and the second is
13351 a non-type parameter. We can tell by looking at the token after
13352 the identifier -- if it is a `,', `=', or `>' then we have a type
13354 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
13356 /* Peek at the token after `class' or `typename'. */
13357 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13358 /* If it's an ellipsis, we have a template type parameter
13360 if (token
->type
== CPP_ELLIPSIS
)
13361 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13362 /* If it's an identifier, skip it. */
13363 if (token
->type
== CPP_NAME
)
13364 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
13365 /* Now, see if the token looks like the end of a template
13367 if (token
->type
== CPP_COMMA
13368 || token
->type
== CPP_EQ
13369 || token
->type
== CPP_GREATER
)
13370 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13373 /* Otherwise, it is a non-type parameter.
13377 When parsing a default template-argument for a non-type
13378 template-parameter, the first non-nested `>' is taken as the end
13379 of the template parameter-list rather than a greater-than
13381 *is_non_type
= true;
13382 parameter_declarator
13383 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
13384 /*parenthesized_p=*/NULL
);
13386 if (!parameter_declarator
)
13387 return error_mark_node
;
13389 /* If the parameter declaration is marked as a parameter pack, set
13390 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13391 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13393 if (parameter_declarator
->declarator
13394 && parameter_declarator
->declarator
->parameter_pack_p
)
13396 *is_parameter_pack
= true;
13397 parameter_declarator
->declarator
->parameter_pack_p
= false;
13400 if (parameter_declarator
->default_argument
)
13402 /* Can happen in some cases of erroneous input (c++/34892). */
13403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13404 /* Consume the `...' for better error recovery. */
13405 cp_lexer_consume_token (parser
->lexer
);
13407 /* If the next token is an ellipsis, and we don't already have it
13408 marked as a parameter pack, then we have a parameter pack (that
13409 has no declarator). */
13410 else if (!*is_parameter_pack
13411 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13412 && (declarator_can_be_parameter_pack
13413 (parameter_declarator
->declarator
)))
13415 /* Consume the `...'. */
13416 cp_lexer_consume_token (parser
->lexer
);
13417 maybe_warn_variadic_templates ();
13419 *is_parameter_pack
= true;
13421 /* We might end up with a pack expansion as the type of the non-type
13422 template parameter, in which case this is a non-type template
13424 else if (parameter_declarator
->decl_specifiers
.type
13425 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
13427 *is_parameter_pack
= true;
13428 parameter_declarator
->decl_specifiers
.type
=
13429 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
13432 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13434 /* Parameter packs cannot have default arguments. However, a
13435 user may try to do so, so we'll parse them and give an
13436 appropriate diagnostic here. */
13438 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13440 /* Find the name of the parameter pack. */
13441 id_declarator
= parameter_declarator
->declarator
;
13442 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
13443 id_declarator
= id_declarator
->declarator
;
13445 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
13446 error_at (start_token
->location
,
13447 "template parameter pack %qD cannot have a default argument",
13448 id_declarator
->u
.id
.unqualified_name
);
13450 error_at (start_token
->location
,
13451 "template parameter pack cannot have a default argument");
13453 /* Parse the default argument, but throw away the result. */
13454 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
13457 parm
= grokdeclarator (parameter_declarator
->declarator
,
13458 ¶meter_declarator
->decl_specifiers
,
13459 TPARM
, /*initialized=*/0,
13460 /*attrlist=*/NULL
);
13461 if (parm
== error_mark_node
)
13462 return error_mark_node
;
13464 return build_tree_list (parameter_declarator
->default_argument
, parm
);
13467 /* Parse a type-parameter.
13470 class identifier [opt]
13471 class identifier [opt] = type-id
13472 typename identifier [opt]
13473 typename identifier [opt] = type-id
13474 template < template-parameter-list > class identifier [opt]
13475 template < template-parameter-list > class identifier [opt]
13478 GNU Extension (variadic templates):
13481 class ... identifier [opt]
13482 typename ... identifier [opt]
13484 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13485 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13486 the declaration of the parameter.
13488 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13491 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13496 /* Look for a keyword to tell us what kind of parameter this is. */
13497 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13499 return error_mark_node
;
13501 switch (token
->keyword
)
13507 tree default_argument
;
13509 /* If the next token is an ellipsis, we have a template
13511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13513 /* Consume the `...' token. */
13514 cp_lexer_consume_token (parser
->lexer
);
13515 maybe_warn_variadic_templates ();
13517 *is_parameter_pack
= true;
13520 /* If the next token is an identifier, then it names the
13522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13523 identifier
= cp_parser_identifier (parser
);
13525 identifier
= NULL_TREE
;
13527 /* Create the parameter. */
13528 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13530 /* If the next token is an `=', we have a default argument. */
13531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13533 /* Consume the `=' token. */
13534 cp_lexer_consume_token (parser
->lexer
);
13535 /* Parse the default-argument. */
13536 push_deferring_access_checks (dk_no_deferred
);
13537 default_argument
= cp_parser_type_id (parser
);
13539 /* Template parameter packs cannot have default
13541 if (*is_parameter_pack
)
13544 error_at (token
->location
,
13545 "template parameter pack %qD cannot have a "
13546 "default argument", identifier
);
13548 error_at (token
->location
,
13549 "template parameter packs cannot have "
13550 "default arguments");
13551 default_argument
= NULL_TREE
;
13553 else if (check_for_bare_parameter_packs (default_argument
))
13554 default_argument
= error_mark_node
;
13555 pop_deferring_access_checks ();
13558 default_argument
= NULL_TREE
;
13560 /* Create the combined representation of the parameter and the
13561 default argument. */
13562 parameter
= build_tree_list (default_argument
, parameter
);
13569 tree default_argument
;
13571 /* Look for the `<'. */
13572 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13573 /* Parse the template-parameter-list. */
13574 cp_parser_template_parameter_list (parser
);
13575 /* Look for the `>'. */
13576 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13577 /* Look for the `class' or 'typename' keywords. */
13578 cp_parser_type_parameter_key (parser
);
13579 /* If the next token is an ellipsis, we have a template
13581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13583 /* Consume the `...' token. */
13584 cp_lexer_consume_token (parser
->lexer
);
13585 maybe_warn_variadic_templates ();
13587 *is_parameter_pack
= true;
13589 /* If the next token is an `=', then there is a
13590 default-argument. If the next token is a `>', we are at
13591 the end of the parameter-list. If the next token is a `,',
13592 then we are at the end of this parameter. */
13593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13594 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13595 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13597 identifier
= cp_parser_identifier (parser
);
13598 /* Treat invalid names as if the parameter were nameless. */
13599 if (identifier
== error_mark_node
)
13600 identifier
= NULL_TREE
;
13603 identifier
= NULL_TREE
;
13605 /* Create the template parameter. */
13606 parameter
= finish_template_template_parm (class_type_node
,
13609 /* If the next token is an `=', then there is a
13610 default-argument. */
13611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13615 /* Consume the `='. */
13616 cp_lexer_consume_token (parser
->lexer
);
13617 /* Parse the id-expression. */
13618 push_deferring_access_checks (dk_no_deferred
);
13619 /* save token before parsing the id-expression, for error
13621 token
= cp_lexer_peek_token (parser
->lexer
);
13623 = cp_parser_id_expression (parser
,
13624 /*template_keyword_p=*/false,
13625 /*check_dependency_p=*/true,
13626 /*template_p=*/&is_template
,
13627 /*declarator_p=*/false,
13628 /*optional_p=*/false);
13629 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13630 /* If the id-expression was a template-id that refers to
13631 a template-class, we already have the declaration here,
13632 so no further lookup is needed. */
13635 /* Look up the name. */
13637 = cp_parser_lookup_name (parser
, default_argument
,
13639 /*is_template=*/is_template
,
13640 /*is_namespace=*/false,
13641 /*check_dependency=*/true,
13642 /*ambiguous_decls=*/NULL
,
13644 /* See if the default argument is valid. */
13646 = check_template_template_default_arg (default_argument
);
13648 /* Template parameter packs cannot have default
13650 if (*is_parameter_pack
)
13653 error_at (token
->location
,
13654 "template parameter pack %qD cannot "
13655 "have a default argument",
13658 error_at (token
->location
, "template parameter packs cannot "
13659 "have default arguments");
13660 default_argument
= NULL_TREE
;
13662 pop_deferring_access_checks ();
13665 default_argument
= NULL_TREE
;
13667 /* Create the combined representation of the parameter and the
13668 default argument. */
13669 parameter
= build_tree_list (default_argument
, parameter
);
13674 gcc_unreachable ();
13681 /* Parse a template-id.
13684 template-name < template-argument-list [opt] >
13686 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13687 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13688 returned. Otherwise, if the template-name names a function, or set
13689 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13690 names a class, returns a TYPE_DECL for the specialization.
13692 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13693 uninstantiated templates. */
13696 cp_parser_template_id (cp_parser
*parser
,
13697 bool template_keyword_p
,
13698 bool check_dependency_p
,
13699 enum tag_types tag_type
,
13700 bool is_declaration
)
13706 cp_token_position start_of_id
= 0;
13707 deferred_access_check
*chk
;
13708 vec
<deferred_access_check
, va_gc
> *access_check
;
13709 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13710 bool is_identifier
;
13712 /* If the next token corresponds to a template-id, there is no need
13714 next_token
= cp_lexer_peek_token (parser
->lexer
);
13715 if (next_token
->type
== CPP_TEMPLATE_ID
)
13717 struct tree_check
*check_value
;
13719 /* Get the stored value. */
13720 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13721 /* Perform any access checks that were deferred. */
13722 access_check
= check_value
->checks
;
13725 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13726 perform_or_defer_access_check (chk
->binfo
,
13729 tf_warning_or_error
);
13731 /* Return the stored value. */
13732 return check_value
->value
;
13735 /* Avoid performing name lookup if there is no possibility of
13736 finding a template-id. */
13737 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13738 || (next_token
->type
== CPP_NAME
13739 && !cp_parser_nth_token_starts_template_argument_list_p
13742 cp_parser_error (parser
, "expected template-id");
13743 return error_mark_node
;
13746 /* Remember where the template-id starts. */
13747 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13748 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13750 push_deferring_access_checks (dk_deferred
);
13752 /* Parse the template-name. */
13753 is_identifier
= false;
13754 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13755 check_dependency_p
,
13759 if (templ
== error_mark_node
|| is_identifier
)
13761 pop_deferring_access_checks ();
13765 /* If we find the sequence `[:' after a template-name, it's probably
13766 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13767 parse correctly the argument list. */
13768 next_token
= cp_lexer_peek_token (parser
->lexer
);
13769 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13770 if (next_token
->type
== CPP_OPEN_SQUARE
13771 && next_token
->flags
& DIGRAPH
13772 && next_token_2
->type
== CPP_COLON
13773 && !(next_token_2
->flags
& PREV_WHITE
))
13775 cp_parser_parse_tentatively (parser
);
13776 /* Change `:' into `::'. */
13777 next_token_2
->type
= CPP_SCOPE
;
13778 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13780 cp_lexer_consume_token (parser
->lexer
);
13782 /* Parse the arguments. */
13783 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13784 if (!cp_parser_parse_definitely (parser
))
13786 /* If we couldn't parse an argument list, then we revert our changes
13787 and return simply an error. Maybe this is not a template-id
13789 next_token_2
->type
= CPP_COLON
;
13790 cp_parser_error (parser
, "expected %<<%>");
13791 pop_deferring_access_checks ();
13792 return error_mark_node
;
13794 /* Otherwise, emit an error about the invalid digraph, but continue
13795 parsing because we got our argument list. */
13796 if (permerror (next_token
->location
,
13797 "%<<::%> cannot begin a template-argument list"))
13799 static bool hint
= false;
13800 inform (next_token
->location
,
13801 "%<<:%> is an alternate spelling for %<[%>."
13802 " Insert whitespace between %<<%> and %<::%>");
13803 if (!hint
&& !flag_permissive
)
13805 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13806 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13807 "accept your code)");
13814 /* Look for the `<' that starts the template-argument-list. */
13815 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13817 pop_deferring_access_checks ();
13818 return error_mark_node
;
13820 /* Parse the arguments. */
13821 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13824 /* Build a representation of the specialization. */
13825 if (identifier_p (templ
))
13826 template_id
= build_min_nt_loc (next_token
->location
,
13829 else if (DECL_TYPE_TEMPLATE_P (templ
)
13830 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13832 bool entering_scope
;
13833 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13834 template (rather than some instantiation thereof) only if
13835 is not nested within some other construct. For example, in
13836 "template <typename T> void f(T) { A<T>::", A<T> is just an
13837 instantiation of A. */
13838 entering_scope
= (template_parm_scope_p ()
13839 && cp_lexer_next_token_is (parser
->lexer
,
13842 = finish_template_type (templ
, arguments
, entering_scope
);
13844 else if (variable_template_p (templ
))
13846 template_id
= lookup_template_variable (templ
, arguments
);
13850 /* If it's not a class-template or a template-template, it should be
13851 a function-template. */
13852 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13853 || TREE_CODE (templ
) == OVERLOAD
13854 || BASELINK_P (templ
)));
13856 template_id
= lookup_template_function (templ
, arguments
);
13859 /* If parsing tentatively, replace the sequence of tokens that makes
13860 up the template-id with a CPP_TEMPLATE_ID token. That way,
13861 should we re-parse the token stream, we will not have to repeat
13862 the effort required to do the parse, nor will we issue duplicate
13863 error messages about problems during instantiation of the
13866 /* Don't do this if we had a parse error in a declarator; re-parsing
13867 might succeed if a name changes meaning (60361). */
13868 && !(cp_parser_error_occurred (parser
)
13869 && cp_parser_parsing_tentatively (parser
)
13870 && parser
->in_declarator_p
))
13872 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13874 /* Reset the contents of the START_OF_ID token. */
13875 token
->type
= CPP_TEMPLATE_ID
;
13876 /* Retrieve any deferred checks. Do not pop this access checks yet
13877 so the memory will not be reclaimed during token replacing below. */
13878 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13879 token
->u
.tree_check_value
->value
= template_id
;
13880 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13881 token
->keyword
= RID_MAX
;
13883 /* Purge all subsequent tokens. */
13884 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13886 /* ??? Can we actually assume that, if template_id ==
13887 error_mark_node, we will have issued a diagnostic to the
13888 user, as opposed to simply marking the tentative parse as
13890 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13891 error_at (token
->location
, "parse error in template argument list");
13894 pop_to_parent_deferring_access_checks ();
13895 return template_id
;
13898 /* Parse a template-name.
13903 The standard should actually say:
13907 operator-function-id
13909 A defect report has been filed about this issue.
13911 A conversion-function-id cannot be a template name because they cannot
13912 be part of a template-id. In fact, looking at this code:
13914 a.operator K<int>()
13916 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13917 It is impossible to call a templated conversion-function-id with an
13918 explicit argument list, since the only allowed template parameter is
13919 the type to which it is converting.
13921 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13922 `template' keyword, in a construction like:
13926 In that case `f' is taken to be a template-name, even though there
13927 is no way of knowing for sure.
13929 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13930 name refers to a set of overloaded functions, at least one of which
13931 is a template, or an IDENTIFIER_NODE with the name of the template,
13932 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13933 names are looked up inside uninstantiated templates. */
13936 cp_parser_template_name (cp_parser
* parser
,
13937 bool template_keyword_p
,
13938 bool check_dependency_p
,
13939 bool is_declaration
,
13940 enum tag_types tag_type
,
13941 bool *is_identifier
)
13946 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13948 /* If the next token is `operator', then we have either an
13949 operator-function-id or a conversion-function-id. */
13950 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13952 /* We don't know whether we're looking at an
13953 operator-function-id or a conversion-function-id. */
13954 cp_parser_parse_tentatively (parser
);
13955 /* Try an operator-function-id. */
13956 identifier
= cp_parser_operator_function_id (parser
);
13957 /* If that didn't work, try a conversion-function-id. */
13958 if (!cp_parser_parse_definitely (parser
))
13960 cp_parser_error (parser
, "expected template-name");
13961 return error_mark_node
;
13964 /* Look for the identifier. */
13966 identifier
= cp_parser_identifier (parser
);
13968 /* If we didn't find an identifier, we don't have a template-id. */
13969 if (identifier
== error_mark_node
)
13970 return error_mark_node
;
13972 /* If the name immediately followed the `template' keyword, then it
13973 is a template-name. However, if the next token is not `<', then
13974 we do not treat it as a template-name, since it is not being used
13975 as part of a template-id. This enables us to handle constructs
13978 template <typename T> struct S { S(); };
13979 template <typename T> S<T>::S();
13981 correctly. We would treat `S' as a template -- if it were `S<T>'
13982 -- but we do not if there is no `<'. */
13984 if (processing_template_decl
13985 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13987 /* In a declaration, in a dependent context, we pretend that the
13988 "template" keyword was present in order to improve error
13989 recovery. For example, given:
13991 template <typename T> void f(T::X<int>);
13993 we want to treat "X<int>" as a template-id. */
13995 && !template_keyword_p
13996 && parser
->scope
&& TYPE_P (parser
->scope
)
13997 && check_dependency_p
13998 && dependent_scope_p (parser
->scope
)
13999 /* Do not do this for dtors (or ctors), since they never
14000 need the template keyword before their name. */
14001 && !constructor_name_p (identifier
, parser
->scope
))
14003 cp_token_position start
= 0;
14005 /* Explain what went wrong. */
14006 error_at (token
->location
, "non-template %qD used as template",
14008 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
14009 parser
->scope
, identifier
);
14010 /* If parsing tentatively, find the location of the "<" token. */
14011 if (cp_parser_simulate_error (parser
))
14012 start
= cp_lexer_token_position (parser
->lexer
, true);
14013 /* Parse the template arguments so that we can issue error
14014 messages about them. */
14015 cp_lexer_consume_token (parser
->lexer
);
14016 cp_parser_enclosed_template_argument_list (parser
);
14017 /* Skip tokens until we find a good place from which to
14018 continue parsing. */
14019 cp_parser_skip_to_closing_parenthesis (parser
,
14020 /*recovering=*/true,
14022 /*consume_paren=*/false);
14023 /* If parsing tentatively, permanently remove the
14024 template argument list. That will prevent duplicate
14025 error messages from being issued about the missing
14026 "template" keyword. */
14028 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
14030 *is_identifier
= true;
14034 /* If the "template" keyword is present, then there is generally
14035 no point in doing name-lookup, so we just return IDENTIFIER.
14036 But, if the qualifying scope is non-dependent then we can
14037 (and must) do name-lookup normally. */
14038 if (template_keyword_p
14040 || (TYPE_P (parser
->scope
)
14041 && dependent_type_p (parser
->scope
))))
14045 /* Look up the name. */
14046 decl
= cp_parser_lookup_name (parser
, identifier
,
14048 /*is_template=*/true,
14049 /*is_namespace=*/false,
14050 check_dependency_p
,
14051 /*ambiguous_decls=*/NULL
,
14054 decl
= strip_using_decl (decl
);
14056 /* If DECL is a template, then the name was a template-name. */
14057 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
14059 if (TREE_DEPRECATED (decl
)
14060 && deprecated_state
!= DEPRECATED_SUPPRESS
)
14061 warn_deprecated_use (decl
, NULL_TREE
);
14065 tree fn
= NULL_TREE
;
14067 /* The standard does not explicitly indicate whether a name that
14068 names a set of overloaded declarations, some of which are
14069 templates, is a template-name. However, such a name should
14070 be a template-name; otherwise, there is no way to form a
14071 template-id for the overloaded templates. */
14072 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
14073 if (TREE_CODE (fns
) == OVERLOAD
)
14074 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
14075 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
14080 /* The name does not name a template. */
14081 cp_parser_error (parser
, "expected template-name");
14082 return error_mark_node
;
14086 /* If DECL is dependent, and refers to a function, then just return
14087 its name; we will look it up again during template instantiation. */
14088 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
14090 tree scope
= ovl_scope (decl
);
14091 if (TYPE_P (scope
) && dependent_type_p (scope
))
14098 /* Parse a template-argument-list.
14100 template-argument-list:
14101 template-argument ... [opt]
14102 template-argument-list , template-argument ... [opt]
14104 Returns a TREE_VEC containing the arguments. */
14107 cp_parser_template_argument_list (cp_parser
* parser
)
14109 tree fixed_args
[10];
14110 unsigned n_args
= 0;
14111 unsigned alloced
= 10;
14112 tree
*arg_ary
= fixed_args
;
14114 bool saved_in_template_argument_list_p
;
14116 bool saved_non_ice_p
;
14118 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
14119 parser
->in_template_argument_list_p
= true;
14120 /* Even if the template-id appears in an integral
14121 constant-expression, the contents of the argument list do
14123 saved_ice_p
= parser
->integral_constant_expression_p
;
14124 parser
->integral_constant_expression_p
= false;
14125 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
14126 parser
->non_integral_constant_expression_p
= false;
14128 /* Parse the arguments. */
14134 /* Consume the comma. */
14135 cp_lexer_consume_token (parser
->lexer
);
14137 /* Parse the template-argument. */
14138 argument
= cp_parser_template_argument (parser
);
14140 /* If the next token is an ellipsis, we're expanding a template
14142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14144 if (argument
== error_mark_node
)
14146 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14147 error_at (token
->location
,
14148 "expected parameter pack before %<...%>");
14150 /* Consume the `...' token. */
14151 cp_lexer_consume_token (parser
->lexer
);
14153 /* Make the argument into a TYPE_PACK_EXPANSION or
14154 EXPR_PACK_EXPANSION. */
14155 argument
= make_pack_expansion (argument
);
14158 if (n_args
== alloced
)
14162 if (arg_ary
== fixed_args
)
14164 arg_ary
= XNEWVEC (tree
, alloced
);
14165 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
14168 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
14170 arg_ary
[n_args
++] = argument
;
14172 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14174 vec
= make_tree_vec (n_args
);
14177 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
14179 if (arg_ary
!= fixed_args
)
14181 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
14182 parser
->integral_constant_expression_p
= saved_ice_p
;
14183 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
14184 #ifdef ENABLE_CHECKING
14185 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
14190 /* Parse a template-argument.
14193 assignment-expression
14197 The representation is that of an assignment-expression, type-id, or
14198 id-expression -- except that the qualified id-expression is
14199 evaluated, so that the value returned is either a DECL or an
14202 Although the standard says "assignment-expression", it forbids
14203 throw-expressions or assignments in the template argument.
14204 Therefore, we use "conditional-expression" instead. */
14207 cp_parser_template_argument (cp_parser
* parser
)
14212 bool maybe_type_id
= false;
14213 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
14214 location_t loc
= 0;
14217 /* There's really no way to know what we're looking at, so we just
14218 try each alternative in order.
14222 In a template-argument, an ambiguity between a type-id and an
14223 expression is resolved to a type-id, regardless of the form of
14224 the corresponding template-parameter.
14226 Therefore, we try a type-id first. */
14227 cp_parser_parse_tentatively (parser
);
14228 argument
= cp_parser_template_type_arg (parser
);
14229 /* If there was no error parsing the type-id but the next token is a
14230 '>>', our behavior depends on which dialect of C++ we're
14231 parsing. In C++98, we probably found a typo for '> >'. But there
14232 are type-id which are also valid expressions. For instance:
14234 struct X { int operator >> (int); };
14235 template <int V> struct Foo {};
14238 Here 'X()' is a valid type-id of a function type, but the user just
14239 wanted to write the expression "X() >> 5". Thus, we remember that we
14240 found a valid type-id, but we still try to parse the argument as an
14241 expression to see what happens.
14243 In C++0x, the '>>' will be considered two separate '>'
14245 if (!cp_parser_error_occurred (parser
)
14246 && cxx_dialect
== cxx98
14247 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14249 maybe_type_id
= true;
14250 cp_parser_abort_tentative_parse (parser
);
14254 /* If the next token isn't a `,' or a `>', then this argument wasn't
14255 really finished. This means that the argument is not a valid
14257 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14258 cp_parser_error (parser
, "expected template-argument");
14259 /* If that worked, we're done. */
14260 if (cp_parser_parse_definitely (parser
))
14263 /* We're still not sure what the argument will be. */
14264 cp_parser_parse_tentatively (parser
);
14265 /* Try a template. */
14266 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
14267 argument
= cp_parser_id_expression (parser
,
14268 /*template_keyword_p=*/false,
14269 /*check_dependency_p=*/true,
14271 /*declarator_p=*/false,
14272 /*optional_p=*/false);
14273 /* If the next token isn't a `,' or a `>', then this argument wasn't
14274 really finished. */
14275 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14276 cp_parser_error (parser
, "expected template-argument");
14277 if (!cp_parser_error_occurred (parser
))
14279 /* Figure out what is being referred to. If the id-expression
14280 was for a class template specialization, then we will have a
14281 TYPE_DECL at this point. There is no need to do name lookup
14282 at this point in that case. */
14283 if (TREE_CODE (argument
) != TYPE_DECL
)
14284 argument
= cp_parser_lookup_name (parser
, argument
,
14286 /*is_template=*/template_p
,
14287 /*is_namespace=*/false,
14288 /*check_dependency=*/true,
14289 /*ambiguous_decls=*/NULL
,
14290 argument_start_token
->location
);
14291 if (TREE_CODE (argument
) != TEMPLATE_DECL
14292 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
14293 cp_parser_error (parser
, "expected template-name");
14295 if (cp_parser_parse_definitely (parser
))
14297 if (TREE_DEPRECATED (argument
))
14298 warn_deprecated_use (argument
, NULL_TREE
);
14301 /* It must be a non-type argument. There permitted cases are given
14302 in [temp.arg.nontype]:
14304 -- an integral constant-expression of integral or enumeration
14307 -- the name of a non-type template-parameter; or
14309 -- the name of an object or function with external linkage...
14311 -- the address of an object or function with external linkage...
14313 -- a pointer to member... */
14314 /* Look for a non-type template parameter. */
14315 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14317 cp_parser_parse_tentatively (parser
);
14318 argument
= cp_parser_primary_expression (parser
,
14319 /*address_p=*/false,
14321 /*template_arg_p=*/true,
14323 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
14324 || !cp_parser_next_token_ends_template_argument_p (parser
))
14325 cp_parser_simulate_error (parser
);
14326 if (cp_parser_parse_definitely (parser
))
14330 /* If the next token is "&", the argument must be the address of an
14331 object or function with external linkage. */
14332 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
14335 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14336 cp_lexer_consume_token (parser
->lexer
);
14338 /* See if we might have an id-expression. */
14339 token
= cp_lexer_peek_token (parser
->lexer
);
14340 if (token
->type
== CPP_NAME
14341 || token
->keyword
== RID_OPERATOR
14342 || token
->type
== CPP_SCOPE
14343 || token
->type
== CPP_TEMPLATE_ID
14344 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
14346 cp_parser_parse_tentatively (parser
);
14347 argument
= cp_parser_primary_expression (parser
,
14350 /*template_arg_p=*/true,
14352 if (cp_parser_error_occurred (parser
)
14353 || !cp_parser_next_token_ends_template_argument_p (parser
))
14354 cp_parser_abort_tentative_parse (parser
);
14359 if (INDIRECT_REF_P (argument
))
14361 /* Strip the dereference temporarily. */
14362 gcc_assert (REFERENCE_REF_P (argument
));
14363 argument
= TREE_OPERAND (argument
, 0);
14366 /* If we're in a template, we represent a qualified-id referring
14367 to a static data member as a SCOPE_REF even if the scope isn't
14368 dependent so that we can check access control later. */
14370 if (TREE_CODE (probe
) == SCOPE_REF
)
14371 probe
= TREE_OPERAND (probe
, 1);
14374 /* A variable without external linkage might still be a
14375 valid constant-expression, so no error is issued here
14376 if the external-linkage check fails. */
14377 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
14378 cp_parser_simulate_error (parser
);
14380 else if (is_overloaded_fn (argument
))
14381 /* All overloaded functions are allowed; if the external
14382 linkage test does not pass, an error will be issued
14386 && (TREE_CODE (argument
) == OFFSET_REF
14387 || TREE_CODE (argument
) == SCOPE_REF
))
14388 /* A pointer-to-member. */
14390 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
14393 cp_parser_simulate_error (parser
);
14395 if (cp_parser_parse_definitely (parser
))
14398 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
14399 tf_warning_or_error
);
14401 argument
= convert_from_reference (argument
);
14406 /* If the argument started with "&", there are no other valid
14407 alternatives at this point. */
14410 cp_parser_error (parser
, "invalid non-type template argument");
14411 return error_mark_node
;
14414 /* If the argument wasn't successfully parsed as a type-id followed
14415 by '>>', the argument can only be a constant expression now.
14416 Otherwise, we try parsing the constant-expression tentatively,
14417 because the argument could really be a type-id. */
14419 cp_parser_parse_tentatively (parser
);
14420 argument
= cp_parser_constant_expression (parser
);
14422 if (!maybe_type_id
)
14424 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14425 cp_parser_error (parser
, "expected template-argument");
14426 if (cp_parser_parse_definitely (parser
))
14428 /* We did our best to parse the argument as a non type-id, but that
14429 was the only alternative that matched (albeit with a '>' after
14430 it). We can assume it's just a typo from the user, and a
14431 diagnostic will then be issued. */
14432 return cp_parser_template_type_arg (parser
);
14435 /* Parse an explicit-instantiation.
14437 explicit-instantiation:
14438 template declaration
14440 Although the standard says `declaration', what it really means is:
14442 explicit-instantiation:
14443 template decl-specifier-seq [opt] declarator [opt] ;
14445 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14446 supposed to be allowed. A defect report has been filed about this
14451 explicit-instantiation:
14452 storage-class-specifier template
14453 decl-specifier-seq [opt] declarator [opt] ;
14454 function-specifier template
14455 decl-specifier-seq [opt] declarator [opt] ; */
14458 cp_parser_explicit_instantiation (cp_parser
* parser
)
14460 int declares_class_or_enum
;
14461 cp_decl_specifier_seq decl_specifiers
;
14462 tree extension_specifier
= NULL_TREE
;
14464 timevar_push (TV_TEMPLATE_INST
);
14466 /* Look for an (optional) storage-class-specifier or
14467 function-specifier. */
14468 if (cp_parser_allow_gnu_extensions_p (parser
))
14470 extension_specifier
14471 = cp_parser_storage_class_specifier_opt (parser
);
14472 if (!extension_specifier
)
14473 extension_specifier
14474 = cp_parser_function_specifier_opt (parser
,
14475 /*decl_specs=*/NULL
);
14478 /* Look for the `template' keyword. */
14479 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14480 /* Let the front end know that we are processing an explicit
14482 begin_explicit_instantiation ();
14483 /* [temp.explicit] says that we are supposed to ignore access
14484 control while processing explicit instantiation directives. */
14485 push_deferring_access_checks (dk_no_check
);
14486 /* Parse a decl-specifier-seq. */
14487 cp_parser_decl_specifier_seq (parser
,
14488 CP_PARSER_FLAGS_OPTIONAL
,
14490 &declares_class_or_enum
);
14491 /* If there was exactly one decl-specifier, and it declared a class,
14492 and there's no declarator, then we have an explicit type
14494 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
14498 type
= check_tag_decl (&decl_specifiers
,
14499 /*explicit_type_instantiation_p=*/true);
14500 /* Turn access control back on for names used during
14501 template instantiation. */
14502 pop_deferring_access_checks ();
14504 do_type_instantiation (type
, extension_specifier
,
14505 /*complain=*/tf_error
);
14509 cp_declarator
*declarator
;
14512 /* Parse the declarator. */
14514 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14515 /*ctor_dtor_or_conv_p=*/NULL
,
14516 /*parenthesized_p=*/NULL
,
14517 /*member_p=*/false,
14518 /*friend_p=*/false);
14519 if (declares_class_or_enum
& 2)
14520 cp_parser_check_for_definition_in_return_type (declarator
,
14521 decl_specifiers
.type
,
14522 decl_specifiers
.locations
[ds_type_spec
]);
14523 if (declarator
!= cp_error_declarator
)
14525 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14526 permerror (decl_specifiers
.locations
[ds_inline
],
14527 "explicit instantiation shall not use"
14528 " %<inline%> specifier");
14529 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14530 permerror (decl_specifiers
.locations
[ds_constexpr
],
14531 "explicit instantiation shall not use"
14532 " %<constexpr%> specifier");
14534 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14535 NORMAL
, 0, &decl_specifiers
.attributes
);
14536 /* Turn access control back on for names used during
14537 template instantiation. */
14538 pop_deferring_access_checks ();
14539 /* Do the explicit instantiation. */
14540 do_decl_instantiation (decl
, extension_specifier
);
14544 pop_deferring_access_checks ();
14545 /* Skip the body of the explicit instantiation. */
14546 cp_parser_skip_to_end_of_statement (parser
);
14549 /* We're done with the instantiation. */
14550 end_explicit_instantiation ();
14552 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14554 timevar_pop (TV_TEMPLATE_INST
);
14557 /* Parse an explicit-specialization.
14559 explicit-specialization:
14560 template < > declaration
14562 Although the standard says `declaration', what it really means is:
14564 explicit-specialization:
14565 template <> decl-specifier [opt] init-declarator [opt] ;
14566 template <> function-definition
14567 template <> explicit-specialization
14568 template <> template-declaration */
14571 cp_parser_explicit_specialization (cp_parser
* parser
)
14573 bool need_lang_pop
;
14574 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14576 /* Look for the `template' keyword. */
14577 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14578 /* Look for the `<'. */
14579 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14580 /* Look for the `>'. */
14581 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14582 /* We have processed another parameter list. */
14583 ++parser
->num_template_parameter_lists
;
14586 A template ... explicit specialization ... shall not have C
14588 if (current_lang_name
== lang_name_c
)
14590 error_at (token
->location
, "template specialization with C linkage");
14591 /* Give it C++ linkage to avoid confusing other parts of the
14593 push_lang_context (lang_name_cplusplus
);
14594 need_lang_pop
= true;
14597 need_lang_pop
= false;
14598 /* Let the front end know that we are beginning a specialization. */
14599 if (!begin_specialization ())
14601 end_specialization ();
14605 /* If the next keyword is `template', we need to figure out whether
14606 or not we're looking a template-declaration. */
14607 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14609 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14610 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14611 cp_parser_template_declaration_after_export (parser
,
14612 /*member_p=*/false);
14614 cp_parser_explicit_specialization (parser
);
14617 /* Parse the dependent declaration. */
14618 cp_parser_single_declaration (parser
,
14620 /*member_p=*/false,
14621 /*explicit_specialization_p=*/true,
14622 /*friend_p=*/NULL
);
14623 /* We're done with the specialization. */
14624 end_specialization ();
14625 /* For the erroneous case of a template with C linkage, we pushed an
14626 implicit C++ linkage scope; exit that scope now. */
14628 pop_lang_context ();
14629 /* We're done with this parameter list. */
14630 --parser
->num_template_parameter_lists
;
14633 /* Parse a type-specifier.
14636 simple-type-specifier
14639 elaborated-type-specifier
14647 Returns a representation of the type-specifier. For a
14648 class-specifier, enum-specifier, or elaborated-type-specifier, a
14649 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14651 The parser flags FLAGS is used to control type-specifier parsing.
14653 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14654 in a decl-specifier-seq.
14656 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14657 class-specifier, enum-specifier, or elaborated-type-specifier, then
14658 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14659 if a type is declared; 2 if it is defined. Otherwise, it is set to
14662 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14663 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14664 is set to FALSE. */
14667 cp_parser_type_specifier (cp_parser
* parser
,
14668 cp_parser_flags flags
,
14669 cp_decl_specifier_seq
*decl_specs
,
14670 bool is_declaration
,
14671 int* declares_class_or_enum
,
14672 bool* is_cv_qualifier
)
14674 tree type_spec
= NULL_TREE
;
14677 cp_decl_spec ds
= ds_last
;
14679 /* Assume this type-specifier does not declare a new type. */
14680 if (declares_class_or_enum
)
14681 *declares_class_or_enum
= 0;
14682 /* And that it does not specify a cv-qualifier. */
14683 if (is_cv_qualifier
)
14684 *is_cv_qualifier
= false;
14685 /* Peek at the next token. */
14686 token
= cp_lexer_peek_token (parser
->lexer
);
14688 /* If we're looking at a keyword, we can use that to guide the
14689 production we choose. */
14690 keyword
= token
->keyword
;
14694 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14695 goto elaborated_type_specifier
;
14697 /* Look for the enum-specifier. */
14698 type_spec
= cp_parser_enum_specifier (parser
);
14699 /* If that worked, we're done. */
14702 if (declares_class_or_enum
)
14703 *declares_class_or_enum
= 2;
14705 cp_parser_set_decl_spec_type (decl_specs
,
14708 /*type_definition_p=*/true);
14712 goto elaborated_type_specifier
;
14714 /* Any of these indicate either a class-specifier, or an
14715 elaborated-type-specifier. */
14719 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14720 goto elaborated_type_specifier
;
14722 /* Parse tentatively so that we can back up if we don't find a
14723 class-specifier. */
14724 cp_parser_parse_tentatively (parser
);
14725 /* Look for the class-specifier. */
14726 type_spec
= cp_parser_class_specifier (parser
);
14727 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14728 /* If that worked, we're done. */
14729 if (cp_parser_parse_definitely (parser
))
14731 if (declares_class_or_enum
)
14732 *declares_class_or_enum
= 2;
14734 cp_parser_set_decl_spec_type (decl_specs
,
14737 /*type_definition_p=*/true);
14741 /* Fall through. */
14742 elaborated_type_specifier
:
14743 /* We're declaring (not defining) a class or enum. */
14744 if (declares_class_or_enum
)
14745 *declares_class_or_enum
= 1;
14747 /* Fall through. */
14749 /* Look for an elaborated-type-specifier. */
14751 = (cp_parser_elaborated_type_specifier
14753 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14756 cp_parser_set_decl_spec_type (decl_specs
,
14759 /*type_definition_p=*/false);
14764 if (is_cv_qualifier
)
14765 *is_cv_qualifier
= true;
14770 if (is_cv_qualifier
)
14771 *is_cv_qualifier
= true;
14776 if (is_cv_qualifier
)
14777 *is_cv_qualifier
= true;
14781 /* The `__complex__' keyword is a GNU extension. */
14789 /* Handle simple keywords. */
14794 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14795 decl_specs
->any_specifiers_p
= true;
14797 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14800 /* If we do not already have a type-specifier, assume we are looking
14801 at a simple-type-specifier. */
14802 type_spec
= cp_parser_simple_type_specifier (parser
,
14806 /* If we didn't find a type-specifier, and a type-specifier was not
14807 optional in this context, issue an error message. */
14808 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14810 cp_parser_error (parser
, "expected type specifier");
14811 return error_mark_node
;
14817 /* Parse a simple-type-specifier.
14819 simple-type-specifier:
14820 :: [opt] nested-name-specifier [opt] type-name
14821 :: [opt] nested-name-specifier template template-id
14836 simple-type-specifier:
14838 decltype ( expression )
14841 __underlying_type ( type-id )
14845 simple-type-specifier:
14847 __typeof__ unary-expression
14848 __typeof__ ( type-id )
14849 __typeof__ ( type-id ) { initializer-list , [opt] }
14851 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14852 appropriately updated. */
14855 cp_parser_simple_type_specifier (cp_parser
* parser
,
14856 cp_decl_specifier_seq
*decl_specs
,
14857 cp_parser_flags flags
)
14859 tree type
= NULL_TREE
;
14863 /* Peek at the next token. */
14864 token
= cp_lexer_peek_token (parser
->lexer
);
14866 /* If we're looking at a keyword, things are easy. */
14867 switch (token
->keyword
)
14871 decl_specs
->explicit_char_p
= true;
14872 type
= char_type_node
;
14875 type
= char16_type_node
;
14878 type
= char32_type_node
;
14881 type
= wchar_type_node
;
14884 type
= boolean_type_node
;
14887 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14888 type
= short_integer_type_node
;
14892 decl_specs
->explicit_int_p
= true;
14893 type
= integer_type_node
;
14899 idx
= token
->keyword
- RID_INT_N_0
;
14900 if (! int_n_enabled_p
[idx
])
14904 decl_specs
->explicit_intN_p
= true;
14905 decl_specs
->int_n_idx
= idx
;
14907 type
= int_n_trees
[idx
].signed_type
;
14911 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14912 type
= long_integer_type_node
;
14915 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14916 type
= integer_type_node
;
14919 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14920 type
= unsigned_type_node
;
14923 type
= float_type_node
;
14926 type
= double_type_node
;
14929 type
= void_type_node
;
14933 maybe_warn_cpp0x (CPP0X_AUTO
);
14934 if (parser
->auto_is_implicit_function_template_parm_p
)
14936 if (cxx_dialect
>= cxx14
)
14937 type
= synthesize_implicit_template_parm (parser
);
14939 type
= error_mark_node
;
14941 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14943 if (cxx_dialect
< cxx14
)
14944 error_at (token
->location
,
14945 "use of %<auto%> in lambda parameter declaration "
14946 "only available with "
14947 "-std=c++14 or -std=gnu++14");
14949 else if (cxx_dialect
< cxx14
)
14950 error_at (token
->location
,
14951 "use of %<auto%> in parameter declaration "
14952 "only available with "
14953 "-std=c++14 or -std=gnu++14");
14955 pedwarn (token
->location
, OPT_Wpedantic
,
14956 "ISO C++ forbids use of %<auto%> in parameter "
14960 type
= make_auto ();
14964 /* Since DR 743, decltype can either be a simple-type-specifier by
14965 itself or begin a nested-name-specifier. Parsing it will replace
14966 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14967 handling below decide what to do. */
14968 cp_parser_decltype (parser
);
14969 cp_lexer_set_token_position (parser
->lexer
, token
);
14973 /* Consume the `typeof' token. */
14974 cp_lexer_consume_token (parser
->lexer
);
14975 /* Parse the operand to `typeof'. */
14976 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14977 /* If it is not already a TYPE, take its type. */
14978 if (!TYPE_P (type
))
14979 type
= finish_typeof (type
);
14982 cp_parser_set_decl_spec_type (decl_specs
, type
,
14984 /*type_definition_p=*/false);
14988 case RID_UNDERLYING_TYPE
:
14989 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14991 cp_parser_set_decl_spec_type (decl_specs
, type
,
14993 /*type_definition_p=*/false);
14998 case RID_DIRECT_BASES
:
14999 type
= cp_parser_trait_expr (parser
, token
->keyword
);
15001 cp_parser_set_decl_spec_type (decl_specs
, type
,
15003 /*type_definition_p=*/false);
15009 /* If token is an already-parsed decltype not followed by ::,
15010 it's a simple-type-specifier. */
15011 if (token
->type
== CPP_DECLTYPE
15012 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
15014 type
= token
->u
.value
;
15017 cp_parser_set_decl_spec_type (decl_specs
, type
,
15019 /*type_definition_p=*/false);
15020 /* Remember that we are handling a decltype in order to
15021 implement the resolution of DR 1510 when the argument
15022 isn't instantiation dependent. */
15023 decl_specs
->decltype_p
= true;
15025 cp_lexer_consume_token (parser
->lexer
);
15029 /* If the type-specifier was for a built-in type, we're done. */
15032 /* Record the type. */
15034 && (token
->keyword
!= RID_SIGNED
15035 && token
->keyword
!= RID_UNSIGNED
15036 && token
->keyword
!= RID_SHORT
15037 && token
->keyword
!= RID_LONG
))
15038 cp_parser_set_decl_spec_type (decl_specs
,
15041 /*type_definition_p=*/false);
15043 decl_specs
->any_specifiers_p
= true;
15045 /* Consume the token. */
15046 cp_lexer_consume_token (parser
->lexer
);
15048 if (type
== error_mark_node
)
15049 return error_mark_node
;
15051 /* There is no valid C++ program where a non-template type is
15052 followed by a "<". That usually indicates that the user thought
15053 that the type was a template. */
15054 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
15057 return TYPE_NAME (type
);
15060 /* The type-specifier must be a user-defined type. */
15061 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
15066 /* Don't gobble tokens or issue error messages if this is an
15067 optional type-specifier. */
15068 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
15069 cp_parser_parse_tentatively (parser
);
15071 /* Look for the optional `::' operator. */
15073 = (cp_parser_global_scope_opt (parser
,
15074 /*current_scope_valid_p=*/false)
15076 /* Look for the nested-name specifier. */
15078 = (cp_parser_nested_name_specifier_opt (parser
,
15079 /*typename_keyword_p=*/false,
15080 /*check_dependency_p=*/true,
15082 /*is_declaration=*/false)
15084 token
= cp_lexer_peek_token (parser
->lexer
);
15085 /* If we have seen a nested-name-specifier, and the next token
15086 is `template', then we are using the template-id production. */
15088 && cp_parser_optional_template_keyword (parser
))
15090 /* Look for the template-id. */
15091 type
= cp_parser_template_id (parser
,
15092 /*template_keyword_p=*/true,
15093 /*check_dependency_p=*/true,
15095 /*is_declaration=*/false);
15096 /* If the template-id did not name a type, we are out of
15098 if (TREE_CODE (type
) != TYPE_DECL
)
15100 cp_parser_error (parser
, "expected template-id for type");
15104 /* Otherwise, look for a type-name. */
15106 type
= cp_parser_type_name (parser
);
15107 /* Keep track of all name-lookups performed in class scopes. */
15111 && TREE_CODE (type
) == TYPE_DECL
15112 && identifier_p (DECL_NAME (type
)))
15113 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
15114 /* If it didn't work out, we don't have a TYPE. */
15115 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
15116 && !cp_parser_parse_definitely (parser
))
15118 if (type
&& decl_specs
)
15119 cp_parser_set_decl_spec_type (decl_specs
, type
,
15121 /*type_definition_p=*/false);
15124 /* If we didn't get a type-name, issue an error message. */
15125 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15127 cp_parser_error (parser
, "expected type-name");
15128 return error_mark_node
;
15131 if (type
&& type
!= error_mark_node
)
15133 /* See if TYPE is an Objective-C type, and if so, parse and
15134 accept any protocol references following it. Do this before
15135 the cp_parser_check_for_invalid_template_id() call, because
15136 Objective-C types can be followed by '<...>' which would
15137 enclose protocol names rather than template arguments, and so
15138 everything is fine. */
15139 if (c_dialect_objc () && !parser
->scope
15140 && (objc_is_id (type
) || objc_is_class_name (type
)))
15142 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15143 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
15145 /* Clobber the "unqualified" type previously entered into
15146 DECL_SPECS with the new, improved protocol-qualified version. */
15148 decl_specs
->type
= qual_type
;
15153 /* There is no valid C++ program where a non-template type is
15154 followed by a "<". That usually indicates that the user
15155 thought that the type was a template. */
15156 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
15164 /* Parse a type-name.
15170 simple-template-id [in c++0x]
15178 Returns a TYPE_DECL for the type. */
15181 cp_parser_type_name (cp_parser
* parser
)
15185 /* We can't know yet whether it is a class-name or not. */
15186 cp_parser_parse_tentatively (parser
);
15187 /* Try a class-name. */
15188 type_decl
= cp_parser_class_name (parser
,
15189 /*typename_keyword_p=*/false,
15190 /*template_keyword_p=*/false,
15192 /*check_dependency_p=*/true,
15193 /*class_head_p=*/false,
15194 /*is_declaration=*/false);
15195 /* If it's not a class-name, keep looking. */
15196 if (!cp_parser_parse_definitely (parser
))
15198 if (cxx_dialect
< cxx11
)
15199 /* It must be a typedef-name or an enum-name. */
15200 return cp_parser_nonclass_name (parser
);
15202 cp_parser_parse_tentatively (parser
);
15203 /* It is either a simple-template-id representing an
15204 instantiation of an alias template... */
15205 type_decl
= cp_parser_template_id (parser
,
15206 /*template_keyword_p=*/false,
15207 /*check_dependency_p=*/true,
15209 /*is_declaration=*/false);
15210 /* Note that this must be an instantiation of an alias template
15211 because [temp.names]/6 says:
15213 A template-id that names an alias template specialization
15216 Whereas [temp.names]/7 says:
15218 A simple-template-id that names a class template
15219 specialization is a class-name. */
15220 if (type_decl
!= NULL_TREE
15221 && TREE_CODE (type_decl
) == TYPE_DECL
15222 && TYPE_DECL_ALIAS_P (type_decl
))
15223 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
15225 cp_parser_simulate_error (parser
);
15227 if (!cp_parser_parse_definitely (parser
))
15228 /* ... Or a typedef-name or an enum-name. */
15229 return cp_parser_nonclass_name (parser
);
15235 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15243 Returns a TYPE_DECL for the type. */
15246 cp_parser_nonclass_name (cp_parser
* parser
)
15251 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15252 identifier
= cp_parser_identifier (parser
);
15253 if (identifier
== error_mark_node
)
15254 return error_mark_node
;
15256 /* Look up the type-name. */
15257 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
15259 type_decl
= strip_using_decl (type_decl
);
15261 if (TREE_CODE (type_decl
) != TYPE_DECL
15262 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
15264 /* See if this is an Objective-C type. */
15265 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15266 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
15268 type_decl
= TYPE_NAME (type
);
15271 /* Issue an error if we did not find a type-name. */
15272 if (TREE_CODE (type_decl
) != TYPE_DECL
15273 /* In Objective-C, we have the complication that class names are
15274 normally type names and start declarations (eg, the
15275 "NSObject" in "NSObject *object;"), but can be used in an
15276 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15277 is an expression. So, a classname followed by a dot is not a
15278 valid type-name. */
15279 || (objc_is_class_name (TREE_TYPE (type_decl
))
15280 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
15282 if (!cp_parser_simulate_error (parser
))
15283 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
15284 NLE_TYPE
, token
->location
);
15285 return error_mark_node
;
15287 /* Remember that the name was used in the definition of the
15288 current class so that we can check later to see if the
15289 meaning would have been different after the class was
15290 entirely defined. */
15291 else if (type_decl
!= error_mark_node
15293 maybe_note_name_used_in_class (identifier
, type_decl
);
15298 /* Parse an elaborated-type-specifier. Note that the grammar given
15299 here incorporates the resolution to DR68.
15301 elaborated-type-specifier:
15302 class-key :: [opt] nested-name-specifier [opt] identifier
15303 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15304 enum-key :: [opt] nested-name-specifier [opt] identifier
15305 typename :: [opt] nested-name-specifier identifier
15306 typename :: [opt] nested-name-specifier template [opt]
15311 elaborated-type-specifier:
15312 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15313 class-key attributes :: [opt] nested-name-specifier [opt]
15314 template [opt] template-id
15315 enum attributes :: [opt] nested-name-specifier [opt] identifier
15317 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15318 declared `friend'. If IS_DECLARATION is TRUE, then this
15319 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15320 something is being declared.
15322 Returns the TYPE specified. */
15325 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
15327 bool is_declaration
)
15329 enum tag_types tag_type
;
15331 tree type
= NULL_TREE
;
15332 tree attributes
= NULL_TREE
;
15334 cp_token
*token
= NULL
;
15336 /* See if we're looking at the `enum' keyword. */
15337 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
15339 /* Consume the `enum' token. */
15340 cp_lexer_consume_token (parser
->lexer
);
15341 /* Remember that it's an enumeration type. */
15342 tag_type
= enum_type
;
15343 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15344 enums) is used here. */
15345 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15346 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15348 pedwarn (input_location
, 0, "elaborated-type-specifier "
15349 "for a scoped enum must not use the %<%D%> keyword",
15350 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
15351 /* Consume the `struct' or `class' and parse it anyway. */
15352 cp_lexer_consume_token (parser
->lexer
);
15354 /* Parse the attributes. */
15355 attributes
= cp_parser_attributes_opt (parser
);
15357 /* Or, it might be `typename'. */
15358 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15361 /* Consume the `typename' token. */
15362 cp_lexer_consume_token (parser
->lexer
);
15363 /* Remember that it's a `typename' type. */
15364 tag_type
= typename_type
;
15366 /* Otherwise it must be a class-key. */
15369 tag_type
= cp_parser_class_key (parser
);
15370 if (tag_type
== none_type
)
15371 return error_mark_node
;
15372 /* Parse the attributes. */
15373 attributes
= cp_parser_attributes_opt (parser
);
15376 /* Look for the `::' operator. */
15377 globalscope
= cp_parser_global_scope_opt (parser
,
15378 /*current_scope_valid_p=*/false);
15379 /* Look for the nested-name-specifier. */
15380 if (tag_type
== typename_type
&& !globalscope
)
15382 if (!cp_parser_nested_name_specifier (parser
,
15383 /*typename_keyword_p=*/true,
15384 /*check_dependency_p=*/true,
15387 return error_mark_node
;
15390 /* Even though `typename' is not present, the proposed resolution
15391 to Core Issue 180 says that in `class A<T>::B', `B' should be
15392 considered a type-name, even if `A<T>' is dependent. */
15393 cp_parser_nested_name_specifier_opt (parser
,
15394 /*typename_keyword_p=*/true,
15395 /*check_dependency_p=*/true,
15398 /* For everything but enumeration types, consider a template-id.
15399 For an enumeration type, consider only a plain identifier. */
15400 if (tag_type
!= enum_type
)
15402 bool template_p
= false;
15405 /* Allow the `template' keyword. */
15406 template_p
= cp_parser_optional_template_keyword (parser
);
15407 /* If we didn't see `template', we don't know if there's a
15408 template-id or not. */
15410 cp_parser_parse_tentatively (parser
);
15411 /* Parse the template-id. */
15412 token
= cp_lexer_peek_token (parser
->lexer
);
15413 decl
= cp_parser_template_id (parser
, template_p
,
15414 /*check_dependency_p=*/true,
15417 /* If we didn't find a template-id, look for an ordinary
15419 if (!template_p
&& !cp_parser_parse_definitely (parser
))
15421 /* We can get here when cp_parser_template_id, called by
15422 cp_parser_class_name with tag_type == none_type, succeeds
15423 and caches a BASELINK. Then, when called again here,
15424 instead of failing and returning an error_mark_node
15425 returns it (see template/typename17.C in C++11).
15426 ??? Could we diagnose this earlier? */
15427 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
15429 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
15430 type
= error_mark_node
;
15432 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15433 in effect, then we must assume that, upon instantiation, the
15434 template will correspond to a class. */
15435 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15436 && tag_type
== typename_type
)
15437 type
= make_typename_type (parser
->scope
, decl
,
15439 /*complain=*/tf_error
);
15440 /* If the `typename' keyword is in effect and DECL is not a type
15441 decl, then type is non existent. */
15442 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
15444 else if (TREE_CODE (decl
) == TYPE_DECL
)
15445 type
= check_elaborated_type_specifier (tag_type
, decl
,
15446 /*allow_template_p=*/true);
15447 else if (decl
== error_mark_node
)
15448 type
= error_mark_node
;
15453 token
= cp_lexer_peek_token (parser
->lexer
);
15454 identifier
= cp_parser_identifier (parser
);
15456 if (identifier
== error_mark_node
)
15458 parser
->scope
= NULL_TREE
;
15459 return error_mark_node
;
15462 /* For a `typename', we needn't call xref_tag. */
15463 if (tag_type
== typename_type
15464 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
15465 return cp_parser_make_typename_type (parser
, identifier
,
15468 /* Template parameter lists apply only if we are not within a
15469 function parameter list. */
15470 bool template_parm_lists_apply
15471 = parser
->num_template_parameter_lists
;
15472 if (template_parm_lists_apply
)
15473 for (cp_binding_level
*s
= current_binding_level
;
15474 s
&& s
->kind
!= sk_template_parms
;
15475 s
= s
->level_chain
)
15476 if (s
->kind
== sk_function_parms
)
15477 template_parm_lists_apply
= false;
15479 /* Look up a qualified name in the usual way. */
15483 tree ambiguous_decls
;
15485 decl
= cp_parser_lookup_name (parser
, identifier
,
15487 /*is_template=*/false,
15488 /*is_namespace=*/false,
15489 /*check_dependency=*/true,
15493 /* If the lookup was ambiguous, an error will already have been
15495 if (ambiguous_decls
)
15496 return error_mark_node
;
15498 /* If we are parsing friend declaration, DECL may be a
15499 TEMPLATE_DECL tree node here. However, we need to check
15500 whether this TEMPLATE_DECL results in valid code. Consider
15501 the following example:
15504 template <class T> class C {};
15507 template <class T> friend class N::C; // #1, valid code
15509 template <class T> class Y {
15510 friend class N::C; // #2, invalid code
15513 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15514 name lookup of `N::C'. We see that friend declaration must
15515 be template for the code to be valid. Note that
15516 processing_template_decl does not work here since it is
15517 always 1 for the above two cases. */
15519 decl
= (cp_parser_maybe_treat_template_as_class
15520 (decl
, /*tag_name_p=*/is_friend
15521 && template_parm_lists_apply
));
15523 if (TREE_CODE (decl
) != TYPE_DECL
)
15525 cp_parser_diagnose_invalid_type_name (parser
,
15528 return error_mark_node
;
15531 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15533 bool allow_template
= (template_parm_lists_apply
15534 || DECL_SELF_REFERENCE_P (decl
));
15535 type
= check_elaborated_type_specifier (tag_type
, decl
,
15538 if (type
== error_mark_node
)
15539 return error_mark_node
;
15542 /* Forward declarations of nested types, such as
15547 are invalid unless all components preceding the final '::'
15548 are complete. If all enclosing types are complete, these
15549 declarations become merely pointless.
15551 Invalid forward declarations of nested types are errors
15552 caught elsewhere in parsing. Those that are pointless arrive
15555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15556 && !is_friend
&& !processing_explicit_instantiation
)
15557 warning (0, "declaration %qD does not declare anything", decl
);
15559 type
= TREE_TYPE (decl
);
15563 /* An elaborated-type-specifier sometimes introduces a new type and
15564 sometimes names an existing type. Normally, the rule is that it
15565 introduces a new type only if there is not an existing type of
15566 the same name already in scope. For example, given:
15569 void f() { struct S s; }
15571 the `struct S' in the body of `f' is the same `struct S' as in
15572 the global scope; the existing definition is used. However, if
15573 there were no global declaration, this would introduce a new
15574 local class named `S'.
15576 An exception to this rule applies to the following code:
15578 namespace N { struct S; }
15580 Here, the elaborated-type-specifier names a new type
15581 unconditionally; even if there is already an `S' in the
15582 containing scope this declaration names a new type.
15583 This exception only applies if the elaborated-type-specifier
15584 forms the complete declaration:
15588 A declaration consisting solely of `class-key identifier ;' is
15589 either a redeclaration of the name in the current scope or a
15590 forward declaration of the identifier as a class name. It
15591 introduces the name into the current scope.
15593 We are in this situation precisely when the next token is a `;'.
15595 An exception to the exception is that a `friend' declaration does
15596 *not* name a new type; i.e., given:
15598 struct S { friend struct T; };
15600 `T' is not a new type in the scope of `S'.
15602 Also, `new struct S' or `sizeof (struct S)' never results in the
15603 definition of a new type; a new type can only be declared in a
15604 declaration context. */
15610 /* Friends have special name lookup rules. */
15611 ts
= ts_within_enclosing_non_class
;
15612 else if (is_declaration
15613 && cp_lexer_next_token_is (parser
->lexer
,
15615 /* This is a `class-key identifier ;' */
15621 (template_parm_lists_apply
15622 && (cp_parser_next_token_starts_class_definition_p (parser
)
15623 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15624 /* An unqualified name was used to reference this type, so
15625 there were no qualifying templates. */
15626 if (template_parm_lists_apply
15627 && !cp_parser_check_template_parameters (parser
,
15628 /*num_templates=*/0,
15630 /*declarator=*/NULL
))
15631 return error_mark_node
;
15632 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15636 if (type
== error_mark_node
)
15637 return error_mark_node
;
15639 /* Allow attributes on forward declarations of classes. */
15642 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15643 warning (OPT_Wattributes
,
15644 "attributes ignored on uninstantiated type");
15645 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15646 && ! processing_explicit_instantiation
)
15647 warning (OPT_Wattributes
,
15648 "attributes ignored on template instantiation");
15649 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15650 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15652 warning (OPT_Wattributes
,
15653 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15656 if (tag_type
!= enum_type
)
15658 /* Indicate whether this class was declared as a `class' or as a
15660 if (TREE_CODE (type
) == RECORD_TYPE
)
15661 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15662 cp_parser_check_class_key (tag_type
, type
);
15665 /* A "<" cannot follow an elaborated type specifier. If that
15666 happens, the user was probably trying to form a template-id. */
15667 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15673 /* Parse an enum-specifier.
15676 enum-head { enumerator-list [opt] }
15677 enum-head { enumerator-list , } [C++0x]
15680 enum-key identifier [opt] enum-base [opt]
15681 enum-key nested-name-specifier identifier enum-base [opt]
15686 enum struct [C++0x]
15689 : type-specifier-seq
15691 opaque-enum-specifier:
15692 enum-key identifier enum-base [opt] ;
15695 enum-key attributes[opt] identifier [opt] enum-base [opt]
15696 { enumerator-list [opt] }attributes[opt]
15697 enum-key attributes[opt] identifier [opt] enum-base [opt]
15698 { enumerator-list, }attributes[opt] [C++0x]
15700 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15701 if the token stream isn't an enum-specifier after all. */
15704 cp_parser_enum_specifier (cp_parser
* parser
)
15707 tree type
= NULL_TREE
;
15709 tree nested_name_specifier
= NULL_TREE
;
15711 bool scoped_enum_p
= false;
15712 bool has_underlying_type
= false;
15713 bool nested_being_defined
= false;
15714 bool new_value_list
= false;
15715 bool is_new_type
= false;
15716 bool is_anonymous
= false;
15717 tree underlying_type
= NULL_TREE
;
15718 cp_token
*type_start_token
= NULL
;
15719 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15721 parser
->colon_corrects_to_scope_p
= false;
15723 /* Parse tentatively so that we can back up if we don't find a
15725 cp_parser_parse_tentatively (parser
);
15727 /* Caller guarantees that the current token is 'enum', an identifier
15728 possibly follows, and the token after that is an opening brace.
15729 If we don't have an identifier, fabricate an anonymous name for
15730 the enumeration being defined. */
15731 cp_lexer_consume_token (parser
->lexer
);
15733 /* Parse the "class" or "struct", which indicates a scoped
15734 enumeration type in C++0x. */
15735 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15736 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15738 if (cxx_dialect
< cxx11
)
15739 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15741 /* Consume the `struct' or `class' token. */
15742 cp_lexer_consume_token (parser
->lexer
);
15744 scoped_enum_p
= true;
15747 attributes
= cp_parser_attributes_opt (parser
);
15749 /* Clear the qualification. */
15750 parser
->scope
= NULL_TREE
;
15751 parser
->qualifying_scope
= NULL_TREE
;
15752 parser
->object_scope
= NULL_TREE
;
15754 /* Figure out in what scope the declaration is being placed. */
15755 prev_scope
= current_scope ();
15757 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15759 push_deferring_access_checks (dk_no_check
);
15760 nested_name_specifier
15761 = cp_parser_nested_name_specifier_opt (parser
,
15762 /*typename_keyword_p=*/true,
15763 /*check_dependency_p=*/false,
15765 /*is_declaration=*/false);
15767 if (nested_name_specifier
)
15771 identifier
= cp_parser_identifier (parser
);
15772 name
= cp_parser_lookup_name (parser
, identifier
,
15774 /*is_template=*/false,
15775 /*is_namespace=*/false,
15776 /*check_dependency=*/true,
15777 /*ambiguous_decls=*/NULL
,
15779 if (name
&& name
!= error_mark_node
)
15781 type
= TREE_TYPE (name
);
15782 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15784 /* Are template enums allowed in ISO? */
15785 if (template_parm_scope_p ())
15786 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15787 "%qD is an enumeration template", name
);
15788 /* ignore a typename reference, for it will be solved by name
15793 else if (nested_name_specifier
== error_mark_node
)
15794 /* We already issued an error. */;
15796 error_at (type_start_token
->location
,
15797 "%qD is not an enumerator-name", identifier
);
15801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15802 identifier
= cp_parser_identifier (parser
);
15805 identifier
= make_anon_name ();
15806 is_anonymous
= true;
15808 error_at (type_start_token
->location
,
15809 "anonymous scoped enum is not allowed");
15812 pop_deferring_access_checks ();
15814 /* Check for the `:' that denotes a specified underlying type in C++0x.
15815 Note that a ':' could also indicate a bitfield width, however. */
15816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15818 cp_decl_specifier_seq type_specifiers
;
15820 /* Consume the `:'. */
15821 cp_lexer_consume_token (parser
->lexer
);
15823 /* Parse the type-specifier-seq. */
15824 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15825 /*is_trailing_return=*/false,
15828 /* At this point this is surely not elaborated type specifier. */
15829 if (!cp_parser_parse_definitely (parser
))
15832 if (cxx_dialect
< cxx11
)
15833 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15835 has_underlying_type
= true;
15837 /* If that didn't work, stop. */
15838 if (type_specifiers
.type
!= error_mark_node
)
15840 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15841 /*initialized=*/0, NULL
);
15842 if (underlying_type
== error_mark_node
15843 || check_for_bare_parameter_packs (underlying_type
))
15844 underlying_type
= NULL_TREE
;
15848 /* Look for the `{' but don't consume it yet. */
15849 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15851 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15853 cp_parser_error (parser
, "expected %<{%>");
15854 if (has_underlying_type
)
15860 /* An opaque-enum-specifier must have a ';' here. */
15861 if ((scoped_enum_p
|| underlying_type
)
15862 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15864 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15865 if (has_underlying_type
)
15873 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15876 if (nested_name_specifier
)
15878 if (CLASS_TYPE_P (nested_name_specifier
))
15880 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15881 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15882 push_scope (nested_name_specifier
);
15884 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15886 push_nested_namespace (nested_name_specifier
);
15890 /* Issue an error message if type-definitions are forbidden here. */
15891 if (!cp_parser_check_type_definition (parser
))
15892 type
= error_mark_node
;
15894 /* Create the new type. We do this before consuming the opening
15895 brace so the enum will be recorded as being on the line of its
15896 tag (or the 'enum' keyword, if there is no tag). */
15897 type
= start_enum (identifier
, type
, underlying_type
,
15898 scoped_enum_p
, &is_new_type
);
15900 /* If the next token is not '{' it is an opaque-enum-specifier or an
15901 elaborated-type-specifier. */
15902 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15904 timevar_push (TV_PARSE_ENUM
);
15905 if (nested_name_specifier
15906 && nested_name_specifier
!= error_mark_node
)
15908 /* The following catches invalid code such as:
15909 enum class S<int>::E { A, B, C }; */
15910 if (!processing_specialization
15911 && CLASS_TYPE_P (nested_name_specifier
)
15912 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15913 error_at (type_start_token
->location
, "cannot add an enumerator "
15914 "list to a template instantiation");
15916 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
15918 error_at (type_start_token
->location
,
15919 "%<%T::%E%> has not been declared",
15920 TYPE_CONTEXT (nested_name_specifier
),
15921 nested_name_specifier
);
15922 type
= error_mark_node
;
15924 /* If that scope does not contain the scope in which the
15925 class was originally declared, the program is invalid. */
15926 else if (prev_scope
&& !is_ancestor (prev_scope
,
15927 nested_name_specifier
))
15929 if (at_namespace_scope_p ())
15930 error_at (type_start_token
->location
,
15931 "declaration of %qD in namespace %qD which does not "
15933 type
, prev_scope
, nested_name_specifier
);
15935 error_at (type_start_token
->location
,
15936 "declaration of %qD in %qD which does not "
15938 type
, prev_scope
, nested_name_specifier
);
15939 type
= error_mark_node
;
15944 begin_scope (sk_scoped_enum
, type
);
15946 /* Consume the opening brace. */
15947 cp_lexer_consume_token (parser
->lexer
);
15949 if (type
== error_mark_node
)
15950 ; /* Nothing to add */
15951 else if (OPAQUE_ENUM_P (type
)
15952 || (cxx_dialect
> cxx98
&& processing_specialization
))
15954 new_value_list
= true;
15955 SET_OPAQUE_ENUM_P (type
, false);
15956 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15960 error_at (type_start_token
->location
,
15961 "multiple definition of %q#T", type
);
15962 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15963 "previous definition here");
15964 type
= error_mark_node
;
15967 if (type
== error_mark_node
)
15968 cp_parser_skip_to_end_of_block_or_statement (parser
);
15969 /* If the next token is not '}', then there are some enumerators. */
15970 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15972 if (is_anonymous
&& !scoped_enum_p
)
15973 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15974 "ISO C++ forbids empty anonymous enum");
15977 cp_parser_enumerator_list (parser
, type
);
15979 /* Consume the final '}'. */
15980 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15984 timevar_pop (TV_PARSE_ENUM
);
15988 /* If a ';' follows, then it is an opaque-enum-specifier
15989 and additional restrictions apply. */
15990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15993 error_at (type_start_token
->location
,
15994 "opaque-enum-specifier without name");
15995 else if (nested_name_specifier
)
15996 error_at (type_start_token
->location
,
15997 "opaque-enum-specifier must use a simple identifier");
16001 /* Look for trailing attributes to apply to this enumeration, and
16002 apply them if appropriate. */
16003 if (cp_parser_allow_gnu_extensions_p (parser
))
16005 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
16006 trailing_attr
= chainon (trailing_attr
, attributes
);
16007 cplus_decl_attributes (&type
,
16009 (int) ATTR_FLAG_TYPE_IN_PLACE
);
16012 /* Finish up the enumeration. */
16013 if (type
!= error_mark_node
)
16015 if (new_value_list
)
16016 finish_enum_value_list (type
);
16018 finish_enum (type
);
16021 if (nested_name_specifier
)
16023 if (CLASS_TYPE_P (nested_name_specifier
))
16025 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
16026 pop_scope (nested_name_specifier
);
16028 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
16030 pop_nested_namespace (nested_name_specifier
);
16034 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
16038 /* Parse an enumerator-list. The enumerators all have the indicated
16042 enumerator-definition
16043 enumerator-list , enumerator-definition */
16046 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
16050 /* Parse an enumerator-definition. */
16051 cp_parser_enumerator_definition (parser
, type
);
16053 /* If the next token is not a ',', we've reached the end of
16055 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16057 /* Otherwise, consume the `,' and keep going. */
16058 cp_lexer_consume_token (parser
->lexer
);
16059 /* If the next token is a `}', there is a trailing comma. */
16060 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
16062 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
16063 pedwarn (input_location
, OPT_Wpedantic
,
16064 "comma at end of enumerator list");
16070 /* Parse an enumerator-definition. The enumerator has the indicated
16073 enumerator-definition:
16075 enumerator = constant-expression
16081 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
16087 /* Save the input location because we are interested in the location
16088 of the identifier and not the location of the explicit value. */
16089 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16091 /* Look for the identifier. */
16092 identifier
= cp_parser_identifier (parser
);
16093 if (identifier
== error_mark_node
)
16096 /* If the next token is an '=', then there is an explicit value. */
16097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16099 /* Consume the `=' token. */
16100 cp_lexer_consume_token (parser
->lexer
);
16101 /* Parse the value. */
16102 value
= cp_parser_constant_expression (parser
);
16107 /* If we are processing a template, make sure the initializer of the
16108 enumerator doesn't contain any bare template parameter pack. */
16109 if (check_for_bare_parameter_packs (value
))
16110 value
= error_mark_node
;
16112 /* Create the enumerator. */
16113 build_enumerator (identifier
, value
, type
, loc
);
16116 /* Parse a namespace-name.
16119 original-namespace-name
16122 Returns the NAMESPACE_DECL for the namespace. */
16125 cp_parser_namespace_name (cp_parser
* parser
)
16128 tree namespace_decl
;
16130 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16132 /* Get the name of the namespace. */
16133 identifier
= cp_parser_identifier (parser
);
16134 if (identifier
== error_mark_node
)
16135 return error_mark_node
;
16137 /* Look up the identifier in the currently active scope. Look only
16138 for namespaces, due to:
16140 [basic.lookup.udir]
16142 When looking up a namespace-name in a using-directive or alias
16143 definition, only namespace names are considered.
16147 [basic.lookup.qual]
16149 During the lookup of a name preceding the :: scope resolution
16150 operator, object, function, and enumerator names are ignored.
16152 (Note that cp_parser_qualifying_entity only calls this
16153 function if the token after the name is the scope resolution
16155 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
16157 /*is_template=*/false,
16158 /*is_namespace=*/true,
16159 /*check_dependency=*/true,
16160 /*ambiguous_decls=*/NULL
,
16162 /* If it's not a namespace, issue an error. */
16163 if (namespace_decl
== error_mark_node
16164 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
16166 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16167 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
16168 cp_parser_error (parser
, "expected namespace-name");
16169 namespace_decl
= error_mark_node
;
16172 return namespace_decl
;
16175 /* Parse a namespace-definition.
16177 namespace-definition:
16178 named-namespace-definition
16179 unnamed-namespace-definition
16181 named-namespace-definition:
16182 original-namespace-definition
16183 extension-namespace-definition
16185 original-namespace-definition:
16186 namespace identifier { namespace-body }
16188 extension-namespace-definition:
16189 namespace original-namespace-name { namespace-body }
16191 unnamed-namespace-definition:
16192 namespace { namespace-body } */
16195 cp_parser_namespace_definition (cp_parser
* parser
)
16197 tree identifier
, attribs
;
16198 bool has_visibility
;
16201 cp_ensure_no_omp_declare_simd (parser
);
16202 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
16204 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
16206 cp_lexer_consume_token (parser
->lexer
);
16211 /* Look for the `namespace' keyword. */
16212 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16214 /* Get the name of the namespace. We do not attempt to distinguish
16215 between an original-namespace-definition and an
16216 extension-namespace-definition at this point. The semantic
16217 analysis routines are responsible for that. */
16218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16219 identifier
= cp_parser_identifier (parser
);
16221 identifier
= NULL_TREE
;
16223 /* Parse any specified attributes. */
16224 attribs
= cp_parser_attributes_opt (parser
);
16226 /* Look for the `{' to start the namespace. */
16227 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
16228 /* Start the namespace. */
16229 push_namespace (identifier
);
16231 /* "inline namespace" is equivalent to a stub namespace definition
16232 followed by a strong using directive. */
16235 tree name_space
= current_namespace
;
16236 /* Set up namespace association. */
16237 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
16238 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
16239 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
16240 /* Import the contents of the inline namespace. */
16242 do_using_directive (name_space
);
16243 push_namespace (identifier
);
16246 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
16248 /* Parse the body of the namespace. */
16249 cp_parser_namespace_body (parser
);
16251 if (has_visibility
)
16252 pop_visibility (1);
16254 /* Finish the namespace. */
16256 /* Look for the final `}'. */
16257 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16260 /* Parse a namespace-body.
16263 declaration-seq [opt] */
16266 cp_parser_namespace_body (cp_parser
* parser
)
16268 cp_parser_declaration_seq_opt (parser
);
16271 /* Parse a namespace-alias-definition.
16273 namespace-alias-definition:
16274 namespace identifier = qualified-namespace-specifier ; */
16277 cp_parser_namespace_alias_definition (cp_parser
* parser
)
16280 tree namespace_specifier
;
16282 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16284 /* Look for the `namespace' keyword. */
16285 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16286 /* Look for the identifier. */
16287 identifier
= cp_parser_identifier (parser
);
16288 if (identifier
== error_mark_node
)
16290 /* Look for the `=' token. */
16291 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
16292 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16294 error_at (token
->location
, "%<namespace%> definition is not allowed here");
16295 /* Skip the definition. */
16296 cp_lexer_consume_token (parser
->lexer
);
16297 if (cp_parser_skip_to_closing_brace (parser
))
16298 cp_lexer_consume_token (parser
->lexer
);
16301 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16302 /* Look for the qualified-namespace-specifier. */
16303 namespace_specifier
16304 = cp_parser_qualified_namespace_specifier (parser
);
16305 /* Look for the `;' token. */
16306 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16308 /* Register the alias in the symbol table. */
16309 do_namespace_alias (identifier
, namespace_specifier
);
16312 /* Parse a qualified-namespace-specifier.
16314 qualified-namespace-specifier:
16315 :: [opt] nested-name-specifier [opt] namespace-name
16317 Returns a NAMESPACE_DECL corresponding to the specified
16321 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
16323 /* Look for the optional `::'. */
16324 cp_parser_global_scope_opt (parser
,
16325 /*current_scope_valid_p=*/false);
16327 /* Look for the optional nested-name-specifier. */
16328 cp_parser_nested_name_specifier_opt (parser
,
16329 /*typename_keyword_p=*/false,
16330 /*check_dependency_p=*/true,
16332 /*is_declaration=*/true);
16334 return cp_parser_namespace_name (parser
);
16337 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16338 access declaration.
16341 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16342 using :: unqualified-id ;
16344 access-declaration:
16350 cp_parser_using_declaration (cp_parser
* parser
,
16351 bool access_declaration_p
)
16354 bool typename_p
= false;
16355 bool global_scope_p
;
16359 int oldcount
= errorcount
;
16360 cp_token
*diag_token
= NULL
;
16362 if (access_declaration_p
)
16364 diag_token
= cp_lexer_peek_token (parser
->lexer
);
16365 cp_parser_parse_tentatively (parser
);
16369 /* Look for the `using' keyword. */
16370 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16372 /* Peek at the next token. */
16373 token
= cp_lexer_peek_token (parser
->lexer
);
16374 /* See if it's `typename'. */
16375 if (token
->keyword
== RID_TYPENAME
)
16377 /* Remember that we've seen it. */
16379 /* Consume the `typename' token. */
16380 cp_lexer_consume_token (parser
->lexer
);
16384 /* Look for the optional global scope qualification. */
16386 = (cp_parser_global_scope_opt (parser
,
16387 /*current_scope_valid_p=*/false)
16390 /* If we saw `typename', or didn't see `::', then there must be a
16391 nested-name-specifier present. */
16392 if (typename_p
|| !global_scope_p
)
16394 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
16395 /*check_dependency_p=*/true,
16397 /*is_declaration=*/true);
16398 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
16400 cp_parser_skip_to_end_of_block_or_statement (parser
);
16404 /* Otherwise, we could be in either of the two productions. In that
16405 case, treat the nested-name-specifier as optional. */
16407 qscope
= cp_parser_nested_name_specifier_opt (parser
,
16408 /*typename_keyword_p=*/false,
16409 /*check_dependency_p=*/true,
16411 /*is_declaration=*/true);
16413 qscope
= global_namespace
;
16414 else if (UNSCOPED_ENUM_P (qscope
))
16415 qscope
= CP_TYPE_CONTEXT (qscope
);
16417 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
16418 /* Something has already gone wrong; there's no need to parse
16419 further. Since an error has occurred, the return value of
16420 cp_parser_parse_definitely will be false, as required. */
16421 return cp_parser_parse_definitely (parser
);
16423 token
= cp_lexer_peek_token (parser
->lexer
);
16424 /* Parse the unqualified-id. */
16425 identifier
= cp_parser_unqualified_id (parser
,
16426 /*template_keyword_p=*/false,
16427 /*check_dependency_p=*/true,
16428 /*declarator_p=*/true,
16429 /*optional_p=*/false);
16431 if (access_declaration_p
)
16433 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16434 cp_parser_simulate_error (parser
);
16435 if (!cp_parser_parse_definitely (parser
))
16439 /* The function we call to handle a using-declaration is different
16440 depending on what scope we are in. */
16441 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
16443 else if (!identifier_p (identifier
)
16444 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
16445 /* [namespace.udecl]
16447 A using declaration shall not name a template-id. */
16448 error_at (token
->location
,
16449 "a template-id may not appear in a using-declaration");
16452 if (at_class_scope_p ())
16454 /* Create the USING_DECL. */
16455 decl
= do_class_using_decl (parser
->scope
, identifier
);
16457 if (decl
&& typename_p
)
16458 USING_DECL_TYPENAME_P (decl
) = 1;
16460 if (check_for_bare_parameter_packs (decl
))
16462 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16466 /* Add it to the list of members in this class. */
16467 finish_member_declaration (decl
);
16471 decl
= cp_parser_lookup_name_simple (parser
,
16474 if (decl
== error_mark_node
)
16475 cp_parser_name_lookup_error (parser
, identifier
,
16478 else if (check_for_bare_parameter_packs (decl
))
16480 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16483 else if (!at_namespace_scope_p ())
16484 do_local_using_decl (decl
, qscope
, identifier
);
16486 do_toplevel_using_decl (decl
, qscope
, identifier
);
16490 /* Look for the final `;'. */
16491 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16493 if (access_declaration_p
&& errorcount
== oldcount
)
16494 warning_at (diag_token
->location
, OPT_Wdeprecated
,
16495 "access declarations are deprecated "
16496 "in favour of using-declarations; "
16497 "suggestion: add the %<using%> keyword");
16502 /* Parse an alias-declaration.
16505 using identifier attribute-specifier-seq [opt] = type-id */
16508 cp_parser_alias_declaration (cp_parser
* parser
)
16510 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
16511 location_t id_location
;
16512 cp_declarator
*declarator
;
16513 cp_decl_specifier_seq decl_specs
;
16515 const char *saved_message
= NULL
;
16517 /* Look for the `using' keyword. */
16518 cp_token
*using_token
16519 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16520 if (using_token
== NULL
)
16521 return error_mark_node
;
16523 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
16524 id
= cp_parser_identifier (parser
);
16525 if (id
== error_mark_node
)
16526 return error_mark_node
;
16528 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
16529 attributes
= cp_parser_attributes_opt (parser
);
16530 if (attributes
== error_mark_node
)
16531 return error_mark_node
;
16533 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16535 if (cp_parser_error_occurred (parser
))
16536 return error_mark_node
;
16538 cp_parser_commit_to_tentative_parse (parser
);
16540 /* Now we are going to parse the type-id of the declaration. */
16545 "A type-specifier-seq shall not define a class or enumeration
16546 unless it appears in the type-id of an alias-declaration (7.1.3) that
16547 is not the declaration of a template-declaration."
16549 In other words, if we currently are in an alias template, the
16550 type-id should not define a type.
16552 So let's set parser->type_definition_forbidden_message in that
16553 case; cp_parser_check_type_definition (called by
16554 cp_parser_class_specifier) will then emit an error if a type is
16555 defined in the type-id. */
16556 if (parser
->num_template_parameter_lists
)
16558 saved_message
= parser
->type_definition_forbidden_message
;
16559 parser
->type_definition_forbidden_message
=
16560 G_("types may not be defined in alias template declarations");
16563 type
= cp_parser_type_id (parser
);
16565 /* Restore the error message if need be. */
16566 if (parser
->num_template_parameter_lists
)
16567 parser
->type_definition_forbidden_message
= saved_message
;
16569 if (type
== error_mark_node
16570 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
16572 cp_parser_skip_to_end_of_block_or_statement (parser
);
16573 return error_mark_node
;
16576 /* A typedef-name can also be introduced by an alias-declaration. The
16577 identifier following the using keyword becomes a typedef-name. It has
16578 the same semantics as if it were introduced by the typedef
16579 specifier. In particular, it does not define a new type and it shall
16580 not appear in the type-id. */
16582 clear_decl_specs (&decl_specs
);
16583 decl_specs
.type
= type
;
16584 if (attributes
!= NULL_TREE
)
16586 decl_specs
.attributes
= attributes
;
16587 set_and_check_decl_spec_loc (&decl_specs
,
16591 set_and_check_decl_spec_loc (&decl_specs
,
16594 set_and_check_decl_spec_loc (&decl_specs
,
16598 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16599 declarator
->id_loc
= id_location
;
16601 member_p
= at_class_scope_p ();
16603 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16604 NULL_TREE
, attributes
);
16606 decl
= start_decl (declarator
, &decl_specs
, 0,
16607 attributes
, NULL_TREE
, &pushed_scope
);
16608 if (decl
== error_mark_node
)
16611 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16614 pop_scope (pushed_scope
);
16616 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16617 added into the symbol table; otherwise, return the TYPE_DECL. */
16618 if (DECL_LANG_SPECIFIC (decl
)
16619 && DECL_TEMPLATE_INFO (decl
)
16620 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16622 decl
= DECL_TI_TEMPLATE (decl
);
16624 check_member_template (decl
);
16630 /* Parse a using-directive.
16633 using namespace :: [opt] nested-name-specifier [opt]
16634 namespace-name ; */
16637 cp_parser_using_directive (cp_parser
* parser
)
16639 tree namespace_decl
;
16642 /* Look for the `using' keyword. */
16643 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16644 /* And the `namespace' keyword. */
16645 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16646 /* Look for the optional `::' operator. */
16647 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16648 /* And the optional nested-name-specifier. */
16649 cp_parser_nested_name_specifier_opt (parser
,
16650 /*typename_keyword_p=*/false,
16651 /*check_dependency_p=*/true,
16653 /*is_declaration=*/true);
16654 /* Get the namespace being used. */
16655 namespace_decl
= cp_parser_namespace_name (parser
);
16656 /* And any specified attributes. */
16657 attribs
= cp_parser_attributes_opt (parser
);
16658 /* Update the symbol table. */
16659 parse_using_directive (namespace_decl
, attribs
);
16660 /* Look for the final `;'. */
16661 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16664 /* Parse an asm-definition.
16667 asm ( string-literal ) ;
16672 asm volatile [opt] ( string-literal ) ;
16673 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16674 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16675 : asm-operand-list [opt] ) ;
16676 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16677 : asm-operand-list [opt]
16678 : asm-clobber-list [opt] ) ;
16679 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16680 : asm-clobber-list [opt]
16681 : asm-goto-list ) ; */
16684 cp_parser_asm_definition (cp_parser
* parser
)
16687 tree outputs
= NULL_TREE
;
16688 tree inputs
= NULL_TREE
;
16689 tree clobbers
= NULL_TREE
;
16690 tree labels
= NULL_TREE
;
16692 bool volatile_p
= false;
16693 bool extended_p
= false;
16694 bool invalid_inputs_p
= false;
16695 bool invalid_outputs_p
= false;
16696 bool goto_p
= false;
16697 required_token missing
= RT_NONE
;
16699 /* Look for the `asm' keyword. */
16700 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16702 if (parser
->in_function_body
16703 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
16705 error ("%<asm%> in %<constexpr%> function");
16706 cp_function_chain
->invalid_constexpr
= true;
16709 /* See if the next token is `volatile'. */
16710 if (cp_parser_allow_gnu_extensions_p (parser
)
16711 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16713 /* Remember that we saw the `volatile' keyword. */
16715 /* Consume the token. */
16716 cp_lexer_consume_token (parser
->lexer
);
16718 if (cp_parser_allow_gnu_extensions_p (parser
)
16719 && parser
->in_function_body
16720 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16722 /* Remember that we saw the `goto' keyword. */
16724 /* Consume the token. */
16725 cp_lexer_consume_token (parser
->lexer
);
16727 /* Look for the opening `('. */
16728 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16730 /* Look for the string. */
16731 string
= cp_parser_string_literal (parser
, false, false);
16732 if (string
== error_mark_node
)
16734 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16735 /*consume_paren=*/true);
16739 /* If we're allowing GNU extensions, check for the extended assembly
16740 syntax. Unfortunately, the `:' tokens need not be separated by
16741 a space in C, and so, for compatibility, we tolerate that here
16742 too. Doing that means that we have to treat the `::' operator as
16744 if (cp_parser_allow_gnu_extensions_p (parser
)
16745 && parser
->in_function_body
16746 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16747 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16749 bool inputs_p
= false;
16750 bool clobbers_p
= false;
16751 bool labels_p
= false;
16753 /* The extended syntax was used. */
16756 /* Look for outputs. */
16757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16759 /* Consume the `:'. */
16760 cp_lexer_consume_token (parser
->lexer
);
16761 /* Parse the output-operands. */
16762 if (cp_lexer_next_token_is_not (parser
->lexer
,
16764 && cp_lexer_next_token_is_not (parser
->lexer
,
16766 && cp_lexer_next_token_is_not (parser
->lexer
,
16769 outputs
= cp_parser_asm_operand_list (parser
);
16771 if (outputs
== error_mark_node
)
16772 invalid_outputs_p
= true;
16774 /* If the next token is `::', there are no outputs, and the
16775 next token is the beginning of the inputs. */
16776 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16777 /* The inputs are coming next. */
16780 /* Look for inputs. */
16782 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16784 /* Consume the `:' or `::'. */
16785 cp_lexer_consume_token (parser
->lexer
);
16786 /* Parse the output-operands. */
16787 if (cp_lexer_next_token_is_not (parser
->lexer
,
16789 && cp_lexer_next_token_is_not (parser
->lexer
,
16791 && cp_lexer_next_token_is_not (parser
->lexer
,
16793 inputs
= cp_parser_asm_operand_list (parser
);
16795 if (inputs
== error_mark_node
)
16796 invalid_inputs_p
= true;
16798 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16799 /* The clobbers are coming next. */
16802 /* Look for clobbers. */
16804 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16807 /* Consume the `:' or `::'. */
16808 cp_lexer_consume_token (parser
->lexer
);
16809 /* Parse the clobbers. */
16810 if (cp_lexer_next_token_is_not (parser
->lexer
,
16812 && cp_lexer_next_token_is_not (parser
->lexer
,
16814 clobbers
= cp_parser_asm_clobber_list (parser
);
16817 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16818 /* The labels are coming next. */
16821 /* Look for labels. */
16823 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16826 /* Consume the `:' or `::'. */
16827 cp_lexer_consume_token (parser
->lexer
);
16828 /* Parse the labels. */
16829 labels
= cp_parser_asm_label_list (parser
);
16832 if (goto_p
&& !labels_p
)
16833 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16836 missing
= RT_COLON_SCOPE
;
16838 /* Look for the closing `)'. */
16839 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16840 missing
? missing
: RT_CLOSE_PAREN
))
16841 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16842 /*consume_paren=*/true);
16843 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16845 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16847 /* Create the ASM_EXPR. */
16848 if (parser
->in_function_body
)
16850 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16851 inputs
, clobbers
, labels
);
16852 /* If the extended syntax was not used, mark the ASM_EXPR. */
16855 tree temp
= asm_stmt
;
16856 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16857 temp
= TREE_OPERAND (temp
, 0);
16859 ASM_INPUT_P (temp
) = 1;
16863 symtab
->finalize_toplevel_asm (string
);
16867 /* Declarators [gram.dcl.decl] */
16869 /* Parse an init-declarator.
16872 declarator initializer [opt]
16877 declarator asm-specification [opt] attributes [opt] initializer [opt]
16879 function-definition:
16880 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16882 decl-specifier-seq [opt] declarator function-try-block
16886 function-definition:
16887 __extension__ function-definition
16891 function-definition:
16892 decl-specifier-seq [opt] declarator function-transaction-block
16894 The DECL_SPECIFIERS apply to this declarator. Returns a
16895 representation of the entity declared. If MEMBER_P is TRUE, then
16896 this declarator appears in a class scope. The new DECL created by
16897 this declarator is returned.
16899 The CHECKS are access checks that should be performed once we know
16900 what entity is being declared (and, therefore, what classes have
16903 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16904 for a function-definition here as well. If the declarator is a
16905 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16906 be TRUE upon return. By that point, the function-definition will
16907 have been completely parsed.
16909 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16912 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16913 parsed declaration if it is an uninitialized single declarator not followed
16914 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16915 if present, will not be consumed. If returned, this declarator will be
16916 created with SD_INITIALIZED but will not call cp_finish_decl.
16918 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16919 and there is an initializer, the pointed location_t is set to the
16920 location of the '=' or `(', or '{' in C++11 token introducing the
16924 cp_parser_init_declarator (cp_parser
* parser
,
16925 cp_decl_specifier_seq
*decl_specifiers
,
16926 vec
<deferred_access_check
, va_gc
> *checks
,
16927 bool function_definition_allowed_p
,
16929 int declares_class_or_enum
,
16930 bool* function_definition_p
,
16931 tree
* maybe_range_for_decl
,
16932 location_t
* init_loc
)
16934 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16935 *attributes_start_token
= NULL
;
16936 cp_declarator
*declarator
;
16937 tree prefix_attributes
;
16938 tree attributes
= NULL
;
16939 tree asm_specification
;
16941 tree decl
= NULL_TREE
;
16943 int is_initialized
;
16944 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16945 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16947 enum cpp_ttype initialization_kind
;
16948 bool is_direct_init
= false;
16949 bool is_non_constant_init
;
16950 int ctor_dtor_or_conv_p
;
16951 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
16952 tree pushed_scope
= NULL_TREE
;
16953 bool range_for_decl_p
= false;
16954 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16955 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
16957 /* Gather the attributes that were provided with the
16958 decl-specifiers. */
16959 prefix_attributes
= decl_specifiers
->attributes
;
16961 /* Assume that this is not the declarator for a function
16963 if (function_definition_p
)
16964 *function_definition_p
= false;
16966 /* Default arguments are only permitted for function parameters. */
16967 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16968 parser
->default_arg_ok_p
= false;
16970 /* Defer access checks while parsing the declarator; we cannot know
16971 what names are accessible until we know what is being
16973 resume_deferring_access_checks ();
16975 /* Parse the declarator. */
16976 token
= cp_lexer_peek_token (parser
->lexer
);
16978 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16979 &ctor_dtor_or_conv_p
,
16980 /*parenthesized_p=*/NULL
,
16981 member_p
, friend_p
);
16982 /* Gather up the deferred checks. */
16983 stop_deferring_access_checks ();
16985 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16987 /* If the DECLARATOR was erroneous, there's no need to go
16989 if (declarator
== cp_error_declarator
)
16990 return error_mark_node
;
16992 /* Check that the number of template-parameter-lists is OK. */
16993 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16995 return error_mark_node
;
16997 if (declares_class_or_enum
& 2)
16998 cp_parser_check_for_definition_in_return_type (declarator
,
16999 decl_specifiers
->type
,
17000 decl_specifiers
->locations
[ds_type_spec
]);
17002 /* Figure out what scope the entity declared by the DECLARATOR is
17003 located in. `grokdeclarator' sometimes changes the scope, so
17004 we compute it now. */
17005 scope
= get_scope_of_declarator (declarator
);
17007 /* Perform any lookups in the declared type which were thought to be
17008 dependent, but are not in the scope of the declarator. */
17009 decl_specifiers
->type
17010 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
17012 /* If we're allowing GNU extensions, look for an
17013 asm-specification. */
17014 if (cp_parser_allow_gnu_extensions_p (parser
))
17016 /* Look for an asm-specification. */
17017 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
17018 asm_specification
= cp_parser_asm_specification_opt (parser
);
17021 asm_specification
= NULL_TREE
;
17023 /* Look for attributes. */
17024 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
17025 attributes
= cp_parser_attributes_opt (parser
);
17027 /* Peek at the next token. */
17028 token
= cp_lexer_peek_token (parser
->lexer
);
17030 bool bogus_implicit_tmpl
= false;
17032 if (function_declarator_p (declarator
))
17034 /* Check to see if the token indicates the start of a
17035 function-definition. */
17036 if (cp_parser_token_starts_function_definition_p (token
))
17038 if (!function_definition_allowed_p
)
17040 /* If a function-definition should not appear here, issue an
17042 cp_parser_error (parser
,
17043 "a function-definition is not allowed here");
17044 return error_mark_node
;
17047 location_t func_brace_location
17048 = cp_lexer_peek_token (parser
->lexer
)->location
;
17050 /* Neither attributes nor an asm-specification are allowed
17051 on a function-definition. */
17052 if (asm_specification
)
17053 error_at (asm_spec_start_token
->location
,
17054 "an asm-specification is not allowed "
17055 "on a function-definition");
17057 error_at (attributes_start_token
->location
,
17058 "attributes are not allowed "
17059 "on a function-definition");
17060 /* This is a function-definition. */
17061 *function_definition_p
= true;
17063 /* Parse the function definition. */
17065 decl
= cp_parser_save_member_function_body (parser
,
17068 prefix_attributes
);
17071 (cp_parser_function_definition_from_specifiers_and_declarator
17072 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
17074 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
17076 /* This is where the prologue starts... */
17077 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
17078 = func_brace_location
;
17084 else if (parser
->fully_implicit_function_template_p
)
17086 /* A non-template declaration involving a function parameter list
17087 containing an implicit template parameter will be made into a
17088 template. If the resulting declaration is not going to be an
17089 actual function then finish the template scope here to prevent it.
17090 An error message will be issued once we have a decl to talk about.
17092 FIXME probably we should do type deduction rather than create an
17093 implicit template, but the standard currently doesn't allow it. */
17094 bogus_implicit_tmpl
= true;
17095 finish_fully_implicit_template (parser
, NULL_TREE
);
17100 Only in function declarations for constructors, destructors, and
17101 type conversions can the decl-specifier-seq be omitted.
17103 We explicitly postpone this check past the point where we handle
17104 function-definitions because we tolerate function-definitions
17105 that are missing their return types in some modes. */
17106 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
17108 cp_parser_error (parser
,
17109 "expected constructor, destructor, or type conversion");
17110 return error_mark_node
;
17113 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17114 if (token
->type
== CPP_EQ
17115 || token
->type
== CPP_OPEN_PAREN
17116 || token
->type
== CPP_OPEN_BRACE
)
17118 is_initialized
= SD_INITIALIZED
;
17119 initialization_kind
= token
->type
;
17120 if (maybe_range_for_decl
)
17121 *maybe_range_for_decl
= error_mark_node
;
17122 tmp_init_loc
= token
->location
;
17123 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
17124 *init_loc
= tmp_init_loc
;
17126 if (token
->type
== CPP_EQ
17127 && function_declarator_p (declarator
))
17129 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17130 if (t2
->keyword
== RID_DEFAULT
)
17131 is_initialized
= SD_DEFAULTED
;
17132 else if (t2
->keyword
== RID_DELETE
)
17133 is_initialized
= SD_DELETED
;
17138 /* If the init-declarator isn't initialized and isn't followed by a
17139 `,' or `;', it's not a valid init-declarator. */
17140 if (token
->type
!= CPP_COMMA
17141 && token
->type
!= CPP_SEMICOLON
)
17143 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
17144 range_for_decl_p
= true;
17147 if (!maybe_range_for_decl
)
17148 cp_parser_error (parser
, "expected initializer");
17149 return error_mark_node
;
17152 is_initialized
= SD_UNINITIALIZED
;
17153 initialization_kind
= CPP_EOF
;
17156 /* Because start_decl has side-effects, we should only call it if we
17157 know we're going ahead. By this point, we know that we cannot
17158 possibly be looking at any other construct. */
17159 cp_parser_commit_to_tentative_parse (parser
);
17161 /* Enter the newly declared entry in the symbol table. If we're
17162 processing a declaration in a class-specifier, we wait until
17163 after processing the initializer. */
17166 if (parser
->in_unbraced_linkage_specification_p
)
17167 decl_specifiers
->storage_class
= sc_extern
;
17168 decl
= start_decl (declarator
, decl_specifiers
,
17169 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
17170 attributes
, prefix_attributes
, &pushed_scope
);
17171 cp_finalize_omp_declare_simd (parser
, decl
);
17172 /* Adjust location of decl if declarator->id_loc is more appropriate:
17173 set, and decl wasn't merged with another decl, in which case its
17174 location would be different from input_location, and more accurate. */
17176 && declarator
->id_loc
!= UNKNOWN_LOCATION
17177 && DECL_SOURCE_LOCATION (decl
) == input_location
)
17178 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
17181 /* Enter the SCOPE. That way unqualified names appearing in the
17182 initializer will be looked up in SCOPE. */
17183 pushed_scope
= push_scope (scope
);
17185 /* Perform deferred access control checks, now that we know in which
17186 SCOPE the declared entity resides. */
17187 if (!member_p
&& decl
)
17189 tree saved_current_function_decl
= NULL_TREE
;
17191 /* If the entity being declared is a function, pretend that we
17192 are in its scope. If it is a `friend', it may have access to
17193 things that would not otherwise be accessible. */
17194 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17196 saved_current_function_decl
= current_function_decl
;
17197 current_function_decl
= decl
;
17200 /* Perform access checks for template parameters. */
17201 cp_parser_perform_template_parameter_access_checks (checks
);
17203 /* Perform the access control checks for the declarator and the
17204 decl-specifiers. */
17205 perform_deferred_access_checks (tf_warning_or_error
);
17207 /* Restore the saved value. */
17208 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17209 current_function_decl
= saved_current_function_decl
;
17212 /* Parse the initializer. */
17213 initializer
= NULL_TREE
;
17214 is_direct_init
= false;
17215 is_non_constant_init
= true;
17216 if (is_initialized
)
17218 if (function_declarator_p (declarator
))
17220 if (initialization_kind
== CPP_EQ
)
17221 initializer
= cp_parser_pure_specifier (parser
);
17224 /* If the declaration was erroneous, we don't really
17225 know what the user intended, so just silently
17226 consume the initializer. */
17227 if (decl
!= error_mark_node
)
17228 error_at (tmp_init_loc
, "initializer provided for function");
17229 cp_parser_skip_to_closing_parenthesis (parser
,
17230 /*recovering=*/true,
17231 /*or_comma=*/false,
17232 /*consume_paren=*/true);
17237 /* We want to record the extra mangling scope for in-class
17238 initializers of class members and initializers of static data
17239 member templates. The former involves deferring
17240 parsing of the initializer until end of class as with default
17241 arguments. So right here we only handle the latter. */
17242 if (!member_p
&& processing_template_decl
)
17243 start_lambda_scope (decl
);
17244 initializer
= cp_parser_initializer (parser
,
17246 &is_non_constant_init
);
17247 if (!member_p
&& processing_template_decl
)
17248 finish_lambda_scope ();
17249 if (initializer
== error_mark_node
)
17250 cp_parser_skip_to_end_of_statement (parser
);
17254 /* The old parser allows attributes to appear after a parenthesized
17255 initializer. Mark Mitchell proposed removing this functionality
17256 on the GCC mailing lists on 2002-08-13. This parser accepts the
17257 attributes -- but ignores them. */
17258 if (cp_parser_allow_gnu_extensions_p (parser
)
17259 && initialization_kind
== CPP_OPEN_PAREN
)
17260 if (cp_parser_attributes_opt (parser
))
17261 warning (OPT_Wattributes
,
17262 "attributes after parenthesized initializer ignored");
17264 /* And now complain about a non-function implicit template. */
17265 if (bogus_implicit_tmpl
)
17266 error_at (DECL_SOURCE_LOCATION (decl
),
17267 "non-function %qD declared as implicit template", decl
);
17269 /* For an in-class declaration, use `grokfield' to create the
17275 pop_scope (pushed_scope
);
17276 pushed_scope
= NULL_TREE
;
17278 decl
= grokfield (declarator
, decl_specifiers
,
17279 initializer
, !is_non_constant_init
,
17280 /*asmspec=*/NULL_TREE
,
17281 chainon (attributes
, prefix_attributes
));
17282 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17283 cp_parser_save_default_args (parser
, decl
);
17284 cp_finalize_omp_declare_simd (parser
, decl
);
17287 /* Finish processing the declaration. But, skip member
17289 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
17291 cp_finish_decl (decl
,
17292 initializer
, !is_non_constant_init
,
17294 /* If the initializer is in parentheses, then this is
17295 a direct-initialization, which means that an
17296 `explicit' constructor is OK. Otherwise, an
17297 `explicit' constructor cannot be used. */
17298 ((is_direct_init
|| !is_initialized
)
17299 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
17301 else if ((cxx_dialect
!= cxx98
) && friend_p
17302 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17303 /* Core issue #226 (C++0x only): A default template-argument
17304 shall not be specified in a friend class template
17306 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
17307 /*is_partial=*/false, /*is_friend_decl=*/1);
17309 if (!friend_p
&& pushed_scope
)
17310 pop_scope (pushed_scope
);
17312 if (function_declarator_p (declarator
)
17313 && parser
->fully_implicit_function_template_p
)
17316 decl
= finish_fully_implicit_template (parser
, decl
);
17318 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
17324 /* Parse a declarator.
17328 ptr-operator declarator
17330 abstract-declarator:
17331 ptr-operator abstract-declarator [opt]
17332 direct-abstract-declarator
17337 attributes [opt] direct-declarator
17338 attributes [opt] ptr-operator declarator
17340 abstract-declarator:
17341 attributes [opt] ptr-operator abstract-declarator [opt]
17342 attributes [opt] direct-abstract-declarator
17344 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17345 detect constructor, destructor or conversion operators. It is set
17346 to -1 if the declarator is a name, and +1 if it is a
17347 function. Otherwise it is set to zero. Usually you just want to
17348 test for >0, but internally the negative value is used.
17350 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17351 a decl-specifier-seq unless it declares a constructor, destructor,
17352 or conversion. It might seem that we could check this condition in
17353 semantic analysis, rather than parsing, but that makes it difficult
17354 to handle something like `f()'. We want to notice that there are
17355 no decl-specifiers, and therefore realize that this is an
17356 expression, not a declaration.)
17358 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17359 the declarator is a direct-declarator of the form "(...)".
17361 MEMBER_P is true iff this declarator is a member-declarator.
17363 FRIEND_P is true iff this declarator is a friend. */
17365 static cp_declarator
*
17366 cp_parser_declarator (cp_parser
* parser
,
17367 cp_parser_declarator_kind dcl_kind
,
17368 int* ctor_dtor_or_conv_p
,
17369 bool* parenthesized_p
,
17370 bool member_p
, bool friend_p
)
17372 cp_declarator
*declarator
;
17373 enum tree_code code
;
17374 cp_cv_quals cv_quals
;
17376 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
17378 /* Assume this is not a constructor, destructor, or type-conversion
17380 if (ctor_dtor_or_conv_p
)
17381 *ctor_dtor_or_conv_p
= 0;
17383 if (cp_parser_allow_gnu_extensions_p (parser
))
17384 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
17386 /* Check for the ptr-operator production. */
17387 cp_parser_parse_tentatively (parser
);
17388 /* Parse the ptr-operator. */
17389 code
= cp_parser_ptr_operator (parser
,
17394 /* If that worked, then we have a ptr-operator. */
17395 if (cp_parser_parse_definitely (parser
))
17397 /* If a ptr-operator was found, then this declarator was not
17399 if (parenthesized_p
)
17400 *parenthesized_p
= true;
17401 /* The dependent declarator is optional if we are parsing an
17402 abstract-declarator. */
17403 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17404 cp_parser_parse_tentatively (parser
);
17406 /* Parse the dependent declarator. */
17407 declarator
= cp_parser_declarator (parser
, dcl_kind
,
17408 /*ctor_dtor_or_conv_p=*/NULL
,
17409 /*parenthesized_p=*/NULL
,
17410 /*member_p=*/false,
17413 /* If we are parsing an abstract-declarator, we must handle the
17414 case where the dependent declarator is absent. */
17415 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
17416 && !cp_parser_parse_definitely (parser
))
17419 declarator
= cp_parser_make_indirect_declarator
17420 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
17422 /* Everything else is a direct-declarator. */
17425 if (parenthesized_p
)
17426 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
17428 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
17429 ctor_dtor_or_conv_p
,
17430 member_p
, friend_p
);
17433 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
17434 declarator
->attributes
= gnu_attributes
;
17438 /* Parse a direct-declarator or direct-abstract-declarator.
17442 direct-declarator ( parameter-declaration-clause )
17443 cv-qualifier-seq [opt]
17444 ref-qualifier [opt]
17445 exception-specification [opt]
17446 direct-declarator [ constant-expression [opt] ]
17449 direct-abstract-declarator:
17450 direct-abstract-declarator [opt]
17451 ( parameter-declaration-clause )
17452 cv-qualifier-seq [opt]
17453 ref-qualifier [opt]
17454 exception-specification [opt]
17455 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17456 ( abstract-declarator )
17458 Returns a representation of the declarator. DCL_KIND is
17459 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17460 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17461 we are parsing a direct-declarator. It is
17462 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17463 of ambiguity we prefer an abstract declarator, as per
17464 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17465 as for cp_parser_declarator. */
17467 static cp_declarator
*
17468 cp_parser_direct_declarator (cp_parser
* parser
,
17469 cp_parser_declarator_kind dcl_kind
,
17470 int* ctor_dtor_or_conv_p
,
17471 bool member_p
, bool friend_p
)
17474 cp_declarator
*declarator
= NULL
;
17475 tree scope
= NULL_TREE
;
17476 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17477 bool saved_in_declarator_p
= parser
->in_declarator_p
;
17479 tree pushed_scope
= NULL_TREE
;
17483 /* Peek at the next token. */
17484 token
= cp_lexer_peek_token (parser
->lexer
);
17485 if (token
->type
== CPP_OPEN_PAREN
)
17487 /* This is either a parameter-declaration-clause, or a
17488 parenthesized declarator. When we know we are parsing a
17489 named declarator, it must be a parenthesized declarator
17490 if FIRST is true. For instance, `(int)' is a
17491 parameter-declaration-clause, with an omitted
17492 direct-abstract-declarator. But `((*))', is a
17493 parenthesized abstract declarator. Finally, when T is a
17494 template parameter `(T)' is a
17495 parameter-declaration-clause, and not a parenthesized
17498 We first try and parse a parameter-declaration-clause,
17499 and then try a nested declarator (if FIRST is true).
17501 It is not an error for it not to be a
17502 parameter-declaration-clause, even when FIRST is
17508 The first is the declaration of a function while the
17509 second is the definition of a variable, including its
17512 Having seen only the parenthesis, we cannot know which of
17513 these two alternatives should be selected. Even more
17514 complex are examples like:
17519 The former is a function-declaration; the latter is a
17520 variable initialization.
17522 Thus again, we try a parameter-declaration-clause, and if
17523 that fails, we back out and return. */
17525 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17528 bool is_declarator
= false;
17530 /* In a member-declarator, the only valid interpretation
17531 of a parenthesis is the start of a
17532 parameter-declaration-clause. (It is invalid to
17533 initialize a static data member with a parenthesized
17534 initializer; only the "=" form of initialization is
17537 cp_parser_parse_tentatively (parser
);
17539 /* Consume the `('. */
17540 cp_lexer_consume_token (parser
->lexer
);
17543 /* If this is going to be an abstract declarator, we're
17544 in a declarator and we can't have default args. */
17545 parser
->default_arg_ok_p
= false;
17546 parser
->in_declarator_p
= true;
17549 begin_scope (sk_function_parms
, NULL_TREE
);
17551 /* Parse the parameter-declaration-clause. */
17552 params
= cp_parser_parameter_declaration_clause (parser
);
17554 /* Consume the `)'. */
17555 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17557 /* If all went well, parse the cv-qualifier-seq,
17558 ref-qualifier and the exception-specification. */
17559 if (member_p
|| cp_parser_parse_definitely (parser
))
17561 cp_cv_quals cv_quals
;
17562 cp_virt_specifiers virt_specifiers
;
17563 cp_ref_qualifier ref_qual
;
17564 tree exception_specification
;
17567 bool memfn
= (member_p
|| (pushed_scope
17568 && CLASS_TYPE_P (pushed_scope
)));
17570 is_declarator
= true;
17572 if (ctor_dtor_or_conv_p
)
17573 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17576 /* Parse the cv-qualifier-seq. */
17577 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17578 /* Parse the ref-qualifier. */
17579 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17580 /* And the exception-specification. */
17581 exception_specification
17582 = cp_parser_exception_specification_opt (parser
);
17584 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17586 /* In here, we handle cases where attribute is used after
17587 the function declaration. For example:
17588 void func (int x) __attribute__((vector(..))); */
17590 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
17592 cp_parser_parse_tentatively (parser
);
17593 tree attr
= cp_parser_gnu_attributes_opt (parser
);
17594 if (cp_lexer_next_token_is_not (parser
->lexer
,
17596 && cp_lexer_next_token_is_not (parser
->lexer
,
17598 cp_parser_abort_tentative_parse (parser
);
17599 else if (!cp_parser_parse_definitely (parser
))
17602 attrs
= chainon (attr
, attrs
);
17604 late_return
= (cp_parser_late_return_type_opt
17605 (parser
, declarator
,
17606 memfn
? cv_quals
: -1));
17609 /* Parse the virt-specifier-seq. */
17610 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17612 /* Create the function-declarator. */
17613 declarator
= make_call_declarator (declarator
,
17618 exception_specification
,
17620 declarator
->std_attributes
= attrs
;
17621 /* Any subsequent parameter lists are to do with
17622 return type, so are not those of the declared
17624 parser
->default_arg_ok_p
= false;
17627 /* Remove the function parms from scope. */
17628 pop_bindings_and_leave_scope ();
17631 /* Repeat the main loop. */
17635 /* If this is the first, we can try a parenthesized
17639 bool saved_in_type_id_in_expr_p
;
17641 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17642 parser
->in_declarator_p
= saved_in_declarator_p
;
17644 /* Consume the `('. */
17645 cp_lexer_consume_token (parser
->lexer
);
17646 /* Parse the nested declarator. */
17647 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17648 parser
->in_type_id_in_expr_p
= true;
17650 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17651 /*parenthesized_p=*/NULL
,
17652 member_p
, friend_p
);
17653 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17655 /* Expect a `)'. */
17656 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17657 declarator
= cp_error_declarator
;
17658 if (declarator
== cp_error_declarator
)
17661 goto handle_declarator
;
17663 /* Otherwise, we must be done. */
17667 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17668 && token
->type
== CPP_OPEN_SQUARE
17669 && !cp_next_tokens_can_be_attribute_p (parser
))
17671 /* Parse an array-declarator. */
17672 tree bounds
, attrs
;
17674 if (ctor_dtor_or_conv_p
)
17675 *ctor_dtor_or_conv_p
= 0;
17678 parser
->default_arg_ok_p
= false;
17679 parser
->in_declarator_p
= true;
17680 /* Consume the `['. */
17681 cp_lexer_consume_token (parser
->lexer
);
17682 /* Peek at the next token. */
17683 token
= cp_lexer_peek_token (parser
->lexer
);
17684 /* If the next token is `]', then there is no
17685 constant-expression. */
17686 if (token
->type
!= CPP_CLOSE_SQUARE
)
17688 bool non_constant_p
;
17690 = cp_parser_constant_expression (parser
,
17691 /*allow_non_constant=*/true,
17693 if (!non_constant_p
)
17695 else if (error_operand_p (bounds
))
17696 /* Already gave an error. */;
17697 else if (!parser
->in_function_body
17698 || current_binding_level
->kind
== sk_function_parms
)
17700 /* Normally, the array bound must be an integral constant
17701 expression. However, as an extension, we allow VLAs
17702 in function scopes as long as they aren't part of a
17703 parameter declaration. */
17704 cp_parser_error (parser
,
17705 "array bound is not an integer constant");
17706 bounds
= error_mark_node
;
17708 else if (processing_template_decl
17709 && !type_dependent_expression_p (bounds
))
17711 /* Remember this wasn't a constant-expression. */
17712 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17713 TREE_SIDE_EFFECTS (bounds
) = 1;
17717 bounds
= NULL_TREE
;
17718 /* Look for the closing `]'. */
17719 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17721 declarator
= cp_error_declarator
;
17725 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17726 declarator
= make_array_declarator (declarator
, bounds
);
17727 declarator
->std_attributes
= attrs
;
17729 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17732 tree qualifying_scope
;
17733 tree unqualified_name
;
17735 special_function_kind sfk
;
17737 bool pack_expansion_p
= false;
17738 cp_token
*declarator_id_start_token
;
17740 /* Parse a declarator-id */
17741 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17744 cp_parser_parse_tentatively (parser
);
17746 /* If we see an ellipsis, we should be looking at a
17748 if (token
->type
== CPP_ELLIPSIS
)
17750 /* Consume the `...' */
17751 cp_lexer_consume_token (parser
->lexer
);
17753 pack_expansion_p
= true;
17757 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17759 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17760 qualifying_scope
= parser
->scope
;
17765 if (!unqualified_name
&& pack_expansion_p
)
17767 /* Check whether an error occurred. */
17768 okay
= !cp_parser_error_occurred (parser
);
17770 /* We already consumed the ellipsis to mark a
17771 parameter pack, but we have no way to report it,
17772 so abort the tentative parse. We will be exiting
17773 immediately anyway. */
17774 cp_parser_abort_tentative_parse (parser
);
17777 okay
= cp_parser_parse_definitely (parser
);
17780 unqualified_name
= error_mark_node
;
17781 else if (unqualified_name
17782 && (qualifying_scope
17783 || (!identifier_p (unqualified_name
))))
17785 cp_parser_error (parser
, "expected unqualified-id");
17786 unqualified_name
= error_mark_node
;
17790 if (!unqualified_name
)
17792 if (unqualified_name
== error_mark_node
)
17794 declarator
= cp_error_declarator
;
17795 pack_expansion_p
= false;
17796 declarator
->parameter_pack_p
= false;
17800 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17802 if (qualifying_scope
&& at_namespace_scope_p ()
17803 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17805 /* In the declaration of a member of a template class
17806 outside of the class itself, the SCOPE will sometimes
17807 be a TYPENAME_TYPE. For example, given:
17809 template <typename T>
17810 int S<T>::R::i = 3;
17812 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17813 this context, we must resolve S<T>::R to an ordinary
17814 type, rather than a typename type.
17816 The reason we normally avoid resolving TYPENAME_TYPEs
17817 is that a specialization of `S' might render
17818 `S<T>::R' not a type. However, if `S' is
17819 specialized, then this `i' will not be used, so there
17820 is no harm in resolving the types here. */
17823 /* Resolve the TYPENAME_TYPE. */
17824 type
= resolve_typename_type (qualifying_scope
,
17825 /*only_current_p=*/false);
17826 /* If that failed, the declarator is invalid. */
17827 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17829 if (typedef_variant_p (type
))
17830 error_at (declarator_id_start_token
->location
,
17831 "cannot define member of dependent typedef "
17834 error_at (declarator_id_start_token
->location
,
17835 "%<%T::%E%> is not a type",
17836 TYPE_CONTEXT (qualifying_scope
),
17837 TYPE_IDENTIFIER (qualifying_scope
));
17839 qualifying_scope
= type
;
17844 if (unqualified_name
)
17848 if (qualifying_scope
17849 && CLASS_TYPE_P (qualifying_scope
))
17850 class_type
= qualifying_scope
;
17852 class_type
= current_class_type
;
17854 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17856 tree name_type
= TREE_TYPE (unqualified_name
);
17857 if (class_type
&& same_type_p (name_type
, class_type
))
17859 if (qualifying_scope
17860 && CLASSTYPE_USE_TEMPLATE (name_type
))
17862 error_at (declarator_id_start_token
->location
,
17863 "invalid use of constructor as a template");
17864 inform (declarator_id_start_token
->location
,
17865 "use %<%T::%D%> instead of %<%T::%D%> to "
17866 "name the constructor in a qualified name",
17868 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17869 class_type
, name_type
);
17870 declarator
= cp_error_declarator
;
17874 unqualified_name
= constructor_name (class_type
);
17878 /* We do not attempt to print the declarator
17879 here because we do not have enough
17880 information about its original syntactic
17882 cp_parser_error (parser
, "invalid declarator");
17883 declarator
= cp_error_declarator
;
17890 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17891 sfk
= sfk_destructor
;
17892 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17893 sfk
= sfk_conversion
;
17894 else if (/* There's no way to declare a constructor
17895 for an anonymous type, even if the type
17896 got a name for linkage purposes. */
17897 !TYPE_WAS_ANONYMOUS (class_type
)
17898 /* Handle correctly (c++/19200):
17912 friend void N::S();
17915 && class_type
!= qualifying_scope
)
17916 && constructor_name_p (unqualified_name
,
17919 unqualified_name
= constructor_name (class_type
);
17920 sfk
= sfk_constructor
;
17922 else if (is_overloaded_fn (unqualified_name
)
17923 && DECL_CONSTRUCTOR_P (get_first_fn
17924 (unqualified_name
)))
17925 sfk
= sfk_constructor
;
17927 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17928 *ctor_dtor_or_conv_p
= -1;
17931 declarator
= make_id_declarator (qualifying_scope
,
17934 declarator
->std_attributes
= attrs
;
17935 declarator
->id_loc
= token
->location
;
17936 declarator
->parameter_pack_p
= pack_expansion_p
;
17938 if (pack_expansion_p
)
17939 maybe_warn_variadic_templates ();
17942 handle_declarator
:;
17943 scope
= get_scope_of_declarator (declarator
);
17946 /* Any names that appear after the declarator-id for a
17947 member are looked up in the containing scope. */
17948 if (at_function_scope_p ())
17950 /* But declarations with qualified-ids can't appear in a
17952 cp_parser_error (parser
, "qualified-id in declaration");
17953 declarator
= cp_error_declarator
;
17956 pushed_scope
= push_scope (scope
);
17958 parser
->in_declarator_p
= true;
17959 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17960 || (declarator
&& declarator
->kind
== cdk_id
))
17961 /* Default args are only allowed on function
17963 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17965 parser
->default_arg_ok_p
= false;
17974 /* For an abstract declarator, we might wind up with nothing at this
17975 point. That's an error; the declarator is not optional. */
17977 cp_parser_error (parser
, "expected declarator");
17979 /* If we entered a scope, we must exit it now. */
17981 pop_scope (pushed_scope
);
17983 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17984 parser
->in_declarator_p
= saved_in_declarator_p
;
17989 /* Parse a ptr-operator.
17992 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17993 * cv-qualifier-seq [opt]
17995 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17996 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18001 & cv-qualifier-seq [opt]
18003 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18004 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18005 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18006 filled in with the TYPE containing the member. *CV_QUALS is
18007 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18008 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18009 Note that the tree codes returned by this function have nothing
18010 to do with the types of trees that will be eventually be created
18011 to represent the pointer or reference type being parsed. They are
18012 just constants with suggestive names. */
18013 static enum tree_code
18014 cp_parser_ptr_operator (cp_parser
* parser
,
18016 cp_cv_quals
*cv_quals
,
18019 enum tree_code code
= ERROR_MARK
;
18021 tree attrs
= NULL_TREE
;
18023 /* Assume that it's not a pointer-to-member. */
18025 /* And that there are no cv-qualifiers. */
18026 *cv_quals
= TYPE_UNQUALIFIED
;
18028 /* Peek at the next token. */
18029 token
= cp_lexer_peek_token (parser
->lexer
);
18031 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18032 if (token
->type
== CPP_MULT
)
18033 code
= INDIRECT_REF
;
18034 else if (token
->type
== CPP_AND
)
18036 else if ((cxx_dialect
!= cxx98
) &&
18037 token
->type
== CPP_AND_AND
) /* C++0x only */
18038 code
= NON_LVALUE_EXPR
;
18040 if (code
!= ERROR_MARK
)
18042 /* Consume the `*', `&' or `&&'. */
18043 cp_lexer_consume_token (parser
->lexer
);
18045 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18046 `&', if we are allowing GNU extensions. (The only qualifier
18047 that can legally appear after `&' is `restrict', but that is
18048 enforced during semantic analysis. */
18049 if (code
== INDIRECT_REF
18050 || cp_parser_allow_gnu_extensions_p (parser
))
18051 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18053 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18054 if (attributes
!= NULL
)
18055 *attributes
= attrs
;
18059 /* Try the pointer-to-member case. */
18060 cp_parser_parse_tentatively (parser
);
18061 /* Look for the optional `::' operator. */
18062 cp_parser_global_scope_opt (parser
,
18063 /*current_scope_valid_p=*/false);
18064 /* Look for the nested-name specifier. */
18065 token
= cp_lexer_peek_token (parser
->lexer
);
18066 cp_parser_nested_name_specifier (parser
,
18067 /*typename_keyword_p=*/false,
18068 /*check_dependency_p=*/true,
18070 /*is_declaration=*/false);
18071 /* If we found it, and the next token is a `*', then we are
18072 indeed looking at a pointer-to-member operator. */
18073 if (!cp_parser_error_occurred (parser
)
18074 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
18076 /* Indicate that the `*' operator was used. */
18077 code
= INDIRECT_REF
;
18079 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18080 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
18081 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
18082 error_at (token
->location
, "cannot form pointer to member of "
18083 "non-class %q#T", parser
->scope
);
18086 /* The type of which the member is a member is given by the
18088 *type
= parser
->scope
;
18089 /* The next name will not be qualified. */
18090 parser
->scope
= NULL_TREE
;
18091 parser
->qualifying_scope
= NULL_TREE
;
18092 parser
->object_scope
= NULL_TREE
;
18093 /* Look for optional c++11 attributes. */
18094 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18095 if (attributes
!= NULL
)
18096 *attributes
= attrs
;
18097 /* Look for the optional cv-qualifier-seq. */
18098 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18101 /* If that didn't work we don't have a ptr-operator. */
18102 if (!cp_parser_parse_definitely (parser
))
18103 cp_parser_error (parser
, "expected ptr-operator");
18109 /* Parse an (optional) cv-qualifier-seq.
18112 cv-qualifier cv-qualifier-seq [opt]
18123 Returns a bitmask representing the cv-qualifiers. */
18126 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
18128 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
18133 cp_cv_quals cv_qualifier
;
18135 /* Peek at the next token. */
18136 token
= cp_lexer_peek_token (parser
->lexer
);
18137 /* See if it's a cv-qualifier. */
18138 switch (token
->keyword
)
18141 cv_qualifier
= TYPE_QUAL_CONST
;
18145 cv_qualifier
= TYPE_QUAL_VOLATILE
;
18149 cv_qualifier
= TYPE_QUAL_RESTRICT
;
18153 cv_qualifier
= TYPE_UNQUALIFIED
;
18160 if (cv_quals
& cv_qualifier
)
18162 error_at (token
->location
, "duplicate cv-qualifier");
18163 cp_lexer_purge_token (parser
->lexer
);
18167 cp_lexer_consume_token (parser
->lexer
);
18168 cv_quals
|= cv_qualifier
;
18175 /* Parse an (optional) ref-qualifier
18181 Returns cp_ref_qualifier representing ref-qualifier. */
18183 static cp_ref_qualifier
18184 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
18186 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
18188 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18189 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
18194 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
18195 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18197 switch (token
->type
)
18200 curr_ref_qual
= REF_QUAL_LVALUE
;
18204 curr_ref_qual
= REF_QUAL_RVALUE
;
18208 curr_ref_qual
= REF_QUAL_NONE
;
18212 if (!curr_ref_qual
)
18216 error_at (token
->location
, "multiple ref-qualifiers");
18217 cp_lexer_purge_token (parser
->lexer
);
18221 ref_qual
= curr_ref_qual
;
18222 cp_lexer_consume_token (parser
->lexer
);
18229 /* Parse an (optional) virt-specifier-seq.
18231 virt-specifier-seq:
18232 virt-specifier virt-specifier-seq [opt]
18238 Returns a bitmask representing the virt-specifiers. */
18240 static cp_virt_specifiers
18241 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
18243 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18248 cp_virt_specifiers virt_specifier
;
18250 /* Peek at the next token. */
18251 token
= cp_lexer_peek_token (parser
->lexer
);
18252 /* See if it's a virt-specifier-qualifier. */
18253 if (token
->type
!= CPP_NAME
)
18255 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
18257 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18258 virt_specifier
= VIRT_SPEC_OVERRIDE
;
18260 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
18262 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18263 virt_specifier
= VIRT_SPEC_FINAL
;
18265 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
18267 virt_specifier
= VIRT_SPEC_FINAL
;
18272 if (virt_specifiers
& virt_specifier
)
18274 error_at (token
->location
, "duplicate virt-specifier");
18275 cp_lexer_purge_token (parser
->lexer
);
18279 cp_lexer_consume_token (parser
->lexer
);
18280 virt_specifiers
|= virt_specifier
;
18283 return virt_specifiers
;
18286 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18287 is in scope even though it isn't real. */
18290 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
18294 if (current_class_ptr
)
18296 /* We don't clear this between NSDMIs. Is it already what we want? */
18297 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
18298 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
18299 && cp_type_quals (type
) == quals
)
18303 this_parm
= build_this_parm (ctype
, quals
);
18304 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18305 current_class_ptr
= NULL_TREE
;
18307 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
18308 current_class_ptr
= this_parm
;
18311 /* Return true iff our current scope is a non-static data member
18315 parsing_nsdmi (void)
18317 /* We recognize NSDMI context by the context-less 'this' pointer set up
18318 by the function above. */
18319 if (current_class_ptr
18320 && TREE_CODE (current_class_ptr
) == PARM_DECL
18321 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
18326 /* Parse a late-specified return type, if any. This is not a separate
18327 non-terminal, but part of a function declarator, which looks like
18329 -> trailing-type-specifier-seq abstract-declarator(opt)
18331 Returns the type indicated by the type-id.
18333 In addition to this this parses any queued up omp declare simd
18334 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18336 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18340 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
18344 tree type
= NULL_TREE
;
18345 bool declare_simd_p
= (parser
->omp_declare_simd
18347 && declarator
->kind
== cdk_id
);
18349 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
18350 && declarator
&& declarator
->kind
== cdk_id
);
18352 /* Peek at the next token. */
18353 token
= cp_lexer_peek_token (parser
->lexer
);
18354 /* A late-specified return type is indicated by an initial '->'. */
18355 if (token
->type
!= CPP_DEREF
&& !(declare_simd_p
|| cilk_simd_fn_vector_p
))
18358 tree save_ccp
= current_class_ptr
;
18359 tree save_ccr
= current_class_ref
;
18362 /* DR 1207: 'this' is in scope in the trailing return type. */
18363 inject_this_parameter (current_class_type
, quals
);
18366 if (token
->type
== CPP_DEREF
)
18368 /* Consume the ->. */
18369 cp_lexer_consume_token (parser
->lexer
);
18371 type
= cp_parser_trailing_type_id (parser
);
18374 if (cilk_simd_fn_vector_p
)
18375 declarator
->std_attributes
18376 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
18377 declarator
->std_attributes
);
18378 if (declare_simd_p
)
18379 declarator
->std_attributes
18380 = cp_parser_late_parsing_omp_declare_simd (parser
,
18381 declarator
->std_attributes
);
18385 current_class_ptr
= save_ccp
;
18386 current_class_ref
= save_ccr
;
18392 /* Parse a declarator-id.
18396 :: [opt] nested-name-specifier [opt] type-name
18398 In the `id-expression' case, the value returned is as for
18399 cp_parser_id_expression if the id-expression was an unqualified-id.
18400 If the id-expression was a qualified-id, then a SCOPE_REF is
18401 returned. The first operand is the scope (either a NAMESPACE_DECL
18402 or TREE_TYPE), but the second is still just a representation of an
18406 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
18409 /* The expression must be an id-expression. Assume that qualified
18410 names are the names of types so that:
18413 int S<T>::R::i = 3;
18415 will work; we must treat `S<T>::R' as the name of a type.
18416 Similarly, assume that qualified names are templates, where
18420 int S<T>::R<T>::i = 3;
18423 id
= cp_parser_id_expression (parser
,
18424 /*template_keyword_p=*/false,
18425 /*check_dependency_p=*/false,
18426 /*template_p=*/NULL
,
18427 /*declarator_p=*/true,
18429 if (id
&& BASELINK_P (id
))
18430 id
= BASELINK_FUNCTIONS (id
);
18434 /* Parse a type-id.
18437 type-specifier-seq abstract-declarator [opt]
18439 Returns the TYPE specified. */
18442 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
18443 bool is_trailing_return
)
18445 cp_decl_specifier_seq type_specifier_seq
;
18446 cp_declarator
*abstract_declarator
;
18448 /* Parse the type-specifier-seq. */
18449 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18450 is_trailing_return
,
18451 &type_specifier_seq
);
18452 if (type_specifier_seq
.type
== error_mark_node
)
18453 return error_mark_node
;
18455 /* There might or might not be an abstract declarator. */
18456 cp_parser_parse_tentatively (parser
);
18457 /* Look for the declarator. */
18458 abstract_declarator
18459 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
18460 /*parenthesized_p=*/NULL
,
18461 /*member_p=*/false,
18462 /*friend_p=*/false);
18463 /* Check to see if there really was a declarator. */
18464 if (!cp_parser_parse_definitely (parser
))
18465 abstract_declarator
= NULL
;
18467 if (type_specifier_seq
.type
18468 /* None of the valid uses of 'auto' in C++14 involve the type-id
18469 nonterminal, but it is valid in a trailing-return-type. */
18470 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
18471 && type_uses_auto (type_specifier_seq
.type
))
18473 /* A type-id with type 'auto' is only ok if the abstract declarator
18474 is a function declarator with a late-specified return type. */
18475 if (abstract_declarator
18476 && abstract_declarator
->kind
== cdk_function
18477 && abstract_declarator
->u
.function
.late_return_type
)
18481 error ("invalid use of %<auto%>");
18482 return error_mark_node
;
18486 return groktypename (&type_specifier_seq
, abstract_declarator
,
18490 static tree
cp_parser_type_id (cp_parser
*parser
)
18492 return cp_parser_type_id_1 (parser
, false, false);
18495 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
18498 const char *saved_message
= parser
->type_definition_forbidden_message
;
18499 parser
->type_definition_forbidden_message
18500 = G_("types may not be defined in template arguments");
18501 r
= cp_parser_type_id_1 (parser
, true, false);
18502 parser
->type_definition_forbidden_message
= saved_message
;
18503 if (cxx_dialect
>= cxx14
&& type_uses_auto (r
))
18505 error ("invalid use of %<auto%> in template argument");
18506 r
= error_mark_node
;
18511 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
18513 return cp_parser_type_id_1 (parser
, false, true);
18516 /* Parse a type-specifier-seq.
18518 type-specifier-seq:
18519 type-specifier type-specifier-seq [opt]
18523 type-specifier-seq:
18524 attributes type-specifier-seq [opt]
18526 If IS_DECLARATION is true, we are at the start of a "condition" or
18527 exception-declaration, so we might be followed by a declarator-id.
18529 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18530 i.e. we've just seen "->".
18532 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18535 cp_parser_type_specifier_seq (cp_parser
* parser
,
18536 bool is_declaration
,
18537 bool is_trailing_return
,
18538 cp_decl_specifier_seq
*type_specifier_seq
)
18540 bool seen_type_specifier
= false;
18541 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
18542 cp_token
*start_token
= NULL
;
18544 /* Clear the TYPE_SPECIFIER_SEQ. */
18545 clear_decl_specs (type_specifier_seq
);
18547 /* In the context of a trailing return type, enum E { } is an
18548 elaborated-type-specifier followed by a function-body, not an
18550 if (is_trailing_return
)
18551 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
18553 /* Parse the type-specifiers and attributes. */
18556 tree type_specifier
;
18557 bool is_cv_qualifier
;
18559 /* Check for attributes first. */
18560 if (cp_next_tokens_can_be_attribute_p (parser
))
18562 type_specifier_seq
->attributes
=
18563 chainon (type_specifier_seq
->attributes
,
18564 cp_parser_attributes_opt (parser
));
18568 /* record the token of the beginning of the type specifier seq,
18569 for error reporting purposes*/
18571 start_token
= cp_lexer_peek_token (parser
->lexer
);
18573 /* Look for the type-specifier. */
18574 type_specifier
= cp_parser_type_specifier (parser
,
18576 type_specifier_seq
,
18577 /*is_declaration=*/false,
18580 if (!type_specifier
)
18582 /* If the first type-specifier could not be found, this is not a
18583 type-specifier-seq at all. */
18584 if (!seen_type_specifier
)
18586 /* Set in_declarator_p to avoid skipping to the semicolon. */
18587 int in_decl
= parser
->in_declarator_p
;
18588 parser
->in_declarator_p
= true;
18590 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
18591 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18592 cp_parser_error (parser
, "expected type-specifier");
18594 parser
->in_declarator_p
= in_decl
;
18596 type_specifier_seq
->type
= error_mark_node
;
18599 /* If subsequent type-specifiers could not be found, the
18600 type-specifier-seq is complete. */
18604 seen_type_specifier
= true;
18605 /* The standard says that a condition can be:
18607 type-specifier-seq declarator = assignment-expression
18614 we should treat the "S" as a declarator, not as a
18615 type-specifier. The standard doesn't say that explicitly for
18616 type-specifier-seq, but it does say that for
18617 decl-specifier-seq in an ordinary declaration. Perhaps it
18618 would be clearer just to allow a decl-specifier-seq here, and
18619 then add a semantic restriction that if any decl-specifiers
18620 that are not type-specifiers appear, the program is invalid. */
18621 if (is_declaration
&& !is_cv_qualifier
)
18622 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18626 /* Return whether the function currently being declared has an associated
18627 template parameter list. */
18630 function_being_declared_is_template_p (cp_parser
* parser
)
18632 if (!current_template_parms
|| processing_template_parmlist
)
18635 if (parser
->implicit_template_scope
)
18638 if (at_class_scope_p ()
18639 && TYPE_BEING_DEFINED (current_class_type
))
18640 return parser
->num_template_parameter_lists
!= 0;
18642 return ((int) parser
->num_template_parameter_lists
> template_class_depth
18643 (current_class_type
));
18646 /* Parse a parameter-declaration-clause.
18648 parameter-declaration-clause:
18649 parameter-declaration-list [opt] ... [opt]
18650 parameter-declaration-list , ...
18652 Returns a representation for the parameter declarations. A return
18653 value of NULL indicates a parameter-declaration-clause consisting
18654 only of an ellipsis. */
18657 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18666 int auto_is_implicit_function_template_parm_p
;
18668 parser
->auto_is_implicit_function_template_parm_p
18669 = auto_is_implicit_function_template_parm_p
;
18671 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18675 if (!processing_specialization
18676 && !processing_template_parmlist
18677 && !processing_explicit_instantiation
)
18678 if (!current_function_decl
18679 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
18680 parser
->auto_is_implicit_function_template_parm_p
= true;
18682 /* Peek at the next token. */
18683 token
= cp_lexer_peek_token (parser
->lexer
);
18684 /* Check for trivial parameter-declaration-clauses. */
18685 if (token
->type
== CPP_ELLIPSIS
)
18687 /* Consume the `...' token. */
18688 cp_lexer_consume_token (parser
->lexer
);
18691 else if (token
->type
== CPP_CLOSE_PAREN
)
18692 /* There are no parameters. */
18694 #ifndef NO_IMPLICIT_EXTERN_C
18695 if (in_system_header_at (input_location
)
18696 && current_class_type
== NULL
18697 && current_lang_name
== lang_name_c
)
18701 return void_list_node
;
18703 /* Check for `(void)', too, which is a special case. */
18704 else if (token
->keyword
== RID_VOID
18705 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18706 == CPP_CLOSE_PAREN
))
18708 /* Consume the `void' token. */
18709 cp_lexer_consume_token (parser
->lexer
);
18710 /* There are no parameters. */
18711 return void_list_node
;
18714 /* Parse the parameter-declaration-list. */
18715 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18716 /* If a parse error occurred while parsing the
18717 parameter-declaration-list, then the entire
18718 parameter-declaration-clause is erroneous. */
18722 /* Peek at the next token. */
18723 token
= cp_lexer_peek_token (parser
->lexer
);
18724 /* If it's a `,', the clause should terminate with an ellipsis. */
18725 if (token
->type
== CPP_COMMA
)
18727 /* Consume the `,'. */
18728 cp_lexer_consume_token (parser
->lexer
);
18729 /* Expect an ellipsis. */
18731 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18733 /* It might also be `...' if the optional trailing `,' was
18735 else if (token
->type
== CPP_ELLIPSIS
)
18737 /* Consume the `...' token. */
18738 cp_lexer_consume_token (parser
->lexer
);
18739 /* And remember that we saw it. */
18743 ellipsis_p
= false;
18745 /* Finish the parameter list. */
18747 parameters
= chainon (parameters
, void_list_node
);
18752 /* Parse a parameter-declaration-list.
18754 parameter-declaration-list:
18755 parameter-declaration
18756 parameter-declaration-list , parameter-declaration
18758 Returns a representation of the parameter-declaration-list, as for
18759 cp_parser_parameter_declaration_clause. However, the
18760 `void_list_node' is never appended to the list. Upon return,
18761 *IS_ERROR will be true iff an error occurred. */
18764 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18766 tree parameters
= NULL_TREE
;
18767 tree
*tail
= ¶meters
;
18768 bool saved_in_unbraced_linkage_specification_p
;
18771 /* Assume all will go well. */
18773 /* The special considerations that apply to a function within an
18774 unbraced linkage specifications do not apply to the parameters
18775 to the function. */
18776 saved_in_unbraced_linkage_specification_p
18777 = parser
->in_unbraced_linkage_specification_p
;
18778 parser
->in_unbraced_linkage_specification_p
= false;
18780 /* Look for more parameters. */
18783 cp_parameter_declarator
*parameter
;
18784 tree decl
= error_mark_node
;
18785 bool parenthesized_p
= false;
18786 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
18787 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18788 (current_template_parms
)) : 0);
18790 /* Parse the parameter. */
18792 = cp_parser_parameter_declaration (parser
,
18793 /*template_parm_p=*/false,
18796 /* We don't know yet if the enclosing context is deprecated, so wait
18797 and warn in grokparms if appropriate. */
18798 deprecated_state
= DEPRECATED_SUPPRESS
;
18802 /* If a function parameter pack was specified and an implicit template
18803 parameter was introduced during cp_parser_parameter_declaration,
18804 change any implicit parameters introduced into packs. */
18805 if (parser
->implicit_template_parms
18806 && parameter
->declarator
18807 && parameter
->declarator
->parameter_pack_p
)
18809 int latest_template_parm_idx
= TREE_VEC_LENGTH
18810 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18812 if (latest_template_parm_idx
!= template_parm_idx
)
18813 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18814 (parameter
->decl_specifiers
.type
,
18815 template_parm_idx
, latest_template_parm_idx
);
18818 decl
= grokdeclarator (parameter
->declarator
,
18819 ¶meter
->decl_specifiers
,
18821 parameter
->default_argument
!= NULL_TREE
,
18822 ¶meter
->decl_specifiers
.attributes
);
18825 deprecated_state
= DEPRECATED_NORMAL
;
18827 /* If a parse error occurred parsing the parameter declaration,
18828 then the entire parameter-declaration-list is erroneous. */
18829 if (decl
== error_mark_node
)
18832 parameters
= error_mark_node
;
18836 if (parameter
->decl_specifiers
.attributes
)
18837 cplus_decl_attributes (&decl
,
18838 parameter
->decl_specifiers
.attributes
,
18840 if (DECL_NAME (decl
))
18841 decl
= pushdecl (decl
);
18843 if (decl
!= error_mark_node
)
18845 retrofit_lang_decl (decl
);
18846 DECL_PARM_INDEX (decl
) = ++index
;
18847 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18850 /* Add the new parameter to the list. */
18851 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18852 tail
= &TREE_CHAIN (*tail
);
18854 /* Peek at the next token. */
18855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18856 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18857 /* These are for Objective-C++ */
18858 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18859 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18860 /* The parameter-declaration-list is complete. */
18862 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18866 /* Peek at the next token. */
18867 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18868 /* If it's an ellipsis, then the list is complete. */
18869 if (token
->type
== CPP_ELLIPSIS
)
18871 /* Otherwise, there must be more parameters. Consume the
18873 cp_lexer_consume_token (parser
->lexer
);
18874 /* When parsing something like:
18876 int i(float f, double d)
18878 we can tell after seeing the declaration for "f" that we
18879 are not looking at an initialization of a variable "i",
18880 but rather at the declaration of a function "i".
18882 Due to the fact that the parsing of template arguments
18883 (as specified to a template-id) requires backtracking we
18884 cannot use this technique when inside a template argument
18886 if (!parser
->in_template_argument_list_p
18887 && !parser
->in_type_id_in_expr_p
18888 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18889 /* However, a parameter-declaration of the form
18890 "float(f)" (which is a valid declaration of a
18891 parameter "f") can also be interpreted as an
18892 expression (the conversion of "f" to "float"). */
18893 && !parenthesized_p
)
18894 cp_parser_commit_to_tentative_parse (parser
);
18898 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18899 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18900 cp_parser_skip_to_closing_parenthesis (parser
,
18901 /*recovering=*/true,
18902 /*or_comma=*/false,
18903 /*consume_paren=*/false);
18908 parser
->in_unbraced_linkage_specification_p
18909 = saved_in_unbraced_linkage_specification_p
;
18911 /* Reset implicit_template_scope if we are about to leave the function
18912 parameter list that introduced it. Note that for out-of-line member
18913 definitions, there will be one or more class scopes before we get to
18914 the template parameter scope. */
18916 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18917 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
18919 while (maybe_its
->kind
== sk_class
)
18920 maybe_its
= maybe_its
->level_chain
;
18921 if (maybe_its
== its
)
18923 parser
->implicit_template_parms
= 0;
18924 parser
->implicit_template_scope
= 0;
18931 /* Parse a parameter declaration.
18933 parameter-declaration:
18934 decl-specifier-seq ... [opt] declarator
18935 decl-specifier-seq declarator = assignment-expression
18936 decl-specifier-seq ... [opt] abstract-declarator [opt]
18937 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18939 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18940 declares a template parameter. (In that case, a non-nested `>'
18941 token encountered during the parsing of the assignment-expression
18942 is not interpreted as a greater-than operator.)
18944 Returns a representation of the parameter, or NULL if an error
18945 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18946 true iff the declarator is of the form "(p)". */
18948 static cp_parameter_declarator
*
18949 cp_parser_parameter_declaration (cp_parser
*parser
,
18950 bool template_parm_p
,
18951 bool *parenthesized_p
)
18953 int declares_class_or_enum
;
18954 cp_decl_specifier_seq decl_specifiers
;
18955 cp_declarator
*declarator
;
18956 tree default_argument
;
18957 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18958 const char *saved_message
;
18960 /* In a template parameter, `>' is not an operator.
18964 When parsing a default template-argument for a non-type
18965 template-parameter, the first non-nested `>' is taken as the end
18966 of the template parameter-list rather than a greater-than
18969 /* Type definitions may not appear in parameter types. */
18970 saved_message
= parser
->type_definition_forbidden_message
;
18971 parser
->type_definition_forbidden_message
18972 = G_("types may not be defined in parameter types");
18974 /* Parse the declaration-specifiers. */
18975 cp_parser_decl_specifier_seq (parser
,
18976 CP_PARSER_FLAGS_NONE
,
18978 &declares_class_or_enum
);
18980 /* Complain about missing 'typename' or other invalid type names. */
18981 if (!decl_specifiers
.any_type_specifiers_p
18982 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18983 decl_specifiers
.type
= error_mark_node
;
18985 /* If an error occurred, there's no reason to attempt to parse the
18986 rest of the declaration. */
18987 if (cp_parser_error_occurred (parser
))
18989 parser
->type_definition_forbidden_message
= saved_message
;
18993 /* Peek at the next token. */
18994 token
= cp_lexer_peek_token (parser
->lexer
);
18996 /* If the next token is a `)', `,', `=', `>', or `...', then there
18997 is no declarator. However, when variadic templates are enabled,
18998 there may be a declarator following `...'. */
18999 if (token
->type
== CPP_CLOSE_PAREN
19000 || token
->type
== CPP_COMMA
19001 || token
->type
== CPP_EQ
19002 || token
->type
== CPP_GREATER
)
19005 if (parenthesized_p
)
19006 *parenthesized_p
= false;
19008 /* Otherwise, there should be a declarator. */
19011 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19012 parser
->default_arg_ok_p
= false;
19014 /* After seeing a decl-specifier-seq, if the next token is not a
19015 "(", there is no possibility that the code is a valid
19016 expression. Therefore, if parsing tentatively, we commit at
19018 if (!parser
->in_template_argument_list_p
19019 /* In an expression context, having seen:
19023 we cannot be sure whether we are looking at a
19024 function-type (taking a "char" as a parameter) or a cast
19025 of some object of type "char" to "int". */
19026 && !parser
->in_type_id_in_expr_p
19027 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
19028 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
19029 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
19030 cp_parser_commit_to_tentative_parse (parser
);
19031 /* Parse the declarator. */
19032 declarator_token_start
= token
;
19033 declarator
= cp_parser_declarator (parser
,
19034 CP_PARSER_DECLARATOR_EITHER
,
19035 /*ctor_dtor_or_conv_p=*/NULL
,
19037 /*member_p=*/false,
19038 /*friend_p=*/false);
19039 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19040 /* After the declarator, allow more attributes. */
19041 decl_specifiers
.attributes
19042 = chainon (decl_specifiers
.attributes
,
19043 cp_parser_attributes_opt (parser
));
19046 /* If the next token is an ellipsis, and we have not seen a
19047 declarator name, and the type of the declarator contains parameter
19048 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19049 a parameter pack expansion expression. Otherwise, leave the
19050 ellipsis for a C-style variadic function. */
19051 token
= cp_lexer_peek_token (parser
->lexer
);
19052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19054 tree type
= decl_specifiers
.type
;
19056 if (type
&& DECL_P (type
))
19057 type
= TREE_TYPE (type
);
19060 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
19061 && declarator_can_be_parameter_pack (declarator
)
19062 && (!declarator
|| !declarator
->parameter_pack_p
)
19063 && uses_parameter_packs (type
))
19065 /* Consume the `...'. */
19066 cp_lexer_consume_token (parser
->lexer
);
19067 maybe_warn_variadic_templates ();
19069 /* Build a pack expansion type */
19071 declarator
->parameter_pack_p
= true;
19073 decl_specifiers
.type
= make_pack_expansion (type
);
19077 /* The restriction on defining new types applies only to the type
19078 of the parameter, not to the default argument. */
19079 parser
->type_definition_forbidden_message
= saved_message
;
19081 /* If the next token is `=', then process a default argument. */
19082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19084 token
= cp_lexer_peek_token (parser
->lexer
);
19085 /* If we are defining a class, then the tokens that make up the
19086 default argument must be saved and processed later. */
19087 if (!template_parm_p
&& at_class_scope_p ()
19088 && TYPE_BEING_DEFINED (current_class_type
)
19089 && !LAMBDA_TYPE_P (current_class_type
))
19090 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
19091 /* Outside of a class definition, we can just parse the
19092 assignment-expression. */
19095 = cp_parser_default_argument (parser
, template_parm_p
);
19097 if (!parser
->default_arg_ok_p
)
19099 if (flag_permissive
)
19100 warning (0, "deprecated use of default argument for parameter of non-function");
19103 error_at (token
->location
,
19104 "default arguments are only "
19105 "permitted for function parameters");
19106 default_argument
= NULL_TREE
;
19109 else if ((declarator
&& declarator
->parameter_pack_p
)
19110 || (decl_specifiers
.type
19111 && PACK_EXPANSION_P (decl_specifiers
.type
)))
19113 /* Find the name of the parameter pack. */
19114 cp_declarator
*id_declarator
= declarator
;
19115 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
19116 id_declarator
= id_declarator
->declarator
;
19118 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
19119 error_at (declarator_token_start
->location
,
19121 ? G_("template parameter pack %qD "
19122 "cannot have a default argument")
19123 : G_("parameter pack %qD cannot have "
19124 "a default argument"),
19125 id_declarator
->u
.id
.unqualified_name
);
19127 error_at (declarator_token_start
->location
,
19129 ? G_("template parameter pack cannot have "
19130 "a default argument")
19131 : G_("parameter pack cannot have a "
19132 "default argument"));
19134 default_argument
= NULL_TREE
;
19138 default_argument
= NULL_TREE
;
19140 return make_parameter_declarator (&decl_specifiers
,
19145 /* Parse a default argument and return it.
19147 TEMPLATE_PARM_P is true if this is a default argument for a
19148 non-type template parameter. */
19150 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
19152 tree default_argument
= NULL_TREE
;
19153 bool saved_greater_than_is_operator_p
;
19154 bool saved_local_variables_forbidden_p
;
19155 bool non_constant_p
, is_direct_init
;
19157 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19159 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
19160 parser
->greater_than_is_operator_p
= !template_parm_p
;
19161 /* Local variable names (and the `this' keyword) may not
19162 appear in a default argument. */
19163 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19164 parser
->local_variables_forbidden_p
= true;
19165 /* Parse the assignment-expression. */
19166 if (template_parm_p
)
19167 push_deferring_access_checks (dk_no_deferred
);
19168 tree saved_class_ptr
= NULL_TREE
;
19169 tree saved_class_ref
= NULL_TREE
;
19170 /* The "this" pointer is not valid in a default argument. */
19173 saved_class_ptr
= current_class_ptr
;
19174 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
19175 saved_class_ref
= current_class_ref
;
19176 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
19179 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
19180 /* Restore the "this" pointer. */
19183 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
19184 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
19186 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
19187 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19188 if (template_parm_p
)
19189 pop_deferring_access_checks ();
19190 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
19191 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
19193 return default_argument
;
19196 /* Parse a function-body.
19199 compound_statement */
19202 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
19204 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
19207 /* Parse a ctor-initializer-opt followed by a function-body. Return
19208 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19209 is true we are parsing a function-try-block. */
19212 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
19213 bool in_function_try_block
)
19216 bool ctor_initializer_p
;
19217 const bool check_body_p
=
19218 DECL_CONSTRUCTOR_P (current_function_decl
)
19219 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
19222 /* Begin the function body. */
19223 body
= begin_function_body ();
19224 /* Parse the optional ctor-initializer. */
19225 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
19227 /* If we're parsing a constexpr constructor definition, we need
19228 to check that the constructor body is indeed empty. However,
19229 before we get to cp_parser_function_body lot of junk has been
19230 generated, so we can't just check that we have an empty block.
19231 Rather we take a snapshot of the outermost block, and check whether
19232 cp_parser_function_body changed its state. */
19235 list
= cur_stmt_list
;
19236 if (STATEMENT_LIST_TAIL (list
))
19237 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
19239 /* Parse the function-body. */
19240 cp_parser_function_body (parser
, in_function_try_block
);
19242 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
19243 /* Finish the function body. */
19244 finish_function_body (body
);
19246 return ctor_initializer_p
;
19249 /* Parse an initializer.
19252 = initializer-clause
19253 ( expression-list )
19255 Returns an expression representing the initializer. If no
19256 initializer is present, NULL_TREE is returned.
19258 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19259 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19260 set to TRUE if there is no initializer present. If there is an
19261 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19262 is set to true; otherwise it is set to false. */
19265 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
19266 bool* non_constant_p
)
19271 /* Peek at the next token. */
19272 token
= cp_lexer_peek_token (parser
->lexer
);
19274 /* Let our caller know whether or not this initializer was
19276 *is_direct_init
= (token
->type
!= CPP_EQ
);
19277 /* Assume that the initializer is constant. */
19278 *non_constant_p
= false;
19280 if (token
->type
== CPP_EQ
)
19282 /* Consume the `='. */
19283 cp_lexer_consume_token (parser
->lexer
);
19284 /* Parse the initializer-clause. */
19285 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
19287 else if (token
->type
== CPP_OPEN_PAREN
)
19289 vec
<tree
, va_gc
> *vec
;
19290 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19292 /*allow_expansion_p=*/true,
19295 return error_mark_node
;
19296 init
= build_tree_list_vec (vec
);
19297 release_tree_vector (vec
);
19299 else if (token
->type
== CPP_OPEN_BRACE
)
19301 cp_lexer_set_source_position (parser
->lexer
);
19302 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19303 init
= cp_parser_braced_list (parser
, non_constant_p
);
19304 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
19308 /* Anything else is an error. */
19309 cp_parser_error (parser
, "expected initializer");
19310 init
= error_mark_node
;
19316 /* Parse an initializer-clause.
19318 initializer-clause:
19319 assignment-expression
19322 Returns an expression representing the initializer.
19324 If the `assignment-expression' production is used the value
19325 returned is simply a representation for the expression.
19327 Otherwise, calls cp_parser_braced_list. */
19330 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
19334 /* Assume the expression is constant. */
19335 *non_constant_p
= false;
19337 /* If it is not a `{', then we are looking at an
19338 assignment-expression. */
19339 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
19342 = cp_parser_constant_expression (parser
,
19343 /*allow_non_constant_p=*/true,
19347 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
19349 return initializer
;
19352 /* Parse a brace-enclosed initializer list.
19355 { initializer-list , [opt] }
19358 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19359 the elements of the initializer-list (or NULL, if the last
19360 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19361 NULL_TREE. There is no way to detect whether or not the optional
19362 trailing `,' was provided. NON_CONSTANT_P is as for
19363 cp_parser_initializer. */
19366 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
19370 /* Consume the `{' token. */
19371 cp_lexer_consume_token (parser
->lexer
);
19372 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19373 initializer
= make_node (CONSTRUCTOR
);
19374 /* If it's not a `}', then there is a non-trivial initializer. */
19375 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
19377 /* Parse the initializer list. */
19378 CONSTRUCTOR_ELTS (initializer
)
19379 = cp_parser_initializer_list (parser
, non_constant_p
);
19380 /* A trailing `,' token is allowed. */
19381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19382 cp_lexer_consume_token (parser
->lexer
);
19385 *non_constant_p
= false;
19386 /* Now, there should be a trailing `}'. */
19387 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19388 TREE_TYPE (initializer
) = init_list_type_node
;
19389 return initializer
;
19392 /* Consume tokens up to, and including, the next non-nested closing `]'.
19393 Returns true iff we found a closing `]'. */
19396 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
19398 unsigned square_depth
= 0;
19402 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
19404 switch (token
->type
)
19407 case CPP_PRAGMA_EOL
:
19408 /* If we've run out of tokens, then there is no closing `]'. */
19411 case CPP_OPEN_SQUARE
:
19415 case CPP_CLOSE_SQUARE
:
19416 if (!square_depth
--)
19418 cp_lexer_consume_token (parser
->lexer
);
19427 /* Consume the token. */
19428 cp_lexer_consume_token (parser
->lexer
);
19432 /* Return true if we are looking at an array-designator, false otherwise. */
19435 cp_parser_array_designator_p (cp_parser
*parser
)
19437 /* Consume the `['. */
19438 cp_lexer_consume_token (parser
->lexer
);
19440 cp_lexer_save_tokens (parser
->lexer
);
19442 /* Skip tokens until the next token is a closing square bracket.
19443 If we find the closing `]', and the next token is a `=', then
19444 we are looking at an array designator. */
19445 bool array_designator_p
19446 = (cp_parser_skip_to_closing_square_bracket (parser
)
19447 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
19449 /* Roll back the tokens we skipped. */
19450 cp_lexer_rollback_tokens (parser
->lexer
);
19452 return array_designator_p
;
19455 /* Parse an initializer-list.
19458 initializer-clause ... [opt]
19459 initializer-list , initializer-clause ... [opt]
19464 designation initializer-clause ...[opt]
19465 initializer-list , designation initializer-clause ...[opt]
19470 [ constant-expression ] =
19472 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19473 for the initializer. If the INDEX of the elt is non-NULL, it is the
19474 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19475 as for cp_parser_initializer. */
19477 static vec
<constructor_elt
, va_gc
> *
19478 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
19480 vec
<constructor_elt
, va_gc
> *v
= NULL
;
19482 /* Assume all of the expressions are constant. */
19483 *non_constant_p
= false;
19485 /* Parse the rest of the list. */
19491 bool clause_non_constant_p
;
19493 /* If the next token is an identifier and the following one is a
19494 colon, we are looking at the GNU designated-initializer
19496 if (cp_parser_allow_gnu_extensions_p (parser
)
19497 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
19498 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
19500 /* Warn the user that they are using an extension. */
19501 pedwarn (input_location
, OPT_Wpedantic
,
19502 "ISO C++ does not allow designated initializers");
19503 /* Consume the identifier. */
19504 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19505 /* Consume the `:'. */
19506 cp_lexer_consume_token (parser
->lexer
);
19508 /* Also handle the C99 syntax, '. id ='. */
19509 else if (cp_parser_allow_gnu_extensions_p (parser
)
19510 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
19511 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
19512 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
19514 /* Warn the user that they are using an extension. */
19515 pedwarn (input_location
, OPT_Wpedantic
,
19516 "ISO C++ does not allow C99 designated initializers");
19517 /* Consume the `.'. */
19518 cp_lexer_consume_token (parser
->lexer
);
19519 /* Consume the identifier. */
19520 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19521 /* Consume the `='. */
19522 cp_lexer_consume_token (parser
->lexer
);
19524 /* Also handle C99 array designators, '[ const ] ='. */
19525 else if (cp_parser_allow_gnu_extensions_p (parser
)
19526 && !c_dialect_objc ()
19527 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19529 /* In C++11, [ could start a lambda-introducer. */
19530 bool non_const
= false;
19532 cp_parser_parse_tentatively (parser
);
19534 if (!cp_parser_array_designator_p (parser
))
19536 cp_parser_simulate_error (parser
);
19537 designator
= NULL_TREE
;
19541 designator
= cp_parser_constant_expression (parser
, true,
19543 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19544 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19547 if (!cp_parser_parse_definitely (parser
))
19548 designator
= NULL_TREE
;
19549 else if (non_const
)
19550 require_potential_rvalue_constant_expression (designator
);
19553 designator
= NULL_TREE
;
19555 /* Parse the initializer. */
19556 initializer
= cp_parser_initializer_clause (parser
,
19557 &clause_non_constant_p
);
19558 /* If any clause is non-constant, so is the entire initializer. */
19559 if (clause_non_constant_p
)
19560 *non_constant_p
= true;
19562 /* If we have an ellipsis, this is an initializer pack
19564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19566 /* Consume the `...'. */
19567 cp_lexer_consume_token (parser
->lexer
);
19569 /* Turn the initializer into an initializer expansion. */
19570 initializer
= make_pack_expansion (initializer
);
19573 /* Add it to the vector. */
19574 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
19576 /* If the next token is not a comma, we have reached the end of
19578 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19581 /* Peek at the next token. */
19582 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19583 /* If the next token is a `}', then we're still done. An
19584 initializer-clause can have a trailing `,' after the
19585 initializer-list and before the closing `}'. */
19586 if (token
->type
== CPP_CLOSE_BRACE
)
19589 /* Consume the `,' token. */
19590 cp_lexer_consume_token (parser
->lexer
);
19596 /* Classes [gram.class] */
19598 /* Parse a class-name.
19604 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19605 to indicate that names looked up in dependent types should be
19606 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19607 keyword has been used to indicate that the name that appears next
19608 is a template. TAG_TYPE indicates the explicit tag given before
19609 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19610 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19611 is the class being defined in a class-head.
19613 Returns the TYPE_DECL representing the class. */
19616 cp_parser_class_name (cp_parser
*parser
,
19617 bool typename_keyword_p
,
19618 bool template_keyword_p
,
19619 enum tag_types tag_type
,
19620 bool check_dependency_p
,
19622 bool is_declaration
)
19628 tree identifier
= NULL_TREE
;
19630 /* All class-names start with an identifier. */
19631 token
= cp_lexer_peek_token (parser
->lexer
);
19632 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
19634 cp_parser_error (parser
, "expected class-name");
19635 return error_mark_node
;
19638 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19639 to a template-id, so we save it here. */
19640 scope
= parser
->scope
;
19641 if (scope
== error_mark_node
)
19642 return error_mark_node
;
19644 /* Any name names a type if we're following the `typename' keyword
19645 in a qualified name where the enclosing scope is type-dependent. */
19646 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
19647 && dependent_type_p (scope
));
19648 /* Handle the common case (an identifier, but not a template-id)
19650 if (token
->type
== CPP_NAME
19651 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
19653 cp_token
*identifier_token
;
19656 /* Look for the identifier. */
19657 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
19658 ambiguous_p
= identifier_token
->error_reported
;
19659 identifier
= cp_parser_identifier (parser
);
19660 /* If the next token isn't an identifier, we are certainly not
19661 looking at a class-name. */
19662 if (identifier
== error_mark_node
)
19663 decl
= error_mark_node
;
19664 /* If we know this is a type-name, there's no need to look it
19666 else if (typename_p
)
19670 tree ambiguous_decls
;
19671 /* If we already know that this lookup is ambiguous, then
19672 we've already issued an error message; there's no reason
19676 cp_parser_simulate_error (parser
);
19677 return error_mark_node
;
19679 /* If the next token is a `::', then the name must be a type
19682 [basic.lookup.qual]
19684 During the lookup for a name preceding the :: scope
19685 resolution operator, object, function, and enumerator
19686 names are ignored. */
19687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19688 tag_type
= typename_type
;
19689 /* Look up the name. */
19690 decl
= cp_parser_lookup_name (parser
, identifier
,
19692 /*is_template=*/false,
19693 /*is_namespace=*/false,
19694 check_dependency_p
,
19696 identifier_token
->location
);
19697 if (ambiguous_decls
)
19699 if (cp_parser_parsing_tentatively (parser
))
19700 cp_parser_simulate_error (parser
);
19701 return error_mark_node
;
19707 /* Try a template-id. */
19708 decl
= cp_parser_template_id (parser
, template_keyword_p
,
19709 check_dependency_p
,
19712 if (decl
== error_mark_node
)
19713 return error_mark_node
;
19716 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
19718 /* If this is a typename, create a TYPENAME_TYPE. */
19719 if (typename_p
&& decl
!= error_mark_node
)
19721 decl
= make_typename_type (scope
, decl
, typename_type
,
19722 /*complain=*/tf_error
);
19723 if (decl
!= error_mark_node
)
19724 decl
= TYPE_NAME (decl
);
19727 decl
= strip_using_decl (decl
);
19729 /* Check to see that it is really the name of a class. */
19730 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19731 && identifier_p (TREE_OPERAND (decl
, 0))
19732 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19733 /* Situations like this:
19735 template <typename T> struct A {
19736 typename T::template X<int>::I i;
19739 are problematic. Is `T::template X<int>' a class-name? The
19740 standard does not seem to be definitive, but there is no other
19741 valid interpretation of the following `::'. Therefore, those
19742 names are considered class-names. */
19744 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19745 if (decl
!= error_mark_node
)
19746 decl
= TYPE_NAME (decl
);
19748 else if (TREE_CODE (decl
) != TYPE_DECL
19749 || TREE_TYPE (decl
) == error_mark_node
19750 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19751 /* In Objective-C 2.0, a classname followed by '.' starts a
19752 dot-syntax expression, and it's not a type-name. */
19753 || (c_dialect_objc ()
19754 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19755 && objc_is_class_name (decl
)))
19756 decl
= error_mark_node
;
19758 if (decl
== error_mark_node
)
19759 cp_parser_error (parser
, "expected class-name");
19760 else if (identifier
&& !parser
->scope
)
19761 maybe_note_name_used_in_class (identifier
, decl
);
19766 /* Parse a class-specifier.
19769 class-head { member-specification [opt] }
19771 Returns the TREE_TYPE representing the class. */
19774 cp_parser_class_specifier_1 (cp_parser
* parser
)
19777 tree attributes
= NULL_TREE
;
19778 bool nested_name_specifier_p
;
19779 unsigned saved_num_template_parameter_lists
;
19780 bool saved_in_function_body
;
19781 unsigned char in_statement
;
19782 bool in_switch_statement_p
;
19783 bool saved_in_unbraced_linkage_specification_p
;
19784 tree old_scope
= NULL_TREE
;
19785 tree scope
= NULL_TREE
;
19786 cp_token
*closing_brace
;
19788 push_deferring_access_checks (dk_no_deferred
);
19790 /* Parse the class-head. */
19791 type
= cp_parser_class_head (parser
,
19792 &nested_name_specifier_p
);
19793 /* If the class-head was a semantic disaster, skip the entire body
19797 cp_parser_skip_to_end_of_block_or_statement (parser
);
19798 pop_deferring_access_checks ();
19799 return error_mark_node
;
19802 /* Look for the `{'. */
19803 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19805 pop_deferring_access_checks ();
19806 return error_mark_node
;
19809 cp_ensure_no_omp_declare_simd (parser
);
19811 /* Issue an error message if type-definitions are forbidden here. */
19812 cp_parser_check_type_definition (parser
);
19813 /* Remember that we are defining one more class. */
19814 ++parser
->num_classes_being_defined
;
19815 /* Inside the class, surrounding template-parameter-lists do not
19817 saved_num_template_parameter_lists
19818 = parser
->num_template_parameter_lists
;
19819 parser
->num_template_parameter_lists
= 0;
19820 /* We are not in a function body. */
19821 saved_in_function_body
= parser
->in_function_body
;
19822 parser
->in_function_body
= false;
19823 /* Or in a loop. */
19824 in_statement
= parser
->in_statement
;
19825 parser
->in_statement
= 0;
19826 /* Or in a switch. */
19827 in_switch_statement_p
= parser
->in_switch_statement_p
;
19828 parser
->in_switch_statement_p
= false;
19829 /* We are not immediately inside an extern "lang" block. */
19830 saved_in_unbraced_linkage_specification_p
19831 = parser
->in_unbraced_linkage_specification_p
;
19832 parser
->in_unbraced_linkage_specification_p
= false;
19834 /* Start the class. */
19835 if (nested_name_specifier_p
)
19837 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19838 old_scope
= push_inner_scope (scope
);
19840 type
= begin_class_definition (type
);
19842 if (type
== error_mark_node
)
19843 /* If the type is erroneous, skip the entire body of the class. */
19844 cp_parser_skip_to_closing_brace (parser
);
19846 /* Parse the member-specification. */
19847 cp_parser_member_specification_opt (parser
);
19849 /* Look for the trailing `}'. */
19850 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19851 /* Look for trailing attributes to apply to this class. */
19852 if (cp_parser_allow_gnu_extensions_p (parser
))
19853 attributes
= cp_parser_gnu_attributes_opt (parser
);
19854 if (type
!= error_mark_node
)
19855 type
= finish_struct (type
, attributes
);
19856 if (nested_name_specifier_p
)
19857 pop_inner_scope (old_scope
, scope
);
19859 /* We've finished a type definition. Check for the common syntax
19860 error of forgetting a semicolon after the definition. We need to
19861 be careful, as we can't just check for not-a-semicolon and be done
19862 with it; the user might have typed:
19864 class X { } c = ...;
19865 class X { } *p = ...;
19867 and so forth. Instead, enumerate all the possible tokens that
19868 might follow this production; if we don't see one of them, then
19869 complain and silently insert the semicolon. */
19871 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19872 bool want_semicolon
= true;
19874 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19875 /* Don't try to parse c++11 attributes here. As per the
19876 grammar, that should be a task for
19877 cp_parser_decl_specifier_seq. */
19878 want_semicolon
= false;
19880 switch (token
->type
)
19883 case CPP_SEMICOLON
:
19886 case CPP_OPEN_PAREN
:
19887 case CPP_CLOSE_PAREN
:
19889 want_semicolon
= false;
19892 /* While it's legal for type qualifiers and storage class
19893 specifiers to follow type definitions in the grammar, only
19894 compiler testsuites contain code like that. Assume that if
19895 we see such code, then what we're really seeing is a case
19899 const <type> var = ...;
19904 static <type> func (...) ...
19906 i.e. the qualifier or specifier applies to the next
19907 declaration. To do so, however, we need to look ahead one
19908 more token to see if *that* token is a type specifier.
19910 This code could be improved to handle:
19913 static const <type> var = ...; */
19915 if (keyword_is_decl_specifier (token
->keyword
))
19917 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19919 /* Handling user-defined types here would be nice, but very
19922 = (lookahead
->type
== CPP_KEYWORD
19923 && keyword_begins_type_specifier (lookahead
->keyword
));
19930 /* If we don't have a type, then something is very wrong and we
19931 shouldn't try to do anything clever. Likewise for not seeing the
19933 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19935 cp_token_position prev
19936 = cp_lexer_previous_token_position (parser
->lexer
);
19937 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19938 location_t loc
= prev_token
->location
;
19940 if (CLASSTYPE_DECLARED_CLASS (type
))
19941 error_at (loc
, "expected %<;%> after class definition");
19942 else if (TREE_CODE (type
) == RECORD_TYPE
)
19943 error_at (loc
, "expected %<;%> after struct definition");
19944 else if (TREE_CODE (type
) == UNION_TYPE
)
19945 error_at (loc
, "expected %<;%> after union definition");
19947 gcc_unreachable ();
19949 /* Unget one token and smash it to look as though we encountered
19950 a semicolon in the input stream. */
19951 cp_lexer_set_token_position (parser
->lexer
, prev
);
19952 token
= cp_lexer_peek_token (parser
->lexer
);
19953 token
->type
= CPP_SEMICOLON
;
19954 token
->keyword
= RID_MAX
;
19958 /* If this class is not itself within the scope of another class,
19959 then we need to parse the bodies of all of the queued function
19960 definitions. Note that the queued functions defined in a class
19961 are not always processed immediately following the
19962 class-specifier for that class. Consider:
19965 struct B { void f() { sizeof (A); } };
19968 If `f' were processed before the processing of `A' were
19969 completed, there would be no way to compute the size of `A'.
19970 Note that the nesting we are interested in here is lexical --
19971 not the semantic nesting given by TYPE_CONTEXT. In particular,
19974 struct A { struct B; };
19975 struct A::B { void f() { } };
19977 there is no need to delay the parsing of `A::B::f'. */
19978 if (--parser
->num_classes_being_defined
== 0)
19981 tree class_type
= NULL_TREE
;
19982 tree pushed_scope
= NULL_TREE
;
19984 cp_default_arg_entry
*e
;
19985 tree save_ccp
, save_ccr
;
19987 /* In a first pass, parse default arguments to the functions.
19988 Then, in a second pass, parse the bodies of the functions.
19989 This two-phased approach handles cases like:
19997 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
20000 /* If there are default arguments that have not yet been processed,
20001 take care of them now. */
20002 if (class_type
!= e
->class_type
)
20005 pop_scope (pushed_scope
);
20006 class_type
= e
->class_type
;
20007 pushed_scope
= push_scope (class_type
);
20009 /* Make sure that any template parameters are in scope. */
20010 maybe_begin_member_template_processing (decl
);
20011 /* Parse the default argument expressions. */
20012 cp_parser_late_parsing_default_args (parser
, decl
);
20013 /* Remove any template parameters from the symbol table. */
20014 maybe_end_member_template_processing ();
20016 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
20017 /* Now parse any NSDMIs. */
20018 save_ccp
= current_class_ptr
;
20019 save_ccr
= current_class_ref
;
20020 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
20022 if (class_type
!= DECL_CONTEXT (decl
))
20025 pop_scope (pushed_scope
);
20026 class_type
= DECL_CONTEXT (decl
);
20027 pushed_scope
= push_scope (class_type
);
20029 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
20030 cp_parser_late_parsing_nsdmi (parser
, decl
);
20032 vec_safe_truncate (unparsed_nsdmis
, 0);
20033 current_class_ptr
= save_ccp
;
20034 current_class_ref
= save_ccr
;
20036 pop_scope (pushed_scope
);
20038 /* Now do some post-NSDMI bookkeeping. */
20039 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
20040 after_nsdmi_defaulted_late_checks (class_type
);
20041 vec_safe_truncate (unparsed_classes
, 0);
20042 after_nsdmi_defaulted_late_checks (type
);
20044 /* Now parse the body of the functions. */
20047 /* OpenMP UDRs need to be parsed before all other functions. */
20048 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20049 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
20050 cp_parser_late_parsing_for_member (parser
, decl
);
20051 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20052 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
20053 cp_parser_late_parsing_for_member (parser
, decl
);
20056 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20057 cp_parser_late_parsing_for_member (parser
, decl
);
20058 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
20061 vec_safe_push (unparsed_classes
, type
);
20063 /* Put back any saved access checks. */
20064 pop_deferring_access_checks ();
20066 /* Restore saved state. */
20067 parser
->in_switch_statement_p
= in_switch_statement_p
;
20068 parser
->in_statement
= in_statement
;
20069 parser
->in_function_body
= saved_in_function_body
;
20070 parser
->num_template_parameter_lists
20071 = saved_num_template_parameter_lists
;
20072 parser
->in_unbraced_linkage_specification_p
20073 = saved_in_unbraced_linkage_specification_p
;
20079 cp_parser_class_specifier (cp_parser
* parser
)
20082 timevar_push (TV_PARSE_STRUCT
);
20083 ret
= cp_parser_class_specifier_1 (parser
);
20084 timevar_pop (TV_PARSE_STRUCT
);
20088 /* Parse a class-head.
20091 class-key identifier [opt] base-clause [opt]
20092 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20093 class-key nested-name-specifier [opt] template-id
20096 class-virt-specifier:
20100 class-key attributes identifier [opt] base-clause [opt]
20101 class-key attributes nested-name-specifier identifier base-clause [opt]
20102 class-key attributes nested-name-specifier [opt] template-id
20105 Upon return BASES is initialized to the list of base classes (or
20106 NULL, if there are none) in the same form returned by
20107 cp_parser_base_clause.
20109 Returns the TYPE of the indicated class. Sets
20110 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20111 involving a nested-name-specifier was used, and FALSE otherwise.
20113 Returns error_mark_node if this is not a class-head.
20115 Returns NULL_TREE if the class-head is syntactically valid, but
20116 semantically invalid in a way that means we should skip the entire
20117 body of the class. */
20120 cp_parser_class_head (cp_parser
* parser
,
20121 bool* nested_name_specifier_p
)
20123 tree nested_name_specifier
;
20124 enum tag_types class_key
;
20125 tree id
= NULL_TREE
;
20126 tree type
= NULL_TREE
;
20129 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20130 bool template_id_p
= false;
20131 bool qualified_p
= false;
20132 bool invalid_nested_name_p
= false;
20133 bool invalid_explicit_specialization_p
= false;
20134 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20135 tree pushed_scope
= NULL_TREE
;
20136 unsigned num_templates
;
20137 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
20138 /* Assume no nested-name-specifier will be present. */
20139 *nested_name_specifier_p
= false;
20140 /* Assume no template parameter lists will be used in defining the
20143 parser
->colon_corrects_to_scope_p
= false;
20145 /* Look for the class-key. */
20146 class_key
= cp_parser_class_key (parser
);
20147 if (class_key
== none_type
)
20148 return error_mark_node
;
20150 /* Parse the attributes. */
20151 attributes
= cp_parser_attributes_opt (parser
);
20153 /* If the next token is `::', that is invalid -- but sometimes
20154 people do try to write:
20158 Handle this gracefully by accepting the extra qualifier, and then
20159 issuing an error about it later if this really is a
20160 class-head. If it turns out just to be an elaborated type
20161 specifier, remain silent. */
20162 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
20163 qualified_p
= true;
20165 push_deferring_access_checks (dk_no_check
);
20167 /* Determine the name of the class. Begin by looking for an
20168 optional nested-name-specifier. */
20169 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
20170 nested_name_specifier
20171 = cp_parser_nested_name_specifier_opt (parser
,
20172 /*typename_keyword_p=*/false,
20173 /*check_dependency_p=*/false,
20175 /*is_declaration=*/false);
20176 /* If there was a nested-name-specifier, then there *must* be an
20178 if (nested_name_specifier
)
20180 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20181 /* Although the grammar says `identifier', it really means
20182 `class-name' or `template-name'. You are only allowed to
20183 define a class that has already been declared with this
20186 The proposed resolution for Core Issue 180 says that wherever
20187 you see `class T::X' you should treat `X' as a type-name.
20189 It is OK to define an inaccessible class; for example:
20191 class A { class B; };
20194 We do not know if we will see a class-name, or a
20195 template-name. We look for a class-name first, in case the
20196 class-name is a template-id; if we looked for the
20197 template-name first we would stop after the template-name. */
20198 cp_parser_parse_tentatively (parser
);
20199 type
= cp_parser_class_name (parser
,
20200 /*typename_keyword_p=*/false,
20201 /*template_keyword_p=*/false,
20203 /*check_dependency_p=*/false,
20204 /*class_head_p=*/true,
20205 /*is_declaration=*/false);
20206 /* If that didn't work, ignore the nested-name-specifier. */
20207 if (!cp_parser_parse_definitely (parser
))
20209 invalid_nested_name_p
= true;
20210 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20211 id
= cp_parser_identifier (parser
);
20212 if (id
== error_mark_node
)
20215 /* If we could not find a corresponding TYPE, treat this
20216 declaration like an unqualified declaration. */
20217 if (type
== error_mark_node
)
20218 nested_name_specifier
= NULL_TREE
;
20219 /* Otherwise, count the number of templates used in TYPE and its
20220 containing scopes. */
20225 for (scope
= TREE_TYPE (type
);
20226 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
20227 scope
= get_containing_scope (scope
))
20229 && CLASS_TYPE_P (scope
)
20230 && CLASSTYPE_TEMPLATE_INFO (scope
)
20231 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
20232 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
20233 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
20237 /* Otherwise, the identifier is optional. */
20240 /* We don't know whether what comes next is a template-id,
20241 an identifier, or nothing at all. */
20242 cp_parser_parse_tentatively (parser
);
20243 /* Check for a template-id. */
20244 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20245 id
= cp_parser_template_id (parser
,
20246 /*template_keyword_p=*/false,
20247 /*check_dependency_p=*/true,
20249 /*is_declaration=*/true);
20250 /* If that didn't work, it could still be an identifier. */
20251 if (!cp_parser_parse_definitely (parser
))
20253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20255 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20256 id
= cp_parser_identifier (parser
);
20263 template_id_p
= true;
20268 pop_deferring_access_checks ();
20272 cp_parser_check_for_invalid_template_id (parser
, id
,
20274 type_start_token
->location
);
20276 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20278 /* If it's not a `:' or a `{' then we can't really be looking at a
20279 class-head, since a class-head only appears as part of a
20280 class-specifier. We have to detect this situation before calling
20281 xref_tag, since that has irreversible side-effects. */
20282 if (!cp_parser_next_token_starts_class_definition_p (parser
))
20284 cp_parser_error (parser
, "expected %<{%> or %<:%>");
20285 type
= error_mark_node
;
20289 /* At this point, we're going ahead with the class-specifier, even
20290 if some other problem occurs. */
20291 cp_parser_commit_to_tentative_parse (parser
);
20292 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
20294 cp_parser_error (parser
,
20295 "cannot specify %<override%> for a class");
20296 type
= error_mark_node
;
20299 /* Issue the error about the overly-qualified name now. */
20302 cp_parser_error (parser
,
20303 "global qualification of class name is invalid");
20304 type
= error_mark_node
;
20307 else if (invalid_nested_name_p
)
20309 cp_parser_error (parser
,
20310 "qualified name does not name a class");
20311 type
= error_mark_node
;
20314 else if (nested_name_specifier
)
20318 /* Reject typedef-names in class heads. */
20319 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
20321 error_at (type_start_token
->location
,
20322 "invalid class name in declaration of %qD",
20328 /* Figure out in what scope the declaration is being placed. */
20329 scope
= current_scope ();
20330 /* If that scope does not contain the scope in which the
20331 class was originally declared, the program is invalid. */
20332 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
20334 if (at_namespace_scope_p ())
20335 error_at (type_start_token
->location
,
20336 "declaration of %qD in namespace %qD which does not "
20338 type
, scope
, nested_name_specifier
);
20340 error_at (type_start_token
->location
,
20341 "declaration of %qD in %qD which does not enclose %qD",
20342 type
, scope
, nested_name_specifier
);
20348 A declarator-id shall not be qualified except for the
20349 definition of a ... nested class outside of its class
20350 ... [or] the definition or explicit instantiation of a
20351 class member of a namespace outside of its namespace. */
20352 if (scope
== nested_name_specifier
)
20354 permerror (nested_name_specifier_token_start
->location
,
20355 "extra qualification not allowed");
20356 nested_name_specifier
= NULL_TREE
;
20360 /* An explicit-specialization must be preceded by "template <>". If
20361 it is not, try to recover gracefully. */
20362 if (at_namespace_scope_p ()
20363 && parser
->num_template_parameter_lists
== 0
20366 error_at (type_start_token
->location
,
20367 "an explicit specialization must be preceded by %<template <>%>");
20368 invalid_explicit_specialization_p
= true;
20369 /* Take the same action that would have been taken by
20370 cp_parser_explicit_specialization. */
20371 ++parser
->num_template_parameter_lists
;
20372 begin_specialization ();
20374 /* There must be no "return" statements between this point and the
20375 end of this function; set "type "to the correct return value and
20376 use "goto done;" to return. */
20377 /* Make sure that the right number of template parameters were
20379 if (!cp_parser_check_template_parameters (parser
, num_templates
,
20380 type_start_token
->location
,
20381 /*declarator=*/NULL
))
20383 /* If something went wrong, there is no point in even trying to
20384 process the class-definition. */
20389 /* Look up the type. */
20392 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
20393 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
20394 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
20396 error_at (type_start_token
->location
,
20397 "function template %qD redeclared as a class template", id
);
20398 type
= error_mark_node
;
20402 type
= TREE_TYPE (id
);
20403 type
= maybe_process_partial_specialization (type
);
20405 if (nested_name_specifier
)
20406 pushed_scope
= push_scope (nested_name_specifier
);
20408 else if (nested_name_specifier
)
20414 template <typename T> struct S { struct T };
20415 template <typename T> struct S<T>::T { };
20417 we will get a TYPENAME_TYPE when processing the definition of
20418 `S::T'. We need to resolve it to the actual type before we
20419 try to define it. */
20420 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
20422 class_type
= resolve_typename_type (TREE_TYPE (type
),
20423 /*only_current_p=*/false);
20424 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
20425 type
= TYPE_NAME (class_type
);
20428 cp_parser_error (parser
, "could not resolve typename type");
20429 type
= error_mark_node
;
20433 if (maybe_process_partial_specialization (TREE_TYPE (type
))
20434 == error_mark_node
)
20440 class_type
= current_class_type
;
20441 /* Enter the scope indicated by the nested-name-specifier. */
20442 pushed_scope
= push_scope (nested_name_specifier
);
20443 /* Get the canonical version of this type. */
20444 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
20445 /* Call push_template_decl if it seems like we should be defining a
20446 template either from the template headers or the type we're
20447 defining, so that we diagnose both extra and missing headers. */
20448 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20449 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
20450 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
20452 type
= push_template_decl (type
);
20453 if (type
== error_mark_node
)
20460 type
= TREE_TYPE (type
);
20461 *nested_name_specifier_p
= true;
20463 else /* The name is not a nested name. */
20465 /* If the class was unnamed, create a dummy name. */
20467 id
= make_anon_name ();
20468 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
20469 parser
->num_template_parameter_lists
);
20472 /* Indicate whether this class was declared as a `class' or as a
20474 if (TREE_CODE (type
) == RECORD_TYPE
)
20475 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
20476 cp_parser_check_class_key (class_key
, type
);
20478 /* If this type was already complete, and we see another definition,
20479 that's an error. */
20480 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
20482 error_at (type_start_token
->location
, "redefinition of %q#T",
20484 error_at (type_start_token
->location
, "previous definition of %q+#T",
20489 else if (type
== error_mark_node
)
20494 /* Apply attributes now, before any use of the class as a template
20495 argument in its base list. */
20496 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
20497 fixup_attribute_variants (type
);
20500 /* We will have entered the scope containing the class; the names of
20501 base classes should be looked up in that context. For example:
20503 struct A { struct B {}; struct C; };
20504 struct A::C : B {};
20508 /* Get the list of base-classes, if there is one. */
20509 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20511 /* PR59482: enter the class scope so that base-specifiers are looked
20515 bases
= cp_parser_base_clause (parser
);
20516 /* PR59482: get out of the previously pushed class scope so that the
20517 subsequent pops pop the right thing. */
20524 /* If we're really defining a class, process the base classes.
20525 If they're invalid, fail. */
20526 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20527 && !xref_basetypes (type
, bases
))
20531 /* Leave the scope given by the nested-name-specifier. We will
20532 enter the class scope itself while processing the members. */
20534 pop_scope (pushed_scope
);
20536 if (invalid_explicit_specialization_p
)
20538 end_specialization ();
20539 --parser
->num_template_parameter_lists
;
20543 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20544 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
20545 CLASSTYPE_FINAL (type
) = 1;
20547 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20551 /* Parse a class-key.
20558 Returns the kind of class-key specified, or none_type to indicate
20561 static enum tag_types
20562 cp_parser_class_key (cp_parser
* parser
)
20565 enum tag_types tag_type
;
20567 /* Look for the class-key. */
20568 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
20572 /* Check to see if the TOKEN is a class-key. */
20573 tag_type
= cp_parser_token_is_class_key (token
);
20575 cp_parser_error (parser
, "expected class-key");
20579 /* Parse a type-parameter-key.
20581 type-parameter-key:
20587 cp_parser_type_parameter_key (cp_parser
* parser
)
20589 /* Look for the type-parameter-key. */
20590 enum tag_types tag_type
= none_type
;
20591 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20592 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
20594 cp_lexer_consume_token (parser
->lexer
);
20595 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
20596 /* typename is not allowed in a template template parameter
20597 by the standard until C++1Z. */
20598 pedwarn (token
->location
, OPT_Wpedantic
,
20599 "ISO C++ forbids typename key in template template parameter;"
20600 " use -std=c++1z or -std=gnu++1z");
20603 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
20608 /* Parse an (optional) member-specification.
20610 member-specification:
20611 member-declaration member-specification [opt]
20612 access-specifier : member-specification [opt] */
20615 cp_parser_member_specification_opt (cp_parser
* parser
)
20622 /* Peek at the next token. */
20623 token
= cp_lexer_peek_token (parser
->lexer
);
20624 /* If it's a `}', or EOF then we've seen all the members. */
20625 if (token
->type
== CPP_CLOSE_BRACE
20626 || token
->type
== CPP_EOF
20627 || token
->type
== CPP_PRAGMA_EOL
)
20630 /* See if this token is a keyword. */
20631 keyword
= token
->keyword
;
20635 case RID_PROTECTED
:
20637 /* Consume the access-specifier. */
20638 cp_lexer_consume_token (parser
->lexer
);
20639 /* Remember which access-specifier is active. */
20640 current_access_specifier
= token
->u
.value
;
20641 /* Look for the `:'. */
20642 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20646 /* Accept #pragmas at class scope. */
20647 if (token
->type
== CPP_PRAGMA
)
20649 cp_parser_pragma (parser
, pragma_member
);
20653 /* Otherwise, the next construction must be a
20654 member-declaration. */
20655 cp_parser_member_declaration (parser
);
20660 /* Parse a member-declaration.
20662 member-declaration:
20663 decl-specifier-seq [opt] member-declarator-list [opt] ;
20664 function-definition ; [opt]
20665 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20667 template-declaration
20670 member-declarator-list:
20672 member-declarator-list , member-declarator
20675 declarator pure-specifier [opt]
20676 declarator constant-initializer [opt]
20677 identifier [opt] : constant-expression
20681 member-declaration:
20682 __extension__ member-declaration
20685 declarator attributes [opt] pure-specifier [opt]
20686 declarator attributes [opt] constant-initializer [opt]
20687 identifier [opt] attributes [opt] : constant-expression
20691 member-declaration:
20692 static_assert-declaration */
20695 cp_parser_member_declaration (cp_parser
* parser
)
20697 cp_decl_specifier_seq decl_specifiers
;
20698 tree prefix_attributes
;
20700 int declares_class_or_enum
;
20702 cp_token
*token
= NULL
;
20703 cp_token
*decl_spec_token_start
= NULL
;
20704 cp_token
*initializer_token_start
= NULL
;
20705 int saved_pedantic
;
20706 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20708 /* Check for the `__extension__' keyword. */
20709 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
20712 cp_parser_member_declaration (parser
);
20713 /* Restore the old value of the PEDANTIC flag. */
20714 pedantic
= saved_pedantic
;
20719 /* Check for a template-declaration. */
20720 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20722 /* An explicit specialization here is an error condition, and we
20723 expect the specialization handler to detect and report this. */
20724 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
20725 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
20726 cp_parser_explicit_specialization (parser
);
20728 cp_parser_template_declaration (parser
, /*member_p=*/true);
20733 /* Check for a using-declaration. */
20734 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
20736 if (cxx_dialect
< cxx11
)
20738 /* Parse the using-declaration. */
20739 cp_parser_using_declaration (parser
,
20740 /*access_declaration_p=*/false);
20746 bool alias_decl_expected
;
20747 cp_parser_parse_tentatively (parser
);
20748 decl
= cp_parser_alias_declaration (parser
);
20749 /* Note that if we actually see the '=' token after the
20750 identifier, cp_parser_alias_declaration commits the
20751 tentative parse. In that case, we really expects an
20752 alias-declaration. Otherwise, we expect a using
20754 alias_decl_expected
=
20755 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
20756 cp_parser_parse_definitely (parser
);
20758 if (alias_decl_expected
)
20759 finish_member_declaration (decl
);
20761 cp_parser_using_declaration (parser
,
20762 /*access_declaration_p=*/false);
20767 /* Check for @defs. */
20768 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
20771 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
20772 ivar
= ivar_chains
;
20776 ivar
= TREE_CHAIN (member
);
20777 TREE_CHAIN (member
) = NULL_TREE
;
20778 finish_member_declaration (member
);
20783 /* If the next token is `static_assert' we have a static assertion. */
20784 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
20786 cp_parser_static_assert (parser
, /*member_p=*/true);
20790 parser
->colon_corrects_to_scope_p
= false;
20792 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20795 /* Parse the decl-specifier-seq. */
20796 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20797 cp_parser_decl_specifier_seq (parser
,
20798 CP_PARSER_FLAGS_OPTIONAL
,
20800 &declares_class_or_enum
);
20801 /* Check for an invalid type-name. */
20802 if (!decl_specifiers
.any_type_specifiers_p
20803 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20805 /* If there is no declarator, then the decl-specifier-seq should
20807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20809 /* If there was no decl-specifier-seq, and the next token is a
20810 `;', then we have something like:
20816 Each member-declaration shall declare at least one member
20817 name of the class. */
20818 if (!decl_specifiers
.any_specifiers_p
)
20820 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20821 if (!in_system_header_at (token
->location
))
20822 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20828 /* See if this declaration is a friend. */
20829 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20830 /* If there were decl-specifiers, check to see if there was
20831 a class-declaration. */
20832 type
= check_tag_decl (&decl_specifiers
,
20833 /*explicit_type_instantiation_p=*/false);
20834 /* Nested classes have already been added to the class, but
20835 a `friend' needs to be explicitly registered. */
20838 /* If the `friend' keyword was present, the friend must
20839 be introduced with a class-key. */
20840 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20841 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20842 "in C++03 a class-key must be used "
20843 "when declaring a friend");
20846 template <typename T> struct A {
20847 friend struct A<T>::B;
20850 A<T>::B will be represented by a TYPENAME_TYPE, and
20851 therefore not recognized by check_tag_decl. */
20854 type
= decl_specifiers
.type
;
20855 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20856 type
= TREE_TYPE (type
);
20858 if (!type
|| !TYPE_P (type
))
20859 error_at (decl_spec_token_start
->location
,
20860 "friend declaration does not name a class or "
20863 make_friend_class (current_class_type
, type
,
20864 /*complain=*/true);
20866 /* If there is no TYPE, an error message will already have
20868 else if (!type
|| type
== error_mark_node
)
20870 /* An anonymous aggregate has to be handled specially; such
20871 a declaration really declares a data member (with a
20872 particular type), as opposed to a nested class. */
20873 else if (ANON_AGGR_TYPE_P (type
))
20876 if (decl_specifiers
.storage_class
!= sc_none
)
20877 error_at (decl_spec_token_start
->location
,
20878 "a storage class on an anonymous aggregate "
20879 "in class scope is not allowed");
20881 /* Remove constructors and such from TYPE, now that we
20882 know it is an anonymous aggregate. */
20883 fixup_anonymous_aggr (type
);
20884 /* And make the corresponding data member. */
20885 decl
= build_decl (decl_spec_token_start
->location
,
20886 FIELD_DECL
, NULL_TREE
, type
);
20887 /* Add it to the class. */
20888 finish_member_declaration (decl
);
20891 cp_parser_check_access_in_redeclaration
20893 decl_spec_token_start
->location
);
20898 bool assume_semicolon
= false;
20900 /* Clear attributes from the decl_specifiers but keep them
20901 around as prefix attributes that apply them to the entity
20903 prefix_attributes
= decl_specifiers
.attributes
;
20904 decl_specifiers
.attributes
= NULL_TREE
;
20906 /* See if these declarations will be friends. */
20907 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20909 /* Keep going until we hit the `;' at the end of the
20911 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20913 tree attributes
= NULL_TREE
;
20914 tree first_attribute
;
20916 /* Peek at the next token. */
20917 token
= cp_lexer_peek_token (parser
->lexer
);
20919 /* Check for a bitfield declaration. */
20920 if (token
->type
== CPP_COLON
20921 || (token
->type
== CPP_NAME
20922 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20928 /* Get the name of the bitfield. Note that we cannot just
20929 check TOKEN here because it may have been invalidated by
20930 the call to cp_lexer_peek_nth_token above. */
20931 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20932 identifier
= cp_parser_identifier (parser
);
20934 identifier
= NULL_TREE
;
20936 /* Consume the `:' token. */
20937 cp_lexer_consume_token (parser
->lexer
);
20938 /* Get the width of the bitfield. */
20940 = cp_parser_constant_expression (parser
);
20942 /* Look for attributes that apply to the bitfield. */
20943 attributes
= cp_parser_attributes_opt (parser
);
20944 /* Remember which attributes are prefix attributes and
20946 first_attribute
= attributes
;
20947 /* Combine the attributes. */
20948 attributes
= chainon (prefix_attributes
, attributes
);
20950 /* Create the bitfield declaration. */
20951 decl
= grokbitfield (identifier
20952 ? make_id_declarator (NULL_TREE
,
20962 cp_declarator
*declarator
;
20964 tree asm_specification
;
20965 int ctor_dtor_or_conv_p
;
20967 /* Parse the declarator. */
20969 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20970 &ctor_dtor_or_conv_p
,
20971 /*parenthesized_p=*/NULL
,
20975 /* If something went wrong parsing the declarator, make sure
20976 that we at least consume some tokens. */
20977 if (declarator
== cp_error_declarator
)
20979 /* Skip to the end of the statement. */
20980 cp_parser_skip_to_end_of_statement (parser
);
20981 /* If the next token is not a semicolon, that is
20982 probably because we just skipped over the body of
20983 a function. So, we consume a semicolon if
20984 present, but do not issue an error message if it
20986 if (cp_lexer_next_token_is (parser
->lexer
,
20988 cp_lexer_consume_token (parser
->lexer
);
20992 if (declares_class_or_enum
& 2)
20993 cp_parser_check_for_definition_in_return_type
20994 (declarator
, decl_specifiers
.type
,
20995 decl_specifiers
.locations
[ds_type_spec
]);
20997 /* Look for an asm-specification. */
20998 asm_specification
= cp_parser_asm_specification_opt (parser
);
20999 /* Look for attributes that apply to the declaration. */
21000 attributes
= cp_parser_attributes_opt (parser
);
21001 /* Remember which attributes are prefix attributes and
21003 first_attribute
= attributes
;
21004 /* Combine the attributes. */
21005 attributes
= chainon (prefix_attributes
, attributes
);
21007 /* If it's an `=', then we have a constant-initializer or a
21008 pure-specifier. It is not correct to parse the
21009 initializer before registering the member declaration
21010 since the member declaration should be in scope while
21011 its initializer is processed. However, the rest of the
21012 front end does not yet provide an interface that allows
21013 us to handle this correctly. */
21014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21018 A pure-specifier shall be used only in the declaration of
21019 a virtual function.
21021 A member-declarator can contain a constant-initializer
21022 only if it declares a static member of integral or
21025 Therefore, if the DECLARATOR is for a function, we look
21026 for a pure-specifier; otherwise, we look for a
21027 constant-initializer. When we call `grokfield', it will
21028 perform more stringent semantics checks. */
21029 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
21030 if (function_declarator_p (declarator
)
21031 || (decl_specifiers
.type
21032 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
21033 && declarator
->kind
== cdk_id
21034 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
21035 == FUNCTION_TYPE
)))
21036 initializer
= cp_parser_pure_specifier (parser
);
21037 else if (decl_specifiers
.storage_class
!= sc_static
)
21038 initializer
= cp_parser_save_nsdmi (parser
);
21039 else if (cxx_dialect
>= cxx11
)
21042 /* Don't require a constant rvalue in C++11, since we
21043 might want a reference constant. We'll enforce
21044 constancy later. */
21045 cp_lexer_consume_token (parser
->lexer
);
21046 /* Parse the initializer. */
21047 initializer
= cp_parser_initializer_clause (parser
,
21051 /* Parse the initializer. */
21052 initializer
= cp_parser_constant_initializer (parser
);
21054 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
21055 && !function_declarator_p (declarator
))
21058 if (decl_specifiers
.storage_class
!= sc_static
)
21059 initializer
= cp_parser_save_nsdmi (parser
);
21061 initializer
= cp_parser_initializer (parser
, &x
, &x
);
21063 /* Otherwise, there is no initializer. */
21065 initializer
= NULL_TREE
;
21067 /* See if we are probably looking at a function
21068 definition. We are certainly not looking at a
21069 member-declarator. Calling `grokfield' has
21070 side-effects, so we must not do it unless we are sure
21071 that we are looking at a member-declarator. */
21072 if (cp_parser_token_starts_function_definition_p
21073 (cp_lexer_peek_token (parser
->lexer
)))
21075 /* The grammar does not allow a pure-specifier to be
21076 used when a member function is defined. (It is
21077 possible that this fact is an oversight in the
21078 standard, since a pure function may be defined
21079 outside of the class-specifier. */
21080 if (initializer
&& initializer_token_start
)
21081 error_at (initializer_token_start
->location
,
21082 "pure-specifier on function-definition");
21083 decl
= cp_parser_save_member_function_body (parser
,
21087 if (parser
->fully_implicit_function_template_p
)
21088 decl
= finish_fully_implicit_template (parser
, decl
);
21089 /* If the member was not a friend, declare it here. */
21091 finish_member_declaration (decl
);
21092 /* Peek at the next token. */
21093 token
= cp_lexer_peek_token (parser
->lexer
);
21094 /* If the next token is a semicolon, consume it. */
21095 if (token
->type
== CPP_SEMICOLON
)
21096 cp_lexer_consume_token (parser
->lexer
);
21100 if (declarator
->kind
== cdk_function
)
21101 declarator
->id_loc
= token
->location
;
21102 /* Create the declaration. */
21103 decl
= grokfield (declarator
, &decl_specifiers
,
21104 initializer
, /*init_const_expr_p=*/true,
21105 asm_specification
, attributes
);
21106 if (parser
->fully_implicit_function_template_p
)
21109 finish_fully_implicit_template (parser
, 0);
21111 decl
= finish_fully_implicit_template (parser
, decl
);
21115 cp_finalize_omp_declare_simd (parser
, decl
);
21117 /* Reset PREFIX_ATTRIBUTES. */
21118 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
21119 attributes
= TREE_CHAIN (attributes
);
21121 TREE_CHAIN (attributes
) = NULL_TREE
;
21123 /* If there is any qualification still in effect, clear it
21124 now; we will be starting fresh with the next declarator. */
21125 parser
->scope
= NULL_TREE
;
21126 parser
->qualifying_scope
= NULL_TREE
;
21127 parser
->object_scope
= NULL_TREE
;
21128 /* If it's a `,', then there are more declarators. */
21129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21131 cp_lexer_consume_token (parser
->lexer
);
21132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21134 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21135 error_at (token
->location
,
21136 "stray %<,%> at end of member declaration");
21139 /* If the next token isn't a `;', then we have a parse error. */
21140 else if (cp_lexer_next_token_is_not (parser
->lexer
,
21143 /* The next token might be a ways away from where the
21144 actual semicolon is missing. Find the previous token
21145 and use that for our error position. */
21146 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21147 error_at (token
->location
,
21148 "expected %<;%> at end of member declaration");
21150 /* Assume that the user meant to provide a semicolon. If
21151 we were to cp_parser_skip_to_end_of_statement, we might
21152 skip to a semicolon inside a member function definition
21153 and issue nonsensical error messages. */
21154 assume_semicolon
= true;
21159 /* Add DECL to the list of members. */
21161 /* Explicitly include, eg, NSDMIs, for better error
21162 recovery (c++/58650). */
21163 || !DECL_DECLARES_FUNCTION_P (decl
))
21164 finish_member_declaration (decl
);
21166 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21167 cp_parser_save_default_args (parser
, decl
);
21168 else if (TREE_CODE (decl
) == FIELD_DECL
21169 && !DECL_C_BIT_FIELD (decl
)
21170 && DECL_INITIAL (decl
))
21171 /* Add DECL to the queue of NSDMI to be parsed later. */
21172 vec_safe_push (unparsed_nsdmis
, decl
);
21175 if (assume_semicolon
)
21180 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21182 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
21185 /* Parse a pure-specifier.
21190 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21191 Otherwise, ERROR_MARK_NODE is returned. */
21194 cp_parser_pure_specifier (cp_parser
* parser
)
21198 /* Look for the `=' token. */
21199 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21200 return error_mark_node
;
21201 /* Look for the `0' token. */
21202 token
= cp_lexer_peek_token (parser
->lexer
);
21204 if (token
->type
== CPP_EOF
21205 || token
->type
== CPP_PRAGMA_EOL
)
21206 return error_mark_node
;
21208 cp_lexer_consume_token (parser
->lexer
);
21210 /* Accept = default or = delete in c++0x mode. */
21211 if (token
->keyword
== RID_DEFAULT
21212 || token
->keyword
== RID_DELETE
)
21214 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
21215 return token
->u
.value
;
21218 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21219 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
21221 cp_parser_error (parser
,
21222 "invalid pure specifier (only %<= 0%> is allowed)");
21223 cp_parser_skip_to_end_of_statement (parser
);
21224 return error_mark_node
;
21226 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21228 error_at (token
->location
, "templates may not be %<virtual%>");
21229 return error_mark_node
;
21232 return integer_zero_node
;
21235 /* Parse a constant-initializer.
21237 constant-initializer:
21238 = constant-expression
21240 Returns a representation of the constant-expression. */
21243 cp_parser_constant_initializer (cp_parser
* parser
)
21245 /* Look for the `=' token. */
21246 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21247 return error_mark_node
;
21249 /* It is invalid to write:
21251 struct S { static const int i = { 7 }; };
21254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21256 cp_parser_error (parser
,
21257 "a brace-enclosed initializer is not allowed here");
21258 /* Consume the opening brace. */
21259 cp_lexer_consume_token (parser
->lexer
);
21260 /* Skip the initializer. */
21261 cp_parser_skip_to_closing_brace (parser
);
21262 /* Look for the trailing `}'. */
21263 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21265 return error_mark_node
;
21268 return cp_parser_constant_expression (parser
);
21271 /* Derived classes [gram.class.derived] */
21273 /* Parse a base-clause.
21276 : base-specifier-list
21278 base-specifier-list:
21279 base-specifier ... [opt]
21280 base-specifier-list , base-specifier ... [opt]
21282 Returns a TREE_LIST representing the base-classes, in the order in
21283 which they were declared. The representation of each node is as
21284 described by cp_parser_base_specifier.
21286 In the case that no bases are specified, this function will return
21287 NULL_TREE, not ERROR_MARK_NODE. */
21290 cp_parser_base_clause (cp_parser
* parser
)
21292 tree bases
= NULL_TREE
;
21294 /* Look for the `:' that begins the list. */
21295 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21297 /* Scan the base-specifier-list. */
21302 bool pack_expansion_p
= false;
21304 /* Look for the base-specifier. */
21305 base
= cp_parser_base_specifier (parser
);
21306 /* Look for the (optional) ellipsis. */
21307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21309 /* Consume the `...'. */
21310 cp_lexer_consume_token (parser
->lexer
);
21312 pack_expansion_p
= true;
21315 /* Add BASE to the front of the list. */
21316 if (base
&& base
!= error_mark_node
)
21318 if (pack_expansion_p
)
21319 /* Make this a pack expansion type. */
21320 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
21322 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
21324 TREE_CHAIN (base
) = bases
;
21328 /* Peek at the next token. */
21329 token
= cp_lexer_peek_token (parser
->lexer
);
21330 /* If it's not a comma, then the list is complete. */
21331 if (token
->type
!= CPP_COMMA
)
21333 /* Consume the `,'. */
21334 cp_lexer_consume_token (parser
->lexer
);
21337 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21338 base class had a qualified name. However, the next name that
21339 appears is certainly not qualified. */
21340 parser
->scope
= NULL_TREE
;
21341 parser
->qualifying_scope
= NULL_TREE
;
21342 parser
->object_scope
= NULL_TREE
;
21344 return nreverse (bases
);
21347 /* Parse a base-specifier.
21350 :: [opt] nested-name-specifier [opt] class-name
21351 virtual access-specifier [opt] :: [opt] nested-name-specifier
21353 access-specifier virtual [opt] :: [opt] nested-name-specifier
21356 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21357 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21358 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21359 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21362 cp_parser_base_specifier (cp_parser
* parser
)
21366 bool virtual_p
= false;
21367 bool duplicate_virtual_error_issued_p
= false;
21368 bool duplicate_access_error_issued_p
= false;
21369 bool class_scope_p
, template_p
;
21370 tree access
= access_default_node
;
21373 /* Process the optional `virtual' and `access-specifier'. */
21376 /* Peek at the next token. */
21377 token
= cp_lexer_peek_token (parser
->lexer
);
21378 /* Process `virtual'. */
21379 switch (token
->keyword
)
21382 /* If `virtual' appears more than once, issue an error. */
21383 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
21385 cp_parser_error (parser
,
21386 "%<virtual%> specified more than once in base-specified");
21387 duplicate_virtual_error_issued_p
= true;
21392 /* Consume the `virtual' token. */
21393 cp_lexer_consume_token (parser
->lexer
);
21398 case RID_PROTECTED
:
21400 /* If more than one access specifier appears, issue an
21402 if (access
!= access_default_node
21403 && !duplicate_access_error_issued_p
)
21405 cp_parser_error (parser
,
21406 "more than one access specifier in base-specified");
21407 duplicate_access_error_issued_p
= true;
21410 access
= ridpointers
[(int) token
->keyword
];
21412 /* Consume the access-specifier. */
21413 cp_lexer_consume_token (parser
->lexer
);
21422 /* It is not uncommon to see programs mechanically, erroneously, use
21423 the 'typename' keyword to denote (dependent) qualified types
21424 as base classes. */
21425 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
21427 token
= cp_lexer_peek_token (parser
->lexer
);
21428 if (!processing_template_decl
)
21429 error_at (token
->location
,
21430 "keyword %<typename%> not allowed outside of templates");
21432 error_at (token
->location
,
21433 "keyword %<typename%> not allowed in this context "
21434 "(the base class is implicitly a type)");
21435 cp_lexer_consume_token (parser
->lexer
);
21438 /* Look for the optional `::' operator. */
21439 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
21440 /* Look for the nested-name-specifier. The simplest way to
21445 The keyword `typename' is not permitted in a base-specifier or
21446 mem-initializer; in these contexts a qualified name that
21447 depends on a template-parameter is implicitly assumed to be a
21450 is to pretend that we have seen the `typename' keyword at this
21452 cp_parser_nested_name_specifier_opt (parser
,
21453 /*typename_keyword_p=*/true,
21454 /*check_dependency_p=*/true,
21456 /*is_declaration=*/true);
21457 /* If the base class is given by a qualified name, assume that names
21458 we see are type names or templates, as appropriate. */
21459 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
21460 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
21463 && cp_lexer_next_token_is_decltype (parser
->lexer
))
21464 /* DR 950 allows decltype as a base-specifier. */
21465 type
= cp_parser_decltype (parser
);
21468 /* Otherwise, look for the class-name. */
21469 type
= cp_parser_class_name (parser
,
21473 /*check_dependency_p=*/true,
21474 /*class_head_p=*/false,
21475 /*is_declaration=*/true);
21476 type
= TREE_TYPE (type
);
21479 if (type
== error_mark_node
)
21480 return error_mark_node
;
21482 return finish_base_specifier (type
, access
, virtual_p
);
21485 /* Exception handling [gram.exception] */
21487 /* Parse an (optional) noexcept-specification.
21489 noexcept-specification:
21490 noexcept ( constant-expression ) [opt]
21492 If no noexcept-specification is present, returns NULL_TREE.
21493 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21494 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21495 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21496 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21497 in which case a boolean condition is returned instead. */
21500 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
21501 bool require_constexpr
,
21502 bool* consumed_expr
,
21506 const char *saved_message
;
21508 /* Peek at the next token. */
21509 token
= cp_lexer_peek_token (parser
->lexer
);
21511 /* Is it a noexcept-specification? */
21512 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
21515 cp_lexer_consume_token (parser
->lexer
);
21517 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
21519 cp_lexer_consume_token (parser
->lexer
);
21521 if (require_constexpr
)
21523 /* Types may not be defined in an exception-specification. */
21524 saved_message
= parser
->type_definition_forbidden_message
;
21525 parser
->type_definition_forbidden_message
21526 = G_("types may not be defined in an exception-specification");
21528 expr
= cp_parser_constant_expression (parser
);
21530 /* Restore the saved message. */
21531 parser
->type_definition_forbidden_message
= saved_message
;
21535 expr
= cp_parser_expression (parser
);
21536 *consumed_expr
= true;
21539 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21543 expr
= boolean_true_node
;
21544 if (!require_constexpr
)
21545 *consumed_expr
= false;
21548 /* We cannot build a noexcept-spec right away because this will check
21549 that expr is a constexpr. */
21551 return build_noexcept_spec (expr
, tf_warning_or_error
);
21559 /* Parse an (optional) exception-specification.
21561 exception-specification:
21562 throw ( type-id-list [opt] )
21564 Returns a TREE_LIST representing the exception-specification. The
21565 TREE_VALUE of each node is a type. */
21568 cp_parser_exception_specification_opt (cp_parser
* parser
)
21572 const char *saved_message
;
21574 /* Peek at the next token. */
21575 token
= cp_lexer_peek_token (parser
->lexer
);
21577 /* Is it a noexcept-specification? */
21578 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
21580 if (type_id_list
!= NULL_TREE
)
21581 return type_id_list
;
21583 /* If it's not `throw', then there's no exception-specification. */
21584 if (!cp_parser_is_keyword (token
, RID_THROW
))
21588 /* Enable this once a lot of code has transitioned to noexcept? */
21589 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
21590 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
21591 "deprecated in C++0x; use %<noexcept%> instead");
21594 /* Consume the `throw'. */
21595 cp_lexer_consume_token (parser
->lexer
);
21597 /* Look for the `('. */
21598 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21600 /* Peek at the next token. */
21601 token
= cp_lexer_peek_token (parser
->lexer
);
21602 /* If it's not a `)', then there is a type-id-list. */
21603 if (token
->type
!= CPP_CLOSE_PAREN
)
21605 /* Types may not be defined in an exception-specification. */
21606 saved_message
= parser
->type_definition_forbidden_message
;
21607 parser
->type_definition_forbidden_message
21608 = G_("types may not be defined in an exception-specification");
21609 /* Parse the type-id-list. */
21610 type_id_list
= cp_parser_type_id_list (parser
);
21611 /* Restore the saved message. */
21612 parser
->type_definition_forbidden_message
= saved_message
;
21615 type_id_list
= empty_except_spec
;
21617 /* Look for the `)'. */
21618 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21620 return type_id_list
;
21623 /* Parse an (optional) type-id-list.
21627 type-id-list , type-id ... [opt]
21629 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21630 in the order that the types were presented. */
21633 cp_parser_type_id_list (cp_parser
* parser
)
21635 tree types
= NULL_TREE
;
21642 /* Get the next type-id. */
21643 type
= cp_parser_type_id (parser
);
21644 /* Parse the optional ellipsis. */
21645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21647 /* Consume the `...'. */
21648 cp_lexer_consume_token (parser
->lexer
);
21650 /* Turn the type into a pack expansion expression. */
21651 type
= make_pack_expansion (type
);
21653 /* Add it to the list. */
21654 types
= add_exception_specifier (types
, type
, /*complain=*/1);
21655 /* Peek at the next token. */
21656 token
= cp_lexer_peek_token (parser
->lexer
);
21657 /* If it is not a `,', we are done. */
21658 if (token
->type
!= CPP_COMMA
)
21660 /* Consume the `,'. */
21661 cp_lexer_consume_token (parser
->lexer
);
21664 return nreverse (types
);
21667 /* Parse a try-block.
21670 try compound-statement handler-seq */
21673 cp_parser_try_block (cp_parser
* parser
)
21677 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
21678 if (parser
->in_function_body
21679 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
21680 error ("%<try%> in %<constexpr%> function");
21682 try_block
= begin_try_block ();
21683 cp_parser_compound_statement (parser
, NULL
, true, false);
21684 finish_try_block (try_block
);
21685 cp_parser_handler_seq (parser
);
21686 finish_handler_sequence (try_block
);
21691 /* Parse a function-try-block.
21693 function-try-block:
21694 try ctor-initializer [opt] function-body handler-seq */
21697 cp_parser_function_try_block (cp_parser
* parser
)
21699 tree compound_stmt
;
21701 bool ctor_initializer_p
;
21703 /* Look for the `try' keyword. */
21704 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
21706 /* Let the rest of the front end know where we are. */
21707 try_block
= begin_function_try_block (&compound_stmt
);
21708 /* Parse the function-body. */
21709 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21710 (parser
, /*in_function_try_block=*/true);
21711 /* We're done with the `try' part. */
21712 finish_function_try_block (try_block
);
21713 /* Parse the handlers. */
21714 cp_parser_handler_seq (parser
);
21715 /* We're done with the handlers. */
21716 finish_function_handler_sequence (try_block
, compound_stmt
);
21718 return ctor_initializer_p
;
21721 /* Parse a handler-seq.
21724 handler handler-seq [opt] */
21727 cp_parser_handler_seq (cp_parser
* parser
)
21733 /* Parse the handler. */
21734 cp_parser_handler (parser
);
21735 /* Peek at the next token. */
21736 token
= cp_lexer_peek_token (parser
->lexer
);
21737 /* If it's not `catch' then there are no more handlers. */
21738 if (!cp_parser_is_keyword (token
, RID_CATCH
))
21743 /* Parse a handler.
21746 catch ( exception-declaration ) compound-statement */
21749 cp_parser_handler (cp_parser
* parser
)
21754 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
21755 handler
= begin_handler ();
21756 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21757 declaration
= cp_parser_exception_declaration (parser
);
21758 finish_handler_parms (declaration
, handler
);
21759 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21760 cp_parser_compound_statement (parser
, NULL
, false, false);
21761 finish_handler (handler
);
21764 /* Parse an exception-declaration.
21766 exception-declaration:
21767 type-specifier-seq declarator
21768 type-specifier-seq abstract-declarator
21772 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21773 ellipsis variant is used. */
21776 cp_parser_exception_declaration (cp_parser
* parser
)
21778 cp_decl_specifier_seq type_specifiers
;
21779 cp_declarator
*declarator
;
21780 const char *saved_message
;
21782 /* If it's an ellipsis, it's easy to handle. */
21783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21785 /* Consume the `...' token. */
21786 cp_lexer_consume_token (parser
->lexer
);
21790 /* Types may not be defined in exception-declarations. */
21791 saved_message
= parser
->type_definition_forbidden_message
;
21792 parser
->type_definition_forbidden_message
21793 = G_("types may not be defined in exception-declarations");
21795 /* Parse the type-specifier-seq. */
21796 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
21797 /*is_trailing_return=*/false,
21799 /* If it's a `)', then there is no declarator. */
21800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21803 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21804 /*ctor_dtor_or_conv_p=*/NULL
,
21805 /*parenthesized_p=*/NULL
,
21806 /*member_p=*/false,
21807 /*friend_p=*/false);
21809 /* Restore the saved message. */
21810 parser
->type_definition_forbidden_message
= saved_message
;
21812 if (!type_specifiers
.any_specifiers_p
)
21813 return error_mark_node
;
21815 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21818 /* Parse a throw-expression.
21821 throw assignment-expression [opt]
21823 Returns a THROW_EXPR representing the throw-expression. */
21826 cp_parser_throw_expression (cp_parser
* parser
)
21831 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21832 token
= cp_lexer_peek_token (parser
->lexer
);
21833 /* Figure out whether or not there is an assignment-expression
21834 following the "throw" keyword. */
21835 if (token
->type
== CPP_COMMA
21836 || token
->type
== CPP_SEMICOLON
21837 || token
->type
== CPP_CLOSE_PAREN
21838 || token
->type
== CPP_CLOSE_SQUARE
21839 || token
->type
== CPP_CLOSE_BRACE
21840 || token
->type
== CPP_COLON
)
21841 expression
= NULL_TREE
;
21843 expression
= cp_parser_assignment_expression (parser
);
21845 return build_throw (expression
);
21848 /* GNU Extensions */
21850 /* Parse an (optional) asm-specification.
21853 asm ( string-literal )
21855 If the asm-specification is present, returns a STRING_CST
21856 corresponding to the string-literal. Otherwise, returns
21860 cp_parser_asm_specification_opt (cp_parser
* parser
)
21863 tree asm_specification
;
21865 /* Peek at the next token. */
21866 token
= cp_lexer_peek_token (parser
->lexer
);
21867 /* If the next token isn't the `asm' keyword, then there's no
21868 asm-specification. */
21869 if (!cp_parser_is_keyword (token
, RID_ASM
))
21872 /* Consume the `asm' token. */
21873 cp_lexer_consume_token (parser
->lexer
);
21874 /* Look for the `('. */
21875 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21877 /* Look for the string-literal. */
21878 asm_specification
= cp_parser_string_literal (parser
, false, false);
21880 /* Look for the `)'. */
21881 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21883 return asm_specification
;
21886 /* Parse an asm-operand-list.
21890 asm-operand-list , asm-operand
21893 string-literal ( expression )
21894 [ string-literal ] string-literal ( expression )
21896 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21897 each node is the expression. The TREE_PURPOSE is itself a
21898 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21899 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21900 is a STRING_CST for the string literal before the parenthesis. Returns
21901 ERROR_MARK_NODE if any of the operands are invalid. */
21904 cp_parser_asm_operand_list (cp_parser
* parser
)
21906 tree asm_operands
= NULL_TREE
;
21907 bool invalid_operands
= false;
21911 tree string_literal
;
21915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21917 /* Consume the `[' token. */
21918 cp_lexer_consume_token (parser
->lexer
);
21919 /* Read the operand name. */
21920 name
= cp_parser_identifier (parser
);
21921 if (name
!= error_mark_node
)
21922 name
= build_string (IDENTIFIER_LENGTH (name
),
21923 IDENTIFIER_POINTER (name
));
21924 /* Look for the closing `]'. */
21925 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21929 /* Look for the string-literal. */
21930 string_literal
= cp_parser_string_literal (parser
, false, false);
21932 /* Look for the `('. */
21933 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21934 /* Parse the expression. */
21935 expression
= cp_parser_expression (parser
);
21936 /* Look for the `)'. */
21937 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21939 if (name
== error_mark_node
21940 || string_literal
== error_mark_node
21941 || expression
== error_mark_node
)
21942 invalid_operands
= true;
21944 /* Add this operand to the list. */
21945 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21948 /* If the next token is not a `,', there are no more
21950 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21952 /* Consume the `,'. */
21953 cp_lexer_consume_token (parser
->lexer
);
21956 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21959 /* Parse an asm-clobber-list.
21963 asm-clobber-list , string-literal
21965 Returns a TREE_LIST, indicating the clobbers in the order that they
21966 appeared. The TREE_VALUE of each node is a STRING_CST. */
21969 cp_parser_asm_clobber_list (cp_parser
* parser
)
21971 tree clobbers
= NULL_TREE
;
21975 tree string_literal
;
21977 /* Look for the string literal. */
21978 string_literal
= cp_parser_string_literal (parser
, false, false);
21979 /* Add it to the list. */
21980 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21981 /* If the next token is not a `,', then the list is
21983 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21985 /* Consume the `,' token. */
21986 cp_lexer_consume_token (parser
->lexer
);
21992 /* Parse an asm-label-list.
21996 asm-label-list , identifier
21998 Returns a TREE_LIST, indicating the labels in the order that they
21999 appeared. The TREE_VALUE of each node is a label. */
22002 cp_parser_asm_label_list (cp_parser
* parser
)
22004 tree labels
= NULL_TREE
;
22008 tree identifier
, label
, name
;
22010 /* Look for the identifier. */
22011 identifier
= cp_parser_identifier (parser
);
22012 if (!error_operand_p (identifier
))
22014 label
= lookup_label (identifier
);
22015 if (TREE_CODE (label
) == LABEL_DECL
)
22017 TREE_USED (label
) = 1;
22018 check_goto (label
);
22019 name
= build_string (IDENTIFIER_LENGTH (identifier
),
22020 IDENTIFIER_POINTER (identifier
));
22021 labels
= tree_cons (name
, label
, labels
);
22024 /* If the next token is not a `,', then the list is
22026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22028 /* Consume the `,' token. */
22029 cp_lexer_consume_token (parser
->lexer
);
22032 return nreverse (labels
);
22035 /* Return TRUE iff the next tokens in the stream are possibly the
22036 beginning of a GNU extension attribute. */
22039 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
22041 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
22044 /* Return TRUE iff the next tokens in the stream are possibly the
22045 beginning of a standard C++-11 attribute specifier. */
22048 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
22050 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
22053 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22054 beginning of a standard C++-11 attribute specifier. */
22057 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
22059 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22061 return (cxx_dialect
>= cxx11
22062 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
22063 || (token
->type
== CPP_OPEN_SQUARE
22064 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
22065 && token
->type
== CPP_OPEN_SQUARE
)));
22068 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22069 beginning of a GNU extension attribute. */
22072 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
22074 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22076 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
22079 /* Return true iff the next tokens can be the beginning of either a
22080 GNU attribute list, or a standard C++11 attribute sequence. */
22083 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
22085 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
22086 || cp_next_tokens_can_be_std_attribute_p (parser
));
22089 /* Return true iff the next Nth tokens can be the beginning of either
22090 a GNU attribute list, or a standard C++11 attribute sequence. */
22093 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
22095 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
22096 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
22099 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22100 of GNU attributes, or return NULL. */
22103 cp_parser_attributes_opt (cp_parser
*parser
)
22105 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
22106 return cp_parser_gnu_attributes_opt (parser
);
22107 return cp_parser_std_attribute_spec_seq (parser
);
22110 #define CILK_SIMD_FN_CLAUSE_MASK \
22111 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22112 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22113 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22114 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22115 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22117 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22118 vector [(<clauses>)] */
22121 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
22123 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
22124 cp_token
*token
= v_token
;
22127 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
22128 parser
->cilk_simd_fn_info
->error_seen
= false;
22129 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
22130 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
22132 int paren_scope
= 0;
22133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22135 cp_lexer_consume_token (parser
->lexer
);
22136 v_token
= cp_lexer_peek_token (parser
->lexer
);
22139 while (paren_scope
> 0)
22141 token
= cp_lexer_peek_token (parser
->lexer
);
22142 if (token
->type
== CPP_OPEN_PAREN
)
22144 else if (token
->type
== CPP_CLOSE_PAREN
)
22146 /* Do not push the last ')' */
22147 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
22148 cp_lexer_consume_token (parser
->lexer
);
22151 token
->type
= CPP_PRAGMA_EOL
;
22152 parser
->lexer
->next_token
= token
;
22153 cp_lexer_consume_token (parser
->lexer
);
22155 struct cp_token_cache
*cp
22156 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
22157 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
22160 /* Parse an (optional) series of attributes.
22163 attributes attribute
22166 __attribute__ (( attribute-list [opt] ))
22168 The return value is as for cp_parser_gnu_attribute_list. */
22171 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
22173 tree attributes
= NULL_TREE
;
22178 tree attribute_list
;
22181 /* Peek at the next token. */
22182 token
= cp_lexer_peek_token (parser
->lexer
);
22183 /* If it's not `__attribute__', then we're done. */
22184 if (token
->keyword
!= RID_ATTRIBUTE
)
22187 /* Consume the `__attribute__' keyword. */
22188 cp_lexer_consume_token (parser
->lexer
);
22189 /* Look for the two `(' tokens. */
22190 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22191 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22193 /* Peek at the next token. */
22194 token
= cp_lexer_peek_token (parser
->lexer
);
22195 if (token
->type
!= CPP_CLOSE_PAREN
)
22196 /* Parse the attribute-list. */
22197 attribute_list
= cp_parser_gnu_attribute_list (parser
);
22199 /* If the next token is a `)', then there is no attribute
22201 attribute_list
= NULL
;
22203 /* Look for the two `)' tokens. */
22204 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22206 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22209 cp_parser_skip_to_end_of_statement (parser
);
22211 /* Add these new attributes to the list. */
22212 attributes
= chainon (attributes
, attribute_list
);
22218 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22219 "__vector" or "__vector__." */
22222 is_cilkplus_vector_p (tree name
)
22224 if (flag_cilkplus
&& is_attribute_p ("vector", name
))
22229 /* Parse a GNU attribute-list.
22233 attribute-list , attribute
22237 identifier ( identifier )
22238 identifier ( identifier , expression-list )
22239 identifier ( expression-list )
22241 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22242 to an attribute. The TREE_PURPOSE of each node is the identifier
22243 indicating which attribute is in use. The TREE_VALUE represents
22244 the arguments, if any. */
22247 cp_parser_gnu_attribute_list (cp_parser
* parser
)
22249 tree attribute_list
= NULL_TREE
;
22250 bool save_translate_strings_p
= parser
->translate_strings_p
;
22252 parser
->translate_strings_p
= false;
22259 /* Look for the identifier. We also allow keywords here; for
22260 example `__attribute__ ((const))' is legal. */
22261 token
= cp_lexer_peek_token (parser
->lexer
);
22262 if (token
->type
== CPP_NAME
22263 || token
->type
== CPP_KEYWORD
)
22265 tree arguments
= NULL_TREE
;
22267 /* Consume the token, but save it since we need it for the
22268 SIMD enabled function parsing. */
22269 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
22271 /* Save away the identifier that indicates which attribute
22273 identifier
= (token
->type
== CPP_KEYWORD
)
22274 /* For keywords, use the canonical spelling, not the
22275 parsed identifier. */
22276 ? ridpointers
[(int) token
->keyword
]
22277 : id_token
->u
.value
;
22279 attribute
= build_tree_list (identifier
, NULL_TREE
);
22281 /* Peek at the next token. */
22282 token
= cp_lexer_peek_token (parser
->lexer
);
22283 /* If it's an `(', then parse the attribute arguments. */
22284 if (token
->type
== CPP_OPEN_PAREN
)
22286 vec
<tree
, va_gc
> *vec
;
22287 int attr_flag
= (attribute_takes_identifier_p (identifier
)
22288 ? id_attr
: normal_attr
);
22289 if (is_cilkplus_vector_p (identifier
))
22291 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22295 vec
= cp_parser_parenthesized_expression_list
22296 (parser
, attr_flag
, /*cast_p=*/false,
22297 /*allow_expansion_p=*/false,
22298 /*non_constant_p=*/NULL
);
22300 arguments
= error_mark_node
;
22303 arguments
= build_tree_list_vec (vec
);
22304 release_tree_vector (vec
);
22306 /* Save the arguments away. */
22307 TREE_VALUE (attribute
) = arguments
;
22309 else if (is_cilkplus_vector_p (identifier
))
22311 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22315 if (arguments
!= error_mark_node
)
22317 /* Add this attribute to the list. */
22318 TREE_CHAIN (attribute
) = attribute_list
;
22319 attribute_list
= attribute
;
22322 token
= cp_lexer_peek_token (parser
->lexer
);
22324 /* Now, look for more attributes. If the next token isn't a
22325 `,', we're done. */
22326 if (token
->type
!= CPP_COMMA
)
22329 /* Consume the comma and keep going. */
22330 cp_lexer_consume_token (parser
->lexer
);
22332 parser
->translate_strings_p
= save_translate_strings_p
;
22334 /* We built up the list in reverse order. */
22335 return nreverse (attribute_list
);
22338 /* Parse a standard C++11 attribute.
22340 The returned representation is a TREE_LIST which TREE_PURPOSE is
22341 the scoped name of the attribute, and the TREE_VALUE is its
22344 Note that the scoped name of the attribute is itself a TREE_LIST
22345 which TREE_PURPOSE is the namespace of the attribute, and
22346 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22347 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22348 and which TREE_PURPOSE is directly the attribute name.
22350 Clients of the attribute code should use get_attribute_namespace
22351 and get_attribute_name to get the actual namespace and name of
22352 attributes, regardless of their being GNU or C++11 attributes.
22355 attribute-token attribute-argument-clause [opt]
22359 attribute-scoped-token
22361 attribute-scoped-token:
22362 attribute-namespace :: identifier
22364 attribute-namespace:
22367 attribute-argument-clause:
22368 ( balanced-token-seq )
22370 balanced-token-seq:
22371 balanced-token [opt]
22372 balanced-token-seq balanced-token
22375 ( balanced-token-seq )
22376 [ balanced-token-seq ]
22377 { balanced-token-seq }. */
22380 cp_parser_std_attribute (cp_parser
*parser
)
22382 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
22385 /* First, parse name of the the attribute, a.k.a
22386 attribute-token. */
22388 token
= cp_lexer_peek_token (parser
->lexer
);
22389 if (token
->type
== CPP_NAME
)
22390 attr_id
= token
->u
.value
;
22391 else if (token
->type
== CPP_KEYWORD
)
22392 attr_id
= ridpointers
[(int) token
->keyword
];
22393 else if (token
->flags
& NAMED_OP
)
22394 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
22396 if (attr_id
== NULL_TREE
)
22399 cp_lexer_consume_token (parser
->lexer
);
22401 token
= cp_lexer_peek_token (parser
->lexer
);
22402 if (token
->type
== CPP_SCOPE
)
22404 /* We are seeing a scoped attribute token. */
22406 cp_lexer_consume_token (parser
->lexer
);
22409 token
= cp_lexer_consume_token (parser
->lexer
);
22410 if (token
->type
== CPP_NAME
)
22411 attr_id
= token
->u
.value
;
22412 else if (token
->type
== CPP_KEYWORD
)
22413 attr_id
= ridpointers
[(int) token
->keyword
];
22416 error_at (token
->location
,
22417 "expected an identifier for the attribute name");
22418 return error_mark_node
;
22420 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
22422 token
= cp_lexer_peek_token (parser
->lexer
);
22426 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
22428 /* C++11 noreturn attribute is equivalent to GNU's. */
22429 if (is_attribute_p ("noreturn", attr_id
))
22430 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22431 /* C++14 deprecated attribute is equivalent to GNU's. */
22432 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
22434 if (cxx_dialect
== cxx11
)
22435 pedwarn (token
->location
, OPT_Wpedantic
,
22436 "%<deprecated%> is a C++14 feature;"
22437 " use %<gnu::deprecated%>");
22438 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22442 /* Now parse the optional argument clause of the attribute. */
22444 if (token
->type
!= CPP_OPEN_PAREN
)
22448 vec
<tree
, va_gc
> *vec
;
22449 int attr_flag
= normal_attr
;
22451 if (attr_ns
== get_identifier ("gnu")
22452 && attribute_takes_identifier_p (attr_id
))
22453 /* A GNU attribute that takes an identifier in parameter. */
22454 attr_flag
= id_attr
;
22456 vec
= cp_parser_parenthesized_expression_list
22457 (parser
, attr_flag
, /*cast_p=*/false,
22458 /*allow_expansion_p=*/true,
22459 /*non_constant_p=*/NULL
);
22461 arguments
= error_mark_node
;
22464 arguments
= build_tree_list_vec (vec
);
22465 release_tree_vector (vec
);
22468 if (arguments
== error_mark_node
)
22469 attribute
= error_mark_node
;
22471 TREE_VALUE (attribute
) = arguments
;
22477 /* Parse a list of standard C++-11 attributes.
22481 attribute-list , attribute[opt]
22483 attribute-list , attribute ...
22487 cp_parser_std_attribute_list (cp_parser
*parser
)
22489 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
22490 cp_token
*token
= NULL
;
22494 attribute
= cp_parser_std_attribute (parser
);
22495 if (attribute
== error_mark_node
)
22497 if (attribute
!= NULL_TREE
)
22499 TREE_CHAIN (attribute
) = attributes
;
22500 attributes
= attribute
;
22502 token
= cp_lexer_peek_token (parser
->lexer
);
22503 if (token
->type
!= CPP_COMMA
)
22505 cp_lexer_consume_token (parser
->lexer
);
22507 attributes
= nreverse (attributes
);
22511 /* Parse a standard C++-11 attribute specifier.
22513 attribute-specifier:
22514 [ [ attribute-list ] ]
22515 alignment-specifier
22517 alignment-specifier:
22518 alignas ( type-id ... [opt] )
22519 alignas ( alignment-expression ... [opt] ). */
22522 cp_parser_std_attribute_spec (cp_parser
*parser
)
22524 tree attributes
= NULL_TREE
;
22525 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22527 if (token
->type
== CPP_OPEN_SQUARE
22528 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
22530 cp_lexer_consume_token (parser
->lexer
);
22531 cp_lexer_consume_token (parser
->lexer
);
22533 attributes
= cp_parser_std_attribute_list (parser
);
22535 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
22536 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
22537 cp_parser_skip_to_end_of_statement (parser
);
22539 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22540 when we are sure that we have actually parsed them. */
22541 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22547 /* Look for an alignment-specifier. */
22549 token
= cp_lexer_peek_token (parser
->lexer
);
22551 if (token
->type
!= CPP_KEYWORD
22552 || token
->keyword
!= RID_ALIGNAS
)
22555 cp_lexer_consume_token (parser
->lexer
);
22556 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22558 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
22560 cp_parser_error (parser
, "expected %<(%>");
22561 return error_mark_node
;
22564 cp_parser_parse_tentatively (parser
);
22565 alignas_expr
= cp_parser_type_id (parser
);
22567 if (!cp_parser_parse_definitely (parser
))
22569 gcc_assert (alignas_expr
== error_mark_node
22570 || alignas_expr
== NULL_TREE
);
22573 cp_parser_assignment_expression (parser
);
22574 if (alignas_expr
== error_mark_node
)
22575 cp_parser_skip_to_end_of_statement (parser
);
22576 if (alignas_expr
== NULL_TREE
22577 || alignas_expr
== error_mark_node
)
22578 return alignas_expr
;
22581 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
22583 cp_parser_error (parser
, "expected %<)%>");
22584 return error_mark_node
;
22587 alignas_expr
= cxx_alignas_expr (alignas_expr
);
22589 /* Build the C++-11 representation of an 'aligned'
22592 build_tree_list (build_tree_list (get_identifier ("gnu"),
22593 get_identifier ("aligned")),
22594 build_tree_list (NULL_TREE
, alignas_expr
));
22600 /* Parse a standard C++-11 attribute-specifier-seq.
22602 attribute-specifier-seq:
22603 attribute-specifier-seq [opt] attribute-specifier
22607 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
22609 tree attr_specs
= NULL
;
22613 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
22614 if (attr_spec
== NULL_TREE
)
22616 if (attr_spec
== error_mark_node
)
22617 return error_mark_node
;
22619 TREE_CHAIN (attr_spec
) = attr_specs
;
22620 attr_specs
= attr_spec
;
22623 attr_specs
= nreverse (attr_specs
);
22627 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22628 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22629 current value of the PEDANTIC flag, regardless of whether or not
22630 the `__extension__' keyword is present. The caller is responsible
22631 for restoring the value of the PEDANTIC flag. */
22634 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
22636 /* Save the old value of the PEDANTIC flag. */
22637 *saved_pedantic
= pedantic
;
22639 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
22641 /* Consume the `__extension__' token. */
22642 cp_lexer_consume_token (parser
->lexer
);
22643 /* We're not being pedantic while the `__extension__' keyword is
22653 /* Parse a label declaration.
22656 __label__ label-declarator-seq ;
22658 label-declarator-seq:
22659 identifier , label-declarator-seq
22663 cp_parser_label_declaration (cp_parser
* parser
)
22665 /* Look for the `__label__' keyword. */
22666 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
22672 /* Look for an identifier. */
22673 identifier
= cp_parser_identifier (parser
);
22674 /* If we failed, stop. */
22675 if (identifier
== error_mark_node
)
22677 /* Declare it as a label. */
22678 finish_label_decl (identifier
);
22679 /* If the next token is a `;', stop. */
22680 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22682 /* Look for the `,' separating the label declarations. */
22683 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
22686 /* Look for the final `;'. */
22687 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22690 /* Support Functions */
22692 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22693 NAME should have one of the representations used for an
22694 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22695 is returned. If PARSER->SCOPE is a dependent type, then a
22696 SCOPE_REF is returned.
22698 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22699 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22700 was formed. Abstractly, such entities should not be passed to this
22701 function, because they do not need to be looked up, but it is
22702 simpler to check for this special case here, rather than at the
22705 In cases not explicitly covered above, this function returns a
22706 DECL, OVERLOAD, or baselink representing the result of the lookup.
22707 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22710 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22711 (e.g., "struct") that was used. In that case bindings that do not
22712 refer to types are ignored.
22714 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22717 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22720 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22723 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22724 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22725 NULL_TREE otherwise. */
22728 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
22729 enum tag_types tag_type
,
22732 bool check_dependency
,
22733 tree
*ambiguous_decls
,
22734 location_t name_location
)
22737 tree object_type
= parser
->context
->object_type
;
22739 /* Assume that the lookup will be unambiguous. */
22740 if (ambiguous_decls
)
22741 *ambiguous_decls
= NULL_TREE
;
22743 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22744 no longer valid. Note that if we are parsing tentatively, and
22745 the parse fails, OBJECT_TYPE will be automatically restored. */
22746 parser
->context
->object_type
= NULL_TREE
;
22748 if (name
== error_mark_node
)
22749 return error_mark_node
;
22751 /* A template-id has already been resolved; there is no lookup to
22753 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
22755 if (BASELINK_P (name
))
22757 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
22758 == TEMPLATE_ID_EXPR
);
22762 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22763 it should already have been checked to make sure that the name
22764 used matches the type being destroyed. */
22765 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
22769 /* Figure out to which type this destructor applies. */
22771 type
= parser
->scope
;
22772 else if (object_type
)
22773 type
= object_type
;
22775 type
= current_class_type
;
22776 /* If that's not a class type, there is no destructor. */
22777 if (!type
|| !CLASS_TYPE_P (type
))
22778 return error_mark_node
;
22779 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
22780 lazily_declare_fn (sfk_destructor
, type
);
22781 if (!CLASSTYPE_DESTRUCTORS (type
))
22782 return error_mark_node
;
22783 /* If it was a class type, return the destructor. */
22784 return CLASSTYPE_DESTRUCTORS (type
);
22787 /* By this point, the NAME should be an ordinary identifier. If
22788 the id-expression was a qualified name, the qualifying scope is
22789 stored in PARSER->SCOPE at this point. */
22790 gcc_assert (identifier_p (name
));
22792 /* Perform the lookup. */
22797 if (parser
->scope
== error_mark_node
)
22798 return error_mark_node
;
22800 /* If the SCOPE is dependent, the lookup must be deferred until
22801 the template is instantiated -- unless we are explicitly
22802 looking up names in uninstantiated templates. Even then, we
22803 cannot look up the name if the scope is not a class type; it
22804 might, for example, be a template type parameter. */
22805 dependent_p
= (TYPE_P (parser
->scope
)
22806 && dependent_scope_p (parser
->scope
));
22807 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
22809 /* Defer lookup. */
22810 decl
= error_mark_node
;
22813 tree pushed_scope
= NULL_TREE
;
22815 /* If PARSER->SCOPE is a dependent type, then it must be a
22816 class type, and we must not be checking dependencies;
22817 otherwise, we would have processed this lookup above. So
22818 that PARSER->SCOPE is not considered a dependent base by
22819 lookup_member, we must enter the scope here. */
22821 pushed_scope
= push_scope (parser
->scope
);
22823 /* If the PARSER->SCOPE is a template specialization, it
22824 may be instantiated during name lookup. In that case,
22825 errors may be issued. Even if we rollback the current
22826 tentative parse, those errors are valid. */
22827 decl
= lookup_qualified_name (parser
->scope
, name
,
22828 tag_type
!= none_type
,
22829 /*complain=*/true);
22831 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22832 lookup result and the nested-name-specifier nominates a class C:
22833 * if the name specified after the nested-name-specifier, when
22834 looked up in C, is the injected-class-name of C (Clause 9), or
22835 * if the name specified after the nested-name-specifier is the
22836 same as the identifier or the simple-template-id's template-
22837 name in the last component of the nested-name-specifier,
22838 the name is instead considered to name the constructor of
22839 class C. [ Note: for example, the constructor is not an
22840 acceptable lookup result in an elaborated-type-specifier so
22841 the constructor would not be used in place of the
22842 injected-class-name. --end note ] Such a constructor name
22843 shall be used only in the declarator-id of a declaration that
22844 names a constructor or in a using-declaration. */
22845 if (tag_type
== none_type
22846 && DECL_SELF_REFERENCE_P (decl
)
22847 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
22848 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
22849 tag_type
!= none_type
,
22850 /*complain=*/true);
22852 /* If we have a single function from a using decl, pull it out. */
22853 if (TREE_CODE (decl
) == OVERLOAD
22854 && !really_overloaded_fn (decl
))
22855 decl
= OVL_FUNCTION (decl
);
22858 pop_scope (pushed_scope
);
22861 /* If the scope is a dependent type and either we deferred lookup or
22862 we did lookup but didn't find the name, rememeber the name. */
22863 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
22864 && dependent_type_p (parser
->scope
))
22870 /* The resolution to Core Issue 180 says that `struct
22871 A::B' should be considered a type-name, even if `A'
22873 type
= make_typename_type (parser
->scope
, name
, tag_type
,
22874 /*complain=*/tf_error
);
22875 if (type
!= error_mark_node
)
22876 decl
= TYPE_NAME (type
);
22878 else if (is_template
22879 && (cp_parser_next_token_ends_template_argument_p (parser
)
22880 || cp_lexer_next_token_is (parser
->lexer
,
22882 decl
= make_unbound_class_template (parser
->scope
,
22884 /*complain=*/tf_error
);
22886 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22887 parser
->scope
, name
,
22890 parser
->qualifying_scope
= parser
->scope
;
22891 parser
->object_scope
= NULL_TREE
;
22893 else if (object_type
)
22895 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22896 OBJECT_TYPE is not a class. */
22897 if (CLASS_TYPE_P (object_type
))
22898 /* If the OBJECT_TYPE is a template specialization, it may
22899 be instantiated during name lookup. In that case, errors
22900 may be issued. Even if we rollback the current tentative
22901 parse, those errors are valid. */
22902 decl
= lookup_member (object_type
,
22905 tag_type
!= none_type
,
22906 tf_warning_or_error
);
22911 /* Look it up in the enclosing context. */
22912 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22914 /*block_p=*/true, is_namespace
, 0);
22915 parser
->object_scope
= object_type
;
22916 parser
->qualifying_scope
= NULL_TREE
;
22920 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22922 /*block_p=*/true, is_namespace
, 0);
22923 parser
->qualifying_scope
= NULL_TREE
;
22924 parser
->object_scope
= NULL_TREE
;
22927 /* If the lookup failed, let our caller know. */
22928 if (!decl
|| decl
== error_mark_node
)
22929 return error_mark_node
;
22931 /* Pull out the template from an injected-class-name (or multiple). */
22933 decl
= maybe_get_template_decl_from_type_decl (decl
);
22935 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22936 if (TREE_CODE (decl
) == TREE_LIST
)
22938 if (ambiguous_decls
)
22939 *ambiguous_decls
= decl
;
22940 /* The error message we have to print is too complicated for
22941 cp_parser_error, so we incorporate its actions directly. */
22942 if (!cp_parser_simulate_error (parser
))
22944 error_at (name_location
, "reference to %qD is ambiguous",
22946 print_candidates (decl
);
22948 return error_mark_node
;
22951 gcc_assert (DECL_P (decl
)
22952 || TREE_CODE (decl
) == OVERLOAD
22953 || TREE_CODE (decl
) == SCOPE_REF
22954 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
22955 || BASELINK_P (decl
));
22957 /* If we have resolved the name of a member declaration, check to
22958 see if the declaration is accessible. When the name resolves to
22959 set of overloaded functions, accessibility is checked when
22960 overload resolution is done.
22962 During an explicit instantiation, access is not checked at all,
22963 as per [temp.explicit]. */
22965 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
22967 maybe_record_typedef_use (decl
);
22972 /* Like cp_parser_lookup_name, but for use in the typical case where
22973 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22974 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22977 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
22979 return cp_parser_lookup_name (parser
, name
,
22981 /*is_template=*/false,
22982 /*is_namespace=*/false,
22983 /*check_dependency=*/true,
22984 /*ambiguous_decls=*/NULL
,
22988 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22989 the current context, return the TYPE_DECL. If TAG_NAME_P is
22990 true, the DECL indicates the class being defined in a class-head,
22991 or declared in an elaborated-type-specifier.
22993 Otherwise, return DECL. */
22996 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22998 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22999 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23002 template <typename T> struct B;
23005 template <typename T> struct A::B {};
23007 Similarly, in an elaborated-type-specifier:
23009 namespace N { struct X{}; }
23012 template <typename T> friend struct N::X;
23015 However, if the DECL refers to a class type, and we are in
23016 the scope of the class, then the name lookup automatically
23017 finds the TYPE_DECL created by build_self_reference rather
23018 than a TEMPLATE_DECL. For example, in:
23020 template <class T> struct S {
23024 there is no need to handle such case. */
23026 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
23027 return DECL_TEMPLATE_RESULT (decl
);
23032 /* If too many, or too few, template-parameter lists apply to the
23033 declarator, issue an error message. Returns TRUE if all went well,
23034 and FALSE otherwise. */
23037 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
23038 cp_declarator
*declarator
,
23039 location_t declarator_location
)
23041 switch (declarator
->kind
)
23045 unsigned num_templates
= 0;
23046 tree scope
= declarator
->u
.id
.qualifying_scope
;
23049 num_templates
= num_template_headers_for_class (scope
);
23050 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
23051 == TEMPLATE_ID_EXPR
)
23052 /* If the DECLARATOR has the form `X<y>' then it uses one
23053 additional level of template parameters. */
23056 return cp_parser_check_template_parameters
23057 (parser
, num_templates
, declarator_location
, declarator
);
23063 case cdk_reference
:
23065 return (cp_parser_check_declarator_template_parameters
23066 (parser
, declarator
->declarator
, declarator_location
));
23072 gcc_unreachable ();
23077 /* NUM_TEMPLATES were used in the current declaration. If that is
23078 invalid, return FALSE and issue an error messages. Otherwise,
23079 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23080 declarator and we can print more accurate diagnostics. */
23083 cp_parser_check_template_parameters (cp_parser
* parser
,
23084 unsigned num_templates
,
23085 location_t location
,
23086 cp_declarator
*declarator
)
23088 /* If there are the same number of template classes and parameter
23089 lists, that's OK. */
23090 if (parser
->num_template_parameter_lists
== num_templates
)
23092 /* If there are more, but only one more, then we are referring to a
23093 member template. That's OK too. */
23094 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
23096 /* If there are more template classes than parameter lists, we have
23099 template <class T> void S<T>::R<T>::f (); */
23100 if (parser
->num_template_parameter_lists
< num_templates
)
23102 if (declarator
&& !current_function_decl
)
23103 error_at (location
, "specializing member %<%T::%E%> "
23104 "requires %<template<>%> syntax",
23105 declarator
->u
.id
.qualifying_scope
,
23106 declarator
->u
.id
.unqualified_name
);
23107 else if (declarator
)
23108 error_at (location
, "invalid declaration of %<%T::%E%>",
23109 declarator
->u
.id
.qualifying_scope
,
23110 declarator
->u
.id
.unqualified_name
);
23112 error_at (location
, "too few template-parameter-lists");
23115 /* Otherwise, there are too many template parameter lists. We have
23118 template <class T> template <class U> void S::f(); */
23119 error_at (location
, "too many template-parameter-lists");
23123 /* Parse an optional `::' token indicating that the following name is
23124 from the global namespace. If so, PARSER->SCOPE is set to the
23125 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23126 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23127 Returns the new value of PARSER->SCOPE, if the `::' token is
23128 present, and NULL_TREE otherwise. */
23131 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
23135 /* Peek at the next token. */
23136 token
= cp_lexer_peek_token (parser
->lexer
);
23137 /* If we're looking at a `::' token then we're starting from the
23138 global namespace, not our current location. */
23139 if (token
->type
== CPP_SCOPE
)
23141 /* Consume the `::' token. */
23142 cp_lexer_consume_token (parser
->lexer
);
23143 /* Set the SCOPE so that we know where to start the lookup. */
23144 parser
->scope
= global_namespace
;
23145 parser
->qualifying_scope
= global_namespace
;
23146 parser
->object_scope
= NULL_TREE
;
23148 return parser
->scope
;
23150 else if (!current_scope_valid_p
)
23152 parser
->scope
= NULL_TREE
;
23153 parser
->qualifying_scope
= NULL_TREE
;
23154 parser
->object_scope
= NULL_TREE
;
23160 /* Returns TRUE if the upcoming token sequence is the start of a
23161 constructor declarator. If FRIEND_P is true, the declarator is
23162 preceded by the `friend' specifier. */
23165 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
23167 bool constructor_p
;
23168 bool outside_class_specifier_p
;
23169 tree nested_name_specifier
;
23170 cp_token
*next_token
;
23172 /* The common case is that this is not a constructor declarator, so
23173 try to avoid doing lots of work if at all possible. It's not
23174 valid declare a constructor at function scope. */
23175 if (parser
->in_function_body
)
23177 /* And only certain tokens can begin a constructor declarator. */
23178 next_token
= cp_lexer_peek_token (parser
->lexer
);
23179 if (next_token
->type
!= CPP_NAME
23180 && next_token
->type
!= CPP_SCOPE
23181 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
23182 && next_token
->type
!= CPP_TEMPLATE_ID
)
23185 /* Parse tentatively; we are going to roll back all of the tokens
23187 cp_parser_parse_tentatively (parser
);
23188 /* Assume that we are looking at a constructor declarator. */
23189 constructor_p
= true;
23191 /* Look for the optional `::' operator. */
23192 cp_parser_global_scope_opt (parser
,
23193 /*current_scope_valid_p=*/false);
23194 /* Look for the nested-name-specifier. */
23195 nested_name_specifier
23196 = (cp_parser_nested_name_specifier_opt (parser
,
23197 /*typename_keyword_p=*/false,
23198 /*check_dependency_p=*/false,
23200 /*is_declaration=*/false));
23202 outside_class_specifier_p
= (!at_class_scope_p ()
23203 || !TYPE_BEING_DEFINED (current_class_type
)
23206 /* Outside of a class-specifier, there must be a
23207 nested-name-specifier. */
23208 if (!nested_name_specifier
&& outside_class_specifier_p
)
23209 constructor_p
= false;
23210 else if (nested_name_specifier
== error_mark_node
)
23211 constructor_p
= false;
23213 /* If we have a class scope, this is easy; DR 147 says that S::S always
23214 names the constructor, and no other qualified name could. */
23215 if (constructor_p
&& nested_name_specifier
23216 && CLASS_TYPE_P (nested_name_specifier
))
23218 tree id
= cp_parser_unqualified_id (parser
,
23219 /*template_keyword_p=*/false,
23220 /*check_dependency_p=*/false,
23221 /*declarator_p=*/true,
23222 /*optional_p=*/false);
23223 if (is_overloaded_fn (id
))
23224 id
= DECL_NAME (get_first_fn (id
));
23225 if (!constructor_name_p (id
, nested_name_specifier
))
23226 constructor_p
= false;
23228 /* If we still think that this might be a constructor-declarator,
23229 look for a class-name. */
23230 else if (constructor_p
)
23234 template <typename T> struct S {
23238 we must recognize that the nested `S' names a class. */
23240 type_decl
= cp_parser_class_name (parser
,
23241 /*typename_keyword_p=*/false,
23242 /*template_keyword_p=*/false,
23244 /*check_dependency_p=*/false,
23245 /*class_head_p=*/false,
23246 /*is_declaration=*/false);
23247 /* If there was no class-name, then this is not a constructor.
23248 Otherwise, if we are in a class-specifier and we aren't
23249 handling a friend declaration, check that its type matches
23250 current_class_type (c++/38313). Note: error_mark_node
23251 is left alone for error recovery purposes. */
23252 constructor_p
= (!cp_parser_error_occurred (parser
)
23253 && (outside_class_specifier_p
23254 || type_decl
== error_mark_node
23255 || same_type_p (current_class_type
,
23256 TREE_TYPE (type_decl
))));
23258 /* If we're still considering a constructor, we have to see a `(',
23259 to begin the parameter-declaration-clause, followed by either a
23260 `)', an `...', or a decl-specifier. We need to check for a
23261 type-specifier to avoid being fooled into thinking that:
23265 is a constructor. (It is actually a function named `f' that
23266 takes one parameter (of type `int') and returns a value of type
23269 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23270 constructor_p
= false;
23273 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
23274 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
23275 /* A parameter declaration begins with a decl-specifier,
23276 which is either the "attribute" keyword, a storage class
23277 specifier, or (usually) a type-specifier. */
23278 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
23281 tree pushed_scope
= NULL_TREE
;
23282 unsigned saved_num_template_parameter_lists
;
23284 /* Names appearing in the type-specifier should be looked up
23285 in the scope of the class. */
23286 if (current_class_type
)
23290 type
= TREE_TYPE (type_decl
);
23291 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23293 type
= resolve_typename_type (type
,
23294 /*only_current_p=*/false);
23295 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23297 cp_parser_abort_tentative_parse (parser
);
23301 pushed_scope
= push_scope (type
);
23304 /* Inside the constructor parameter list, surrounding
23305 template-parameter-lists do not apply. */
23306 saved_num_template_parameter_lists
23307 = parser
->num_template_parameter_lists
;
23308 parser
->num_template_parameter_lists
= 0;
23310 /* Look for the type-specifier. */
23311 cp_parser_type_specifier (parser
,
23312 CP_PARSER_FLAGS_NONE
,
23313 /*decl_specs=*/NULL
,
23314 /*is_declarator=*/true,
23315 /*declares_class_or_enum=*/NULL
,
23316 /*is_cv_qualifier=*/NULL
);
23318 parser
->num_template_parameter_lists
23319 = saved_num_template_parameter_lists
;
23321 /* Leave the scope of the class. */
23323 pop_scope (pushed_scope
);
23325 constructor_p
= !cp_parser_error_occurred (parser
);
23329 /* We did not really want to consume any tokens. */
23330 cp_parser_abort_tentative_parse (parser
);
23332 return constructor_p
;
23335 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23336 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23337 they must be performed once we are in the scope of the function.
23339 Returns the function defined. */
23342 cp_parser_function_definition_from_specifiers_and_declarator
23343 (cp_parser
* parser
,
23344 cp_decl_specifier_seq
*decl_specifiers
,
23346 const cp_declarator
*declarator
)
23351 /* Begin the function-definition. */
23352 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
23354 /* The things we're about to see are not directly qualified by any
23355 template headers we've seen thus far. */
23356 reset_specialization ();
23358 /* If there were names looked up in the decl-specifier-seq that we
23359 did not check, check them now. We must wait until we are in the
23360 scope of the function to perform the checks, since the function
23361 might be a friend. */
23362 perform_deferred_access_checks (tf_warning_or_error
);
23366 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
23367 parser
->omp_declare_simd
= NULL
;
23372 /* Skip the entire function. */
23373 cp_parser_skip_to_end_of_block_or_statement (parser
);
23374 fn
= error_mark_node
;
23376 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
23378 /* Seen already, skip it. An error message has already been output. */
23379 cp_parser_skip_to_end_of_block_or_statement (parser
);
23380 fn
= current_function_decl
;
23381 current_function_decl
= NULL_TREE
;
23382 /* If this is a function from a class, pop the nested class. */
23383 if (current_class_name
)
23384 pop_nested_class ();
23389 if (DECL_DECLARED_INLINE_P (current_function_decl
))
23390 tv
= TV_PARSE_INLINE
;
23392 tv
= TV_PARSE_FUNC
;
23394 fn
= cp_parser_function_definition_after_declarator (parser
,
23395 /*inline_p=*/false);
23402 /* Parse the part of a function-definition that follows the
23403 declarator. INLINE_P is TRUE iff this function is an inline
23404 function defined within a class-specifier.
23406 Returns the function defined. */
23409 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
23413 bool ctor_initializer_p
= false;
23414 bool saved_in_unbraced_linkage_specification_p
;
23415 bool saved_in_function_body
;
23416 unsigned saved_num_template_parameter_lists
;
23418 bool fully_implicit_function_template_p
23419 = parser
->fully_implicit_function_template_p
;
23420 parser
->fully_implicit_function_template_p
= false;
23421 tree implicit_template_parms
23422 = parser
->implicit_template_parms
;
23423 parser
->implicit_template_parms
= 0;
23424 cp_binding_level
* implicit_template_scope
23425 = parser
->implicit_template_scope
;
23426 parser
->implicit_template_scope
= 0;
23428 saved_in_function_body
= parser
->in_function_body
;
23429 parser
->in_function_body
= true;
23430 /* If the next token is `return', then the code may be trying to
23431 make use of the "named return value" extension that G++ used to
23433 token
= cp_lexer_peek_token (parser
->lexer
);
23434 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
23436 /* Consume the `return' keyword. */
23437 cp_lexer_consume_token (parser
->lexer
);
23438 /* Look for the identifier that indicates what value is to be
23440 cp_parser_identifier (parser
);
23441 /* Issue an error message. */
23442 error_at (token
->location
,
23443 "named return values are no longer supported");
23444 /* Skip tokens until we reach the start of the function body. */
23447 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23448 if (token
->type
== CPP_OPEN_BRACE
23449 || token
->type
== CPP_EOF
23450 || token
->type
== CPP_PRAGMA_EOL
)
23452 cp_lexer_consume_token (parser
->lexer
);
23455 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23456 anything declared inside `f'. */
23457 saved_in_unbraced_linkage_specification_p
23458 = parser
->in_unbraced_linkage_specification_p
;
23459 parser
->in_unbraced_linkage_specification_p
= false;
23460 /* Inside the function, surrounding template-parameter-lists do not
23462 saved_num_template_parameter_lists
23463 = parser
->num_template_parameter_lists
;
23464 parser
->num_template_parameter_lists
= 0;
23466 start_lambda_scope (current_function_decl
);
23468 /* If the next token is `try', `__transaction_atomic', or
23469 `__transaction_relaxed`, then we are looking at either function-try-block
23470 or function-transaction-block. Note that all of these include the
23472 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
23473 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23474 RID_TRANSACTION_ATOMIC
);
23475 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23476 RID_TRANSACTION_RELAXED
))
23477 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23478 RID_TRANSACTION_RELAXED
);
23479 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23480 ctor_initializer_p
= cp_parser_function_try_block (parser
);
23482 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
23483 (parser
, /*in_function_try_block=*/false);
23485 finish_lambda_scope ();
23487 /* Finish the function. */
23488 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
23489 (inline_p
? 2 : 0));
23490 /* Generate code for it, if necessary. */
23491 expand_or_defer_fn (fn
);
23492 /* Restore the saved values. */
23493 parser
->in_unbraced_linkage_specification_p
23494 = saved_in_unbraced_linkage_specification_p
;
23495 parser
->num_template_parameter_lists
23496 = saved_num_template_parameter_lists
;
23497 parser
->in_function_body
= saved_in_function_body
;
23499 parser
->fully_implicit_function_template_p
23500 = fully_implicit_function_template_p
;
23501 parser
->implicit_template_parms
23502 = implicit_template_parms
;
23503 parser
->implicit_template_scope
23504 = implicit_template_scope
;
23506 if (parser
->fully_implicit_function_template_p
)
23507 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
23512 /* Parse a template-declaration, assuming that the `export' (and
23513 `extern') keywords, if present, has already been scanned. MEMBER_P
23514 is as for cp_parser_template_declaration. */
23517 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
23519 tree decl
= NULL_TREE
;
23520 vec
<deferred_access_check
, va_gc
> *checks
;
23521 tree parameter_list
;
23522 bool friend_p
= false;
23523 bool need_lang_pop
;
23526 /* Look for the `template' keyword. */
23527 token
= cp_lexer_peek_token (parser
->lexer
);
23528 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
23532 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
23534 if (at_class_scope_p () && current_function_decl
)
23536 /* 14.5.2.2 [temp.mem]
23538 A local class shall not have member templates. */
23539 error_at (token
->location
,
23540 "invalid declaration of member template in local class");
23541 cp_parser_skip_to_end_of_block_or_statement (parser
);
23546 A template ... shall not have C linkage. */
23547 if (current_lang_name
== lang_name_c
)
23549 error_at (token
->location
, "template with C linkage");
23550 /* Give it C++ linkage to avoid confusing other parts of the
23552 push_lang_context (lang_name_cplusplus
);
23553 need_lang_pop
= true;
23556 need_lang_pop
= false;
23558 /* We cannot perform access checks on the template parameter
23559 declarations until we know what is being declared, just as we
23560 cannot check the decl-specifier list. */
23561 push_deferring_access_checks (dk_deferred
);
23563 /* If the next token is `>', then we have an invalid
23564 specialization. Rather than complain about an invalid template
23565 parameter, issue an error message here. */
23566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
23568 cp_parser_error (parser
, "invalid explicit specialization");
23569 begin_specialization ();
23570 parameter_list
= NULL_TREE
;
23574 /* Parse the template parameters. */
23575 parameter_list
= cp_parser_template_parameter_list (parser
);
23578 /* Get the deferred access checks from the parameter list. These
23579 will be checked once we know what is being declared, as for a
23580 member template the checks must be performed in the scope of the
23581 class containing the member. */
23582 checks
= get_deferred_access_checks ();
23584 /* Look for the `>'. */
23585 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23586 /* We just processed one more parameter list. */
23587 ++parser
->num_template_parameter_lists
;
23588 /* If the next token is `template', there are more template
23590 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23592 cp_parser_template_declaration_after_export (parser
, member_p
);
23593 else if (cxx_dialect
>= cxx11
23594 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23595 decl
= cp_parser_alias_declaration (parser
);
23598 /* There are no access checks when parsing a template, as we do not
23599 know if a specialization will be a friend. */
23600 push_deferring_access_checks (dk_no_check
);
23601 token
= cp_lexer_peek_token (parser
->lexer
);
23602 decl
= cp_parser_single_declaration (parser
,
23605 /*explicit_specialization_p=*/false,
23607 pop_deferring_access_checks ();
23609 /* If this is a member template declaration, let the front
23611 if (member_p
&& !friend_p
&& decl
)
23613 if (TREE_CODE (decl
) == TYPE_DECL
)
23614 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
23616 decl
= finish_member_template_decl (decl
);
23618 else if (friend_p
&& decl
23619 && DECL_DECLARES_TYPE_P (decl
))
23620 make_friend_class (current_class_type
, TREE_TYPE (decl
),
23621 /*complain=*/true);
23623 /* We are done with the current parameter list. */
23624 --parser
->num_template_parameter_lists
;
23626 pop_deferring_access_checks ();
23629 finish_template_decl (parameter_list
);
23631 /* Check the template arguments for a literal operator template. */
23633 && DECL_DECLARES_FUNCTION_P (decl
)
23634 && UDLIT_OPER_P (DECL_NAME (decl
)))
23637 if (parameter_list
== NULL_TREE
)
23641 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
23642 if (num_parms
== 1)
23644 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
23645 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23646 if (TREE_TYPE (parm
) != char_type_node
23647 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23650 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
23652 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
23653 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
23654 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
23655 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23656 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
23657 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23665 if (cxx_dialect
>= cxx14
)
23666 error ("literal operator template %qD has invalid parameter list."
23667 " Expected non-type template argument pack <char...>"
23668 " or <typename CharT, CharT...>",
23671 error ("literal operator template %qD has invalid parameter list."
23672 " Expected non-type template argument pack <char...>",
23676 /* Register member declarations. */
23677 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
23678 finish_member_declaration (decl
);
23679 /* For the erroneous case of a template with C linkage, we pushed an
23680 implicit C++ linkage scope; exit that scope now. */
23682 pop_lang_context ();
23683 /* If DECL is a function template, we must return to parse it later.
23684 (Even though there is no definition, there might be default
23685 arguments that need handling.) */
23686 if (member_p
&& decl
23687 && DECL_DECLARES_FUNCTION_P (decl
))
23688 vec_safe_push (unparsed_funs_with_definitions
, decl
);
23691 /* Perform the deferred access checks from a template-parameter-list.
23692 CHECKS is a TREE_LIST of access checks, as returned by
23693 get_deferred_access_checks. */
23696 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
23698 ++processing_template_parmlist
;
23699 perform_access_checks (checks
, tf_warning_or_error
);
23700 --processing_template_parmlist
;
23703 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23704 `function-definition' sequence that follows a template header.
23705 If MEMBER_P is true, this declaration appears in a class scope.
23707 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23708 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23711 cp_parser_single_declaration (cp_parser
* parser
,
23712 vec
<deferred_access_check
, va_gc
> *checks
,
23714 bool explicit_specialization_p
,
23717 int declares_class_or_enum
;
23718 tree decl
= NULL_TREE
;
23719 cp_decl_specifier_seq decl_specifiers
;
23720 bool function_definition_p
= false;
23721 cp_token
*decl_spec_token_start
;
23723 /* This function is only used when processing a template
23725 gcc_assert (innermost_scope_kind () == sk_template_parms
23726 || innermost_scope_kind () == sk_template_spec
);
23728 /* Defer access checks until we know what is being declared. */
23729 push_deferring_access_checks (dk_deferred
);
23731 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23733 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23734 cp_parser_decl_specifier_seq (parser
,
23735 CP_PARSER_FLAGS_OPTIONAL
,
23737 &declares_class_or_enum
);
23739 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
23741 /* There are no template typedefs. */
23742 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
23744 error_at (decl_spec_token_start
->location
,
23745 "template declaration of %<typedef%>");
23746 decl
= error_mark_node
;
23749 /* Gather up the access checks that occurred the
23750 decl-specifier-seq. */
23751 stop_deferring_access_checks ();
23753 /* Check for the declaration of a template class. */
23754 if (declares_class_or_enum
)
23756 if (cp_parser_declares_only_class_p (parser
))
23758 decl
= shadow_tag (&decl_specifiers
);
23763 friend template <typename T> struct A<T>::B;
23766 A<T>::B will be represented by a TYPENAME_TYPE, and
23767 therefore not recognized by shadow_tag. */
23768 if (friend_p
&& *friend_p
23770 && decl_specifiers
.type
23771 && TYPE_P (decl_specifiers
.type
))
23772 decl
= decl_specifiers
.type
;
23774 if (decl
&& decl
!= error_mark_node
)
23775 decl
= TYPE_NAME (decl
);
23777 decl
= error_mark_node
;
23779 /* Perform access checks for template parameters. */
23780 cp_parser_perform_template_parameter_access_checks (checks
);
23784 /* Complain about missing 'typename' or other invalid type names. */
23785 if (!decl_specifiers
.any_type_specifiers_p
23786 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23788 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23789 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23790 the rest of this declaration. */
23791 decl
= error_mark_node
;
23795 /* If it's not a template class, try for a template function. If
23796 the next token is a `;', then this declaration does not declare
23797 anything. But, if there were errors in the decl-specifiers, then
23798 the error might well have come from an attempted class-specifier.
23799 In that case, there's no need to warn about a missing declarator. */
23801 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
23802 || decl_specifiers
.type
!= error_mark_node
))
23804 decl
= cp_parser_init_declarator (parser
,
23807 /*function_definition_allowed_p=*/true,
23809 declares_class_or_enum
,
23810 &function_definition_p
,
23813 /* 7.1.1-1 [dcl.stc]
23815 A storage-class-specifier shall not be specified in an explicit
23816 specialization... */
23818 && explicit_specialization_p
23819 && decl_specifiers
.storage_class
!= sc_none
)
23821 error_at (decl_spec_token_start
->location
,
23822 "explicit template specialization cannot have a storage class");
23823 decl
= error_mark_node
;
23826 if (decl
&& VAR_P (decl
))
23827 check_template_variable (decl
);
23830 /* Look for a trailing `;' after the declaration. */
23831 if (!function_definition_p
23832 && (decl
== error_mark_node
23833 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
23834 cp_parser_skip_to_end_of_block_or_statement (parser
);
23837 pop_deferring_access_checks ();
23839 /* Clear any current qualification; whatever comes next is the start
23840 of something new. */
23841 parser
->scope
= NULL_TREE
;
23842 parser
->qualifying_scope
= NULL_TREE
;
23843 parser
->object_scope
= NULL_TREE
;
23848 /* Parse a cast-expression that is not the operand of a unary "&". */
23851 cp_parser_simple_cast_expression (cp_parser
*parser
)
23853 return cp_parser_cast_expression (parser
, /*address_p=*/false,
23854 /*cast_p=*/false, /*decltype*/false, NULL
);
23857 /* Parse a functional cast to TYPE. Returns an expression
23858 representing the cast. */
23861 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
23863 vec
<tree
, va_gc
> *vec
;
23864 tree expression_list
;
23869 type
= error_mark_node
;
23871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23873 cp_lexer_set_source_position (parser
->lexer
);
23874 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23875 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
23876 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
23877 if (TREE_CODE (type
) == TYPE_DECL
)
23878 type
= TREE_TYPE (type
);
23879 return finish_compound_literal (type
, expression_list
,
23880 tf_warning_or_error
);
23884 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23886 /*allow_expansion_p=*/true,
23887 /*non_constant_p=*/NULL
);
23889 expression_list
= error_mark_node
;
23892 expression_list
= build_tree_list_vec (vec
);
23893 release_tree_vector (vec
);
23896 cast
= build_functional_cast (type
, expression_list
,
23897 tf_warning_or_error
);
23898 /* [expr.const]/1: In an integral constant expression "only type
23899 conversions to integral or enumeration type can be used". */
23900 if (TREE_CODE (type
) == TYPE_DECL
)
23901 type
= TREE_TYPE (type
);
23902 if (cast
!= error_mark_node
23903 && !cast_valid_in_integral_constant_expression_p (type
)
23904 && cp_parser_non_integral_constant_expression (parser
,
23906 return error_mark_node
;
23910 /* Save the tokens that make up the body of a member function defined
23911 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23912 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23913 specifiers applied to the declaration. Returns the FUNCTION_DECL
23914 for the member function. */
23917 cp_parser_save_member_function_body (cp_parser
* parser
,
23918 cp_decl_specifier_seq
*decl_specifiers
,
23919 cp_declarator
*declarator
,
23926 /* Create the FUNCTION_DECL. */
23927 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
23928 cp_finalize_omp_declare_simd (parser
, fn
);
23929 /* If something went badly wrong, bail out now. */
23930 if (fn
== error_mark_node
)
23932 /* If there's a function-body, skip it. */
23933 if (cp_parser_token_starts_function_definition_p
23934 (cp_lexer_peek_token (parser
->lexer
)))
23935 cp_parser_skip_to_end_of_block_or_statement (parser
);
23936 return error_mark_node
;
23939 /* Remember it, if there default args to post process. */
23940 cp_parser_save_default_args (parser
, fn
);
23942 /* Save away the tokens that make up the body of the
23944 first
= parser
->lexer
->next_token
;
23945 /* Handle function try blocks. */
23946 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23947 cp_lexer_consume_token (parser
->lexer
);
23948 /* We can have braced-init-list mem-initializers before the fn body. */
23949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23951 cp_lexer_consume_token (parser
->lexer
);
23952 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23954 /* cache_group will stop after an un-nested { } pair, too. */
23955 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
23958 /* variadic mem-inits have ... after the ')'. */
23959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23960 cp_lexer_consume_token (parser
->lexer
);
23963 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23964 /* Handle function try blocks. */
23965 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
23966 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23967 last
= parser
->lexer
->next_token
;
23969 /* Save away the inline definition; we will process it when the
23970 class is complete. */
23971 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
23972 DECL_PENDING_INLINE_P (fn
) = 1;
23974 /* We need to know that this was defined in the class, so that
23975 friend templates are handled correctly. */
23976 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
23978 /* Add FN to the queue of functions to be parsed later. */
23979 vec_safe_push (unparsed_funs_with_definitions
, fn
);
23984 /* Save the tokens that make up the in-class initializer for a non-static
23985 data member. Returns a DEFAULT_ARG. */
23988 cp_parser_save_nsdmi (cp_parser
* parser
)
23990 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
23993 /* Parse a template-argument-list, as well as the trailing ">" (but
23994 not the opening "<"). See cp_parser_template_argument_list for the
23998 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
24002 tree saved_qualifying_scope
;
24003 tree saved_object_scope
;
24004 bool saved_greater_than_is_operator_p
;
24005 int saved_unevaluated_operand
;
24006 int saved_inhibit_evaluation_warnings
;
24010 When parsing a template-id, the first non-nested `>' is taken as
24011 the end of the template-argument-list rather than a greater-than
24013 saved_greater_than_is_operator_p
24014 = parser
->greater_than_is_operator_p
;
24015 parser
->greater_than_is_operator_p
= false;
24016 /* Parsing the argument list may modify SCOPE, so we save it
24018 saved_scope
= parser
->scope
;
24019 saved_qualifying_scope
= parser
->qualifying_scope
;
24020 saved_object_scope
= parser
->object_scope
;
24021 /* We need to evaluate the template arguments, even though this
24022 template-id may be nested within a "sizeof". */
24023 saved_unevaluated_operand
= cp_unevaluated_operand
;
24024 cp_unevaluated_operand
= 0;
24025 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24026 c_inhibit_evaluation_warnings
= 0;
24027 /* Parse the template-argument-list itself. */
24028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
24029 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
24030 arguments
= NULL_TREE
;
24032 arguments
= cp_parser_template_argument_list (parser
);
24033 /* Look for the `>' that ends the template-argument-list. If we find
24034 a '>>' instead, it's probably just a typo. */
24035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
24037 if (cxx_dialect
!= cxx98
)
24039 /* In C++0x, a `>>' in a template argument list or cast
24040 expression is considered to be two separate `>'
24041 tokens. So, change the current token to a `>', but don't
24042 consume it: it will be consumed later when the outer
24043 template argument list (or cast expression) is parsed.
24044 Note that this replacement of `>' for `>>' is necessary
24045 even if we are parsing tentatively: in the tentative
24046 case, after calling
24047 cp_parser_enclosed_template_argument_list we will always
24048 throw away all of the template arguments and the first
24049 closing `>', either because the template argument list
24050 was erroneous or because we are replacing those tokens
24051 with a CPP_TEMPLATE_ID token. The second `>' (which will
24052 not have been thrown away) is needed either to close an
24053 outer template argument list or to complete a new-style
24055 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24056 token
->type
= CPP_GREATER
;
24058 else if (!saved_greater_than_is_operator_p
)
24060 /* If we're in a nested template argument list, the '>>' has
24061 to be a typo for '> >'. We emit the error message, but we
24062 continue parsing and we push a '>' as next token, so that
24063 the argument list will be parsed correctly. Note that the
24064 global source location is still on the token before the
24065 '>>', so we need to say explicitly where we want it. */
24066 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24067 error_at (token
->location
, "%<>>%> should be %<> >%> "
24068 "within a nested template argument list");
24070 token
->type
= CPP_GREATER
;
24074 /* If this is not a nested template argument list, the '>>'
24075 is a typo for '>'. Emit an error message and continue.
24076 Same deal about the token location, but here we can get it
24077 right by consuming the '>>' before issuing the diagnostic. */
24078 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
24079 error_at (token
->location
,
24080 "spurious %<>>%>, use %<>%> to terminate "
24081 "a template argument list");
24085 cp_parser_skip_to_end_of_template_parameter_list (parser
);
24086 /* The `>' token might be a greater-than operator again now. */
24087 parser
->greater_than_is_operator_p
24088 = saved_greater_than_is_operator_p
;
24089 /* Restore the SAVED_SCOPE. */
24090 parser
->scope
= saved_scope
;
24091 parser
->qualifying_scope
= saved_qualifying_scope
;
24092 parser
->object_scope
= saved_object_scope
;
24093 cp_unevaluated_operand
= saved_unevaluated_operand
;
24094 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24099 /* MEMBER_FUNCTION is a member function, or a friend. If default
24100 arguments, or the body of the function have not yet been parsed,
24104 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
24106 timevar_push (TV_PARSE_INMETH
);
24107 /* If this member is a template, get the underlying
24109 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
24110 member_function
= DECL_TEMPLATE_RESULT (member_function
);
24112 /* There should not be any class definitions in progress at this
24113 point; the bodies of members are only parsed outside of all class
24115 gcc_assert (parser
->num_classes_being_defined
== 0);
24116 /* While we're parsing the member functions we might encounter more
24117 classes. We want to handle them right away, but we don't want
24118 them getting mixed up with functions that are currently in the
24120 push_unparsed_function_queues (parser
);
24122 /* Make sure that any template parameters are in scope. */
24123 maybe_begin_member_template_processing (member_function
);
24125 /* If the body of the function has not yet been parsed, parse it
24127 if (DECL_PENDING_INLINE_P (member_function
))
24129 tree function_scope
;
24130 cp_token_cache
*tokens
;
24132 /* The function is no longer pending; we are processing it. */
24133 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
24134 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
24135 DECL_PENDING_INLINE_P (member_function
) = 0;
24137 /* If this is a local class, enter the scope of the containing
24139 function_scope
= current_function_decl
;
24140 if (function_scope
)
24141 push_function_context ();
24143 /* Push the body of the function onto the lexer stack. */
24144 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24146 /* Let the front end know that we going to be defining this
24148 start_preparsed_function (member_function
, NULL_TREE
,
24149 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
24151 /* Don't do access checking if it is a templated function. */
24152 if (processing_template_decl
)
24153 push_deferring_access_checks (dk_no_check
);
24155 /* #pragma omp declare reduction needs special parsing. */
24156 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
24158 parser
->lexer
->in_pragma
= true;
24159 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
24160 finish_function (/*inline*/2);
24161 cp_check_omp_declare_reduction (member_function
);
24164 /* Now, parse the body of the function. */
24165 cp_parser_function_definition_after_declarator (parser
,
24166 /*inline_p=*/true);
24168 if (processing_template_decl
)
24169 pop_deferring_access_checks ();
24171 /* Leave the scope of the containing function. */
24172 if (function_scope
)
24173 pop_function_context ();
24174 cp_parser_pop_lexer (parser
);
24177 /* Remove any template parameters from the symbol table. */
24178 maybe_end_member_template_processing ();
24180 /* Restore the queue. */
24181 pop_unparsed_function_queues (parser
);
24182 timevar_pop (TV_PARSE_INMETH
);
24185 /* If DECL contains any default args, remember it on the unparsed
24186 functions queue. */
24189 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
24193 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
24195 probe
= TREE_CHAIN (probe
))
24196 if (TREE_PURPOSE (probe
))
24198 cp_default_arg_entry entry
= {current_class_type
, decl
};
24199 vec_safe_push (unparsed_funs_with_default_args
, entry
);
24204 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24205 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24206 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24207 from the parameter-type-list. */
24210 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
24211 tree default_arg
, tree parmtype
)
24213 cp_token_cache
*tokens
;
24217 if (default_arg
== error_mark_node
)
24218 return error_mark_node
;
24220 /* Push the saved tokens for the default argument onto the parser's
24222 tokens
= DEFARG_TOKENS (default_arg
);
24223 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24225 start_lambda_scope (decl
);
24227 /* Parse the default argument. */
24228 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
24229 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
24230 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
24232 finish_lambda_scope ();
24234 if (parsed_arg
== error_mark_node
)
24235 cp_parser_skip_to_end_of_statement (parser
);
24237 if (!processing_template_decl
)
24239 /* In a non-template class, check conversions now. In a template,
24240 we'll wait and instantiate these as needed. */
24241 if (TREE_CODE (decl
) == PARM_DECL
)
24242 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
24243 tf_warning_or_error
);
24245 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
24248 /* If the token stream has not been completely used up, then
24249 there was extra junk after the end of the default
24251 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
24253 if (TREE_CODE (decl
) == PARM_DECL
)
24254 cp_parser_error (parser
, "expected %<,%>");
24256 cp_parser_error (parser
, "expected %<;%>");
24259 /* Revert to the main lexer. */
24260 cp_parser_pop_lexer (parser
);
24265 /* FIELD is a non-static data member with an initializer which we saved for
24266 later; parse it now. */
24269 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
24273 maybe_begin_member_template_processing (field
);
24275 push_unparsed_function_queues (parser
);
24276 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
24277 DECL_INITIAL (field
),
24279 pop_unparsed_function_queues (parser
);
24281 maybe_end_member_template_processing ();
24283 DECL_INITIAL (field
) = def
;
24286 /* FN is a FUNCTION_DECL which may contains a parameter with an
24287 unparsed DEFAULT_ARG. Parse the default args now. This function
24288 assumes that the current scope is the scope in which the default
24289 argument should be processed. */
24292 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
24294 bool saved_local_variables_forbidden_p
;
24295 tree parm
, parmdecl
;
24297 /* While we're parsing the default args, we might (due to the
24298 statement expression extension) encounter more classes. We want
24299 to handle them right away, but we don't want them getting mixed
24300 up with default args that are currently in the queue. */
24301 push_unparsed_function_queues (parser
);
24303 /* Local variable names (and the `this' keyword) may not appear
24304 in a default argument. */
24305 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
24306 parser
->local_variables_forbidden_p
= true;
24308 push_defarg_context (fn
);
24310 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
24311 parmdecl
= DECL_ARGUMENTS (fn
);
24312 parm
&& parm
!= void_list_node
;
24313 parm
= TREE_CHAIN (parm
),
24314 parmdecl
= DECL_CHAIN (parmdecl
))
24316 tree default_arg
= TREE_PURPOSE (parm
);
24318 vec
<tree
, va_gc
> *insts
;
24325 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
24326 /* This can happen for a friend declaration for a function
24327 already declared with default arguments. */
24331 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
24333 TREE_VALUE (parm
));
24334 if (parsed_arg
== error_mark_node
)
24339 TREE_PURPOSE (parm
) = parsed_arg
;
24341 /* Update any instantiations we've already created. */
24342 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
24343 vec_safe_iterate (insts
, ix
, ©
); ix
++)
24344 TREE_PURPOSE (copy
) = parsed_arg
;
24347 pop_defarg_context ();
24349 /* Make sure no default arg is missing. */
24350 check_default_args (fn
);
24352 /* Restore the state of local_variables_forbidden_p. */
24353 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
24355 /* Restore the queue. */
24356 pop_unparsed_function_queues (parser
);
24359 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24361 sizeof ... ( identifier )
24363 where the 'sizeof' token has already been consumed. */
24366 cp_parser_sizeof_pack (cp_parser
*parser
)
24368 /* Consume the `...'. */
24369 cp_lexer_consume_token (parser
->lexer
);
24370 maybe_warn_variadic_templates ();
24372 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
24374 cp_lexer_consume_token (parser
->lexer
);
24376 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
24377 "%<sizeof...%> argument must be surrounded by parentheses");
24379 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24380 tree name
= cp_parser_identifier (parser
);
24381 if (name
== error_mark_node
)
24382 return error_mark_node
;
24383 /* The name is not qualified. */
24384 parser
->scope
= NULL_TREE
;
24385 parser
->qualifying_scope
= NULL_TREE
;
24386 parser
->object_scope
= NULL_TREE
;
24387 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
24388 if (expr
== error_mark_node
)
24389 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
24391 if (TREE_CODE (expr
) == TYPE_DECL
)
24392 expr
= TREE_TYPE (expr
);
24393 else if (TREE_CODE (expr
) == CONST_DECL
)
24394 expr
= DECL_INITIAL (expr
);
24395 expr
= make_pack_expansion (expr
);
24398 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24403 /* Parse the operand of `sizeof' (or a similar operator). Returns
24404 either a TYPE or an expression, depending on the form of the
24405 input. The KEYWORD indicates which kind of expression we have
24409 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
24411 tree expr
= NULL_TREE
;
24412 const char *saved_message
;
24414 bool saved_integral_constant_expression_p
;
24415 bool saved_non_integral_constant_expression_p
;
24417 /* If it's a `...', then we are computing the length of a parameter
24419 if (keyword
== RID_SIZEOF
24420 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24421 return cp_parser_sizeof_pack (parser
);
24423 /* Types cannot be defined in a `sizeof' expression. Save away the
24425 saved_message
= parser
->type_definition_forbidden_message
;
24426 /* And create the new one. */
24427 tmp
= concat ("types may not be defined in %<",
24428 IDENTIFIER_POINTER (ridpointers
[keyword
]),
24429 "%> expressions", NULL
);
24430 parser
->type_definition_forbidden_message
= tmp
;
24432 /* The restrictions on constant-expressions do not apply inside
24433 sizeof expressions. */
24434 saved_integral_constant_expression_p
24435 = parser
->integral_constant_expression_p
;
24436 saved_non_integral_constant_expression_p
24437 = parser
->non_integral_constant_expression_p
;
24438 parser
->integral_constant_expression_p
= false;
24440 /* Do not actually evaluate the expression. */
24441 ++cp_unevaluated_operand
;
24442 ++c_inhibit_evaluation_warnings
;
24443 /* If it's a `(', then we might be looking at the type-id
24445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24447 tree type
= NULL_TREE
;
24449 /* We can't be sure yet whether we're looking at a type-id or an
24451 cp_parser_parse_tentatively (parser
);
24452 /* Note: as a GNU Extension, compound literals are considered
24453 postfix-expressions as they are in C99, so they are valid
24454 arguments to sizeof. See comment in cp_parser_cast_expression
24456 if (cp_parser_compound_literal_p (parser
))
24457 cp_parser_simulate_error (parser
);
24460 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
24461 parser
->in_type_id_in_expr_p
= true;
24462 /* Look for the type-id. */
24463 type
= cp_parser_type_id (parser
);
24464 /* Look for the closing `)'. */
24465 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24466 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
24469 /* If all went well, then we're done. */
24470 if (cp_parser_parse_definitely (parser
))
24472 cp_decl_specifier_seq decl_specs
;
24474 /* Build a trivial decl-specifier-seq. */
24475 clear_decl_specs (&decl_specs
);
24476 decl_specs
.type
= type
;
24478 /* Call grokdeclarator to figure out what type this is. */
24479 expr
= grokdeclarator (NULL
,
24483 /*attrlist=*/NULL
);
24487 /* If the type-id production did not work out, then we must be
24488 looking at the unary-expression production. */
24490 expr
= cp_parser_unary_expression (parser
);
24492 /* Go back to evaluating expressions. */
24493 --cp_unevaluated_operand
;
24494 --c_inhibit_evaluation_warnings
;
24496 /* Free the message we created. */
24498 /* And restore the old one. */
24499 parser
->type_definition_forbidden_message
= saved_message
;
24500 parser
->integral_constant_expression_p
24501 = saved_integral_constant_expression_p
;
24502 parser
->non_integral_constant_expression_p
24503 = saved_non_integral_constant_expression_p
;
24508 /* If the current declaration has no declarator, return true. */
24511 cp_parser_declares_only_class_p (cp_parser
*parser
)
24513 /* If the next token is a `;' or a `,' then there is no
24515 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24516 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
24519 /* Update the DECL_SPECS to reflect the storage class indicated by
24523 cp_parser_set_storage_class (cp_parser
*parser
,
24524 cp_decl_specifier_seq
*decl_specs
,
24528 cp_storage_class storage_class
;
24530 if (parser
->in_unbraced_linkage_specification_p
)
24532 error_at (token
->location
, "invalid use of %qD in linkage specification",
24533 ridpointers
[keyword
]);
24536 else if (decl_specs
->storage_class
!= sc_none
)
24538 decl_specs
->conflicting_specifiers_p
= true;
24542 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
24543 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
24544 && decl_specs
->gnu_thread_keyword_p
)
24546 pedwarn (decl_specs
->locations
[ds_thread
], 0,
24547 "%<__thread%> before %qD", ridpointers
[keyword
]);
24553 storage_class
= sc_auto
;
24556 storage_class
= sc_register
;
24559 storage_class
= sc_static
;
24562 storage_class
= sc_extern
;
24565 storage_class
= sc_mutable
;
24568 gcc_unreachable ();
24570 decl_specs
->storage_class
= storage_class
;
24571 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
24573 /* A storage class specifier cannot be applied alongside a typedef
24574 specifier. If there is a typedef specifier present then set
24575 conflicting_specifiers_p which will trigger an error later
24576 on in grokdeclarator. */
24577 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
24578 decl_specs
->conflicting_specifiers_p
= true;
24581 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24582 is true, the type is a class or enum definition. */
24585 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
24588 bool type_definition_p
)
24590 decl_specs
->any_specifiers_p
= true;
24592 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24593 (with, for example, in "typedef int wchar_t;") we remember that
24594 this is what happened. In system headers, we ignore these
24595 declarations so that G++ can work with system headers that are not
24597 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
24598 && !type_definition_p
24599 && (type_spec
== boolean_type_node
24600 || type_spec
== char16_type_node
24601 || type_spec
== char32_type_node
24602 || type_spec
== wchar_type_node
)
24603 && (decl_specs
->type
24604 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
24605 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
24606 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
24607 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
24609 decl_specs
->redefined_builtin_type
= type_spec
;
24610 set_and_check_decl_spec_loc (decl_specs
,
24611 ds_redefined_builtin_type_spec
,
24613 if (!decl_specs
->type
)
24615 decl_specs
->type
= type_spec
;
24616 decl_specs
->type_definition_p
= false;
24617 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
24620 else if (decl_specs
->type
)
24621 decl_specs
->multiple_types_p
= true;
24624 decl_specs
->type
= type_spec
;
24625 decl_specs
->type_definition_p
= type_definition_p
;
24626 decl_specs
->redefined_builtin_type
= NULL_TREE
;
24627 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
24631 /* True iff TOKEN is the GNU keyword __thread. */
24634 token_is__thread (cp_token
*token
)
24636 gcc_assert (token
->keyword
== RID_THREAD
);
24637 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
24640 /* Set the location for a declarator specifier and check if it is
24643 DECL_SPECS is the sequence of declarator specifiers onto which to
24646 DS is the single declarator specifier to set which location is to
24647 be set onto the existing sequence of declarators.
24649 LOCATION is the location for the declarator specifier to
24653 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
24654 cp_decl_spec ds
, cp_token
*token
)
24656 gcc_assert (ds
< ds_last
);
24658 if (decl_specs
== NULL
)
24661 source_location location
= token
->location
;
24663 if (decl_specs
->locations
[ds
] == 0)
24665 decl_specs
->locations
[ds
] = location
;
24666 if (ds
== ds_thread
)
24667 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
24673 if (decl_specs
->locations
[ds_long_long
] != 0)
24674 error_at (location
,
24675 "%<long long long%> is too long for GCC");
24678 decl_specs
->locations
[ds_long_long
] = location
;
24679 pedwarn_cxx98 (location
,
24681 "ISO C++ 1998 does not support %<long long%>");
24684 else if (ds
== ds_thread
)
24686 bool gnu
= token_is__thread (token
);
24687 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
24688 error_at (location
,
24689 "both %<__thread%> and %<thread_local%> specified");
24691 error_at (location
, "duplicate %qD", token
->u
.value
);
24695 static const char *const decl_spec_names
[] = {
24712 error_at (location
,
24713 "duplicate %qs", decl_spec_names
[ds
]);
24718 /* Return true iff the declarator specifier DS is present in the
24719 sequence of declarator specifiers DECL_SPECS. */
24722 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
24725 gcc_assert (ds
< ds_last
);
24727 if (decl_specs
== NULL
)
24730 return decl_specs
->locations
[ds
] != 0;
24733 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24734 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24737 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
24739 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
24742 /* Issue an error message indicating that TOKEN_DESC was expected.
24743 If KEYWORD is true, it indicated this function is called by
24744 cp_parser_require_keword and the required token can only be
24745 a indicated keyword. */
24748 cp_parser_required_error (cp_parser
*parser
,
24749 required_token token_desc
,
24752 switch (token_desc
)
24755 cp_parser_error (parser
, "expected %<new%>");
24758 cp_parser_error (parser
, "expected %<delete%>");
24761 cp_parser_error (parser
, "expected %<return%>");
24764 cp_parser_error (parser
, "expected %<while%>");
24767 cp_parser_error (parser
, "expected %<extern%>");
24769 case RT_STATIC_ASSERT
:
24770 cp_parser_error (parser
, "expected %<static_assert%>");
24773 cp_parser_error (parser
, "expected %<decltype%>");
24776 cp_parser_error (parser
, "expected %<operator%>");
24779 cp_parser_error (parser
, "expected %<class%>");
24782 cp_parser_error (parser
, "expected %<template%>");
24785 cp_parser_error (parser
, "expected %<namespace%>");
24788 cp_parser_error (parser
, "expected %<using%>");
24791 cp_parser_error (parser
, "expected %<asm%>");
24794 cp_parser_error (parser
, "expected %<try%>");
24797 cp_parser_error (parser
, "expected %<catch%>");
24800 cp_parser_error (parser
, "expected %<throw%>");
24803 cp_parser_error (parser
, "expected %<__label__%>");
24806 cp_parser_error (parser
, "expected %<@try%>");
24808 case RT_AT_SYNCHRONIZED
:
24809 cp_parser_error (parser
, "expected %<@synchronized%>");
24812 cp_parser_error (parser
, "expected %<@throw%>");
24814 case RT_TRANSACTION_ATOMIC
:
24815 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
24817 case RT_TRANSACTION_RELAXED
:
24818 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
24825 switch (token_desc
)
24828 cp_parser_error (parser
, "expected %<;%>");
24830 case RT_OPEN_PAREN
:
24831 cp_parser_error (parser
, "expected %<(%>");
24833 case RT_CLOSE_BRACE
:
24834 cp_parser_error (parser
, "expected %<}%>");
24836 case RT_OPEN_BRACE
:
24837 cp_parser_error (parser
, "expected %<{%>");
24839 case RT_CLOSE_SQUARE
:
24840 cp_parser_error (parser
, "expected %<]%>");
24842 case RT_OPEN_SQUARE
:
24843 cp_parser_error (parser
, "expected %<[%>");
24846 cp_parser_error (parser
, "expected %<,%>");
24849 cp_parser_error (parser
, "expected %<::%>");
24852 cp_parser_error (parser
, "expected %<<%>");
24855 cp_parser_error (parser
, "expected %<>%>");
24858 cp_parser_error (parser
, "expected %<=%>");
24861 cp_parser_error (parser
, "expected %<...%>");
24864 cp_parser_error (parser
, "expected %<*%>");
24867 cp_parser_error (parser
, "expected %<~%>");
24870 cp_parser_error (parser
, "expected %<:%>");
24872 case RT_COLON_SCOPE
:
24873 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24875 case RT_CLOSE_PAREN
:
24876 cp_parser_error (parser
, "expected %<)%>");
24878 case RT_COMMA_CLOSE_PAREN
:
24879 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24881 case RT_PRAGMA_EOL
:
24882 cp_parser_error (parser
, "expected end of line");
24885 cp_parser_error (parser
, "expected identifier");
24888 cp_parser_error (parser
, "expected selection-statement");
24890 case RT_INTERATION
:
24891 cp_parser_error (parser
, "expected iteration-statement");
24894 cp_parser_error (parser
, "expected jump-statement");
24897 cp_parser_error (parser
, "expected class-key");
24899 case RT_CLASS_TYPENAME_TEMPLATE
:
24900 cp_parser_error (parser
,
24901 "expected %<class%>, %<typename%>, or %<template%>");
24904 gcc_unreachable ();
24908 gcc_unreachable ();
24913 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24914 issue an error message indicating that TOKEN_DESC was expected.
24916 Returns the token consumed, if the token had the appropriate type.
24917 Otherwise, returns NULL. */
24920 cp_parser_require (cp_parser
* parser
,
24921 enum cpp_ttype type
,
24922 required_token token_desc
)
24924 if (cp_lexer_next_token_is (parser
->lexer
, type
))
24925 return cp_lexer_consume_token (parser
->lexer
);
24928 /* Output the MESSAGE -- unless we're parsing tentatively. */
24929 if (!cp_parser_simulate_error (parser
))
24930 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
24935 /* An error message is produced if the next token is not '>'.
24936 All further tokens are skipped until the desired token is
24937 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24940 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
24942 /* Current level of '< ... >'. */
24943 unsigned level
= 0;
24944 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24945 unsigned nesting_depth
= 0;
24947 /* Are we ready, yet? If not, issue error message. */
24948 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
24951 /* Skip tokens until the desired token is found. */
24954 /* Peek at the next token. */
24955 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24958 if (!nesting_depth
)
24963 if (cxx_dialect
== cxx98
)
24964 /* C++0x views the `>>' operator as two `>' tokens, but
24967 else if (!nesting_depth
&& level
-- == 0)
24969 /* We've hit a `>>' where the first `>' closes the
24970 template argument list, and the second `>' is
24971 spurious. Just consume the `>>' and stop; we've
24972 already produced at least one error. */
24973 cp_lexer_consume_token (parser
->lexer
);
24976 /* Fall through for C++0x, so we handle the second `>' in
24980 if (!nesting_depth
&& level
-- == 0)
24982 /* We've reached the token we want, consume it and stop. */
24983 cp_lexer_consume_token (parser
->lexer
);
24988 case CPP_OPEN_PAREN
:
24989 case CPP_OPEN_SQUARE
:
24993 case CPP_CLOSE_PAREN
:
24994 case CPP_CLOSE_SQUARE
:
24995 if (nesting_depth
-- == 0)
25000 case CPP_PRAGMA_EOL
:
25001 case CPP_SEMICOLON
:
25002 case CPP_OPEN_BRACE
:
25003 case CPP_CLOSE_BRACE
:
25004 /* The '>' was probably forgotten, don't look further. */
25011 /* Consume this token. */
25012 cp_lexer_consume_token (parser
->lexer
);
25016 /* If the next token is the indicated keyword, consume it. Otherwise,
25017 issue an error message indicating that TOKEN_DESC was expected.
25019 Returns the token consumed, if the token had the appropriate type.
25020 Otherwise, returns NULL. */
25023 cp_parser_require_keyword (cp_parser
* parser
,
25025 required_token token_desc
)
25027 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
25029 if (token
&& token
->keyword
!= keyword
)
25031 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
25038 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25039 function-definition. */
25042 cp_parser_token_starts_function_definition_p (cp_token
* token
)
25044 return (/* An ordinary function-body begins with an `{'. */
25045 token
->type
== CPP_OPEN_BRACE
25046 /* A ctor-initializer begins with a `:'. */
25047 || token
->type
== CPP_COLON
25048 /* A function-try-block begins with `try'. */
25049 || token
->keyword
== RID_TRY
25050 /* A function-transaction-block begins with `__transaction_atomic'
25051 or `__transaction_relaxed'. */
25052 || token
->keyword
== RID_TRANSACTION_ATOMIC
25053 || token
->keyword
== RID_TRANSACTION_RELAXED
25054 /* The named return value extension begins with `return'. */
25055 || token
->keyword
== RID_RETURN
);
25058 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25062 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
25066 token
= cp_lexer_peek_token (parser
->lexer
);
25067 return (token
->type
== CPP_OPEN_BRACE
25068 || (token
->type
== CPP_COLON
25069 && !parser
->colon_doesnt_start_class_def_p
));
25072 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25073 C++0x) ending a template-argument. */
25076 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
25080 token
= cp_lexer_peek_token (parser
->lexer
);
25081 return (token
->type
== CPP_COMMA
25082 || token
->type
== CPP_GREATER
25083 || token
->type
== CPP_ELLIPSIS
25084 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
25087 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25088 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25091 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
25096 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25097 if (token
->type
== CPP_LESS
)
25099 /* Check for the sequence `<::' in the original code. It would be lexed as
25100 `[:', where `[' is a digraph, and there is no whitespace before
25102 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
25105 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
25106 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
25112 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25113 or none_type otherwise. */
25115 static enum tag_types
25116 cp_parser_token_is_class_key (cp_token
* token
)
25118 switch (token
->keyword
)
25123 return record_type
;
25132 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25133 or none_type otherwise or if the token is null. */
25135 static enum tag_types
25136 cp_parser_token_is_type_parameter_key (cp_token
* token
)
25141 switch (token
->keyword
)
25146 return typename_type
;
25153 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25156 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
25158 if (type
== error_mark_node
)
25160 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
25162 if (permerror (input_location
, "%qs tag used in naming %q#T",
25163 class_key
== union_type
? "union"
25164 : class_key
== record_type
? "struct" : "class",
25166 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
25167 "%q#T was previously declared here", type
);
25171 /* Issue an error message if DECL is redeclared with different
25172 access than its original declaration [class.access.spec/3].
25173 This applies to nested classes and nested class templates.
25177 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
25179 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
25182 if ((TREE_PRIVATE (decl
)
25183 != (current_access_specifier
== access_private_node
))
25184 || (TREE_PROTECTED (decl
)
25185 != (current_access_specifier
== access_protected_node
)))
25186 error_at (location
, "%qD redeclared with different access", decl
);
25189 /* Look for the `template' keyword, as a syntactic disambiguator.
25190 Return TRUE iff it is present, in which case it will be
25194 cp_parser_optional_template_keyword (cp_parser
*parser
)
25196 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25198 /* In C++98 the `template' keyword can only be used within templates;
25199 outside templates the parser can always figure out what is a
25200 template and what is not. In C++11, per the resolution of DR 468,
25201 `template' is allowed in cases where it is not strictly necessary. */
25202 if (!processing_template_decl
25203 && pedantic
&& cxx_dialect
== cxx98
)
25205 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25206 pedwarn (token
->location
, OPT_Wpedantic
,
25207 "in C++98 %<template%> (as a disambiguator) is only "
25208 "allowed within templates");
25209 /* If this part of the token stream is rescanned, the same
25210 error message would be generated. So, we purge the token
25211 from the stream. */
25212 cp_lexer_purge_token (parser
->lexer
);
25217 /* Consume the `template' keyword. */
25218 cp_lexer_consume_token (parser
->lexer
);
25225 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25226 set PARSER->SCOPE, and perform other related actions. */
25229 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
25232 struct tree_check
*check_value
;
25233 deferred_access_check
*chk
;
25234 vec
<deferred_access_check
, va_gc
> *checks
;
25236 /* Get the stored value. */
25237 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
25238 /* Perform any access checks that were deferred. */
25239 checks
= check_value
->checks
;
25242 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
25243 perform_or_defer_access_check (chk
->binfo
,
25245 chk
->diag_decl
, tf_warning_or_error
);
25247 /* Set the scope from the stored value. */
25248 parser
->scope
= check_value
->value
;
25249 parser
->qualifying_scope
= check_value
->qualifying_scope
;
25250 parser
->object_scope
= NULL_TREE
;
25253 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25254 encounter the end of a block before what we were looking for. */
25257 cp_parser_cache_group (cp_parser
*parser
,
25258 enum cpp_ttype end
,
25263 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25265 /* Abort a parenthesized expression if we encounter a semicolon. */
25266 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
25267 && token
->type
== CPP_SEMICOLON
)
25269 /* If we've reached the end of the file, stop. */
25270 if (token
->type
== CPP_EOF
25271 || (end
!= CPP_PRAGMA_EOL
25272 && token
->type
== CPP_PRAGMA_EOL
))
25274 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
25275 /* We've hit the end of an enclosing block, so there's been some
25276 kind of syntax error. */
25279 /* Consume the token. */
25280 cp_lexer_consume_token (parser
->lexer
);
25281 /* See if it starts a new group. */
25282 if (token
->type
== CPP_OPEN_BRACE
)
25284 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
25285 /* In theory this should probably check end == '}', but
25286 cp_parser_save_member_function_body needs it to exit
25287 after either '}' or ')' when called with ')'. */
25291 else if (token
->type
== CPP_OPEN_PAREN
)
25293 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
25294 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
25297 else if (token
->type
== CPP_PRAGMA
)
25298 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
25299 else if (token
->type
== end
)
25304 /* Like above, for caching a default argument or NSDMI. Both of these are
25305 terminated by a non-nested comma, but it can be unclear whether or not a
25306 comma is nested in a template argument list unless we do more parsing.
25307 In order to handle this ambiguity, when we encounter a ',' after a '<'
25308 we try to parse what follows as a parameter-declaration-list (in the
25309 case of a default argument) or a member-declarator (in the case of an
25310 NSDMI). If that succeeds, then we stop caching. */
25313 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
25315 unsigned depth
= 0;
25316 int maybe_template_id
= 0;
25317 cp_token
*first_token
;
25319 tree default_argument
;
25321 /* Add tokens until we have processed the entire default
25322 argument. We add the range [first_token, token). */
25323 first_token
= cp_lexer_peek_token (parser
->lexer
);
25324 if (first_token
->type
== CPP_OPEN_BRACE
)
25326 /* For list-initialization, this is straightforward. */
25327 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25328 token
= cp_lexer_peek_token (parser
->lexer
);
25334 /* Peek at the next token. */
25335 token
= cp_lexer_peek_token (parser
->lexer
);
25336 /* What we do depends on what token we have. */
25337 switch (token
->type
)
25339 /* In valid code, a default argument must be
25340 immediately followed by a `,' `)', or `...'. */
25342 if (depth
== 0 && maybe_template_id
)
25344 /* If we've seen a '<', we might be in a
25345 template-argument-list. Until Core issue 325 is
25346 resolved, we don't know how this situation ought
25347 to be handled, so try to DTRT. We check whether
25348 what comes after the comma is a valid parameter
25349 declaration list. If it is, then the comma ends
25350 the default argument; otherwise the default
25351 argument continues. */
25352 bool error
= false;
25354 /* Set ITALP so cp_parser_parameter_declaration_list
25355 doesn't decide to commit to this parse. */
25356 bool saved_italp
= parser
->in_template_argument_list_p
;
25357 parser
->in_template_argument_list_p
= true;
25359 cp_parser_parse_tentatively (parser
);
25360 cp_lexer_consume_token (parser
->lexer
);
25364 int ctor_dtor_or_conv_p
;
25365 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25366 &ctor_dtor_or_conv_p
,
25367 /*parenthesized_p=*/NULL
,
25369 /*friend_p=*/false);
25373 begin_scope (sk_function_parms
, NULL_TREE
);
25374 cp_parser_parameter_declaration_list (parser
, &error
);
25375 pop_bindings_and_leave_scope ();
25377 if (!cp_parser_error_occurred (parser
) && !error
)
25379 cp_parser_abort_tentative_parse (parser
);
25381 parser
->in_template_argument_list_p
= saved_italp
;
25384 case CPP_CLOSE_PAREN
:
25386 /* If we run into a non-nested `;', `}', or `]',
25387 then the code is invalid -- but the default
25388 argument is certainly over. */
25389 case CPP_SEMICOLON
:
25390 case CPP_CLOSE_BRACE
:
25391 case CPP_CLOSE_SQUARE
:
25393 /* Handle correctly int n = sizeof ... ( p ); */
25394 && token
->type
!= CPP_ELLIPSIS
)
25396 /* Update DEPTH, if necessary. */
25397 else if (token
->type
== CPP_CLOSE_PAREN
25398 || token
->type
== CPP_CLOSE_BRACE
25399 || token
->type
== CPP_CLOSE_SQUARE
)
25403 case CPP_OPEN_PAREN
:
25404 case CPP_OPEN_SQUARE
:
25405 case CPP_OPEN_BRACE
:
25411 /* This might be the comparison operator, or it might
25412 start a template argument list. */
25413 ++maybe_template_id
;
25417 if (cxx_dialect
== cxx98
)
25419 /* Fall through for C++0x, which treats the `>>'
25420 operator like two `>' tokens in certain
25426 /* This might be an operator, or it might close a
25427 template argument list. But if a previous '<'
25428 started a template argument list, this will have
25429 closed it, so we can't be in one anymore. */
25430 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
25431 if (maybe_template_id
< 0)
25432 maybe_template_id
= 0;
25436 /* If we run out of tokens, issue an error message. */
25438 case CPP_PRAGMA_EOL
:
25439 error_at (token
->location
, "file ends in default argument");
25445 /* In these cases, we should look for template-ids.
25446 For example, if the default argument is
25447 `X<int, double>()', we need to do name lookup to
25448 figure out whether or not `X' is a template; if
25449 so, the `,' does not end the default argument.
25451 That is not yet done. */
25458 /* If we've reached the end, stop. */
25462 /* Add the token to the token block. */
25463 token
= cp_lexer_consume_token (parser
->lexer
);
25466 /* Create a DEFAULT_ARG to represent the unparsed default
25468 default_argument
= make_node (DEFAULT_ARG
);
25469 DEFARG_TOKENS (default_argument
)
25470 = cp_token_cache_new (first_token
, token
);
25471 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
25473 return default_argument
;
25476 /* Begin parsing tentatively. We always save tokens while parsing
25477 tentatively so that if the tentative parsing fails we can restore the
25481 cp_parser_parse_tentatively (cp_parser
* parser
)
25483 /* Enter a new parsing context. */
25484 parser
->context
= cp_parser_context_new (parser
->context
);
25485 /* Begin saving tokens. */
25486 cp_lexer_save_tokens (parser
->lexer
);
25487 /* In order to avoid repetitive access control error messages,
25488 access checks are queued up until we are no longer parsing
25490 push_deferring_access_checks (dk_deferred
);
25493 /* Commit to the currently active tentative parse. */
25496 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
25498 cp_parser_context
*context
;
25501 /* Mark all of the levels as committed. */
25502 lexer
= parser
->lexer
;
25503 for (context
= parser
->context
; context
->next
; context
= context
->next
)
25505 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25507 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25508 while (!cp_lexer_saving_tokens (lexer
))
25509 lexer
= lexer
->next
;
25510 cp_lexer_commit_tokens (lexer
);
25514 /* Commit to the topmost currently active tentative parse.
25516 Note that this function shouldn't be called when there are
25517 irreversible side-effects while in a tentative state. For
25518 example, we shouldn't create a permanent entry in the symbol
25519 table, or issue an error message that might not apply if the
25520 tentative parse is aborted. */
25523 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
25525 cp_parser_context
*context
= parser
->context
;
25526 cp_lexer
*lexer
= parser
->lexer
;
25530 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25532 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25534 while (!cp_lexer_saving_tokens (lexer
))
25535 lexer
= lexer
->next
;
25536 cp_lexer_commit_tokens (lexer
);
25540 /* Abort the currently active tentative parse. All consumed tokens
25541 will be rolled back, and no diagnostics will be issued. */
25544 cp_parser_abort_tentative_parse (cp_parser
* parser
)
25546 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
25547 || errorcount
> 0);
25548 cp_parser_simulate_error (parser
);
25549 /* Now, pretend that we want to see if the construct was
25550 successfully parsed. */
25551 cp_parser_parse_definitely (parser
);
25554 /* Stop parsing tentatively. If a parse error has occurred, restore the
25555 token stream. Otherwise, commit to the tokens we have consumed.
25556 Returns true if no error occurred; false otherwise. */
25559 cp_parser_parse_definitely (cp_parser
* parser
)
25561 bool error_occurred
;
25562 cp_parser_context
*context
;
25564 /* Remember whether or not an error occurred, since we are about to
25565 destroy that information. */
25566 error_occurred
= cp_parser_error_occurred (parser
);
25567 /* Remove the topmost context from the stack. */
25568 context
= parser
->context
;
25569 parser
->context
= context
->next
;
25570 /* If no parse errors occurred, commit to the tentative parse. */
25571 if (!error_occurred
)
25573 /* Commit to the tokens read tentatively, unless that was
25575 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
25576 cp_lexer_commit_tokens (parser
->lexer
);
25578 pop_to_parent_deferring_access_checks ();
25580 /* Otherwise, if errors occurred, roll back our state so that things
25581 are just as they were before we began the tentative parse. */
25584 cp_lexer_rollback_tokens (parser
->lexer
);
25585 pop_deferring_access_checks ();
25587 /* Add the context to the front of the free list. */
25588 context
->next
= cp_parser_context_free_list
;
25589 cp_parser_context_free_list
= context
;
25591 return !error_occurred
;
25594 /* Returns true if we are parsing tentatively and are not committed to
25595 this tentative parse. */
25598 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
25600 return (cp_parser_parsing_tentatively (parser
)
25601 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
25604 /* Returns nonzero iff an error has occurred during the most recent
25605 tentative parse. */
25608 cp_parser_error_occurred (cp_parser
* parser
)
25610 return (cp_parser_parsing_tentatively (parser
)
25611 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
25614 /* Returns nonzero if GNU extensions are allowed. */
25617 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
25619 return parser
->allow_gnu_extensions_p
;
25622 /* Objective-C++ Productions */
25625 /* Parse an Objective-C expression, which feeds into a primary-expression
25629 objc-message-expression
25630 objc-string-literal
25631 objc-encode-expression
25632 objc-protocol-expression
25633 objc-selector-expression
25635 Returns a tree representation of the expression. */
25638 cp_parser_objc_expression (cp_parser
* parser
)
25640 /* Try to figure out what kind of declaration is present. */
25641 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25645 case CPP_OPEN_SQUARE
:
25646 return cp_parser_objc_message_expression (parser
);
25648 case CPP_OBJC_STRING
:
25649 kwd
= cp_lexer_consume_token (parser
->lexer
);
25650 return objc_build_string_object (kwd
->u
.value
);
25653 switch (kwd
->keyword
)
25655 case RID_AT_ENCODE
:
25656 return cp_parser_objc_encode_expression (parser
);
25658 case RID_AT_PROTOCOL
:
25659 return cp_parser_objc_protocol_expression (parser
);
25661 case RID_AT_SELECTOR
:
25662 return cp_parser_objc_selector_expression (parser
);
25668 error_at (kwd
->location
,
25669 "misplaced %<@%D%> Objective-C++ construct",
25671 cp_parser_skip_to_end_of_block_or_statement (parser
);
25674 return error_mark_node
;
25677 /* Parse an Objective-C message expression.
25679 objc-message-expression:
25680 [ objc-message-receiver objc-message-args ]
25682 Returns a representation of an Objective-C message. */
25685 cp_parser_objc_message_expression (cp_parser
* parser
)
25687 tree receiver
, messageargs
;
25689 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
25690 receiver
= cp_parser_objc_message_receiver (parser
);
25691 messageargs
= cp_parser_objc_message_args (parser
);
25692 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25694 return objc_build_message_expr (receiver
, messageargs
);
25697 /* Parse an objc-message-receiver.
25699 objc-message-receiver:
25701 simple-type-specifier
25703 Returns a representation of the type or expression. */
25706 cp_parser_objc_message_receiver (cp_parser
* parser
)
25710 /* An Objective-C message receiver may be either (1) a type
25711 or (2) an expression. */
25712 cp_parser_parse_tentatively (parser
);
25713 rcv
= cp_parser_expression (parser
);
25715 /* If that worked out, fine. */
25716 if (cp_parser_parse_definitely (parser
))
25719 cp_parser_parse_tentatively (parser
);
25720 rcv
= cp_parser_simple_type_specifier (parser
,
25721 /*decl_specs=*/NULL
,
25722 CP_PARSER_FLAGS_NONE
);
25724 if (cp_parser_parse_definitely (parser
))
25725 return objc_get_class_reference (rcv
);
25727 cp_parser_error (parser
, "objective-c++ message receiver expected");
25728 return error_mark_node
;
25731 /* Parse the arguments and selectors comprising an Objective-C message.
25736 objc-selector-args , objc-comma-args
25738 objc-selector-args:
25739 objc-selector [opt] : assignment-expression
25740 objc-selector-args objc-selector [opt] : assignment-expression
25743 assignment-expression
25744 objc-comma-args , assignment-expression
25746 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25747 selector arguments and TREE_VALUE containing a list of comma
25751 cp_parser_objc_message_args (cp_parser
* parser
)
25753 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
25754 bool maybe_unary_selector_p
= true;
25755 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25757 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25759 tree selector
= NULL_TREE
, arg
;
25761 if (token
->type
!= CPP_COLON
)
25762 selector
= cp_parser_objc_selector (parser
);
25764 /* Detect if we have a unary selector. */
25765 if (maybe_unary_selector_p
25766 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25767 return build_tree_list (selector
, NULL_TREE
);
25769 maybe_unary_selector_p
= false;
25770 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25771 arg
= cp_parser_assignment_expression (parser
);
25774 = chainon (sel_args
,
25775 build_tree_list (selector
, arg
));
25777 token
= cp_lexer_peek_token (parser
->lexer
);
25780 /* Handle non-selector arguments, if any. */
25781 while (token
->type
== CPP_COMMA
)
25785 cp_lexer_consume_token (parser
->lexer
);
25786 arg
= cp_parser_assignment_expression (parser
);
25789 = chainon (addl_args
,
25790 build_tree_list (NULL_TREE
, arg
));
25792 token
= cp_lexer_peek_token (parser
->lexer
);
25795 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
25797 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
25798 return build_tree_list (error_mark_node
, error_mark_node
);
25801 return build_tree_list (sel_args
, addl_args
);
25804 /* Parse an Objective-C encode expression.
25806 objc-encode-expression:
25807 @encode objc-typename
25809 Returns an encoded representation of the type argument. */
25812 cp_parser_objc_encode_expression (cp_parser
* parser
)
25817 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
25818 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25819 token
= cp_lexer_peek_token (parser
->lexer
);
25820 type
= complete_type (cp_parser_type_id (parser
));
25821 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25825 error_at (token
->location
,
25826 "%<@encode%> must specify a type as an argument");
25827 return error_mark_node
;
25830 /* This happens if we find @encode(T) (where T is a template
25831 typename or something dependent on a template typename) when
25832 parsing a template. In that case, we can't compile it
25833 immediately, but we rather create an AT_ENCODE_EXPR which will
25834 need to be instantiated when the template is used.
25836 if (dependent_type_p (type
))
25838 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
25839 TREE_READONLY (value
) = 1;
25843 return objc_build_encode_expr (type
);
25846 /* Parse an Objective-C @defs expression. */
25849 cp_parser_objc_defs_expression (cp_parser
*parser
)
25853 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
25854 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25855 name
= cp_parser_identifier (parser
);
25856 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25858 return objc_get_class_ivars (name
);
25861 /* Parse an Objective-C protocol expression.
25863 objc-protocol-expression:
25864 @protocol ( identifier )
25866 Returns a representation of the protocol expression. */
25869 cp_parser_objc_protocol_expression (cp_parser
* parser
)
25873 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25874 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25875 proto
= cp_parser_identifier (parser
);
25876 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25878 return objc_build_protocol_expr (proto
);
25881 /* Parse an Objective-C selector expression.
25883 objc-selector-expression:
25884 @selector ( objc-method-signature )
25886 objc-method-signature:
25892 objc-selector-seq objc-selector :
25894 Returns a representation of the method selector. */
25897 cp_parser_objc_selector_expression (cp_parser
* parser
)
25899 tree sel_seq
= NULL_TREE
;
25900 bool maybe_unary_selector_p
= true;
25902 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25904 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
25905 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25906 token
= cp_lexer_peek_token (parser
->lexer
);
25908 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
25909 || token
->type
== CPP_SCOPE
)
25911 tree selector
= NULL_TREE
;
25913 if (token
->type
!= CPP_COLON
25914 || token
->type
== CPP_SCOPE
)
25915 selector
= cp_parser_objc_selector (parser
);
25917 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
25918 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
25920 /* Detect if we have a unary selector. */
25921 if (maybe_unary_selector_p
)
25923 sel_seq
= selector
;
25924 goto finish_selector
;
25928 cp_parser_error (parser
, "expected %<:%>");
25931 maybe_unary_selector_p
= false;
25932 token
= cp_lexer_consume_token (parser
->lexer
);
25934 if (token
->type
== CPP_SCOPE
)
25937 = chainon (sel_seq
,
25938 build_tree_list (selector
, NULL_TREE
));
25940 = chainon (sel_seq
,
25941 build_tree_list (NULL_TREE
, NULL_TREE
));
25945 = chainon (sel_seq
,
25946 build_tree_list (selector
, NULL_TREE
));
25948 token
= cp_lexer_peek_token (parser
->lexer
);
25952 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25954 return objc_build_selector_expr (loc
, sel_seq
);
25957 /* Parse a list of identifiers.
25959 objc-identifier-list:
25961 objc-identifier-list , identifier
25963 Returns a TREE_LIST of identifier nodes. */
25966 cp_parser_objc_identifier_list (cp_parser
* parser
)
25972 identifier
= cp_parser_identifier (parser
);
25973 if (identifier
== error_mark_node
)
25974 return error_mark_node
;
25976 list
= build_tree_list (NULL_TREE
, identifier
);
25977 sep
= cp_lexer_peek_token (parser
->lexer
);
25979 while (sep
->type
== CPP_COMMA
)
25981 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25982 identifier
= cp_parser_identifier (parser
);
25983 if (identifier
== error_mark_node
)
25986 list
= chainon (list
, build_tree_list (NULL_TREE
,
25988 sep
= cp_lexer_peek_token (parser
->lexer
);
25994 /* Parse an Objective-C alias declaration.
25996 objc-alias-declaration:
25997 @compatibility_alias identifier identifier ;
25999 This function registers the alias mapping with the Objective-C front end.
26000 It returns nothing. */
26003 cp_parser_objc_alias_declaration (cp_parser
* parser
)
26007 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
26008 alias
= cp_parser_identifier (parser
);
26009 orig
= cp_parser_identifier (parser
);
26010 objc_declare_alias (alias
, orig
);
26011 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26014 /* Parse an Objective-C class forward-declaration.
26016 objc-class-declaration:
26017 @class objc-identifier-list ;
26019 The function registers the forward declarations with the Objective-C
26020 front end. It returns nothing. */
26023 cp_parser_objc_class_declaration (cp_parser
* parser
)
26025 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
26030 id
= cp_parser_identifier (parser
);
26031 if (id
== error_mark_node
)
26034 objc_declare_class (id
);
26036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26037 cp_lexer_consume_token (parser
->lexer
);
26041 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26044 /* Parse a list of Objective-C protocol references.
26046 objc-protocol-refs-opt:
26047 objc-protocol-refs [opt]
26049 objc-protocol-refs:
26050 < objc-identifier-list >
26052 Returns a TREE_LIST of identifiers, if any. */
26055 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
26057 tree protorefs
= NULL_TREE
;
26059 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
26061 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
26062 protorefs
= cp_parser_objc_identifier_list (parser
);
26063 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
26069 /* Parse a Objective-C visibility specification. */
26072 cp_parser_objc_visibility_spec (cp_parser
* parser
)
26074 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
26076 switch (vis
->keyword
)
26078 case RID_AT_PRIVATE
:
26079 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
26081 case RID_AT_PROTECTED
:
26082 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
26084 case RID_AT_PUBLIC
:
26085 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
26087 case RID_AT_PACKAGE
:
26088 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
26094 /* Eat '@private'/'@protected'/'@public'. */
26095 cp_lexer_consume_token (parser
->lexer
);
26098 /* Parse an Objective-C method type. Return 'true' if it is a class
26099 (+) method, and 'false' if it is an instance (-) method. */
26102 cp_parser_objc_method_type (cp_parser
* parser
)
26104 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
26110 /* Parse an Objective-C protocol qualifier. */
26113 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
26115 tree quals
= NULL_TREE
, node
;
26116 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26118 node
= token
->u
.value
;
26120 while (node
&& identifier_p (node
)
26121 && (node
== ridpointers
[(int) RID_IN
]
26122 || node
== ridpointers
[(int) RID_OUT
]
26123 || node
== ridpointers
[(int) RID_INOUT
]
26124 || node
== ridpointers
[(int) RID_BYCOPY
]
26125 || node
== ridpointers
[(int) RID_BYREF
]
26126 || node
== ridpointers
[(int) RID_ONEWAY
]))
26128 quals
= tree_cons (NULL_TREE
, node
, quals
);
26129 cp_lexer_consume_token (parser
->lexer
);
26130 token
= cp_lexer_peek_token (parser
->lexer
);
26131 node
= token
->u
.value
;
26137 /* Parse an Objective-C typename. */
26140 cp_parser_objc_typename (cp_parser
* parser
)
26142 tree type_name
= NULL_TREE
;
26144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26146 tree proto_quals
, cp_type
= NULL_TREE
;
26148 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26149 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
26151 /* An ObjC type name may consist of just protocol qualifiers, in which
26152 case the type shall default to 'id'. */
26153 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
26155 cp_type
= cp_parser_type_id (parser
);
26157 /* If the type could not be parsed, an error has already
26158 been produced. For error recovery, behave as if it had
26159 not been specified, which will use the default type
26161 if (cp_type
== error_mark_node
)
26163 cp_type
= NULL_TREE
;
26164 /* We need to skip to the closing parenthesis as
26165 cp_parser_type_id() does not seem to do it for
26167 cp_parser_skip_to_closing_parenthesis (parser
,
26168 /*recovering=*/true,
26169 /*or_comma=*/false,
26170 /*consume_paren=*/false);
26174 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26175 type_name
= build_tree_list (proto_quals
, cp_type
);
26181 /* Check to see if TYPE refers to an Objective-C selector name. */
26184 cp_parser_objc_selector_p (enum cpp_ttype type
)
26186 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
26187 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
26188 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
26189 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
26190 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
26193 /* Parse an Objective-C selector. */
26196 cp_parser_objc_selector (cp_parser
* parser
)
26198 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
26200 if (!cp_parser_objc_selector_p (token
->type
))
26202 error_at (token
->location
, "invalid Objective-C++ selector name");
26203 return error_mark_node
;
26206 /* C++ operator names are allowed to appear in ObjC selectors. */
26207 switch (token
->type
)
26209 case CPP_AND_AND
: return get_identifier ("and");
26210 case CPP_AND_EQ
: return get_identifier ("and_eq");
26211 case CPP_AND
: return get_identifier ("bitand");
26212 case CPP_OR
: return get_identifier ("bitor");
26213 case CPP_COMPL
: return get_identifier ("compl");
26214 case CPP_NOT
: return get_identifier ("not");
26215 case CPP_NOT_EQ
: return get_identifier ("not_eq");
26216 case CPP_OR_OR
: return get_identifier ("or");
26217 case CPP_OR_EQ
: return get_identifier ("or_eq");
26218 case CPP_XOR
: return get_identifier ("xor");
26219 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
26220 default: return token
->u
.value
;
26224 /* Parse an Objective-C params list. */
26227 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
26229 tree params
= NULL_TREE
;
26230 bool maybe_unary_selector_p
= true;
26231 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26233 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
26235 tree selector
= NULL_TREE
, type_name
, identifier
;
26236 tree parm_attr
= NULL_TREE
;
26238 if (token
->keyword
== RID_ATTRIBUTE
)
26241 if (token
->type
!= CPP_COLON
)
26242 selector
= cp_parser_objc_selector (parser
);
26244 /* Detect if we have a unary selector. */
26245 if (maybe_unary_selector_p
26246 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26248 params
= selector
; /* Might be followed by attributes. */
26252 maybe_unary_selector_p
= false;
26253 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26255 /* Something went quite wrong. There should be a colon
26256 here, but there is not. Stop parsing parameters. */
26259 type_name
= cp_parser_objc_typename (parser
);
26260 /* New ObjC allows attributes on parameters too. */
26261 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
26262 parm_attr
= cp_parser_attributes_opt (parser
);
26263 identifier
= cp_parser_identifier (parser
);
26267 objc_build_keyword_decl (selector
,
26272 token
= cp_lexer_peek_token (parser
->lexer
);
26275 if (params
== NULL_TREE
)
26277 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26278 return error_mark_node
;
26281 /* We allow tail attributes for the method. */
26282 if (token
->keyword
== RID_ATTRIBUTE
)
26284 *attributes
= cp_parser_attributes_opt (parser
);
26285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26286 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26288 cp_parser_error (parser
,
26289 "method attributes must be specified at the end");
26290 return error_mark_node
;
26293 if (params
== NULL_TREE
)
26295 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26296 return error_mark_node
;
26301 /* Parse the non-keyword Objective-C params. */
26304 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
26307 tree params
= make_node (TREE_LIST
);
26308 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26309 *ellipsisp
= false; /* Initially, assume no ellipsis. */
26311 while (token
->type
== CPP_COMMA
)
26313 cp_parameter_declarator
*parmdecl
;
26316 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26317 token
= cp_lexer_peek_token (parser
->lexer
);
26319 if (token
->type
== CPP_ELLIPSIS
)
26321 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
26323 token
= cp_lexer_peek_token (parser
->lexer
);
26327 /* TODO: parse attributes for tail parameters. */
26328 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
26329 parm
= grokdeclarator (parmdecl
->declarator
,
26330 &parmdecl
->decl_specifiers
,
26331 PARM
, /*initialized=*/0,
26332 /*attrlist=*/NULL
);
26334 chainon (params
, build_tree_list (NULL_TREE
, parm
));
26335 token
= cp_lexer_peek_token (parser
->lexer
);
26338 /* We allow tail attributes for the method. */
26339 if (token
->keyword
== RID_ATTRIBUTE
)
26341 if (*attributes
== NULL_TREE
)
26343 *attributes
= cp_parser_attributes_opt (parser
);
26344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26345 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26349 /* We have an error, but parse the attributes, so that we can
26351 *attributes
= cp_parser_attributes_opt (parser
);
26353 cp_parser_error (parser
,
26354 "method attributes must be specified at the end");
26355 return error_mark_node
;
26361 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26364 cp_parser_objc_interstitial_code (cp_parser
* parser
)
26366 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26368 /* If the next token is `extern' and the following token is a string
26369 literal, then we have a linkage specification. */
26370 if (token
->keyword
== RID_EXTERN
26371 && cp_parser_is_pure_string_literal
26372 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
26373 cp_parser_linkage_specification (parser
);
26374 /* Handle #pragma, if any. */
26375 else if (token
->type
== CPP_PRAGMA
)
26376 cp_parser_pragma (parser
, pragma_objc_icode
);
26377 /* Allow stray semicolons. */
26378 else if (token
->type
== CPP_SEMICOLON
)
26379 cp_lexer_consume_token (parser
->lexer
);
26380 /* Mark methods as optional or required, when building protocols. */
26381 else if (token
->keyword
== RID_AT_OPTIONAL
)
26383 cp_lexer_consume_token (parser
->lexer
);
26384 objc_set_method_opt (true);
26386 else if (token
->keyword
== RID_AT_REQUIRED
)
26388 cp_lexer_consume_token (parser
->lexer
);
26389 objc_set_method_opt (false);
26391 else if (token
->keyword
== RID_NAMESPACE
)
26392 cp_parser_namespace_definition (parser
);
26393 /* Other stray characters must generate errors. */
26394 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
26396 cp_lexer_consume_token (parser
->lexer
);
26397 error ("stray %qs between Objective-C++ methods",
26398 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
26400 /* Finally, try to parse a block-declaration, or a function-definition. */
26402 cp_parser_block_declaration (parser
, /*statement_p=*/false);
26405 /* Parse a method signature. */
26408 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
26410 tree rettype
, kwdparms
, optparms
;
26411 bool ellipsis
= false;
26412 bool is_class_method
;
26414 is_class_method
= cp_parser_objc_method_type (parser
);
26415 rettype
= cp_parser_objc_typename (parser
);
26416 *attributes
= NULL_TREE
;
26417 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
26418 if (kwdparms
== error_mark_node
)
26419 return error_mark_node
;
26420 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
26421 if (optparms
== error_mark_node
)
26422 return error_mark_node
;
26424 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
26428 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
26431 cp_lexer_save_tokens (parser
->lexer
);
26432 tattr
= cp_parser_attributes_opt (parser
);
26433 gcc_assert (tattr
) ;
26435 /* If the attributes are followed by a method introducer, this is not allowed.
26436 Dump the attributes and flag the situation. */
26437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
26438 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
26441 /* Otherwise, the attributes introduce some interstitial code, possibly so
26442 rewind to allow that check. */
26443 cp_lexer_rollback_tokens (parser
->lexer
);
26447 /* Parse an Objective-C method prototype list. */
26450 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
26452 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26454 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26456 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26458 tree attributes
, sig
;
26459 bool is_class_method
;
26460 if (token
->type
== CPP_PLUS
)
26461 is_class_method
= true;
26463 is_class_method
= false;
26464 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
26465 if (sig
== error_mark_node
)
26467 cp_parser_skip_to_end_of_block_or_statement (parser
);
26468 token
= cp_lexer_peek_token (parser
->lexer
);
26471 objc_add_method_declaration (is_class_method
, sig
, attributes
);
26472 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26474 else if (token
->keyword
== RID_AT_PROPERTY
)
26475 cp_parser_objc_at_property_declaration (parser
);
26476 else if (token
->keyword
== RID_ATTRIBUTE
26477 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26478 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26480 "prefix attributes are ignored for methods");
26482 /* Allow for interspersed non-ObjC++ code. */
26483 cp_parser_objc_interstitial_code (parser
);
26485 token
= cp_lexer_peek_token (parser
->lexer
);
26488 if (token
->type
!= CPP_EOF
)
26489 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26491 cp_parser_error (parser
, "expected %<@end%>");
26493 objc_finish_interface ();
26496 /* Parse an Objective-C method definition list. */
26499 cp_parser_objc_method_definition_list (cp_parser
* parser
)
26501 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26503 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26507 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26510 tree sig
, attribute
;
26511 bool is_class_method
;
26512 if (token
->type
== CPP_PLUS
)
26513 is_class_method
= true;
26515 is_class_method
= false;
26516 push_deferring_access_checks (dk_deferred
);
26517 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
26518 if (sig
== error_mark_node
)
26520 cp_parser_skip_to_end_of_block_or_statement (parser
);
26521 token
= cp_lexer_peek_token (parser
->lexer
);
26524 objc_start_method_definition (is_class_method
, sig
, attribute
,
26527 /* For historical reasons, we accept an optional semicolon. */
26528 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26529 cp_lexer_consume_token (parser
->lexer
);
26531 ptk
= cp_lexer_peek_token (parser
->lexer
);
26532 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
26533 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
26535 perform_deferred_access_checks (tf_warning_or_error
);
26536 stop_deferring_access_checks ();
26537 meth
= cp_parser_function_definition_after_declarator (parser
,
26539 pop_deferring_access_checks ();
26540 objc_finish_method_definition (meth
);
26543 /* The following case will be removed once @synthesize is
26544 completely implemented. */
26545 else if (token
->keyword
== RID_AT_PROPERTY
)
26546 cp_parser_objc_at_property_declaration (parser
);
26547 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
26548 cp_parser_objc_at_synthesize_declaration (parser
);
26549 else if (token
->keyword
== RID_AT_DYNAMIC
)
26550 cp_parser_objc_at_dynamic_declaration (parser
);
26551 else if (token
->keyword
== RID_ATTRIBUTE
26552 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26553 warning_at (token
->location
, OPT_Wattributes
,
26554 "prefix attributes are ignored for methods");
26556 /* Allow for interspersed non-ObjC++ code. */
26557 cp_parser_objc_interstitial_code (parser
);
26559 token
= cp_lexer_peek_token (parser
->lexer
);
26562 if (token
->type
!= CPP_EOF
)
26563 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26565 cp_parser_error (parser
, "expected %<@end%>");
26567 objc_finish_implementation ();
26570 /* Parse Objective-C ivars. */
26573 cp_parser_objc_class_ivars (cp_parser
* parser
)
26575 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26577 if (token
->type
!= CPP_OPEN_BRACE
)
26578 return; /* No ivars specified. */
26580 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
26581 token
= cp_lexer_peek_token (parser
->lexer
);
26583 while (token
->type
!= CPP_CLOSE_BRACE
26584 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26586 cp_decl_specifier_seq declspecs
;
26587 int decl_class_or_enum_p
;
26588 tree prefix_attributes
;
26590 cp_parser_objc_visibility_spec (parser
);
26592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26595 cp_parser_decl_specifier_seq (parser
,
26596 CP_PARSER_FLAGS_OPTIONAL
,
26598 &decl_class_or_enum_p
);
26600 /* auto, register, static, extern, mutable. */
26601 if (declspecs
.storage_class
!= sc_none
)
26603 cp_parser_error (parser
, "invalid type for instance variable");
26604 declspecs
.storage_class
= sc_none
;
26607 /* thread_local. */
26608 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26610 cp_parser_error (parser
, "invalid type for instance variable");
26611 declspecs
.locations
[ds_thread
] = 0;
26615 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26617 cp_parser_error (parser
, "invalid type for instance variable");
26618 declspecs
.locations
[ds_typedef
] = 0;
26621 prefix_attributes
= declspecs
.attributes
;
26622 declspecs
.attributes
= NULL_TREE
;
26624 /* Keep going until we hit the `;' at the end of the
26626 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26628 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
26629 cp_declarator
*declarator
= NULL
;
26630 int ctor_dtor_or_conv_p
;
26632 /* Check for a (possibly unnamed) bitfield declaration. */
26633 token
= cp_lexer_peek_token (parser
->lexer
);
26634 if (token
->type
== CPP_COLON
)
26637 if (token
->type
== CPP_NAME
26638 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
26641 /* Get the name of the bitfield. */
26642 declarator
= make_id_declarator (NULL_TREE
,
26643 cp_parser_identifier (parser
),
26647 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26648 /* Get the width of the bitfield. */
26650 = cp_parser_constant_expression (parser
);
26654 /* Parse the declarator. */
26656 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26657 &ctor_dtor_or_conv_p
,
26658 /*parenthesized_p=*/NULL
,
26659 /*member_p=*/false,
26660 /*friend_p=*/false);
26663 /* Look for attributes that apply to the ivar. */
26664 attributes
= cp_parser_attributes_opt (parser
);
26665 /* Remember which attributes are prefix attributes and
26667 first_attribute
= attributes
;
26668 /* Combine the attributes. */
26669 attributes
= chainon (prefix_attributes
, attributes
);
26672 /* Create the bitfield declaration. */
26673 decl
= grokbitfield (declarator
, &declspecs
,
26677 decl
= grokfield (declarator
, &declspecs
,
26678 NULL_TREE
, /*init_const_expr_p=*/false,
26679 NULL_TREE
, attributes
);
26681 /* Add the instance variable. */
26682 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
26683 objc_add_instance_variable (decl
);
26685 /* Reset PREFIX_ATTRIBUTES. */
26686 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26687 attributes
= TREE_CHAIN (attributes
);
26689 TREE_CHAIN (attributes
) = NULL_TREE
;
26691 token
= cp_lexer_peek_token (parser
->lexer
);
26693 if (token
->type
== CPP_COMMA
)
26695 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26701 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26702 token
= cp_lexer_peek_token (parser
->lexer
);
26705 if (token
->keyword
== RID_AT_END
)
26706 cp_parser_error (parser
, "expected %<}%>");
26708 /* Do not consume the RID_AT_END, so it will be read again as terminating
26709 the @interface of @implementation. */
26710 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26711 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
26713 /* For historical reasons, we accept an optional semicolon. */
26714 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26715 cp_lexer_consume_token (parser
->lexer
);
26718 /* Parse an Objective-C protocol declaration. */
26721 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
26723 tree proto
, protorefs
;
26726 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
26727 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
26729 tok
= cp_lexer_peek_token (parser
->lexer
);
26730 error_at (tok
->location
, "identifier expected after %<@protocol%>");
26731 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26735 /* See if we have a forward declaration or a definition. */
26736 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26738 /* Try a forward declaration first. */
26739 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
26745 id
= cp_parser_identifier (parser
);
26746 if (id
== error_mark_node
)
26749 objc_declare_protocol (id
, attributes
);
26751 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26752 cp_lexer_consume_token (parser
->lexer
);
26756 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26759 /* Ok, we got a full-fledged definition (or at least should). */
26762 proto
= cp_parser_identifier (parser
);
26763 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
26764 objc_start_protocol (proto
, protorefs
, attributes
);
26765 cp_parser_objc_method_prototype_list (parser
);
26769 /* Parse an Objective-C superclass or category. */
26772 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
26775 tree
*categ
, bool *is_class_extension
)
26777 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
26779 *super
= *categ
= NULL_TREE
;
26780 *is_class_extension
= false;
26781 if (next
->type
== CPP_COLON
)
26783 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26784 *super
= cp_parser_identifier (parser
);
26786 else if (next
->type
== CPP_OPEN_PAREN
)
26788 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26790 /* If there is no category name, and this is an @interface, we
26791 have a class extension. */
26792 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26794 *categ
= NULL_TREE
;
26795 *is_class_extension
= true;
26798 *categ
= cp_parser_identifier (parser
);
26800 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26804 /* Parse an Objective-C class interface. */
26807 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
26809 tree name
, super
, categ
, protos
;
26810 bool is_class_extension
;
26812 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
26813 name
= cp_parser_identifier (parser
);
26814 if (name
== error_mark_node
)
26816 /* It's hard to recover because even if valid @interface stuff
26817 is to follow, we can't compile it (or validate it) if we
26818 don't even know which class it refers to. Let's assume this
26819 was a stray '@interface' token in the stream and skip it.
26823 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
26824 &is_class_extension
);
26825 protos
= cp_parser_objc_protocol_refs_opt (parser
);
26827 /* We have either a class or a category on our hands. */
26828 if (categ
|| is_class_extension
)
26829 objc_start_category_interface (name
, categ
, protos
, attributes
);
26832 objc_start_class_interface (name
, super
, protos
, attributes
);
26833 /* Handle instance variable declarations, if any. */
26834 cp_parser_objc_class_ivars (parser
);
26835 objc_continue_interface ();
26838 cp_parser_objc_method_prototype_list (parser
);
26841 /* Parse an Objective-C class implementation. */
26844 cp_parser_objc_class_implementation (cp_parser
* parser
)
26846 tree name
, super
, categ
;
26847 bool is_class_extension
;
26849 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
26850 name
= cp_parser_identifier (parser
);
26851 if (name
== error_mark_node
)
26853 /* It's hard to recover because even if valid @implementation
26854 stuff is to follow, we can't compile it (or validate it) if
26855 we don't even know which class it refers to. Let's assume
26856 this was a stray '@implementation' token in the stream and
26861 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
26862 &is_class_extension
);
26864 /* We have either a class or a category on our hands. */
26866 objc_start_category_implementation (name
, categ
);
26869 objc_start_class_implementation (name
, super
);
26870 /* Handle instance variable declarations, if any. */
26871 cp_parser_objc_class_ivars (parser
);
26872 objc_continue_implementation ();
26875 cp_parser_objc_method_definition_list (parser
);
26878 /* Consume the @end token and finish off the implementation. */
26881 cp_parser_objc_end_implementation (cp_parser
* parser
)
26883 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26884 objc_finish_implementation ();
26887 /* Parse an Objective-C declaration. */
26890 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
26892 /* Try to figure out what kind of declaration is present. */
26893 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26896 switch (kwd
->keyword
)
26901 error_at (kwd
->location
, "attributes may not be specified before"
26902 " the %<@%D%> Objective-C++ keyword",
26906 case RID_AT_IMPLEMENTATION
:
26907 warning_at (kwd
->location
, OPT_Wattributes
,
26908 "prefix attributes are ignored before %<@%D%>",
26915 switch (kwd
->keyword
)
26918 cp_parser_objc_alias_declaration (parser
);
26921 cp_parser_objc_class_declaration (parser
);
26923 case RID_AT_PROTOCOL
:
26924 cp_parser_objc_protocol_declaration (parser
, attributes
);
26926 case RID_AT_INTERFACE
:
26927 cp_parser_objc_class_interface (parser
, attributes
);
26929 case RID_AT_IMPLEMENTATION
:
26930 cp_parser_objc_class_implementation (parser
);
26933 cp_parser_objc_end_implementation (parser
);
26936 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26938 cp_parser_skip_to_end_of_block_or_statement (parser
);
26942 /* Parse an Objective-C try-catch-finally statement.
26944 objc-try-catch-finally-stmt:
26945 @try compound-statement objc-catch-clause-seq [opt]
26946 objc-finally-clause [opt]
26948 objc-catch-clause-seq:
26949 objc-catch-clause objc-catch-clause-seq [opt]
26952 @catch ( objc-exception-declaration ) compound-statement
26954 objc-finally-clause:
26955 @finally compound-statement
26957 objc-exception-declaration:
26958 parameter-declaration
26961 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26965 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26966 for C. Keep them in sync. */
26969 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
26971 location_t location
;
26974 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
26975 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26976 objc_maybe_warn_exceptions (location
);
26977 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26978 node, lest it get absorbed into the surrounding block. */
26979 stmt
= push_stmt_list ();
26980 cp_parser_compound_statement (parser
, NULL
, false, false);
26981 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
26983 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
26985 cp_parameter_declarator
*parm
;
26986 tree parameter_declaration
= error_mark_node
;
26987 bool seen_open_paren
= false;
26989 cp_lexer_consume_token (parser
->lexer
);
26990 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26991 seen_open_paren
= true;
26992 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26994 /* We have "@catch (...)" (where the '...' are literally
26995 what is in the code). Skip the '...'.
26996 parameter_declaration is set to NULL_TREE, and
26997 objc_being_catch_clauses() knows that that means
26999 cp_lexer_consume_token (parser
->lexer
);
27000 parameter_declaration
= NULL_TREE
;
27004 /* We have "@catch (NSException *exception)" or something
27005 like that. Parse the parameter declaration. */
27006 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
27008 parameter_declaration
= error_mark_node
;
27010 parameter_declaration
= grokdeclarator (parm
->declarator
,
27011 &parm
->decl_specifiers
,
27012 PARM
, /*initialized=*/0,
27013 /*attrlist=*/NULL
);
27015 if (seen_open_paren
)
27016 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27019 /* If there was no open parenthesis, we are recovering from
27020 an error, and we are trying to figure out what mistake
27021 the user has made. */
27023 /* If there is an immediate closing parenthesis, the user
27024 probably forgot the opening one (ie, they typed "@catch
27025 NSException *e)". Parse the closing parenthesis and keep
27027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
27028 cp_lexer_consume_token (parser
->lexer
);
27030 /* If these is no immediate closing parenthesis, the user
27031 probably doesn't know that parenthesis are required at
27032 all (ie, they typed "@catch NSException *e"). So, just
27033 forget about the closing parenthesis and keep going. */
27035 objc_begin_catch_clause (parameter_declaration
);
27036 cp_parser_compound_statement (parser
, NULL
, false, false);
27037 objc_finish_catch_clause ();
27039 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
27041 cp_lexer_consume_token (parser
->lexer
);
27042 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27043 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27044 node, lest it get absorbed into the surrounding block. */
27045 stmt
= push_stmt_list ();
27046 cp_parser_compound_statement (parser
, NULL
, false, false);
27047 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
27050 return objc_finish_try_stmt ();
27053 /* Parse an Objective-C synchronized statement.
27055 objc-synchronized-stmt:
27056 @synchronized ( expression ) compound-statement
27058 Returns NULL_TREE. */
27061 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
27063 location_t location
;
27066 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
27068 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27069 objc_maybe_warn_exceptions (location
);
27070 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27071 lock
= cp_parser_expression (parser
);
27072 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27074 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27075 node, lest it get absorbed into the surrounding block. */
27076 stmt
= push_stmt_list ();
27077 cp_parser_compound_statement (parser
, NULL
, false, false);
27079 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
27082 /* Parse an Objective-C throw statement.
27085 @throw assignment-expression [opt] ;
27087 Returns a constructed '@throw' statement. */
27090 cp_parser_objc_throw_statement (cp_parser
*parser
)
27092 tree expr
= NULL_TREE
;
27093 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27095 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
27097 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27098 expr
= cp_parser_expression (parser
);
27100 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27102 return objc_build_throw_stmt (loc
, expr
);
27105 /* Parse an Objective-C statement. */
27108 cp_parser_objc_statement (cp_parser
* parser
)
27110 /* Try to figure out what kind of declaration is present. */
27111 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
27113 switch (kwd
->keyword
)
27116 return cp_parser_objc_try_catch_finally_statement (parser
);
27117 case RID_AT_SYNCHRONIZED
:
27118 return cp_parser_objc_synchronized_statement (parser
);
27120 return cp_parser_objc_throw_statement (parser
);
27122 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
27124 cp_parser_skip_to_end_of_block_or_statement (parser
);
27127 return error_mark_node
;
27130 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27131 look ahead to see if an objc keyword follows the attributes. This
27132 is to detect the use of prefix attributes on ObjC @interface and
27136 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
27138 cp_lexer_save_tokens (parser
->lexer
);
27139 *attrib
= cp_parser_attributes_opt (parser
);
27140 gcc_assert (*attrib
);
27141 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
27143 cp_lexer_commit_tokens (parser
->lexer
);
27146 cp_lexer_rollback_tokens (parser
->lexer
);
27150 /* This routine is a minimal replacement for
27151 c_parser_struct_declaration () used when parsing the list of
27152 types/names or ObjC++ properties. For example, when parsing the
27155 @property (readonly) int a, b, c;
27157 this function is responsible for parsing "int a, int b, int c" and
27158 returning the declarations as CHAIN of DECLs.
27160 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27161 similar parsing. */
27163 cp_parser_objc_struct_declaration (cp_parser
*parser
)
27165 tree decls
= NULL_TREE
;
27166 cp_decl_specifier_seq declspecs
;
27167 int decl_class_or_enum_p
;
27168 tree prefix_attributes
;
27170 cp_parser_decl_specifier_seq (parser
,
27171 CP_PARSER_FLAGS_NONE
,
27173 &decl_class_or_enum_p
);
27175 if (declspecs
.type
== error_mark_node
)
27176 return error_mark_node
;
27178 /* auto, register, static, extern, mutable. */
27179 if (declspecs
.storage_class
!= sc_none
)
27181 cp_parser_error (parser
, "invalid type for property");
27182 declspecs
.storage_class
= sc_none
;
27185 /* thread_local. */
27186 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
27188 cp_parser_error (parser
, "invalid type for property");
27189 declspecs
.locations
[ds_thread
] = 0;
27193 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
27195 cp_parser_error (parser
, "invalid type for property");
27196 declspecs
.locations
[ds_typedef
] = 0;
27199 prefix_attributes
= declspecs
.attributes
;
27200 declspecs
.attributes
= NULL_TREE
;
27202 /* Keep going until we hit the `;' at the end of the declaration. */
27203 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27205 tree attributes
, first_attribute
, decl
;
27206 cp_declarator
*declarator
;
27209 /* Parse the declarator. */
27210 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27211 NULL
, NULL
, false, false);
27213 /* Look for attributes that apply to the ivar. */
27214 attributes
= cp_parser_attributes_opt (parser
);
27215 /* Remember which attributes are prefix attributes and
27217 first_attribute
= attributes
;
27218 /* Combine the attributes. */
27219 attributes
= chainon (prefix_attributes
, attributes
);
27221 decl
= grokfield (declarator
, &declspecs
,
27222 NULL_TREE
, /*init_const_expr_p=*/false,
27223 NULL_TREE
, attributes
);
27225 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
27226 return error_mark_node
;
27228 /* Reset PREFIX_ATTRIBUTES. */
27229 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
27230 attributes
= TREE_CHAIN (attributes
);
27232 TREE_CHAIN (attributes
) = NULL_TREE
;
27234 DECL_CHAIN (decl
) = decls
;
27237 token
= cp_lexer_peek_token (parser
->lexer
);
27238 if (token
->type
== CPP_COMMA
)
27240 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27249 /* Parse an Objective-C @property declaration. The syntax is:
27251 objc-property-declaration:
27252 '@property' objc-property-attributes[opt] struct-declaration ;
27254 objc-property-attributes:
27255 '(' objc-property-attribute-list ')'
27257 objc-property-attribute-list:
27258 objc-property-attribute
27259 objc-property-attribute-list, objc-property-attribute
27261 objc-property-attribute
27262 'getter' = identifier
27263 'setter' = identifier
27272 @property NSString *name;
27273 @property (readonly) id object;
27274 @property (retain, nonatomic, getter=getTheName) id name;
27275 @property int a, b, c;
27277 PS: This function is identical to
27278 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27280 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
27282 /* The following variables hold the attributes of the properties as
27283 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27284 seen. When we see an attribute, we set them to 'true' (if they
27285 are boolean properties) or to the identifier (if they have an
27286 argument, ie, for getter and setter). Note that here we only
27287 parse the list of attributes, check the syntax and accumulate the
27288 attributes that we find. objc_add_property_declaration() will
27289 then process the information. */
27290 bool property_assign
= false;
27291 bool property_copy
= false;
27292 tree property_getter_ident
= NULL_TREE
;
27293 bool property_nonatomic
= false;
27294 bool property_readonly
= false;
27295 bool property_readwrite
= false;
27296 bool property_retain
= false;
27297 tree property_setter_ident
= NULL_TREE
;
27299 /* 'properties' is the list of properties that we read. Usually a
27300 single one, but maybe more (eg, in "@property int a, b, c;" there
27305 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27307 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
27309 /* Parse the optional attribute list... */
27310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27313 cp_lexer_consume_token (parser
->lexer
);
27317 bool syntax_error
= false;
27318 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27321 if (token
->type
!= CPP_NAME
)
27323 cp_parser_error (parser
, "expected identifier");
27326 keyword
= C_RID_CODE (token
->u
.value
);
27327 cp_lexer_consume_token (parser
->lexer
);
27330 case RID_ASSIGN
: property_assign
= true; break;
27331 case RID_COPY
: property_copy
= true; break;
27332 case RID_NONATOMIC
: property_nonatomic
= true; break;
27333 case RID_READONLY
: property_readonly
= true; break;
27334 case RID_READWRITE
: property_readwrite
= true; break;
27335 case RID_RETAIN
: property_retain
= true; break;
27339 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
27341 if (keyword
== RID_GETTER
)
27342 cp_parser_error (parser
,
27343 "missing %<=%> (after %<getter%> attribute)");
27345 cp_parser_error (parser
,
27346 "missing %<=%> (after %<setter%> attribute)");
27347 syntax_error
= true;
27350 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
27351 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
27353 cp_parser_error (parser
, "expected identifier");
27354 syntax_error
= true;
27357 if (keyword
== RID_SETTER
)
27359 if (property_setter_ident
!= NULL_TREE
)
27361 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
27362 cp_lexer_consume_token (parser
->lexer
);
27365 property_setter_ident
= cp_parser_objc_selector (parser
);
27366 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27367 cp_parser_error (parser
, "setter name must terminate with %<:%>");
27369 cp_lexer_consume_token (parser
->lexer
);
27373 if (property_getter_ident
!= NULL_TREE
)
27375 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
27376 cp_lexer_consume_token (parser
->lexer
);
27379 property_getter_ident
= cp_parser_objc_selector (parser
);
27383 cp_parser_error (parser
, "unknown property attribute");
27384 syntax_error
= true;
27391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27392 cp_lexer_consume_token (parser
->lexer
);
27397 /* FIXME: "@property (setter, assign);" will generate a spurious
27398 "error: expected ‘)’ before ‘,’ token". This is because
27399 cp_parser_require, unlike the C counterpart, will produce an
27400 error even if we are in error recovery. */
27401 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27403 cp_parser_skip_to_closing_parenthesis (parser
,
27404 /*recovering=*/true,
27405 /*or_comma=*/false,
27406 /*consume_paren=*/true);
27410 /* ... and the property declaration(s). */
27411 properties
= cp_parser_objc_struct_declaration (parser
);
27413 if (properties
== error_mark_node
)
27415 cp_parser_skip_to_end_of_statement (parser
);
27416 /* If the next token is now a `;', consume it. */
27417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27418 cp_lexer_consume_token (parser
->lexer
);
27422 if (properties
== NULL_TREE
)
27423 cp_parser_error (parser
, "expected identifier");
27426 /* Comma-separated properties are chained together in
27427 reverse order; add them one by one. */
27428 properties
= nreverse (properties
);
27430 for (; properties
; properties
= TREE_CHAIN (properties
))
27431 objc_add_property_declaration (loc
, copy_node (properties
),
27432 property_readonly
, property_readwrite
,
27433 property_assign
, property_retain
,
27434 property_copy
, property_nonatomic
,
27435 property_getter_ident
, property_setter_ident
);
27438 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27441 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27443 objc-synthesize-declaration:
27444 @synthesize objc-synthesize-identifier-list ;
27446 objc-synthesize-identifier-list:
27447 objc-synthesize-identifier
27448 objc-synthesize-identifier-list, objc-synthesize-identifier
27450 objc-synthesize-identifier
27452 identifier = identifier
27455 @synthesize MyProperty;
27456 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27458 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27459 for C. Keep them in sync.
27462 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
27464 tree list
= NULL_TREE
;
27466 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27468 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
27471 tree property
, ivar
;
27472 property
= cp_parser_identifier (parser
);
27473 if (property
== error_mark_node
)
27475 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
27480 cp_lexer_consume_token (parser
->lexer
);
27481 ivar
= cp_parser_identifier (parser
);
27482 if (ivar
== error_mark_node
)
27484 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27490 list
= chainon (list
, build_tree_list (ivar
, property
));
27491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27492 cp_lexer_consume_token (parser
->lexer
);
27496 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27497 objc_add_synthesize_declaration (loc
, list
);
27500 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27502 objc-dynamic-declaration:
27503 @dynamic identifier-list ;
27506 @dynamic MyProperty;
27507 @dynamic MyProperty, AnotherProperty;
27509 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27510 for C. Keep them in sync.
27513 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
27515 tree list
= NULL_TREE
;
27517 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27519 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
27523 property
= cp_parser_identifier (parser
);
27524 if (property
== error_mark_node
)
27526 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27529 list
= chainon (list
, build_tree_list (NULL
, property
));
27530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27531 cp_lexer_consume_token (parser
->lexer
);
27535 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27536 objc_add_dynamic_declaration (loc
, list
);
27540 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27542 /* Returns name of the next clause.
27543 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27544 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27545 returned and the token is consumed. */
27547 static pragma_omp_clause
27548 cp_parser_omp_clause_name (cp_parser
*parser
)
27550 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
27552 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
27553 result
= PRAGMA_OMP_CLAUSE_IF
;
27554 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
27555 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
27556 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
27557 result
= PRAGMA_OACC_CLAUSE_DELETE
;
27558 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
27559 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
27560 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27561 result
= PRAGMA_OMP_CLAUSE_FOR
;
27562 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27564 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27565 const char *p
= IDENTIFIER_POINTER (id
);
27570 if (!strcmp ("aligned", p
))
27571 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
27572 else if (!strcmp ("async", p
))
27573 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
27576 if (!strcmp ("collapse", p
))
27577 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
27578 else if (!strcmp ("copy", p
))
27579 result
= PRAGMA_OACC_CLAUSE_COPY
;
27580 else if (!strcmp ("copyin", p
))
27581 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
27582 else if (!strcmp ("copyout", p
))
27583 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
27584 else if (!strcmp ("copyprivate", p
))
27585 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
27586 else if (!strcmp ("create", p
))
27587 result
= PRAGMA_OACC_CLAUSE_CREATE
;
27590 if (!strcmp ("depend", p
))
27591 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
27592 else if (!strcmp ("device", p
))
27593 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
27594 else if (!strcmp ("deviceptr", p
))
27595 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
27596 else if (!strcmp ("dist_schedule", p
))
27597 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
27600 if (!strcmp ("final", p
))
27601 result
= PRAGMA_OMP_CLAUSE_FINAL
;
27602 else if (!strcmp ("firstprivate", p
))
27603 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
27604 else if (!strcmp ("from", p
))
27605 result
= PRAGMA_OMP_CLAUSE_FROM
;
27608 if (!strcmp ("host", p
))
27609 result
= PRAGMA_OACC_CLAUSE_HOST
;
27612 if (!strcmp ("inbranch", p
))
27613 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
27616 if (!strcmp ("lastprivate", p
))
27617 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
27618 else if (!strcmp ("linear", p
))
27619 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
27622 if (!strcmp ("map", p
))
27623 result
= PRAGMA_OMP_CLAUSE_MAP
;
27624 else if (!strcmp ("mergeable", p
))
27625 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
27626 else if (flag_cilkplus
&& !strcmp ("mask", p
))
27627 result
= PRAGMA_CILK_CLAUSE_MASK
;
27630 if (!strcmp ("notinbranch", p
))
27631 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
27632 else if (!strcmp ("nowait", p
))
27633 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
27634 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
27635 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
27636 else if (!strcmp ("num_gangs", p
))
27637 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
27638 else if (!strcmp ("num_teams", p
))
27639 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
27640 else if (!strcmp ("num_threads", p
))
27641 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
27642 else if (!strcmp ("num_workers", p
))
27643 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
27646 if (!strcmp ("ordered", p
))
27647 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
27650 if (!strcmp ("parallel", p
))
27651 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
27652 else if (!strcmp ("present", p
))
27653 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
27654 else if (!strcmp ("present_or_copy", p
)
27655 || !strcmp ("pcopy", p
))
27656 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
27657 else if (!strcmp ("present_or_copyin", p
)
27658 || !strcmp ("pcopyin", p
))
27659 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
27660 else if (!strcmp ("present_or_copyout", p
)
27661 || !strcmp ("pcopyout", p
))
27662 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
27663 else if (!strcmp ("present_or_create", p
)
27664 || !strcmp ("pcreate", p
))
27665 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
27666 else if (!strcmp ("proc_bind", p
))
27667 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
27670 if (!strcmp ("reduction", p
))
27671 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
27674 if (!strcmp ("safelen", p
))
27675 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
27676 else if (!strcmp ("schedule", p
))
27677 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
27678 else if (!strcmp ("sections", p
))
27679 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
27680 else if (!strcmp ("self", p
))
27681 result
= PRAGMA_OACC_CLAUSE_SELF
;
27682 else if (!strcmp ("shared", p
))
27683 result
= PRAGMA_OMP_CLAUSE_SHARED
;
27684 else if (!strcmp ("simdlen", p
))
27685 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
27688 if (!strcmp ("taskgroup", p
))
27689 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
27690 else if (!strcmp ("thread_limit", p
))
27691 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
27692 else if (!strcmp ("to", p
))
27693 result
= PRAGMA_OMP_CLAUSE_TO
;
27696 if (!strcmp ("uniform", p
))
27697 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
27698 else if (!strcmp ("untied", p
))
27699 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
27702 if (!strcmp ("vector_length", p
))
27703 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
27704 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
27705 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
27708 if (!strcmp ("wait", p
))
27709 result
= PRAGMA_OACC_CLAUSE_WAIT
;
27714 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
27715 cp_lexer_consume_token (parser
->lexer
);
27720 /* Validate that a clause of the given type does not already exist. */
27723 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
27724 const char *name
, location_t location
)
27728 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27729 if (OMP_CLAUSE_CODE (c
) == code
)
27731 error_at (location
, "too many %qs clauses", name
);
27739 variable-list , identifier
27741 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27742 colon). An opening parenthesis will have been consumed by the caller.
27744 If KIND is nonzero, create the appropriate node and install the decl
27745 in OMP_CLAUSE_DECL and add the node to the head of the list.
27747 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27748 return the list created.
27750 COLON can be NULL if only closing parenthesis should end the list,
27751 or pointer to bool which will receive false if the list is terminated
27752 by closing parenthesis or true if the list is terminated by colon. */
27755 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
27756 tree list
, bool *colon
)
27759 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27762 parser
->colon_corrects_to_scope_p
= false;
27769 token
= cp_lexer_peek_token (parser
->lexer
);
27770 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
27771 /*check_dependency_p=*/true,
27772 /*template_p=*/NULL
,
27773 /*declarator_p=*/false,
27774 /*optional_p=*/false);
27775 if (name
== error_mark_node
)
27778 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27779 if (decl
== error_mark_node
)
27780 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
27782 else if (kind
!= 0)
27786 case OMP_CLAUSE__CACHE_
:
27787 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
27789 error_at (token
->location
, "expected %<[%>");
27790 decl
= error_mark_node
;
27793 /* FALL THROUGH. */
27794 case OMP_CLAUSE_MAP
:
27795 case OMP_CLAUSE_FROM
:
27796 case OMP_CLAUSE_TO
:
27797 case OMP_CLAUSE_DEPEND
:
27798 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27800 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
27802 parser
->colon_corrects_to_scope_p
= false;
27803 cp_lexer_consume_token (parser
->lexer
);
27804 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27805 low_bound
= cp_parser_expression (parser
);
27807 parser
->colon_corrects_to_scope_p
27808 = saved_colon_corrects_to_scope_p
;
27809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
27810 length
= integer_one_node
;
27813 /* Look for `:'. */
27814 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27816 if (!cp_lexer_next_token_is (parser
->lexer
,
27818 length
= cp_parser_expression (parser
);
27820 /* Look for the closing `]'. */
27821 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
27825 if (kind
== OMP_CLAUSE__CACHE_
)
27827 if (TREE_CODE (low_bound
) != INTEGER_CST
27828 && !TREE_READONLY (low_bound
))
27830 error_at (token
->location
,
27831 "%qD is not a constant", low_bound
);
27832 decl
= error_mark_node
;
27835 if (TREE_CODE (length
) != INTEGER_CST
27836 && !TREE_READONLY (length
))
27838 error_at (token
->location
,
27839 "%qD is not a constant", length
);
27840 decl
= error_mark_node
;
27844 decl
= tree_cons (low_bound
, length
, decl
);
27851 tree u
= build_omp_clause (token
->location
, kind
);
27852 OMP_CLAUSE_DECL (u
) = decl
;
27853 OMP_CLAUSE_CHAIN (u
) = list
;
27857 list
= tree_cons (decl
, NULL_TREE
, list
);
27860 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27862 cp_lexer_consume_token (parser
->lexer
);
27866 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27868 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27871 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27875 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27879 /* Try to resync to an unnested comma. Copied from
27880 cp_parser_parenthesized_expression_list. */
27883 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27884 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
27885 /*recovering=*/true,
27887 /*consume_paren=*/true);
27895 /* Similarly, but expect leading and trailing parenthesis. This is a very
27896 common case for omp clauses. */
27899 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
27901 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27902 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
27907 copy ( variable-list )
27908 copyin ( variable-list )
27909 copyout ( variable-list )
27910 create ( variable-list )
27911 delete ( variable-list )
27912 present ( variable-list )
27913 present_or_copy ( variable-list )
27914 pcopy ( variable-list )
27915 present_or_copyin ( variable-list )
27916 pcopyin ( variable-list )
27917 present_or_copyout ( variable-list )
27918 pcopyout ( variable-list )
27919 present_or_create ( variable-list )
27920 pcreate ( variable-list ) */
27923 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
27926 enum gomp_map_kind kind
;
27929 case PRAGMA_OACC_CLAUSE_COPY
:
27930 kind
= GOMP_MAP_FORCE_TOFROM
;
27932 case PRAGMA_OACC_CLAUSE_COPYIN
:
27933 kind
= GOMP_MAP_FORCE_TO
;
27935 case PRAGMA_OACC_CLAUSE_COPYOUT
:
27936 kind
= GOMP_MAP_FORCE_FROM
;
27938 case PRAGMA_OACC_CLAUSE_CREATE
:
27939 kind
= GOMP_MAP_FORCE_ALLOC
;
27941 case PRAGMA_OACC_CLAUSE_DELETE
:
27942 kind
= GOMP_MAP_FORCE_DEALLOC
;
27944 case PRAGMA_OACC_CLAUSE_DEVICE
:
27945 kind
= GOMP_MAP_FORCE_TO
;
27947 case PRAGMA_OACC_CLAUSE_HOST
:
27948 case PRAGMA_OACC_CLAUSE_SELF
:
27949 kind
= GOMP_MAP_FORCE_FROM
;
27951 case PRAGMA_OACC_CLAUSE_PRESENT
:
27952 kind
= GOMP_MAP_FORCE_PRESENT
;
27954 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
27955 kind
= GOMP_MAP_TOFROM
;
27957 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
27958 kind
= GOMP_MAP_TO
;
27960 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
27961 kind
= GOMP_MAP_FROM
;
27963 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
27964 kind
= GOMP_MAP_ALLOC
;
27967 gcc_unreachable ();
27970 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
27972 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27973 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
27979 deviceptr ( variable-list ) */
27982 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
27984 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27987 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27988 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
27989 variable-list must only allow for pointer variables. */
27990 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
27991 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
27993 tree v
= TREE_PURPOSE (t
);
27995 /* FIXME diagnostics: Ideally we should keep individual
27996 locations for all the variables in the var list to make the
27997 following errors more precise. Perhaps
27998 c_parser_omp_var_list_parens should construct a list of
27999 locations to go along with the var list. */
28001 if (TREE_CODE (v
) != VAR_DECL
)
28002 error_at (loc
, "%qD is not a variable", v
);
28003 else if (TREE_TYPE (v
) == error_mark_node
)
28005 else if (!POINTER_TYPE_P (TREE_TYPE (v
)))
28006 error_at (loc
, "%qD is not a pointer variable", v
);
28008 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
28009 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
28010 OMP_CLAUSE_DECL (u
) = v
;
28011 OMP_CLAUSE_CHAIN (u
) = list
;
28019 vector_length ( expression ) */
28022 cp_parser_oacc_clause_vector_length (cp_parser
*parser
, tree list
)
28025 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28026 bool error
= false;
28028 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28031 t
= cp_parser_condition (parser
);
28032 if (t
== error_mark_node
|| !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
28034 error_at (location
, "expected positive integer expression");
28038 if (error
|| !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28040 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28041 /*or_comma=*/false,
28042 /*consume_paren=*/true);
28046 check_no_duplicate_clause (list
, OMP_CLAUSE_VECTOR_LENGTH
, "vector_length",
28049 c
= build_omp_clause (location
, OMP_CLAUSE_VECTOR_LENGTH
);
28050 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c
) = t
;
28051 OMP_CLAUSE_CHAIN (c
) = list
;
28058 Parse wait clause or directive parameters. */
28061 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
28063 vec
<tree
, va_gc
> *args
;
28066 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28068 /*allow_expansion_p=*/true,
28069 /*non_constant_p=*/NULL
);
28071 if (args
== NULL
|| args
->length () == 0)
28073 cp_parser_error (parser
, "expected integer expression before ')'");
28075 release_tree_vector (args
);
28079 args_tree
= build_tree_list_vec (args
);
28081 release_tree_vector (args
);
28083 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
28085 tree targ
= TREE_VALUE (t
);
28087 if (targ
!= error_mark_node
)
28089 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
28090 error ("%<wait%> expression must be integral");
28093 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
28095 mark_rvalue_use (targ
);
28096 OMP_CLAUSE_DECL (c
) = targ
;
28097 OMP_CLAUSE_CHAIN (c
) = list
;
28107 wait ( int-expr-list ) */
28110 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
28112 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28114 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
28117 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
28123 collapse ( constant-expression ) */
28126 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
28132 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28133 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28136 num
= cp_parser_constant_expression (parser
);
28138 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28139 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28140 /*or_comma=*/false,
28141 /*consume_paren=*/true);
28143 if (num
== error_mark_node
)
28145 num
= fold_non_dependent_expr (num
);
28146 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
28147 || !tree_fits_shwi_p (num
)
28148 || (n
= tree_to_shwi (num
)) <= 0
28151 error_at (loc
, "collapse argument needs positive constant integer expression");
28155 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
28156 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
28157 OMP_CLAUSE_CHAIN (c
) = list
;
28158 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
28164 default ( shared | none ) */
28167 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
28169 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
28172 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28176 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28177 const char *p
= IDENTIFIER_POINTER (id
);
28182 if (strcmp ("none", p
) != 0)
28184 kind
= OMP_CLAUSE_DEFAULT_NONE
;
28188 if (strcmp ("shared", p
) != 0)
28190 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
28197 cp_lexer_consume_token (parser
->lexer
);
28202 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
28205 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28206 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28207 /*or_comma=*/false,
28208 /*consume_paren=*/true);
28210 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
28213 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
28214 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
28215 OMP_CLAUSE_CHAIN (c
) = list
;
28216 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
28222 final ( expression ) */
28225 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
28229 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28232 t
= cp_parser_condition (parser
);
28234 if (t
== error_mark_node
28235 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28236 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28237 /*or_comma=*/false,
28238 /*consume_paren=*/true);
28240 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
28242 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
28243 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
28244 OMP_CLAUSE_CHAIN (c
) = list
;
28250 if ( expression ) */
28253 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
28257 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28260 t
= cp_parser_condition (parser
);
28262 if (t
== error_mark_node
28263 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28264 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28265 /*or_comma=*/false,
28266 /*consume_paren=*/true);
28268 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
28270 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
28271 OMP_CLAUSE_IF_EXPR (c
) = t
;
28272 OMP_CLAUSE_CHAIN (c
) = list
;
28281 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
28282 tree list
, location_t location
)
28286 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
28289 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
28290 OMP_CLAUSE_CHAIN (c
) = list
;
28298 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
28299 tree list
, location_t location
)
28303 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
28305 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
28306 OMP_CLAUSE_CHAIN (c
) = list
;
28311 num_gangs ( expression ) */
28314 cp_parser_omp_clause_num_gangs (cp_parser
*parser
, tree list
)
28317 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28319 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28322 t
= cp_parser_condition (parser
);
28324 if (t
== error_mark_node
28325 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28326 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28327 /*or_comma=*/false,
28328 /*consume_paren=*/true);
28330 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
28332 error_at (location
, "expected positive integer expression");
28336 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_GANGS
, "num_gangs", location
);
28338 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_GANGS
);
28339 OMP_CLAUSE_NUM_GANGS_EXPR (c
) = t
;
28340 OMP_CLAUSE_CHAIN (c
) = list
;
28347 num_threads ( expression ) */
28350 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
28351 location_t location
)
28355 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28358 t
= cp_parser_expression (parser
);
28360 if (t
== error_mark_node
28361 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28362 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28363 /*or_comma=*/false,
28364 /*consume_paren=*/true);
28366 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
28367 "num_threads", location
);
28369 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
28370 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
28371 OMP_CLAUSE_CHAIN (c
) = list
;
28377 num_workers ( expression ) */
28380 cp_parser_omp_clause_num_workers (cp_parser
*parser
, tree list
)
28383 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28385 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28388 t
= cp_parser_condition (parser
);
28390 if (t
== error_mark_node
28391 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28392 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28393 /*or_comma=*/false,
28394 /*consume_paren=*/true);
28396 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
28398 error_at (location
, "expected positive integer expression");
28402 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_WORKERS
, "num_gangs",
28405 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_WORKERS
);
28406 OMP_CLAUSE_NUM_WORKERS_EXPR (c
) = t
;
28407 OMP_CLAUSE_CHAIN (c
) = list
;
28417 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
28418 tree list
, location_t location
)
28422 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
28423 "ordered", location
);
28425 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
28426 OMP_CLAUSE_CHAIN (c
) = list
;
28431 reduction ( reduction-operator : variable-list )
28433 reduction-operator:
28434 One of: + * - & ^ | && ||
28438 reduction-operator:
28439 One of: + * - & ^ | && || min max
28443 reduction-operator:
28444 One of: + * - & ^ | && ||
28448 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
28450 enum tree_code code
= ERROR_MARK
;
28451 tree nlist
, c
, id
= NULL_TREE
;
28453 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28456 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28458 case CPP_PLUS
: code
= PLUS_EXPR
; break;
28459 case CPP_MULT
: code
= MULT_EXPR
; break;
28460 case CPP_MINUS
: code
= MINUS_EXPR
; break;
28461 case CPP_AND
: code
= BIT_AND_EXPR
; break;
28462 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
28463 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
28464 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
28465 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
28469 if (code
!= ERROR_MARK
)
28470 cp_lexer_consume_token (parser
->lexer
);
28473 bool saved_colon_corrects_to_scope_p
;
28474 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
28475 parser
->colon_corrects_to_scope_p
= false;
28476 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
28477 /*check_dependency_p=*/true,
28478 /*template_p=*/NULL
,
28479 /*declarator_p=*/false,
28480 /*optional_p=*/false);
28481 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
28482 if (identifier_p (id
))
28484 const char *p
= IDENTIFIER_POINTER (id
);
28486 if (strcmp (p
, "min") == 0)
28488 else if (strcmp (p
, "max") == 0)
28490 else if (id
== ansi_opname (PLUS_EXPR
))
28492 else if (id
== ansi_opname (MULT_EXPR
))
28494 else if (id
== ansi_opname (MINUS_EXPR
))
28496 else if (id
== ansi_opname (BIT_AND_EXPR
))
28497 code
= BIT_AND_EXPR
;
28498 else if (id
== ansi_opname (BIT_IOR_EXPR
))
28499 code
= BIT_IOR_EXPR
;
28500 else if (id
== ansi_opname (BIT_XOR_EXPR
))
28501 code
= BIT_XOR_EXPR
;
28502 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
28503 code
= TRUTH_ANDIF_EXPR
;
28504 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
28505 code
= TRUTH_ORIF_EXPR
;
28506 id
= omp_reduction_id (code
, id
, NULL_TREE
);
28507 tree scope
= parser
->scope
;
28509 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
28510 parser
->scope
= NULL_TREE
;
28511 parser
->qualifying_scope
= NULL_TREE
;
28512 parser
->object_scope
= NULL_TREE
;
28516 error ("invalid reduction-identifier");
28518 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28519 /*or_comma=*/false,
28520 /*consume_paren=*/true);
28525 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28528 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
28530 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28532 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
28533 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
28540 schedule ( schedule-kind )
28541 schedule ( schedule-kind , expression )
28544 static | dynamic | guided | runtime | auto */
28547 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
28551 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28554 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
28556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28558 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28559 const char *p
= IDENTIFIER_POINTER (id
);
28564 if (strcmp ("dynamic", p
) != 0)
28566 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
28570 if (strcmp ("guided", p
) != 0)
28572 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
28576 if (strcmp ("runtime", p
) != 0)
28578 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
28585 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28586 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
28587 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
28588 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
28591 cp_lexer_consume_token (parser
->lexer
);
28593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28596 cp_lexer_consume_token (parser
->lexer
);
28598 token
= cp_lexer_peek_token (parser
->lexer
);
28599 t
= cp_parser_assignment_expression (parser
);
28601 if (t
== error_mark_node
)
28603 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
28604 error_at (token
->location
, "schedule %<runtime%> does not take "
28605 "a %<chunk_size%> parameter");
28606 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
28607 error_at (token
->location
, "schedule %<auto%> does not take "
28608 "a %<chunk_size%> parameter");
28610 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
28612 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28615 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28618 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
28619 OMP_CLAUSE_CHAIN (c
) = list
;
28623 cp_parser_error (parser
, "invalid schedule kind");
28625 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28626 /*or_comma=*/false,
28627 /*consume_paren=*/true);
28635 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
28636 tree list
, location_t location
)
28640 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
28642 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
28643 OMP_CLAUSE_CHAIN (c
) = list
;
28652 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
28653 tree list
, location_t location
)
28655 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
28656 tree c
= build_omp_clause (location
, code
);
28657 OMP_CLAUSE_CHAIN (c
) = list
;
28668 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
28669 enum omp_clause_code code
,
28670 tree list
, location_t location
)
28672 tree c
= build_omp_clause (location
, code
);
28673 OMP_CLAUSE_CHAIN (c
) = list
;
28678 num_teams ( expression ) */
28681 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
28682 location_t location
)
28686 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28689 t
= cp_parser_expression (parser
);
28691 if (t
== error_mark_node
28692 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28693 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28694 /*or_comma=*/false,
28695 /*consume_paren=*/true);
28697 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
28698 "num_teams", location
);
28700 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
28701 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
28702 OMP_CLAUSE_CHAIN (c
) = list
;
28708 thread_limit ( expression ) */
28711 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
28712 location_t location
)
28716 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28719 t
= cp_parser_expression (parser
);
28721 if (t
== error_mark_node
28722 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28723 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28724 /*or_comma=*/false,
28725 /*consume_paren=*/true);
28727 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
28728 "thread_limit", location
);
28730 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
28731 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
28732 OMP_CLAUSE_CHAIN (c
) = list
;
28738 aligned ( variable-list )
28739 aligned ( variable-list : constant-expression ) */
28742 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
28744 tree nlist
, c
, alignment
= NULL_TREE
;
28747 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28750 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
28755 alignment
= cp_parser_constant_expression (parser
);
28757 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28758 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28759 /*or_comma=*/false,
28760 /*consume_paren=*/true);
28762 if (alignment
== error_mark_node
)
28763 alignment
= NULL_TREE
;
28766 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28767 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
28773 linear ( variable-list )
28774 linear ( variable-list : expression ) */
28777 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
28778 bool is_cilk_simd_fn
)
28780 tree nlist
, c
, step
= integer_one_node
;
28783 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28786 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
28791 step
= cp_parser_expression (parser
);
28793 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
28795 sorry ("using parameters for %<linear%> step is not supported yet");
28796 step
= integer_one_node
;
28798 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28799 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28800 /*or_comma=*/false,
28801 /*consume_paren=*/true);
28803 if (step
== error_mark_node
)
28807 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28808 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
28814 safelen ( constant-expression ) */
28817 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
28818 location_t location
)
28822 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28825 t
= cp_parser_constant_expression (parser
);
28827 if (t
== error_mark_node
28828 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28829 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28830 /*or_comma=*/false,
28831 /*consume_paren=*/true);
28833 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
28835 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
28836 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
28837 OMP_CLAUSE_CHAIN (c
) = list
;
28843 simdlen ( constant-expression ) */
28846 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
28847 location_t location
)
28851 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28854 t
= cp_parser_constant_expression (parser
);
28856 if (t
== error_mark_node
28857 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28858 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28859 /*or_comma=*/false,
28860 /*consume_paren=*/true);
28862 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
28864 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
28865 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
28866 OMP_CLAUSE_CHAIN (c
) = list
;
28872 depend ( depend-kind : variable-list )
28875 in | out | inout */
28878 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
28881 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
28883 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28888 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28889 const char *p
= IDENTIFIER_POINTER (id
);
28891 if (strcmp ("in", p
) == 0)
28892 kind
= OMP_CLAUSE_DEPEND_IN
;
28893 else if (strcmp ("inout", p
) == 0)
28894 kind
= OMP_CLAUSE_DEPEND_INOUT
;
28895 else if (strcmp ("out", p
) == 0)
28896 kind
= OMP_CLAUSE_DEPEND_OUT
;
28903 cp_lexer_consume_token (parser
->lexer
);
28904 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28907 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
28910 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28911 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
28916 cp_parser_error (parser
, "invalid depend kind");
28918 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28919 /*or_comma=*/false,
28920 /*consume_paren=*/true);
28925 map ( map-kind : variable-list )
28926 map ( variable-list )
28929 alloc | to | from | tofrom */
28932 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
28935 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
28937 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
28941 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
28943 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28944 const char *p
= IDENTIFIER_POINTER (id
);
28946 if (strcmp ("alloc", p
) == 0)
28947 kind
= GOMP_MAP_ALLOC
;
28948 else if (strcmp ("to", p
) == 0)
28949 kind
= GOMP_MAP_TO
;
28950 else if (strcmp ("from", p
) == 0)
28951 kind
= GOMP_MAP_FROM
;
28952 else if (strcmp ("tofrom", p
) == 0)
28953 kind
= GOMP_MAP_TOFROM
;
28956 cp_parser_error (parser
, "invalid map kind");
28957 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28958 /*or_comma=*/false,
28959 /*consume_paren=*/true);
28962 cp_lexer_consume_token (parser
->lexer
);
28963 cp_lexer_consume_token (parser
->lexer
);
28966 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
28969 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28970 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
28976 device ( expression ) */
28979 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
28980 location_t location
)
28984 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28987 t
= cp_parser_expression (parser
);
28989 if (t
== error_mark_node
28990 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28991 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28992 /*or_comma=*/false,
28993 /*consume_paren=*/true);
28995 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
28996 "device", location
);
28998 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
28999 OMP_CLAUSE_DEVICE_ID (c
) = t
;
29000 OMP_CLAUSE_CHAIN (c
) = list
;
29006 dist_schedule ( static )
29007 dist_schedule ( static , expression ) */
29010 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
29011 location_t location
)
29015 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29018 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
29020 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
29022 cp_lexer_consume_token (parser
->lexer
);
29024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29026 cp_lexer_consume_token (parser
->lexer
);
29028 t
= cp_parser_assignment_expression (parser
);
29030 if (t
== error_mark_node
)
29032 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
29034 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29037 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
29040 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
29042 OMP_CLAUSE_CHAIN (c
) = list
;
29046 cp_parser_error (parser
, "invalid dist_schedule kind");
29048 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29049 /*or_comma=*/false,
29050 /*consume_paren=*/true);
29055 proc_bind ( proc-bind-kind )
29058 master | close | spread */
29061 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
29062 location_t location
)
29065 enum omp_clause_proc_bind_kind kind
;
29067 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29072 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29073 const char *p
= IDENTIFIER_POINTER (id
);
29075 if (strcmp ("master", p
) == 0)
29076 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
29077 else if (strcmp ("close", p
) == 0)
29078 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
29079 else if (strcmp ("spread", p
) == 0)
29080 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
29087 cp_lexer_consume_token (parser
->lexer
);
29088 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
29091 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
29092 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
29094 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
29095 OMP_CLAUSE_CHAIN (c
) = list
;
29099 cp_parser_error (parser
, "invalid depend kind");
29101 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29102 /*or_comma=*/false,
29103 /*consume_paren=*/true);
29108 async [( int-expr )] */
29111 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
29114 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29116 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
29118 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
29120 cp_lexer_consume_token (parser
->lexer
);
29122 t
= cp_parser_expression (parser
);
29123 if (t
== error_mark_node
29124 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29125 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29126 /*or_comma=*/false,
29127 /*consume_paren=*/true);
29130 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
29132 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
29133 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
29134 OMP_CLAUSE_CHAIN (c
) = list
;
29140 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29141 is a bitmask in MASK. Return the list of clauses found. */
29144 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
29145 const char *where
, cp_token
*pragma_tok
,
29146 bool finish_p
= true)
29148 tree clauses
= NULL
;
29151 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29154 pragma_omp_clause c_kind
;
29155 const char *c_name
;
29156 tree prev
= clauses
;
29158 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29159 cp_lexer_consume_token (parser
->lexer
);
29161 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
29162 c_kind
= cp_parser_omp_clause_name (parser
);
29166 case PRAGMA_OACC_CLAUSE_ASYNC
:
29167 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
29170 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
29171 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
29172 c_name
= "collapse";
29174 case PRAGMA_OACC_CLAUSE_COPY
:
29175 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29178 case PRAGMA_OACC_CLAUSE_COPYIN
:
29179 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29182 case PRAGMA_OACC_CLAUSE_COPYOUT
:
29183 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29184 c_name
= "copyout";
29186 case PRAGMA_OACC_CLAUSE_CREATE
:
29187 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29190 case PRAGMA_OACC_CLAUSE_DELETE
:
29191 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29194 case PRAGMA_OACC_CLAUSE_DEVICE
:
29195 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29198 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
29199 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
29200 c_name
= "deviceptr";
29202 case PRAGMA_OACC_CLAUSE_HOST
:
29203 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29206 case PRAGMA_OACC_CLAUSE_IF
:
29207 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
);
29210 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
29211 clauses
= cp_parser_omp_clause_num_gangs (parser
, clauses
);
29212 c_name
= "num_gangs";
29214 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
29215 clauses
= cp_parser_omp_clause_num_workers (parser
, clauses
);
29216 c_name
= "num_workers";
29218 case PRAGMA_OACC_CLAUSE_PRESENT
:
29219 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29220 c_name
= "present";
29222 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
29223 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29224 c_name
= "present_or_copy";
29226 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
29227 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29228 c_name
= "present_or_copyin";
29230 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
29231 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29232 c_name
= "present_or_copyout";
29234 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
29235 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29236 c_name
= "present_or_create";
29238 case PRAGMA_OACC_CLAUSE_REDUCTION
:
29239 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
29240 c_name
= "reduction";
29242 case PRAGMA_OACC_CLAUSE_SELF
:
29243 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29246 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
29247 clauses
= cp_parser_oacc_clause_vector_length (parser
, clauses
);
29248 c_name
= "vector_length";
29250 case PRAGMA_OACC_CLAUSE_WAIT
:
29251 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
29255 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
29261 if (((mask
>> c_kind
) & 1) == 0)
29263 /* Remove the invalid clause(s) from the list to avoid
29264 confusing the rest of the compiler. */
29266 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
29271 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29274 return finish_omp_clauses (clauses
);
29279 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29280 is a bitmask in MASK. Return the list of clauses found; the result
29281 of clause default goes in *pdefault. */
29284 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
29285 const char *where
, cp_token
*pragma_tok
,
29286 bool finish_p
= true)
29288 tree clauses
= NULL
;
29290 cp_token
*token
= NULL
;
29291 bool cilk_simd_fn
= false;
29293 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29295 pragma_omp_clause c_kind
;
29296 const char *c_name
;
29297 tree prev
= clauses
;
29299 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29300 cp_lexer_consume_token (parser
->lexer
);
29302 token
= cp_lexer_peek_token (parser
->lexer
);
29303 c_kind
= cp_parser_omp_clause_name (parser
);
29307 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
29308 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
29310 c_name
= "collapse";
29312 case PRAGMA_OMP_CLAUSE_COPYIN
:
29313 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
29316 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
29317 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
29319 c_name
= "copyprivate";
29321 case PRAGMA_OMP_CLAUSE_DEFAULT
:
29322 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
29324 c_name
= "default";
29326 case PRAGMA_OMP_CLAUSE_FINAL
:
29327 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
29330 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
29331 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
29333 c_name
= "firstprivate";
29335 case PRAGMA_OMP_CLAUSE_IF
:
29336 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
29339 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
29340 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
29342 c_name
= "lastprivate";
29344 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
29345 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
29347 c_name
= "mergeable";
29349 case PRAGMA_OMP_CLAUSE_NOWAIT
:
29350 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
29353 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
29354 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
29356 c_name
= "num_threads";
29358 case PRAGMA_OMP_CLAUSE_ORDERED
:
29359 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
29361 c_name
= "ordered";
29363 case PRAGMA_OMP_CLAUSE_PRIVATE
:
29364 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
29366 c_name
= "private";
29368 case PRAGMA_OMP_CLAUSE_REDUCTION
:
29369 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
29370 c_name
= "reduction";
29372 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
29373 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
29375 c_name
= "schedule";
29377 case PRAGMA_OMP_CLAUSE_SHARED
:
29378 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
29382 case PRAGMA_OMP_CLAUSE_UNTIED
:
29383 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
29387 case PRAGMA_OMP_CLAUSE_INBRANCH
:
29388 case PRAGMA_CILK_CLAUSE_MASK
:
29389 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
29390 clauses
, token
->location
);
29391 c_name
= "inbranch";
29393 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
29394 case PRAGMA_CILK_CLAUSE_NOMASK
:
29395 clauses
= cp_parser_omp_clause_branch (parser
,
29396 OMP_CLAUSE_NOTINBRANCH
,
29397 clauses
, token
->location
);
29398 c_name
= "notinbranch";
29400 case PRAGMA_OMP_CLAUSE_PARALLEL
:
29401 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
29402 clauses
, token
->location
);
29403 c_name
= "parallel";
29407 error_at (token
->location
, "%qs must be the first clause of %qs",
29412 case PRAGMA_OMP_CLAUSE_FOR
:
29413 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
29414 clauses
, token
->location
);
29417 goto clause_not_first
;
29419 case PRAGMA_OMP_CLAUSE_SECTIONS
:
29420 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
29421 clauses
, token
->location
);
29422 c_name
= "sections";
29424 goto clause_not_first
;
29426 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
29427 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
29428 clauses
, token
->location
);
29429 c_name
= "taskgroup";
29431 goto clause_not_first
;
29433 case PRAGMA_OMP_CLAUSE_TO
:
29434 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
29438 case PRAGMA_OMP_CLAUSE_FROM
:
29439 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
29443 case PRAGMA_OMP_CLAUSE_UNIFORM
:
29444 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
29446 c_name
= "uniform";
29448 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
29449 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
29451 c_name
= "num_teams";
29453 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
29454 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
29456 c_name
= "thread_limit";
29458 case PRAGMA_OMP_CLAUSE_ALIGNED
:
29459 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
29460 c_name
= "aligned";
29462 case PRAGMA_OMP_CLAUSE_LINEAR
:
29463 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
29464 cilk_simd_fn
= true;
29465 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, cilk_simd_fn
);
29468 case PRAGMA_OMP_CLAUSE_DEPEND
:
29469 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
29472 case PRAGMA_OMP_CLAUSE_MAP
:
29473 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
29476 case PRAGMA_OMP_CLAUSE_DEVICE
:
29477 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
29481 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
29482 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
29484 c_name
= "dist_schedule";
29486 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
29487 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
29489 c_name
= "proc_bind";
29491 case PRAGMA_OMP_CLAUSE_SAFELEN
:
29492 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
29494 c_name
= "safelen";
29496 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
29497 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
29499 c_name
= "simdlen";
29501 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
29502 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
29503 c_name
= "simdlen";
29506 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
29512 if (((mask
>> c_kind
) & 1) == 0)
29514 /* Remove the invalid clause(s) from the list to avoid
29515 confusing the rest of the compiler. */
29517 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
29521 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29522 no reason to skip to the end. */
29523 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
29524 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29526 return finish_omp_clauses (clauses
);
29534 In practice, we're also interested in adding the statement to an
29535 outer node. So it is convenient if we work around the fact that
29536 cp_parser_statement calls add_stmt. */
29539 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
29541 unsigned save
= parser
->in_statement
;
29543 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29544 This preserves the "not within loop or switch" style error messages
29545 for nonsense cases like
29551 if (parser
->in_statement
)
29552 parser
->in_statement
= IN_OMP_BLOCK
;
29558 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
29560 parser
->in_statement
= save
;
29564 cp_parser_omp_structured_block (cp_parser
*parser
)
29566 tree stmt
= begin_omp_structured_block ();
29567 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
29569 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29571 cp_parser_end_omp_structured_block (parser
, save
);
29572 return finish_omp_structured_block (stmt
);
29576 # pragma omp atomic new-line
29580 x binop= expr | x++ | ++x | x-- | --x
29582 +, *, -, /, &, ^, |, <<, >>
29584 where x is an lvalue expression with scalar type.
29587 # pragma omp atomic new-line
29590 # pragma omp atomic read new-line
29593 # pragma omp atomic write new-line
29596 # pragma omp atomic update new-line
29599 # pragma omp atomic capture new-line
29602 # pragma omp atomic capture new-line
29610 expression-stmt | x = x binop expr
29612 v = expression-stmt
29614 { v = x; update-stmt; } | { update-stmt; v = x; }
29618 expression-stmt | x = x binop expr | x = expr binop x
29622 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29624 where x and v are lvalue expressions with scalar type. */
29627 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
29629 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
29630 tree rhs1
= NULL_TREE
, orig_lhs
;
29631 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
29632 bool structured_block
= false;
29633 bool seq_cst
= false;
29635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29637 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29638 const char *p
= IDENTIFIER_POINTER (id
);
29640 if (!strcmp (p
, "seq_cst"))
29643 cp_lexer_consume_token (parser
->lexer
);
29644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29645 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29646 cp_lexer_consume_token (parser
->lexer
);
29649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29651 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29652 const char *p
= IDENTIFIER_POINTER (id
);
29654 if (!strcmp (p
, "read"))
29655 code
= OMP_ATOMIC_READ
;
29656 else if (!strcmp (p
, "write"))
29658 else if (!strcmp (p
, "update"))
29660 else if (!strcmp (p
, "capture"))
29661 code
= OMP_ATOMIC_CAPTURE_NEW
;
29665 cp_lexer_consume_token (parser
->lexer
);
29669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29670 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29671 cp_lexer_consume_token (parser
->lexer
);
29673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29675 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29676 const char *p
= IDENTIFIER_POINTER (id
);
29678 if (!strcmp (p
, "seq_cst"))
29681 cp_lexer_consume_token (parser
->lexer
);
29685 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29689 case OMP_ATOMIC_READ
:
29690 case NOP_EXPR
: /* atomic write */
29691 v
= cp_parser_unary_expression (parser
);
29692 if (v
== error_mark_node
)
29694 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29696 if (code
== NOP_EXPR
)
29697 lhs
= cp_parser_expression (parser
);
29699 lhs
= cp_parser_unary_expression (parser
);
29700 if (lhs
== error_mark_node
)
29702 if (code
== NOP_EXPR
)
29704 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29712 case OMP_ATOMIC_CAPTURE_NEW
:
29713 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29715 cp_lexer_consume_token (parser
->lexer
);
29716 structured_block
= true;
29720 v
= cp_parser_unary_expression (parser
);
29721 if (v
== error_mark_node
)
29723 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29731 lhs
= cp_parser_unary_expression (parser
);
29733 switch (TREE_CODE (lhs
))
29738 case POSTINCREMENT_EXPR
:
29739 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29740 code
= OMP_ATOMIC_CAPTURE_OLD
;
29742 case PREINCREMENT_EXPR
:
29743 lhs
= TREE_OPERAND (lhs
, 0);
29744 opcode
= PLUS_EXPR
;
29745 rhs
= integer_one_node
;
29748 case POSTDECREMENT_EXPR
:
29749 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29750 code
= OMP_ATOMIC_CAPTURE_OLD
;
29752 case PREDECREMENT_EXPR
:
29753 lhs
= TREE_OPERAND (lhs
, 0);
29754 opcode
= MINUS_EXPR
;
29755 rhs
= integer_one_node
;
29758 case COMPOUND_EXPR
:
29759 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
29760 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
29761 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
29762 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
29763 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29764 (TREE_OPERAND (lhs
, 1), 0), 0)))
29766 /* Undo effects of boolean_increment for post {in,de}crement. */
29767 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
29770 if (TREE_CODE (lhs
) == MODIFY_EXPR
29771 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
29773 /* Undo effects of boolean_increment. */
29774 if (integer_onep (TREE_OPERAND (lhs
, 1)))
29776 /* This is pre or post increment. */
29777 rhs
= TREE_OPERAND (lhs
, 1);
29778 lhs
= TREE_OPERAND (lhs
, 0);
29780 if (code
== OMP_ATOMIC_CAPTURE_NEW
29781 && !structured_block
29782 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
29783 code
= OMP_ATOMIC_CAPTURE_OLD
;
29789 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29792 opcode
= MULT_EXPR
;
29795 opcode
= TRUNC_DIV_EXPR
;
29798 opcode
= PLUS_EXPR
;
29801 opcode
= MINUS_EXPR
;
29803 case CPP_LSHIFT_EQ
:
29804 opcode
= LSHIFT_EXPR
;
29806 case CPP_RSHIFT_EQ
:
29807 opcode
= RSHIFT_EXPR
;
29810 opcode
= BIT_AND_EXPR
;
29813 opcode
= BIT_IOR_EXPR
;
29816 opcode
= BIT_XOR_EXPR
;
29819 enum cp_parser_prec oprec
;
29821 cp_lexer_consume_token (parser
->lexer
);
29822 cp_parser_parse_tentatively (parser
);
29823 rhs1
= cp_parser_simple_cast_expression (parser
);
29824 if (rhs1
== error_mark_node
)
29826 cp_parser_abort_tentative_parse (parser
);
29827 cp_parser_simple_cast_expression (parser
);
29830 token
= cp_lexer_peek_token (parser
->lexer
);
29831 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
29833 cp_parser_abort_tentative_parse (parser
);
29834 cp_parser_parse_tentatively (parser
);
29835 rhs
= cp_parser_binary_expression (parser
, false, true,
29836 PREC_NOT_OPERATOR
, NULL
);
29837 if (rhs
== error_mark_node
)
29839 cp_parser_abort_tentative_parse (parser
);
29840 cp_parser_binary_expression (parser
, false, true,
29841 PREC_NOT_OPERATOR
, NULL
);
29844 switch (TREE_CODE (rhs
))
29847 case TRUNC_DIV_EXPR
:
29856 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
29858 if (cp_parser_parse_definitely (parser
))
29860 opcode
= TREE_CODE (rhs
);
29861 rhs1
= TREE_OPERAND (rhs
, 0);
29862 rhs
= TREE_OPERAND (rhs
, 1);
29872 cp_parser_abort_tentative_parse (parser
);
29873 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
29875 rhs
= cp_parser_expression (parser
);
29876 if (rhs
== error_mark_node
)
29882 cp_parser_error (parser
,
29883 "invalid form of %<#pragma omp atomic%>");
29886 if (!cp_parser_parse_definitely (parser
))
29888 switch (token
->type
)
29890 case CPP_SEMICOLON
:
29891 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29893 code
= OMP_ATOMIC_CAPTURE_OLD
;
29898 cp_lexer_consume_token (parser
->lexer
);
29901 else if (structured_block
)
29908 cp_parser_error (parser
,
29909 "invalid form of %<#pragma omp atomic%>");
29912 opcode
= MULT_EXPR
;
29915 opcode
= TRUNC_DIV_EXPR
;
29918 opcode
= PLUS_EXPR
;
29921 opcode
= MINUS_EXPR
;
29924 opcode
= LSHIFT_EXPR
;
29927 opcode
= RSHIFT_EXPR
;
29930 opcode
= BIT_AND_EXPR
;
29933 opcode
= BIT_IOR_EXPR
;
29936 opcode
= BIT_XOR_EXPR
;
29939 cp_parser_error (parser
,
29940 "invalid operator for %<#pragma omp atomic%>");
29943 oprec
= TOKEN_PRECEDENCE (token
);
29944 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
29945 if (commutative_tree_code (opcode
))
29946 oprec
= (enum cp_parser_prec
) (oprec
- 1);
29947 cp_lexer_consume_token (parser
->lexer
);
29948 rhs
= cp_parser_binary_expression (parser
, false, false,
29950 if (rhs
== error_mark_node
)
29955 cp_parser_error (parser
,
29956 "invalid operator for %<#pragma omp atomic%>");
29959 cp_lexer_consume_token (parser
->lexer
);
29961 rhs
= cp_parser_expression (parser
);
29962 if (rhs
== error_mark_node
)
29967 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29969 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
29971 v
= cp_parser_unary_expression (parser
);
29972 if (v
== error_mark_node
)
29974 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29976 lhs1
= cp_parser_unary_expression (parser
);
29977 if (lhs1
== error_mark_node
)
29980 if (structured_block
)
29982 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29983 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29986 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
29987 if (!structured_block
)
29988 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29992 cp_parser_skip_to_end_of_block_or_statement (parser
);
29993 if (structured_block
)
29995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29996 cp_lexer_consume_token (parser
->lexer
);
29997 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
29999 cp_parser_skip_to_end_of_block_or_statement (parser
);
30000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30001 cp_lexer_consume_token (parser
->lexer
);
30008 # pragma omp barrier new-line */
30011 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
30013 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30014 finish_omp_barrier ();
30018 # pragma omp critical [(name)] new-line
30019 structured-block */
30022 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
30024 tree stmt
, name
= NULL
;
30026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30028 cp_lexer_consume_token (parser
->lexer
);
30030 name
= cp_parser_identifier (parser
);
30032 if (name
== error_mark_node
30033 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30034 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30035 /*or_comma=*/false,
30036 /*consume_paren=*/true);
30037 if (name
== error_mark_node
)
30040 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30042 stmt
= cp_parser_omp_structured_block (parser
);
30043 return c_finish_omp_critical (input_location
, stmt
, name
);
30047 # pragma omp flush flush-vars[opt] new-line
30050 ( variable-list ) */
30053 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
30055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30056 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
30057 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30059 finish_omp_flush ();
30062 /* Helper function, to parse omp for increment expression. */
30065 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
30067 tree cond
= cp_parser_binary_expression (parser
, false, true,
30068 PREC_NOT_OPERATOR
, NULL
);
30069 if (cond
== error_mark_node
30070 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30072 cp_parser_skip_to_end_of_statement (parser
);
30073 return error_mark_node
;
30076 switch (TREE_CODE (cond
))
30084 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
30086 /* Fall through: OpenMP disallows NE_EXPR. */
30088 return error_mark_node
;
30091 /* If decl is an iterator, preserve LHS and RHS of the relational
30092 expr until finish_omp_for. */
30094 && (type_dependent_expression_p (decl
)
30095 || CLASS_TYPE_P (TREE_TYPE (decl
))))
30098 return build_x_binary_op (input_location
, TREE_CODE (cond
),
30099 TREE_OPERAND (cond
, 0), ERROR_MARK
,
30100 TREE_OPERAND (cond
, 1), ERROR_MARK
,
30101 /*overload=*/NULL
, tf_warning_or_error
);
30104 /* Helper function, to parse omp for increment expression. */
30107 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
30109 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30115 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
30117 op
= (token
->type
== CPP_PLUS_PLUS
30118 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
30119 cp_lexer_consume_token (parser
->lexer
);
30120 lhs
= cp_parser_simple_cast_expression (parser
);
30122 return error_mark_node
;
30123 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
30126 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
30128 return error_mark_node
;
30130 token
= cp_lexer_peek_token (parser
->lexer
);
30131 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
30133 op
= (token
->type
== CPP_PLUS_PLUS
30134 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
30135 cp_lexer_consume_token (parser
->lexer
);
30136 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
30139 op
= cp_parser_assignment_operator_opt (parser
);
30140 if (op
== ERROR_MARK
)
30141 return error_mark_node
;
30143 if (op
!= NOP_EXPR
)
30145 rhs
= cp_parser_assignment_expression (parser
);
30146 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
30147 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
30150 lhs
= cp_parser_binary_expression (parser
, false, false,
30151 PREC_ADDITIVE_EXPRESSION
, NULL
);
30152 token
= cp_lexer_peek_token (parser
->lexer
);
30153 decl_first
= lhs
== decl
;
30156 if (token
->type
!= CPP_PLUS
30157 && token
->type
!= CPP_MINUS
)
30158 return error_mark_node
;
30162 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
30163 cp_lexer_consume_token (parser
->lexer
);
30164 rhs
= cp_parser_binary_expression (parser
, false, false,
30165 PREC_ADDITIVE_EXPRESSION
, NULL
);
30166 token
= cp_lexer_peek_token (parser
->lexer
);
30167 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
30169 if (lhs
== NULL_TREE
)
30171 if (op
== PLUS_EXPR
)
30174 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
30175 tf_warning_or_error
);
30178 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
30179 ERROR_MARK
, NULL
, tf_warning_or_error
);
30182 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
30186 if (rhs
!= decl
|| op
== MINUS_EXPR
)
30187 return error_mark_node
;
30188 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
30191 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
30193 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
30196 /* Parse the initialization statement of either an OpenMP for loop or
30197 a Cilk Plus for loop.
30199 Return true if the resulting construct should have an
30200 OMP_CLAUSE_PRIVATE added to it. */
30203 cp_parser_omp_for_loop_init (cp_parser
*parser
,
30204 enum tree_code code
,
30205 tree
&this_pre_body
,
30206 vec
<tree
, va_gc
> *for_block
,
30211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30214 bool add_private_clause
= false;
30216 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30220 integer-type var = lb
30221 random-access-iterator-type var = lb
30222 pointer-type var = lb
30224 cp_decl_specifier_seq type_specifiers
;
30226 /* First, try to parse as an initialized declaration. See
30227 cp_parser_condition, from whence the bulk of this is copied. */
30229 cp_parser_parse_tentatively (parser
);
30230 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
30231 /*is_trailing_return=*/false,
30233 if (cp_parser_parse_definitely (parser
))
30235 /* If parsing a type specifier seq succeeded, then this
30236 MUST be a initialized declaration. */
30237 tree asm_specification
, attributes
;
30238 cp_declarator
*declarator
;
30240 declarator
= cp_parser_declarator (parser
,
30241 CP_PARSER_DECLARATOR_NAMED
,
30242 /*ctor_dtor_or_conv_p=*/NULL
,
30243 /*parenthesized_p=*/NULL
,
30244 /*member_p=*/false,
30245 /*friend_p=*/false);
30246 attributes
= cp_parser_attributes_opt (parser
);
30247 asm_specification
= cp_parser_asm_specification_opt (parser
);
30249 if (declarator
== cp_error_declarator
)
30250 cp_parser_skip_to_end_of_statement (parser
);
30254 tree pushed_scope
, auto_node
;
30256 decl
= start_decl (declarator
, &type_specifiers
,
30257 SD_INITIALIZED
, attributes
,
30258 /*prefix_attributes=*/NULL_TREE
,
30261 auto_node
= type_uses_auto (TREE_TYPE (decl
));
30262 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30264 if (cp_lexer_next_token_is (parser
->lexer
,
30267 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
30268 error ("parenthesized initialization is not allowed in "
30269 "OpenMP %<for%> loop");
30271 error ("parenthesized initialization is "
30272 "not allowed in for-loop");
30275 /* Trigger an error. */
30276 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
30278 init
= error_mark_node
;
30279 cp_parser_skip_to_end_of_statement (parser
);
30281 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
30282 || type_dependent_expression_p (decl
)
30285 bool is_direct_init
, is_non_constant_init
;
30287 init
= cp_parser_initializer (parser
,
30289 &is_non_constant_init
);
30294 = do_auto_deduction (TREE_TYPE (decl
), init
,
30297 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
30298 && !type_dependent_expression_p (decl
))
30302 cp_finish_decl (decl
, init
, !is_non_constant_init
,
30304 LOOKUP_ONLYCONVERTING
);
30305 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
30307 vec_safe_push (for_block
, this_pre_body
);
30311 init
= pop_stmt_list (this_pre_body
);
30312 this_pre_body
= NULL_TREE
;
30317 cp_lexer_consume_token (parser
->lexer
);
30318 init
= cp_parser_assignment_expression (parser
);
30321 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
30322 init
= error_mark_node
;
30324 cp_finish_decl (decl
, NULL_TREE
,
30325 /*init_const_expr_p=*/false,
30327 LOOKUP_ONLYCONVERTING
);
30331 pop_scope (pushed_scope
);
30337 /* If parsing a type specifier sequence failed, then
30338 this MUST be a simple expression. */
30339 if (code
== CILK_FOR
)
30340 error ("%<_Cilk_for%> allows expression instead of declaration only "
30341 "in C, not in C++");
30342 cp_parser_parse_tentatively (parser
);
30343 decl
= cp_parser_primary_expression (parser
, false, false,
30345 if (!cp_parser_error_occurred (parser
)
30348 && CLASS_TYPE_P (TREE_TYPE (decl
)))
30352 cp_parser_parse_definitely (parser
);
30353 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
30354 rhs
= cp_parser_assignment_expression (parser
);
30355 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
30358 tf_warning_or_error
));
30359 add_private_clause
= true;
30364 cp_parser_abort_tentative_parse (parser
);
30365 init
= cp_parser_expression (parser
);
30368 if (TREE_CODE (init
) == MODIFY_EXPR
30369 || TREE_CODE (init
) == MODOP_EXPR
)
30370 real_decl
= TREE_OPERAND (init
, 0);
30374 return add_private_clause
;
30377 /* Parse the restricted form of the for statement allowed by OpenMP. */
30380 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
30383 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
30384 tree real_decl
, initv
, condv
, incrv
, declv
;
30385 tree this_pre_body
, cl
;
30386 location_t loc_first
;
30387 bool collapse_err
= false;
30388 int i
, collapse
= 1, nbraces
= 0;
30389 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
30391 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
30392 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
30393 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
30395 gcc_assert (collapse
>= 1);
30397 declv
= make_tree_vec (collapse
);
30398 initv
= make_tree_vec (collapse
);
30399 condv
= make_tree_vec (collapse
);
30400 incrv
= make_tree_vec (collapse
);
30402 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
30404 for (i
= 0; i
< collapse
; i
++)
30406 int bracecount
= 0;
30407 bool add_private_clause
= false;
30410 if (code
!= CILK_FOR
30411 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30413 cp_parser_error (parser
, "for statement expected");
30416 if (code
== CILK_FOR
30417 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
30419 cp_parser_error (parser
, "_Cilk_for statement expected");
30422 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
30424 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30427 init
= decl
= real_decl
= NULL
;
30428 this_pre_body
= push_stmt_list ();
30431 |= cp_parser_omp_for_loop_init (parser
, code
,
30432 this_pre_body
, for_block
,
30433 init
, decl
, real_decl
);
30435 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30438 this_pre_body
= pop_stmt_list (this_pre_body
);
30442 pre_body
= push_stmt_list ();
30444 add_stmt (this_pre_body
);
30445 pre_body
= pop_stmt_list (pre_body
);
30448 pre_body
= this_pre_body
;
30453 if (cclauses
!= NULL
30454 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
30455 && real_decl
!= NULL_TREE
)
30458 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
30459 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
30460 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30462 error_at (loc
, "iteration variable %qD"
30463 " should not be firstprivate", real_decl
);
30464 *c
= OMP_CLAUSE_CHAIN (*c
);
30466 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
30467 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30469 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30470 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30471 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
30472 OMP_CLAUSE_DECL (l
) = real_decl
;
30473 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
30474 if (code
== OMP_SIMD
)
30476 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30477 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
30481 OMP_CLAUSE_CHAIN (l
) = clauses
;
30484 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
30485 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
30486 add_private_clause
= false;
30490 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
30491 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30492 add_private_clause
= false;
30493 c
= &OMP_CLAUSE_CHAIN (*c
);
30497 if (add_private_clause
)
30500 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30502 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
30503 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
30504 && OMP_CLAUSE_DECL (c
) == decl
)
30506 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
30507 && OMP_CLAUSE_DECL (c
) == decl
)
30508 error_at (loc
, "iteration variable %qD "
30509 "should not be firstprivate",
30511 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
30512 && OMP_CLAUSE_DECL (c
) == decl
)
30513 error_at (loc
, "iteration variable %qD should not be reduction",
30518 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
30519 OMP_CLAUSE_DECL (c
) = decl
;
30520 c
= finish_omp_clauses (c
);
30523 OMP_CLAUSE_CHAIN (c
) = clauses
;
30530 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30531 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
30532 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30535 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30537 /* If decl is an iterator, preserve the operator on decl
30538 until finish_omp_for. */
30540 && ((processing_template_decl
30541 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
30542 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
30543 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
30545 incr
= cp_parser_expression (parser
);
30546 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
30547 SET_EXPR_LOCATION (incr
, input_location
);
30550 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30551 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30552 /*or_comma=*/false,
30553 /*consume_paren=*/true);
30555 TREE_VEC_ELT (declv
, i
) = decl
;
30556 TREE_VEC_ELT (initv
, i
) = init
;
30557 TREE_VEC_ELT (condv
, i
) = cond
;
30558 TREE_VEC_ELT (incrv
, i
) = incr
;
30560 if (i
== collapse
- 1)
30563 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30564 in between the collapsed for loops to be still considered perfectly
30565 nested. Hopefully the final version clarifies this.
30566 For now handle (multiple) {'s and empty statements. */
30567 cp_parser_parse_tentatively (parser
);
30570 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30572 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30574 cp_lexer_consume_token (parser
->lexer
);
30577 else if (bracecount
30578 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30579 cp_lexer_consume_token (parser
->lexer
);
30582 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30583 error_at (loc
, "not enough collapsed for loops");
30584 collapse_err
= true;
30585 cp_parser_abort_tentative_parse (parser
);
30594 cp_parser_parse_definitely (parser
);
30595 nbraces
+= bracecount
;
30599 /* Note that we saved the original contents of this flag when we entered
30600 the structured block, and so we don't need to re-save it here. */
30601 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
30602 parser
->in_statement
= IN_CILK_SIMD_FOR
;
30604 parser
->in_statement
= IN_OMP_FOR
;
30606 /* Note that the grammar doesn't call for a structured block here,
30607 though the loop as a whole is a structured block. */
30608 body
= push_stmt_list ();
30609 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30610 body
= pop_stmt_list (body
);
30612 if (declv
== NULL_TREE
)
30615 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
30616 pre_body
, clauses
);
30620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30622 cp_lexer_consume_token (parser
->lexer
);
30625 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30626 cp_lexer_consume_token (parser
->lexer
);
30631 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30632 "collapsed loops not perfectly nested");
30634 collapse_err
= true;
30635 cp_parser_statement_seq_opt (parser
, NULL
);
30636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
30641 while (!for_block
->is_empty ())
30642 add_stmt (pop_stmt_list (for_block
->pop ()));
30643 release_tree_vector (for_block
);
30648 /* Helper function for OpenMP parsing, split clauses and call
30649 finish_omp_clauses on each of the set of clauses afterwards. */
30652 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
30653 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
30656 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
30657 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
30659 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
30663 #pragma omp simd simd-clause[optseq] new-line
30666 #define OMP_SIMD_CLAUSE_MASK \
30667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30676 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30677 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30679 tree clauses
, sb
, ret
;
30681 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30683 strcat (p_name
, " simd");
30684 mask
|= OMP_SIMD_CLAUSE_MASK
;
30685 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
30687 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30691 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
30692 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
30695 sb
= begin_omp_structured_block ();
30696 save
= cp_parser_begin_omp_structured_block (parser
);
30698 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
30700 cp_parser_end_omp_structured_block (parser
, save
);
30701 add_stmt (finish_omp_structured_block (sb
));
30707 #pragma omp for for-clause[optseq] new-line
30711 #pragma omp for simd for-simd-clause[optseq] new-line
30714 #define OMP_FOR_CLAUSE_MASK \
30715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30725 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
30726 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30728 tree clauses
, sb
, ret
;
30730 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30732 strcat (p_name
, " for");
30733 mask
|= OMP_FOR_CLAUSE_MASK
;
30735 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30739 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30740 const char *p
= IDENTIFIER_POINTER (id
);
30742 if (strcmp (p
, "simd") == 0)
30744 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30745 if (cclauses
== NULL
)
30746 cclauses
= cclauses_buf
;
30748 cp_lexer_consume_token (parser
->lexer
);
30749 if (!flag_openmp
) /* flag_openmp_simd */
30750 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30752 sb
= begin_omp_structured_block ();
30753 save
= cp_parser_begin_omp_structured_block (parser
);
30754 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30756 cp_parser_end_omp_structured_block (parser
, save
);
30757 tree body
= finish_omp_structured_block (sb
);
30760 ret
= make_node (OMP_FOR
);
30761 TREE_TYPE (ret
) = void_type_node
;
30762 OMP_FOR_BODY (ret
) = body
;
30763 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30764 SET_EXPR_LOCATION (ret
, loc
);
30769 if (!flag_openmp
) /* flag_openmp_simd */
30771 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30775 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30779 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
30780 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30783 sb
= begin_omp_structured_block ();
30784 save
= cp_parser_begin_omp_structured_block (parser
);
30786 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
30788 cp_parser_end_omp_structured_block (parser
, save
);
30789 add_stmt (finish_omp_structured_block (sb
));
30795 # pragma omp master new-line
30796 structured-block */
30799 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
30801 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30802 return c_finish_omp_master (input_location
,
30803 cp_parser_omp_structured_block (parser
));
30807 # pragma omp ordered new-line
30808 structured-block */
30811 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
30813 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30814 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30815 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
30821 { section-sequence }
30824 section-directive[opt] structured-block
30825 section-sequence section-directive structured-block */
30828 cp_parser_omp_sections_scope (cp_parser
*parser
)
30830 tree stmt
, substmt
;
30831 bool error_suppress
= false;
30834 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
30837 stmt
= push_stmt_list ();
30839 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
30841 substmt
= cp_parser_omp_structured_block (parser
);
30842 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30843 add_stmt (substmt
);
30848 tok
= cp_lexer_peek_token (parser
->lexer
);
30849 if (tok
->type
== CPP_CLOSE_BRACE
)
30851 if (tok
->type
== CPP_EOF
)
30854 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
30856 cp_lexer_consume_token (parser
->lexer
);
30857 cp_parser_require_pragma_eol (parser
, tok
);
30858 error_suppress
= false;
30860 else if (!error_suppress
)
30862 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
30863 error_suppress
= true;
30866 substmt
= cp_parser_omp_structured_block (parser
);
30867 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30868 add_stmt (substmt
);
30870 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
30872 substmt
= pop_stmt_list (stmt
);
30874 stmt
= make_node (OMP_SECTIONS
);
30875 TREE_TYPE (stmt
) = void_type_node
;
30876 OMP_SECTIONS_BODY (stmt
) = substmt
;
30883 # pragma omp sections sections-clause[optseq] newline
30886 #define OMP_SECTIONS_CLAUSE_MASK \
30887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30894 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
30895 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30898 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30900 strcat (p_name
, " sections");
30901 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
30903 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30905 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30909 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
30910 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
30913 ret
= cp_parser_omp_sections_scope (parser
);
30915 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
30921 # pragma omp parallel parallel-clause[optseq] new-line
30923 # pragma omp parallel for parallel-for-clause[optseq] new-line
30925 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30929 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30930 structured-block */
30932 #define OMP_PARALLEL_CLAUSE_MASK \
30933 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30944 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
30945 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30947 tree stmt
, clauses
, block
;
30949 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30951 strcat (p_name
, " parallel");
30952 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
30954 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30956 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30957 if (cclauses
== NULL
)
30958 cclauses
= cclauses_buf
;
30960 cp_lexer_consume_token (parser
->lexer
);
30961 if (!flag_openmp
) /* flag_openmp_simd */
30962 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30963 block
= begin_omp_parallel ();
30964 save
= cp_parser_begin_omp_structured_block (parser
);
30965 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30966 cp_parser_end_omp_structured_block (parser
, save
);
30967 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30969 if (ret
== NULL_TREE
)
30971 OMP_PARALLEL_COMBINED (stmt
) = 1;
30976 error_at (loc
, "expected %<for%> after %qs", p_name
);
30977 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30980 else if (!flag_openmp
) /* flag_openmp_simd */
30982 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30985 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30987 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30988 const char *p
= IDENTIFIER_POINTER (id
);
30989 if (strcmp (p
, "sections") == 0)
30991 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30992 cclauses
= cclauses_buf
;
30994 cp_lexer_consume_token (parser
->lexer
);
30995 block
= begin_omp_parallel ();
30996 save
= cp_parser_begin_omp_structured_block (parser
);
30997 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30998 cp_parser_end_omp_structured_block (parser
, save
);
30999 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
31001 OMP_PARALLEL_COMBINED (stmt
) = 1;
31006 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
31008 block
= begin_omp_parallel ();
31009 save
= cp_parser_begin_omp_structured_block (parser
);
31010 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31011 cp_parser_end_omp_structured_block (parser
, save
);
31012 stmt
= finish_omp_parallel (clauses
, block
);
31017 # pragma omp single single-clause[optseq] new-line
31018 structured-block */
31020 #define OMP_SINGLE_CLAUSE_MASK \
31021 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31027 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
31029 tree stmt
= make_node (OMP_SINGLE
);
31030 TREE_TYPE (stmt
) = void_type_node
;
31032 OMP_SINGLE_CLAUSES (stmt
)
31033 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
31034 "#pragma omp single", pragma_tok
);
31035 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31037 return add_stmt (stmt
);
31041 # pragma omp task task-clause[optseq] new-line
31042 structured-block */
31044 #define OMP_TASK_CLAUSE_MASK \
31045 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31056 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
31058 tree clauses
, block
;
31061 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
31062 "#pragma omp task", pragma_tok
);
31063 block
= begin_omp_task ();
31064 save
= cp_parser_begin_omp_structured_block (parser
);
31065 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31066 cp_parser_end_omp_structured_block (parser
, save
);
31067 return finish_omp_task (clauses
, block
);
31071 # pragma omp taskwait new-line */
31074 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
31076 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31077 finish_omp_taskwait ();
31081 # pragma omp taskyield new-line */
31084 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
31086 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31087 finish_omp_taskyield ();
31091 # pragma omp taskgroup new-line
31092 structured-block */
31095 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
31097 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31098 return c_finish_omp_taskgroup (input_location
,
31099 cp_parser_omp_structured_block (parser
));
31104 # pragma omp threadprivate (variable-list) */
31107 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
31111 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31112 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31114 finish_omp_threadprivate (vars
);
31118 # pragma omp cancel cancel-clause[optseq] new-line */
31120 #define OMP_CANCEL_CLAUSE_MASK \
31121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31128 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
31130 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
31131 "#pragma omp cancel", pragma_tok
);
31132 finish_omp_cancel (clauses
);
31136 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31138 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31139 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31145 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
31148 bool point_seen
= false;
31150 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31152 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31153 const char *p
= IDENTIFIER_POINTER (id
);
31155 if (strcmp (p
, "point") == 0)
31157 cp_lexer_consume_token (parser
->lexer
);
31163 cp_parser_error (parser
, "expected %<point%>");
31164 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31168 clauses
= cp_parser_omp_all_clauses (parser
,
31169 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
31170 "#pragma omp cancellation point",
31172 finish_omp_cancellation_point (clauses
);
31176 #pragma omp distribute distribute-clause[optseq] new-line
31179 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31180 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31186 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
31187 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31189 tree clauses
, sb
, ret
;
31191 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31193 strcat (p_name
, " distribute");
31194 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
31196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31198 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31199 const char *p
= IDENTIFIER_POINTER (id
);
31201 bool parallel
= false;
31203 if (strcmp (p
, "simd") == 0)
31206 parallel
= strcmp (p
, "parallel") == 0;
31207 if (parallel
|| simd
)
31209 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31210 if (cclauses
== NULL
)
31211 cclauses
= cclauses_buf
;
31212 cp_lexer_consume_token (parser
->lexer
);
31213 if (!flag_openmp
) /* flag_openmp_simd */
31216 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
31219 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
31222 sb
= begin_omp_structured_block ();
31223 save
= cp_parser_begin_omp_structured_block (parser
);
31225 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
31228 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
31230 cp_parser_end_omp_structured_block (parser
, save
);
31231 tree body
= finish_omp_structured_block (sb
);
31234 ret
= make_node (OMP_DISTRIBUTE
);
31235 TREE_TYPE (ret
) = void_type_node
;
31236 OMP_FOR_BODY (ret
) = body
;
31237 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
31238 SET_EXPR_LOCATION (ret
, loc
);
31243 if (!flag_openmp
) /* flag_openmp_simd */
31245 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31249 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31253 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
31254 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
31257 sb
= begin_omp_structured_block ();
31258 save
= cp_parser_begin_omp_structured_block (parser
);
31260 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
31262 cp_parser_end_omp_structured_block (parser
, save
);
31263 add_stmt (finish_omp_structured_block (sb
));
31269 # pragma omp teams teams-clause[optseq] new-line
31270 structured-block */
31272 #define OMP_TEAMS_CLAUSE_MASK \
31273 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31282 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
31283 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31285 tree clauses
, sb
, ret
;
31287 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31289 strcat (p_name
, " teams");
31290 mask
|= OMP_TEAMS_CLAUSE_MASK
;
31292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31294 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31295 const char *p
= IDENTIFIER_POINTER (id
);
31296 if (strcmp (p
, "distribute") == 0)
31298 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31299 if (cclauses
== NULL
)
31300 cclauses
= cclauses_buf
;
31302 cp_lexer_consume_token (parser
->lexer
);
31303 if (!flag_openmp
) /* flag_openmp_simd */
31304 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
31306 sb
= begin_omp_structured_block ();
31307 save
= cp_parser_begin_omp_structured_block (parser
);
31308 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
31310 cp_parser_end_omp_structured_block (parser
, save
);
31311 tree body
= finish_omp_structured_block (sb
);
31314 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
31315 ret
= make_node (OMP_TEAMS
);
31316 TREE_TYPE (ret
) = void_type_node
;
31317 OMP_TEAMS_CLAUSES (ret
) = clauses
;
31318 OMP_TEAMS_BODY (ret
) = body
;
31319 return add_stmt (ret
);
31322 if (!flag_openmp
) /* flag_openmp_simd */
31324 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31328 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31332 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
31333 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
31336 tree stmt
= make_node (OMP_TEAMS
);
31337 TREE_TYPE (stmt
) = void_type_node
;
31338 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
31339 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31341 return add_stmt (stmt
);
31345 # pragma omp target data target-data-clause[optseq] new-line
31346 structured-block */
31348 #define OMP_TARGET_DATA_CLAUSE_MASK \
31349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31354 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
31356 tree stmt
= make_node (OMP_TARGET_DATA
);
31357 TREE_TYPE (stmt
) = void_type_node
;
31359 OMP_TARGET_DATA_CLAUSES (stmt
)
31360 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
31361 "#pragma omp target data", pragma_tok
);
31362 keep_next_level (true);
31363 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31365 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31366 return add_stmt (stmt
);
31370 # pragma omp target update target-update-clause[optseq] new-line */
31372 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31373 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31379 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
31380 enum pragma_context context
)
31382 if (context
== pragma_stmt
)
31384 error_at (pragma_tok
->location
,
31385 "%<#pragma omp target update%> may only be "
31386 "used in compound statements");
31387 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31392 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
31393 "#pragma omp target update", pragma_tok
);
31394 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
31395 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
31397 error_at (pragma_tok
->location
,
31398 "%<#pragma omp target update must contain at least one "
31399 "%<from%> or %<to%> clauses");
31403 tree stmt
= make_node (OMP_TARGET_UPDATE
);
31404 TREE_TYPE (stmt
) = void_type_node
;
31405 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
31406 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31412 # pragma omp target target-clause[optseq] new-line
31413 structured-block */
31415 #define OMP_TARGET_CLAUSE_MASK \
31416 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31421 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
31422 enum pragma_context context
)
31424 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
31426 cp_parser_error (parser
, "expected declaration specifiers");
31427 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31433 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31434 const char *p
= IDENTIFIER_POINTER (id
);
31436 if (strcmp (p
, "teams") == 0)
31438 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
31439 char p_name
[sizeof ("#pragma omp target teams distribute "
31440 "parallel for simd")];
31442 cp_lexer_consume_token (parser
->lexer
);
31443 strcpy (p_name
, "#pragma omp target");
31444 if (!flag_openmp
) /* flag_openmp_simd */
31446 tree stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
31447 OMP_TARGET_CLAUSE_MASK
,
31449 return stmt
!= NULL_TREE
;
31451 keep_next_level (true);
31452 tree sb
= begin_omp_structured_block ();
31453 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
31454 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
31455 OMP_TARGET_CLAUSE_MASK
, cclauses
);
31456 cp_parser_end_omp_structured_block (parser
, save
);
31457 tree body
= finish_omp_structured_block (sb
);
31458 if (ret
== NULL_TREE
)
31460 tree stmt
= make_node (OMP_TARGET
);
31461 TREE_TYPE (stmt
) = void_type_node
;
31462 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
31463 OMP_TARGET_BODY (stmt
) = body
;
31467 else if (!flag_openmp
) /* flag_openmp_simd */
31469 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31472 else if (strcmp (p
, "data") == 0)
31474 cp_lexer_consume_token (parser
->lexer
);
31475 cp_parser_omp_target_data (parser
, pragma_tok
);
31478 else if (strcmp (p
, "update") == 0)
31480 cp_lexer_consume_token (parser
->lexer
);
31481 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
31485 tree stmt
= make_node (OMP_TARGET
);
31486 TREE_TYPE (stmt
) = void_type_node
;
31488 OMP_TARGET_CLAUSES (stmt
)
31489 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
31490 "#pragma omp target", pragma_tok
);
31491 keep_next_level (true);
31492 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31494 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31500 # pragma acc cache (variable-list) new-line
31504 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
31506 tree stmt
, clauses
;
31508 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
31509 clauses
= finish_omp_clauses (clauses
);
31511 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
31513 stmt
= make_node (OACC_CACHE
);
31514 TREE_TYPE (stmt
) = void_type_node
;
31515 OACC_CACHE_CLAUSES (stmt
) = clauses
;
31516 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31523 # pragma acc data oacc-data-clause[optseq] new-line
31524 structured-block */
31526 #define OACC_DATA_CLAUSE_MASK \
31527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31540 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
)
31542 tree stmt
, clauses
, block
;
31545 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
31546 "#pragma acc data", pragma_tok
);
31548 block
= begin_omp_parallel ();
31549 save
= cp_parser_begin_omp_structured_block (parser
);
31550 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31551 cp_parser_end_omp_structured_block (parser
, save
);
31552 stmt
= finish_oacc_data (clauses
, block
);
31557 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31561 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31563 LOC is the location of the #pragma token.
31566 #define OACC_ENTER_DATA_CLAUSE_MASK \
31567 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31575 #define OACC_EXIT_DATA_CLAUSE_MASK \
31576 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31583 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
31586 tree stmt
, clauses
;
31588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)
31589 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31591 cp_parser_error (parser
, enter
31592 ? "expected %<data%> in %<#pragma acc enter data%>"
31593 : "expected %<data%> in %<#pragma acc exit data%>");
31594 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31599 IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
31600 if (strcmp (p
, "data") != 0)
31602 cp_parser_error (parser
, "invalid pragma");
31603 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31607 cp_lexer_consume_token (parser
->lexer
);
31610 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
31611 "#pragma acc enter data", pragma_tok
);
31613 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
31614 "#pragma acc exit data", pragma_tok
);
31616 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
31618 error_at (pragma_tok
->location
,
31619 "%<#pragma acc enter data%> has no data movement clause");
31623 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
31624 TREE_TYPE (stmt
) = void_type_node
;
31626 OACC_ENTER_DATA_CLAUSES (stmt
) = clauses
;
31628 OACC_EXIT_DATA_CLAUSES (stmt
) = clauses
;
31629 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31635 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31636 structured-block */
31638 #define OACC_KERNELS_CLAUSE_MASK \
31639 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31654 cp_parser_oacc_kernels (cp_parser
*parser
, cp_token
*pragma_tok
)
31656 tree stmt
, clauses
, block
;
31659 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_KERNELS_CLAUSE_MASK
,
31660 "#pragma acc kernels", pragma_tok
);
31662 block
= begin_omp_parallel ();
31663 save
= cp_parser_begin_omp_structured_block (parser
);
31664 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31665 cp_parser_end_omp_structured_block (parser
, save
);
31666 stmt
= finish_oacc_kernels (clauses
, block
);
31671 # pragma acc loop oacc-loop-clause[optseq] new-line
31672 structured-block */
31674 #define OACC_LOOP_CLAUSE_MASK \
31675 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31679 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
)
31681 tree stmt
, clauses
, block
;
31684 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_LOOP_CLAUSE_MASK
,
31685 "#pragma acc loop", pragma_tok
);
31687 block
= begin_omp_structured_block ();
31688 save
= cp_parser_begin_omp_structured_block (parser
);
31689 stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
);
31690 cp_parser_end_omp_structured_block (parser
, save
);
31691 add_stmt (finish_omp_structured_block (block
));
31696 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31697 structured-block */
31699 #define OACC_PARALLEL_CLAUSE_MASK \
31700 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31719 cp_parser_oacc_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
31721 tree stmt
, clauses
, block
;
31724 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_PARALLEL_CLAUSE_MASK
,
31725 "#pragma acc parallel", pragma_tok
);
31727 block
= begin_omp_parallel ();
31728 save
= cp_parser_begin_omp_structured_block (parser
);
31729 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31730 cp_parser_end_omp_structured_block (parser
, save
);
31731 stmt
= finish_oacc_parallel (clauses
, block
);
31736 # pragma acc update oacc-update-clause[optseq] new-line
31739 #define OACC_UPDATE_CLAUSE_MASK \
31740 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31748 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
31750 tree stmt
, clauses
;
31752 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
31753 "#pragma acc update", pragma_tok
);
31755 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
31757 error_at (pragma_tok
->location
,
31758 "%<#pragma acc update%> must contain at least one "
31759 "%<device%> or %<host/self%> clause");
31763 stmt
= make_node (OACC_UPDATE
);
31764 TREE_TYPE (stmt
) = void_type_node
;
31765 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
31766 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31772 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31774 LOC is the location of the #pragma token.
31777 #define OACC_WAIT_CLAUSE_MASK \
31778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31781 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
31783 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
31784 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31786 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31787 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
31789 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
31790 "#pragma acc wait", pragma_tok
);
31792 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
31798 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31800 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31809 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
31810 enum pragma_context context
)
31812 bool first_p
= parser
->omp_declare_simd
== NULL
;
31813 cp_omp_declare_simd_data data
;
31816 data
.error_seen
= false;
31817 data
.fndecl_seen
= false;
31818 data
.tokens
= vNULL
;
31819 parser
->omp_declare_simd
= &data
;
31821 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31822 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
31823 cp_lexer_consume_token (parser
->lexer
);
31824 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31825 parser
->omp_declare_simd
->error_seen
= true;
31826 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31827 struct cp_token_cache
*cp
31828 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
31829 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
31832 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
31833 cp_parser_pragma (parser
, context
);
31836 case pragma_external
:
31837 cp_parser_declaration (parser
);
31839 case pragma_member
:
31840 cp_parser_member_declaration (parser
);
31842 case pragma_objc_icode
:
31843 cp_parser_block_declaration (parser
, /*statement_p=*/false);
31846 cp_parser_declaration_statement (parser
);
31849 if (parser
->omp_declare_simd
31850 && !parser
->omp_declare_simd
->error_seen
31851 && !parser
->omp_declare_simd
->fndecl_seen
)
31852 error_at (pragma_tok
->location
,
31853 "%<#pragma omp declare simd%> not immediately followed by "
31854 "function declaration or definition");
31855 data
.tokens
.release ();
31856 parser
->omp_declare_simd
= NULL
;
31860 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31861 This function is modelled similar to the late parsing of omp declare
31865 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
31867 struct cp_token_cache
*ce
;
31868 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
31871 if (parser
->omp_declare_simd
!= NULL
)
31873 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31874 " marked as a Cilk Plus SIMD-enabled function");
31875 XDELETE (parser
->cilk_simd_fn_info
);
31876 parser
->cilk_simd_fn_info
= NULL
;
31879 if (!info
->error_seen
&& info
->fndecl_seen
)
31881 error ("vector attribute not immediately followed by a single function"
31882 " declaration or definition");
31883 info
->error_seen
= true;
31885 if (info
->error_seen
)
31888 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
31892 cp_parser_push_lexer_for_tokens (parser
, ce
);
31893 parser
->lexer
->in_pragma
= true;
31894 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
31895 "SIMD-enabled functions attribute",
31897 cp_parser_pop_lexer (parser
);
31899 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
31901 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
31902 TREE_CHAIN (c
) = attrs
;
31905 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
31906 TREE_CHAIN (c
) = attrs
;
31907 if (processing_template_decl
)
31908 ATTR_IS_DEPENDENT (c
) = 1;
31911 info
->fndecl_seen
= true;
31912 XDELETE (parser
->cilk_simd_fn_info
);
31913 parser
->cilk_simd_fn_info
= NULL
;
31917 /* Finalize #pragma omp declare simd clauses after direct declarator has
31918 been parsed, and put that into "omp declare simd" attribute. */
31921 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
31923 struct cp_token_cache
*ce
;
31924 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
31927 if (!data
->error_seen
&& data
->fndecl_seen
)
31929 error ("%<#pragma omp declare simd%> not immediately followed by "
31930 "a single function declaration or definition");
31931 data
->error_seen
= true;
31934 if (data
->error_seen
)
31937 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
31941 cp_parser_push_lexer_for_tokens (parser
, ce
);
31942 parser
->lexer
->in_pragma
= true;
31943 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
31944 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
31945 cp_lexer_consume_token (parser
->lexer
);
31946 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
31947 "#pragma omp declare simd", pragma_tok
);
31948 cp_parser_pop_lexer (parser
);
31950 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
31951 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
31952 TREE_CHAIN (c
) = attrs
;
31953 if (processing_template_decl
)
31954 ATTR_IS_DEPENDENT (c
) = 1;
31958 data
->fndecl_seen
= true;
31964 # pragma omp declare target new-line
31965 declarations and definitions
31966 # pragma omp end declare target new-line */
31969 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31971 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31972 scope_chain
->omp_declare_target_attribute
++;
31976 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31978 const char *p
= "";
31979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31981 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31982 p
= IDENTIFIER_POINTER (id
);
31984 if (strcmp (p
, "declare") == 0)
31986 cp_lexer_consume_token (parser
->lexer
);
31988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31990 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31991 p
= IDENTIFIER_POINTER (id
);
31993 if (strcmp (p
, "target") == 0)
31994 cp_lexer_consume_token (parser
->lexer
);
31997 cp_parser_error (parser
, "expected %<target%>");
31998 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32004 cp_parser_error (parser
, "expected %<declare%>");
32005 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32008 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32009 if (!scope_chain
->omp_declare_target_attribute
)
32010 error_at (pragma_tok
->location
,
32011 "%<#pragma omp end declare target%> without corresponding "
32012 "%<#pragma omp declare target%>");
32014 scope_chain
->omp_declare_target_attribute
--;
32017 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32018 expression and optional initializer clause of
32019 #pragma omp declare reduction. We store the expression(s) as
32020 either 3, 6 or 7 special statements inside of the artificial function's
32021 body. The first two statements are DECL_EXPRs for the artificial
32022 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32023 expression that uses those variables.
32024 If there was any INITIALIZER clause, this is followed by further statements,
32025 the fourth and fifth statements are DECL_EXPRs for the artificial
32026 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32027 constructor variant (first token after open paren is not omp_priv),
32028 then the sixth statement is a statement with the function call expression
32029 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32030 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32031 to initialize the OMP_PRIV artificial variable and there is seventh
32032 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32035 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
32037 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
32038 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
32039 type
= TREE_TYPE (type
);
32040 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
32041 DECL_ARTIFICIAL (omp_out
) = 1;
32042 pushdecl (omp_out
);
32043 add_decl_expr (omp_out
);
32044 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
32045 DECL_ARTIFICIAL (omp_in
) = 1;
32047 add_decl_expr (omp_in
);
32049 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
32051 keep_next_level (true);
32052 tree block
= begin_omp_structured_block ();
32053 combiner
= cp_parser_expression (parser
);
32054 finish_expr_stmt (combiner
);
32055 block
= finish_omp_structured_block (block
);
32058 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32061 const char *p
= "";
32062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32064 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32065 p
= IDENTIFIER_POINTER (id
);
32068 if (strcmp (p
, "initializer") == 0)
32070 cp_lexer_consume_token (parser
->lexer
);
32071 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32077 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32078 p
= IDENTIFIER_POINTER (id
);
32081 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
32082 DECL_ARTIFICIAL (omp_priv
) = 1;
32083 pushdecl (omp_priv
);
32084 add_decl_expr (omp_priv
);
32085 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
32086 DECL_ARTIFICIAL (omp_orig
) = 1;
32087 pushdecl (omp_orig
);
32088 add_decl_expr (omp_orig
);
32090 keep_next_level (true);
32091 block
= begin_omp_structured_block ();
32094 if (strcmp (p
, "omp_priv") == 0)
32096 bool is_direct_init
, is_non_constant_init
;
32098 cp_lexer_consume_token (parser
->lexer
);
32099 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
32101 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
32102 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
32104 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
32105 == CPP_CLOSE_PAREN
))
32107 finish_omp_structured_block (block
);
32108 error ("invalid initializer clause");
32111 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
32112 &is_non_constant_init
);
32113 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
32114 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
32118 cp_parser_parse_tentatively (parser
);
32119 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32120 /*check_dependency_p=*/true,
32121 /*template_p=*/NULL
,
32122 /*declarator_p=*/false,
32123 /*optional_p=*/false);
32124 vec
<tree
, va_gc
> *args
;
32125 if (fn_name
== error_mark_node
32126 || cp_parser_error_occurred (parser
)
32127 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
32128 || ((args
= cp_parser_parenthesized_expression_list
32129 (parser
, non_attr
, /*cast_p=*/false,
32130 /*allow_expansion_p=*/true,
32131 /*non_constant_p=*/NULL
)),
32132 cp_parser_error_occurred (parser
)))
32134 finish_omp_structured_block (block
);
32135 cp_parser_abort_tentative_parse (parser
);
32136 cp_parser_error (parser
, "expected id-expression (arguments)");
32141 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
32142 if (arg
== omp_priv
32143 || (TREE_CODE (arg
) == ADDR_EXPR
32144 && TREE_OPERAND (arg
, 0) == omp_priv
))
32146 cp_parser_abort_tentative_parse (parser
);
32147 if (arg
== NULL_TREE
)
32148 error ("one of the initializer call arguments should be %<omp_priv%>"
32149 " or %<&omp_priv%>");
32150 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
32152 finish_expr_stmt (initializer
);
32155 block
= finish_omp_structured_block (block
);
32156 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
32160 add_decl_expr (omp_orig
);
32162 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32166 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
32167 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
32173 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32174 initializer-clause[opt] new-line
32176 initializer-clause:
32177 initializer (omp_priv initializer)
32178 initializer (function-name (argument-list)) */
32181 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
32182 enum pragma_context
)
32184 auto_vec
<tree
> types
;
32185 enum tree_code reduc_code
= ERROR_MARK
;
32186 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
32188 cp_token
*first_token
;
32189 cp_token_cache
*cp
;
32193 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32194 p
= obstack_alloc (&declarator_obstack
, 0);
32196 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32199 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32202 reduc_code
= PLUS_EXPR
;
32205 reduc_code
= MULT_EXPR
;
32208 reduc_code
= MINUS_EXPR
;
32211 reduc_code
= BIT_AND_EXPR
;
32214 reduc_code
= BIT_XOR_EXPR
;
32217 reduc_code
= BIT_IOR_EXPR
;
32220 reduc_code
= TRUTH_ANDIF_EXPR
;
32223 reduc_code
= TRUTH_ORIF_EXPR
;
32226 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
32229 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32230 "%<|%>, %<&&%>, %<||%> or identifier");
32234 if (reduc_code
!= ERROR_MARK
)
32235 cp_lexer_consume_token (parser
->lexer
);
32237 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
32238 if (reduc_id
== error_mark_node
)
32241 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32244 /* Types may not be defined in declare reduction type list. */
32245 const char *saved_message
;
32246 saved_message
= parser
->type_definition_forbidden_message
;
32247 parser
->type_definition_forbidden_message
32248 = G_("types may not be defined in declare reduction type list");
32249 bool saved_colon_corrects_to_scope_p
;
32250 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32251 parser
->colon_corrects_to_scope_p
= false;
32252 bool saved_colon_doesnt_start_class_def_p
;
32253 saved_colon_doesnt_start_class_def_p
32254 = parser
->colon_doesnt_start_class_def_p
;
32255 parser
->colon_doesnt_start_class_def_p
= true;
32259 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32260 type
= cp_parser_type_id (parser
);
32261 if (type
== error_mark_node
)
32263 else if (ARITHMETIC_TYPE_P (type
)
32264 && (orig_reduc_id
== NULL_TREE
32265 || (TREE_CODE (type
) != COMPLEX_TYPE
32266 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
32268 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
32270 error_at (loc
, "predeclared arithmetic type %qT in "
32271 "%<#pragma omp declare reduction%>", type
);
32272 else if (TREE_CODE (type
) == FUNCTION_TYPE
32273 || TREE_CODE (type
) == METHOD_TYPE
32274 || TREE_CODE (type
) == ARRAY_TYPE
)
32275 error_at (loc
, "function or array type %qT in "
32276 "%<#pragma omp declare reduction%>", type
);
32277 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
32278 error_at (loc
, "reference type %qT in "
32279 "%<#pragma omp declare reduction%>", type
);
32280 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
32281 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
32282 "%<#pragma omp declare reduction%>", type
);
32284 types
.safe_push (type
);
32286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32287 cp_lexer_consume_token (parser
->lexer
);
32292 /* Restore the saved message. */
32293 parser
->type_definition_forbidden_message
= saved_message
;
32294 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32295 parser
->colon_doesnt_start_class_def_p
32296 = saved_colon_doesnt_start_class_def_p
;
32298 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
32299 || types
.is_empty ())
32302 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32306 first_token
= cp_lexer_peek_token (parser
->lexer
);
32309 FOR_EACH_VEC_ELT (types
, i
, type
)
32312 = build_function_type_list (void_type_node
,
32313 cp_build_reference_type (type
, false),
32315 tree this_reduc_id
= reduc_id
;
32316 if (!dependent_type_p (type
))
32317 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
32318 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
32319 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
32320 DECL_ARTIFICIAL (fndecl
) = 1;
32321 DECL_EXTERNAL (fndecl
) = 1;
32322 DECL_DECLARED_INLINE_P (fndecl
) = 1;
32323 DECL_IGNORED_P (fndecl
) = 1;
32324 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
32325 DECL_ATTRIBUTES (fndecl
)
32326 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
32327 DECL_ATTRIBUTES (fndecl
));
32328 if (processing_template_decl
)
32329 fndecl
= push_template_decl (fndecl
);
32330 bool block_scope
= false;
32331 tree block
= NULL_TREE
;
32332 if (current_function_decl
)
32334 block_scope
= true;
32335 DECL_CONTEXT (fndecl
) = global_namespace
;
32336 if (!processing_template_decl
)
32339 else if (current_class_type
)
32343 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
32344 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
32345 cp_lexer_consume_token (parser
->lexer
);
32346 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32348 cp
= cp_token_cache_new (first_token
,
32349 cp_lexer_peek_nth_token (parser
->lexer
,
32352 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
32353 finish_member_declaration (fndecl
);
32354 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
32355 DECL_PENDING_INLINE_P (fndecl
) = 1;
32356 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
32361 DECL_CONTEXT (fndecl
) = current_namespace
;
32365 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
32367 block
= begin_omp_structured_block ();
32370 cp_parser_push_lexer_for_tokens (parser
, cp
);
32371 parser
->lexer
->in_pragma
= true;
32373 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
32376 finish_function (0);
32378 DECL_CONTEXT (fndecl
) = current_function_decl
;
32380 cp_parser_pop_lexer (parser
);
32384 cp_parser_pop_lexer (parser
);
32386 finish_function (0);
32389 DECL_CONTEXT (fndecl
) = current_function_decl
;
32390 block
= finish_omp_structured_block (block
);
32391 if (TREE_CODE (block
) == BIND_EXPR
)
32392 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
32393 else if (TREE_CODE (block
) == STATEMENT_LIST
)
32394 DECL_SAVED_TREE (fndecl
) = block
;
32395 if (processing_template_decl
)
32396 add_decl_expr (fndecl
);
32398 cp_check_omp_declare_reduction (fndecl
);
32399 if (cp
== NULL
&& types
.length () > 1)
32400 cp
= cp_token_cache_new (first_token
,
32401 cp_lexer_peek_nth_token (parser
->lexer
, 2));
32402 if (errs
!= errorcount
)
32406 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32409 /* Free any declarators allocated. */
32410 obstack_free (&declarator_obstack
, p
);
32414 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32415 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32416 initializer-clause[opt] new-line
32417 #pragma omp declare target new-line */
32420 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
32421 enum pragma_context context
)
32423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32425 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32426 const char *p
= IDENTIFIER_POINTER (id
);
32428 if (strcmp (p
, "simd") == 0)
32430 cp_lexer_consume_token (parser
->lexer
);
32431 cp_parser_omp_declare_simd (parser
, pragma_tok
,
32435 cp_ensure_no_omp_declare_simd (parser
);
32436 if (strcmp (p
, "reduction") == 0)
32438 cp_lexer_consume_token (parser
->lexer
);
32439 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
32443 if (!flag_openmp
) /* flag_openmp_simd */
32445 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32448 if (strcmp (p
, "target") == 0)
32450 cp_lexer_consume_token (parser
->lexer
);
32451 cp_parser_omp_declare_target (parser
, pragma_tok
);
32455 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
32457 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32460 /* Main entry point to OpenMP statement pragmas. */
32463 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
32466 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
32467 omp_clause_mask
mask (0);
32469 switch (pragma_tok
->pragma_kind
)
32471 case PRAGMA_OACC_CACHE
:
32472 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
32474 case PRAGMA_OACC_DATA
:
32475 stmt
= cp_parser_oacc_data (parser
, pragma_tok
);
32477 case PRAGMA_OACC_ENTER_DATA
:
32478 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
32480 case PRAGMA_OACC_EXIT_DATA
:
32481 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
32483 case PRAGMA_OACC_KERNELS
:
32484 stmt
= cp_parser_oacc_kernels (parser
, pragma_tok
);
32486 case PRAGMA_OACC_LOOP
:
32487 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
);
32489 case PRAGMA_OACC_PARALLEL
:
32490 stmt
= cp_parser_oacc_parallel (parser
, pragma_tok
);
32492 case PRAGMA_OACC_UPDATE
:
32493 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
32495 case PRAGMA_OACC_WAIT
:
32496 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
32498 case PRAGMA_OMP_ATOMIC
:
32499 cp_parser_omp_atomic (parser
, pragma_tok
);
32501 case PRAGMA_OMP_CRITICAL
:
32502 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
32504 case PRAGMA_OMP_DISTRIBUTE
:
32505 strcpy (p_name
, "#pragma omp");
32506 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
32508 case PRAGMA_OMP_FOR
:
32509 strcpy (p_name
, "#pragma omp");
32510 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
32512 case PRAGMA_OMP_MASTER
:
32513 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
32515 case PRAGMA_OMP_ORDERED
:
32516 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
32518 case PRAGMA_OMP_PARALLEL
:
32519 strcpy (p_name
, "#pragma omp");
32520 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
32522 case PRAGMA_OMP_SECTIONS
:
32523 strcpy (p_name
, "#pragma omp");
32524 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
32526 case PRAGMA_OMP_SIMD
:
32527 strcpy (p_name
, "#pragma omp");
32528 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
32530 case PRAGMA_OMP_SINGLE
:
32531 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
32533 case PRAGMA_OMP_TASK
:
32534 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
32536 case PRAGMA_OMP_TASKGROUP
:
32537 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
32539 case PRAGMA_OMP_TEAMS
:
32540 strcpy (p_name
, "#pragma omp");
32541 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
32544 gcc_unreachable ();
32548 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
32551 /* Transactional Memory parsing routines. */
32553 /* Parse a transaction attribute.
32559 ??? Simplify this when C++0x bracket attributes are
32560 implemented properly. */
32563 cp_parser_txn_attribute_opt (cp_parser
*parser
)
32566 tree attr_name
, attr
= NULL
;
32568 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
32569 return cp_parser_attributes_opt (parser
);
32571 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
32573 cp_lexer_consume_token (parser
->lexer
);
32574 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
32577 token
= cp_lexer_peek_token (parser
->lexer
);
32578 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
32580 token
= cp_lexer_consume_token (parser
->lexer
);
32582 attr_name
= (token
->type
== CPP_KEYWORD
32583 /* For keywords, use the canonical spelling,
32584 not the parsed identifier. */
32585 ? ridpointers
[(int) token
->keyword
]
32587 attr
= build_tree_list (attr_name
, NULL_TREE
);
32590 cp_parser_error (parser
, "expected identifier");
32592 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32594 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32598 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32600 transaction-statement:
32601 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32603 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32607 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
32609 unsigned char old_in
= parser
->in_transaction
;
32610 unsigned char this_in
= 1, new_in
;
32612 tree stmt
, attrs
, noex
;
32614 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32615 || keyword
== RID_TRANSACTION_RELAXED
);
32616 token
= cp_parser_require_keyword (parser
, keyword
,
32617 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32618 : RT_TRANSACTION_RELAXED
));
32619 gcc_assert (token
!= NULL
);
32621 if (keyword
== RID_TRANSACTION_RELAXED
)
32622 this_in
|= TM_STMT_ATTR_RELAXED
;
32625 attrs
= cp_parser_txn_attribute_opt (parser
);
32627 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
32630 /* Parse a noexcept specification. */
32631 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
32633 /* Keep track if we're in the lexical scope of an outer transaction. */
32634 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
32636 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
32638 parser
->in_transaction
= new_in
;
32639 cp_parser_compound_statement (parser
, NULL
, false, false);
32640 parser
->in_transaction
= old_in
;
32642 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
32647 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32649 transaction-expression:
32650 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32651 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32655 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
32657 unsigned char old_in
= parser
->in_transaction
;
32658 unsigned char this_in
= 1;
32663 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32664 || keyword
== RID_TRANSACTION_RELAXED
);
32667 error (keyword
== RID_TRANSACTION_RELAXED
32668 ? G_("%<__transaction_relaxed%> without transactional memory "
32670 : G_("%<__transaction_atomic%> without transactional memory "
32671 "support enabled"));
32673 token
= cp_parser_require_keyword (parser
, keyword
,
32674 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32675 : RT_TRANSACTION_RELAXED
));
32676 gcc_assert (token
!= NULL
);
32678 if (keyword
== RID_TRANSACTION_RELAXED
)
32679 this_in
|= TM_STMT_ATTR_RELAXED
;
32681 /* Set this early. This might mean that we allow transaction_cancel in
32682 an expression that we find out later actually has to be a constexpr.
32683 However, we expect that cxx_constant_value will be able to deal with
32684 this; also, if the noexcept has no constexpr, then what we parse next
32685 really is a transaction's body. */
32686 parser
->in_transaction
= this_in
;
32688 /* Parse a noexcept specification. */
32689 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
32692 if (!noex
|| !noex_expr
32693 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32695 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
32697 expr
= cp_parser_expression (parser
);
32698 expr
= finish_parenthesized_expr (expr
);
32700 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
32704 /* The only expression that is available got parsed for the noexcept
32705 already. noexcept is true then. */
32707 noex
= boolean_true_node
;
32710 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
32711 parser
->in_transaction
= old_in
;
32713 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
32714 return error_mark_node
;
32716 return (flag_tm
? expr
: error_mark_node
);
32719 /* Parse a function-transaction-block.
32721 function-transaction-block:
32722 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32724 __transaction_atomic txn-attribute[opt] function-try-block
32725 __transaction_relaxed ctor-initializer[opt] function-body
32726 __transaction_relaxed function-try-block
32730 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
32732 unsigned char old_in
= parser
->in_transaction
;
32733 unsigned char new_in
= 1;
32734 tree compound_stmt
, stmt
, attrs
;
32735 bool ctor_initializer_p
;
32738 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32739 || keyword
== RID_TRANSACTION_RELAXED
);
32740 token
= cp_parser_require_keyword (parser
, keyword
,
32741 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32742 : RT_TRANSACTION_RELAXED
));
32743 gcc_assert (token
!= NULL
);
32745 if (keyword
== RID_TRANSACTION_RELAXED
)
32746 new_in
|= TM_STMT_ATTR_RELAXED
;
32749 attrs
= cp_parser_txn_attribute_opt (parser
);
32751 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
32754 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
32756 parser
->in_transaction
= new_in
;
32758 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
32759 ctor_initializer_p
= cp_parser_function_try_block (parser
);
32761 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
32762 (parser
, /*in_function_try_block=*/false);
32764 parser
->in_transaction
= old_in
;
32766 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
32768 return ctor_initializer_p
;
32771 /* Parse a __transaction_cancel statement.
32774 __transaction_cancel txn-attribute[opt] ;
32775 __transaction_cancel txn-attribute[opt] throw-expression ;
32777 ??? Cancel and throw is not yet implemented. */
32780 cp_parser_transaction_cancel (cp_parser
*parser
)
32783 bool is_outer
= false;
32786 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
32787 RT_TRANSACTION_CANCEL
);
32788 gcc_assert (token
!= NULL
);
32790 attrs
= cp_parser_txn_attribute_opt (parser
);
32792 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
32794 /* ??? Parse cancel-and-throw here. */
32796 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
32800 error_at (token
->location
, "%<__transaction_cancel%> without "
32801 "transactional memory support enabled");
32802 return error_mark_node
;
32804 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
32806 error_at (token
->location
, "%<__transaction_cancel%> within a "
32807 "%<__transaction_relaxed%>");
32808 return error_mark_node
;
32812 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
32813 && !is_tm_may_cancel_outer (current_function_decl
))
32815 error_at (token
->location
, "outer %<__transaction_cancel%> not "
32816 "within outer %<__transaction_atomic%>");
32817 error_at (token
->location
,
32818 " or a %<transaction_may_cancel_outer%> function");
32819 return error_mark_node
;
32822 else if (parser
->in_transaction
== 0)
32824 error_at (token
->location
, "%<__transaction_cancel%> not within "
32825 "%<__transaction_atomic%>");
32826 return error_mark_node
;
32829 stmt
= build_tm_abort_call (token
->location
, is_outer
);
32837 static GTY (()) cp_parser
*the_parser
;
32840 /* Special handling for the first token or line in the file. The first
32841 thing in the file might be #pragma GCC pch_preprocess, which loads a
32842 PCH file, which is a GC collection point. So we need to handle this
32843 first pragma without benefit of an existing lexer structure.
32845 Always returns one token to the caller in *FIRST_TOKEN. This is
32846 either the true first token of the file, or the first token after
32847 the initial pragma. */
32850 cp_parser_initial_pragma (cp_token
*first_token
)
32854 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32855 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
32858 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32859 if (first_token
->type
== CPP_STRING
)
32861 name
= first_token
->u
.value
;
32863 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32864 if (first_token
->type
!= CPP_PRAGMA_EOL
)
32865 error_at (first_token
->location
,
32866 "junk at end of %<#pragma GCC pch_preprocess%>");
32869 error_at (first_token
->location
, "expected string literal");
32871 /* Skip to the end of the pragma. */
32872 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
32873 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32875 /* Now actually load the PCH file. */
32877 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
32879 /* Read one more token to return to our caller. We have to do this
32880 after reading the PCH file in, since its pointers have to be
32882 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32885 /* Parses the grainsize pragma for the _Cilk_for statement.
32887 #pragma cilk grainsize = <VALUE>. */
32890 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
)
32892 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32894 tree exp
= cp_parser_binary_expression (parser
, false, false,
32895 PREC_NOT_OPERATOR
, NULL
);
32896 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32897 if (!exp
|| exp
== error_mark_node
)
32899 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
32903 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32904 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
32905 cp_parser_cilk_for (parser
, exp
);
32907 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
32908 "%<#pragma cilk grainsize%> is not followed by "
32912 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32915 /* Normal parsing of a pragma token. Here we can (and must) use the
32919 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
32921 cp_token
*pragma_tok
;
32924 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
32925 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
32926 parser
->lexer
->in_pragma
= true;
32928 id
= pragma_tok
->pragma_kind
;
32929 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
32930 cp_ensure_no_omp_declare_simd (parser
);
32933 case PRAGMA_GCC_PCH_PREPROCESS
:
32934 error_at (pragma_tok
->location
,
32935 "%<#pragma GCC pch_preprocess%> must be first");
32938 case PRAGMA_OMP_BARRIER
:
32941 case pragma_compound
:
32942 cp_parser_omp_barrier (parser
, pragma_tok
);
32945 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
32946 "used in compound statements");
32953 case PRAGMA_OMP_FLUSH
:
32956 case pragma_compound
:
32957 cp_parser_omp_flush (parser
, pragma_tok
);
32960 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
32961 "used in compound statements");
32968 case PRAGMA_OMP_TASKWAIT
:
32971 case pragma_compound
:
32972 cp_parser_omp_taskwait (parser
, pragma_tok
);
32975 error_at (pragma_tok
->location
,
32976 "%<#pragma omp taskwait%> may only be "
32977 "used in compound statements");
32984 case PRAGMA_OMP_TASKYIELD
:
32987 case pragma_compound
:
32988 cp_parser_omp_taskyield (parser
, pragma_tok
);
32991 error_at (pragma_tok
->location
,
32992 "%<#pragma omp taskyield%> may only be "
32993 "used in compound statements");
33000 case PRAGMA_OMP_CANCEL
:
33003 case pragma_compound
:
33004 cp_parser_omp_cancel (parser
, pragma_tok
);
33007 error_at (pragma_tok
->location
,
33008 "%<#pragma omp cancel%> may only be "
33009 "used in compound statements");
33016 case PRAGMA_OMP_CANCELLATION_POINT
:
33019 case pragma_compound
:
33020 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
33023 error_at (pragma_tok
->location
,
33024 "%<#pragma omp cancellation point%> may only be "
33025 "used in compound statements");
33032 case PRAGMA_OMP_THREADPRIVATE
:
33033 cp_parser_omp_threadprivate (parser
, pragma_tok
);
33036 case PRAGMA_OMP_DECLARE_REDUCTION
:
33037 cp_parser_omp_declare (parser
, pragma_tok
, context
);
33040 case PRAGMA_OACC_CACHE
:
33041 case PRAGMA_OACC_DATA
:
33042 case PRAGMA_OACC_ENTER_DATA
:
33043 case PRAGMA_OACC_EXIT_DATA
:
33044 case PRAGMA_OACC_KERNELS
:
33045 case PRAGMA_OACC_PARALLEL
:
33046 case PRAGMA_OACC_LOOP
:
33047 case PRAGMA_OACC_UPDATE
:
33048 case PRAGMA_OACC_WAIT
:
33049 case PRAGMA_OMP_ATOMIC
:
33050 case PRAGMA_OMP_CRITICAL
:
33051 case PRAGMA_OMP_DISTRIBUTE
:
33052 case PRAGMA_OMP_FOR
:
33053 case PRAGMA_OMP_MASTER
:
33054 case PRAGMA_OMP_ORDERED
:
33055 case PRAGMA_OMP_PARALLEL
:
33056 case PRAGMA_OMP_SECTIONS
:
33057 case PRAGMA_OMP_SIMD
:
33058 case PRAGMA_OMP_SINGLE
:
33059 case PRAGMA_OMP_TASK
:
33060 case PRAGMA_OMP_TASKGROUP
:
33061 case PRAGMA_OMP_TEAMS
:
33062 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
33064 cp_parser_omp_construct (parser
, pragma_tok
);
33067 case PRAGMA_OMP_TARGET
:
33068 return cp_parser_omp_target (parser
, pragma_tok
, context
);
33070 case PRAGMA_OMP_END_DECLARE_TARGET
:
33071 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
33074 case PRAGMA_OMP_SECTION
:
33075 error_at (pragma_tok
->location
,
33076 "%<#pragma omp section%> may only be used in "
33077 "%<#pragma omp sections%> construct");
33082 if (context
== pragma_external
)
33084 error_at (pragma_tok
->location
,
33085 "%<#pragma GCC ivdep%> must be inside a function");
33088 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33090 tok
= cp_lexer_peek_token (the_parser
->lexer
);
33091 if (tok
->type
!= CPP_KEYWORD
33092 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
33093 && tok
->keyword
!= RID_DO
))
33095 cp_parser_error (parser
, "for, while or do statement expected");
33098 cp_parser_iteration_statement (parser
, true);
33102 case PRAGMA_CILK_SIMD
:
33103 if (context
== pragma_external
)
33105 error_at (pragma_tok
->location
,
33106 "%<#pragma simd%> must be inside a function");
33109 cp_parser_cilk_simd (parser
, pragma_tok
);
33112 case PRAGMA_CILK_GRAINSIZE
:
33113 if (context
== pragma_external
)
33115 error_at (pragma_tok
->location
,
33116 "%<#pragma cilk grainsize%> must be inside a function");
33120 /* Ignore the pragma if Cilk Plus is not enabled. */
33123 cp_parser_cilk_grainsize (parser
, pragma_tok
);
33128 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
33129 "%<#pragma cilk grainsize%>");
33134 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
33135 c_invoke_pragma_handler (id
);
33139 cp_parser_error (parser
, "expected declaration specifiers");
33143 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33147 /* The interface the pragma parsers have to the lexer. */
33150 pragma_lex (tree
*value
)
33153 enum cpp_ttype ret
;
33155 tok
= cp_lexer_peek_token (the_parser
->lexer
);
33158 *value
= tok
->u
.value
;
33160 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
33162 else if (ret
== CPP_STRING
)
33163 *value
= cp_parser_string_literal (the_parser
, false, false);
33166 cp_lexer_consume_token (the_parser
->lexer
);
33167 if (ret
== CPP_KEYWORD
)
33175 /* External interface. */
33177 /* Parse one entire translation unit. */
33180 c_parse_file (void)
33182 static bool already_called
= false;
33184 if (already_called
)
33185 fatal_error (input_location
,
33186 "inter-module optimizations not implemented for C++");
33187 already_called
= true;
33189 the_parser
= cp_parser_new ();
33190 push_deferring_access_checks (flag_access_control
33191 ? dk_no_deferred
: dk_no_check
);
33192 cp_parser_translation_unit (the_parser
);
33196 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33197 vectorlength clause:
33199 vectorlength ( constant-expression ) */
33202 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
33205 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33207 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33208 safelen clause. Thus, vectorlength is represented as OMP 4.0
33209 safelen. For SIMD-enabled function it is represented by OMP 4.0
33212 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
33215 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
33218 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33219 return error_mark_node
;
33221 expr
= cp_parser_constant_expression (parser
);
33222 expr
= maybe_constant_value (expr
);
33224 /* If expr == error_mark_node, then don't emit any errors nor
33225 create a clause. if any of the above functions returns
33226 error mark node then they would have emitted an error message. */
33227 if (expr
== error_mark_node
)
33229 else if (!TREE_TYPE (expr
)
33230 || !TREE_CONSTANT (expr
)
33231 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
33232 error_at (loc
, "vectorlength must be an integer constant");
33233 else if (TREE_CONSTANT (expr
)
33234 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
33235 error_at (loc
, "vectorlength must be a power of 2");
33241 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
33242 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
33243 OMP_CLAUSE_CHAIN (c
) = clauses
;
33248 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
33249 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
33250 OMP_CLAUSE_CHAIN (c
) = clauses
;
33255 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33256 return error_mark_node
;
33260 /* Handles the Cilk Plus #pragma simd linear clause.
33262 linear ( simd-linear-variable-list )
33264 simd-linear-variable-list:
33265 simd-linear-variable
33266 simd-linear-variable-list , simd-linear-variable
33268 simd-linear-variable:
33270 id-expression : simd-linear-step
33273 conditional-expression */
33276 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
33278 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33280 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33282 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33284 cp_parser_error (parser
, "expected identifier");
33285 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33286 return error_mark_node
;
33289 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33290 parser
->colon_corrects_to_scope_p
= false;
33293 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33294 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33296 cp_parser_error (parser
, "expected variable-name");
33297 clauses
= error_mark_node
;
33301 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
33303 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
33305 if (decl
== error_mark_node
)
33307 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
33309 clauses
= error_mark_node
;
33313 tree e
= NULL_TREE
;
33314 tree step_size
= integer_one_node
;
33316 /* If present, parse the linear step. Otherwise, assume the default
33318 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
33320 cp_lexer_consume_token (parser
->lexer
);
33322 e
= cp_parser_assignment_expression (parser
);
33323 e
= maybe_constant_value (e
);
33325 if (e
== error_mark_node
)
33327 /* If an error has occurred, then the whole pragma is
33328 considered ill-formed. Thus, no reason to keep
33330 clauses
= error_mark_node
;
33333 else if (type_dependent_expression_p (e
)
33334 || value_dependent_expression_p (e
)
33336 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
33337 && (TREE_CONSTANT (e
)
33341 cp_parser_error (parser
,
33342 "step size must be an integer constant "
33343 "expression or an integer variable");
33346 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33347 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
33348 OMP_CLAUSE_DECL (l
) = decl
;
33349 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
33350 OMP_CLAUSE_CHAIN (l
) = clauses
;
33353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33354 cp_lexer_consume_token (parser
->lexer
);
33355 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33359 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33360 "expected %<,%> or %<)%> after %qE", decl
);
33361 clauses
= error_mark_node
;
33365 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33366 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33370 /* Returns the name of the next clause. If the clause is not
33371 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33372 token is not consumed. Otherwise, the appropriate enum from the
33373 pragma_simd_clause is returned and the token is consumed. */
33375 static pragma_omp_clause
33376 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
33378 pragma_omp_clause clause_type
;
33379 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33381 if (token
->keyword
== RID_PRIVATE
)
33382 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
33383 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
33384 return PRAGMA_CILK_CLAUSE_NONE
;
33385 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
33386 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
33387 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
33388 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
33389 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
33390 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
33391 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
33392 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
33393 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
33394 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
33396 return PRAGMA_CILK_CLAUSE_NONE
;
33398 cp_lexer_consume_token (parser
->lexer
);
33399 return clause_type
;
33402 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33405 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
33407 tree clauses
= NULL_TREE
;
33409 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
33410 && clauses
!= error_mark_node
)
33412 pragma_omp_clause c_kind
;
33413 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
33414 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
33415 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
33416 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
33417 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
33418 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
33419 /* Use the OpenMP 4.0 equivalent function. */
33420 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
33421 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
33422 /* Use the OpenMP 4.0 equivalent function. */
33423 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33425 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
33426 /* Use the OMP 4.0 equivalent function. */
33427 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33429 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
33430 /* Use the OMP 4.0 equivalent function. */
33431 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33434 clauses
= error_mark_node
;
33435 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
33440 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
33442 if (clauses
== error_mark_node
)
33443 return error_mark_node
;
33445 return c_finish_cilk_clauses (clauses
);
33448 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33451 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
33453 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
33455 if (clauses
== error_mark_node
)
33458 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
33460 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33461 "for statement expected");
33465 tree sb
= begin_omp_structured_block ();
33466 int save
= cp_parser_begin_omp_structured_block (parser
);
33467 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
33469 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
33470 cp_parser_end_omp_structured_block (parser
, save
);
33471 add_stmt (finish_omp_structured_block (sb
));
33474 /* Main entry-point for parsing Cilk Plus _Cilk_for
33475 loops. The return value is error_mark_node
33476 when errors happen and CILK_FOR tree on success. */
33479 cp_parser_cilk_for (cp_parser
*parser
, tree grain
)
33481 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
33482 gcc_unreachable ();
33484 tree sb
= begin_omp_structured_block ();
33485 int save
= cp_parser_begin_omp_structured_block (parser
);
33487 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
33488 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
33489 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
33490 clauses
= finish_omp_clauses (clauses
);
33492 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
);
33494 cpp_validate_cilk_plus_loop (ret
);
33496 ret
= error_mark_node
;
33498 cp_parser_end_omp_structured_block (parser
, save
);
33499 add_stmt (finish_omp_structured_block (sb
));
33503 /* Create an identifier for a generic parameter type (a synthesized
33504 template parameter implied by `auto' or a concept identifier). */
33506 static GTY(()) int generic_parm_count
;
33508 make_generic_type_name ()
33511 sprintf (buf
, "auto:%d", ++generic_parm_count
);
33512 return get_identifier (buf
);
33515 /* Predicate that behaves as is_auto_or_concept but matches the parent
33516 node of the generic type rather than the generic type itself. This
33517 allows for type transformation in add_implicit_template_parms. */
33520 tree_type_is_auto_or_concept (const_tree t
)
33522 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
33525 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33526 (creating a new template parameter list if necessary). Returns the newly
33527 created template type parm. */
33530 synthesize_implicit_template_parm (cp_parser
*parser
)
33532 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
33534 /* We are either continuing a function template that already contains implicit
33535 template parameters, creating a new fully-implicit function template, or
33536 extending an existing explicit function template with implicit template
33539 cp_binding_level
*const entry_scope
= current_binding_level
;
33541 bool become_template
= false;
33542 cp_binding_level
*parent_scope
= 0;
33544 if (parser
->implicit_template_scope
)
33546 gcc_assert (parser
->implicit_template_parms
);
33548 current_binding_level
= parser
->implicit_template_scope
;
33552 /* Roll back to the existing template parameter scope (in the case of
33553 extending an explicit function template) or introduce a new template
33554 parameter scope ahead of the function parameter scope (or class scope
33555 in the case of out-of-line member definitions). The function scope is
33556 added back after template parameter synthesis below. */
33558 cp_binding_level
*scope
= entry_scope
;
33560 while (scope
->kind
== sk_function_parms
)
33562 parent_scope
= scope
;
33563 scope
= scope
->level_chain
;
33565 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
33567 /* If not defining a class, then any class scope is a scope level in
33568 an out-of-line member definition. In this case simply wind back
33569 beyond the first such scope to inject the template parameter list.
33570 Otherwise wind back to the class being defined. The latter can
33571 occur in class member friend declarations such as:
33577 friend void A::foo (auto);
33580 The template parameter list synthesized for the friend declaration
33581 must be injected in the scope of 'B'. This can also occur in
33582 erroneous cases such as:
33588 void B::foo (auto) {}
33591 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33592 but, nevertheless, the template parameter list synthesized for the
33593 declarator should be injected into the scope of 'A' as if the
33594 ill-formed template was specified explicitly. */
33596 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
33598 parent_scope
= scope
;
33599 scope
= scope
->level_chain
;
33603 current_binding_level
= scope
;
33605 if (scope
->kind
!= sk_template_parms
33606 || !function_being_declared_is_template_p (parser
))
33608 /* Introduce a new template parameter list for implicit template
33611 become_template
= true;
33613 parser
->implicit_template_scope
33614 = begin_scope (sk_template_parms
, NULL
);
33616 ++processing_template_decl
;
33618 parser
->fully_implicit_function_template_p
= true;
33619 ++parser
->num_template_parameter_lists
;
33623 /* Synthesize implicit template parameters at the end of the explicit
33624 template parameter list. */
33626 gcc_assert (current_template_parms
);
33628 parser
->implicit_template_scope
= scope
;
33630 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
33631 parser
->implicit_template_parms
33632 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
33636 /* Synthesize a new template parameter and track the current template
33637 parameter chain with implicit_template_parms. */
33639 tree synth_id
= make_generic_type_name ();
33640 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
33643 = process_template_parm (parser
->implicit_template_parms
,
33645 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
33646 /*non_type=*/false,
33647 /*param_pack=*/false);
33650 if (parser
->implicit_template_parms
)
33651 parser
->implicit_template_parms
33652 = TREE_CHAIN (parser
->implicit_template_parms
);
33654 parser
->implicit_template_parms
= new_parm
;
33656 tree new_type
= TREE_TYPE (getdecls ());
33658 /* If creating a fully implicit function template, start the new implicit
33659 template parameter list with this synthesized type, otherwise grow the
33660 current template parameter list. */
33662 if (become_template
)
33664 parent_scope
->level_chain
= current_binding_level
;
33666 tree new_parms
= make_tree_vec (1);
33667 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
33668 current_template_parms
= tree_cons (size_int (processing_template_decl
),
33669 new_parms
, current_template_parms
);
33673 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
33674 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
33675 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
33676 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
33679 current_binding_level
= entry_scope
;
33684 /* Finish the declaration of a fully implicit function template. Such a
33685 template has no explicit template parameter list so has not been through the
33686 normal template head and tail processing. synthesize_implicit_template_parm
33687 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33688 provided if the declaration is a class member such that its template
33689 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33690 form is returned. Otherwise NULL_TREE is returned. */
33693 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
33695 gcc_assert (parser
->fully_implicit_function_template_p
);
33697 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
33698 && DECL_VIRTUAL_P (member_decl_opt
))
33700 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
33701 "implicit templates may not be %<virtual%>");
33702 DECL_VIRTUAL_P (member_decl_opt
) = false;
33705 if (member_decl_opt
)
33706 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
33707 end_template_decl ();
33709 parser
->fully_implicit_function_template_p
= false;
33710 --parser
->num_template_parameter_lists
;
33712 return member_decl_opt
;
33715 #include "gt-cp-parser.h"