2 Copyright (C) 2000-2014 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"
28 #include "print-tree.h"
29 #include "stringpool.h"
31 #include "trans-mem.h"
34 #include "c-family/c-pragma.h"
37 #include "diagnostic-core.h"
41 #include "plugin-api.h"
46 #include "hard-reg-set.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
54 #include "tree-pretty-print.h"
56 #include "type-utils.h"
62 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
63 and c-lex.c) and the C++ parser. */
65 static cp_token eof_token
=
67 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
70 /* The various kinds of non integral constant we encounter. */
71 typedef enum non_integral_constant
{
73 /* floating-point literal */
77 /* %<__FUNCTION__%> */
79 /* %<__PRETTY_FUNCTION__%> */
87 /* %<typeid%> operator */
89 /* non-constant compound literals */
97 /* an array reference */
103 /* the address of a label */
117 /* calls to overloaded operators */
121 /* a comma operator */
123 /* a call to a constructor */
125 /* a transaction expression */
127 } non_integral_constant
;
129 /* The various kinds of errors about name-lookup failing. */
130 typedef enum name_lookup_error
{
135 /* is not a class or namespace */
137 /* is not a class, namespace, or enumeration */
141 /* The various kinds of required token */
142 typedef enum required_token
{
144 RT_SEMICOLON
, /* ';' */
145 RT_OPEN_PAREN
, /* '(' */
146 RT_CLOSE_BRACE
, /* '}' */
147 RT_OPEN_BRACE
, /* '{' */
148 RT_CLOSE_SQUARE
, /* ']' */
149 RT_OPEN_SQUARE
, /* '[' */
153 RT_GREATER
, /* '>' */
155 RT_ELLIPSIS
, /* '...' */
159 RT_COLON_SCOPE
, /* ':' or '::' */
160 RT_CLOSE_PAREN
, /* ')' */
161 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
162 RT_PRAGMA_EOL
, /* end of line */
163 RT_NAME
, /* identifier */
165 /* The type is CPP_KEYWORD */
167 RT_DELETE
, /* delete */
168 RT_RETURN
, /* return */
169 RT_WHILE
, /* while */
170 RT_EXTERN
, /* extern */
171 RT_STATIC_ASSERT
, /* static_assert */
172 RT_DECLTYPE
, /* decltype */
173 RT_OPERATOR
, /* operator */
174 RT_CLASS
, /* class */
175 RT_TEMPLATE
, /* template */
176 RT_NAMESPACE
, /* namespace */
177 RT_USING
, /* using */
180 RT_CATCH
, /* catch */
181 RT_THROW
, /* throw */
182 RT_LABEL
, /* __label__ */
183 RT_AT_TRY
, /* @try */
184 RT_AT_SYNCHRONIZED
, /* @synchronized */
185 RT_AT_THROW
, /* @throw */
187 RT_SELECT
, /* selection-statement */
188 RT_INTERATION
, /* iteration-statement */
189 RT_JUMP
, /* jump-statement */
190 RT_CLASS_KEY
, /* class-key */
191 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
192 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
193 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
194 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
199 static cp_lexer
*cp_lexer_new_main
201 static cp_lexer
*cp_lexer_new_from_tokens
202 (cp_token_cache
*tokens
);
203 static void cp_lexer_destroy
205 static int cp_lexer_saving_tokens
207 static cp_token
*cp_lexer_token_at
208 (cp_lexer
*, cp_token_position
);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer
*, cp_token
*);
211 static inline cp_token
*cp_lexer_peek_token
213 static cp_token
*cp_lexer_peek_nth_token
214 (cp_lexer
*, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer
*, enum cpp_ttype
);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer
*, enum cpp_ttype
);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer
*, enum rid
);
221 static cp_token
*cp_lexer_consume_token
223 static void cp_lexer_purge_token
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer
*, cp_token_position
);
227 static void cp_lexer_save_tokens
229 static void cp_lexer_commit_tokens
231 static void cp_lexer_rollback_tokens
233 static void cp_lexer_print_token
234 (FILE *, cp_token
*);
235 static inline bool cp_lexer_debugging_p
237 static void cp_lexer_start_debugging
238 (cp_lexer
*) ATTRIBUTE_UNUSED
;
239 static void cp_lexer_stop_debugging
240 (cp_lexer
*) ATTRIBUTE_UNUSED
;
242 static cp_token_cache
*cp_token_cache_new
243 (cp_token
*, cp_token
*);
245 static void cp_parser_initial_pragma
248 static tree cp_literal_operator_id
251 static void cp_parser_cilk_simd
252 (cp_parser
*, cp_token
*);
253 static tree cp_parser_cilk_for
255 static bool cp_parser_omp_declare_reduction_exprs
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser
*, tree
, bool);
260 /* Manifest constants. */
261 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
262 #define CP_SAVED_TOKEN_STACK 5
266 /* The stream to which debugging output should be written. */
267 static FILE *cp_lexer_debug_stream
;
269 /* Nonzero if we are parsing an unevaluated operand: an operand to
270 sizeof, typeof, or alignof. */
271 int cp_unevaluated_operand
;
273 /* Dump up to NUM tokens in BUFFER to FILE starting with token
274 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
275 first token in BUFFER. If NUM is 0, dump all the tokens. If
276 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
277 highlighted by surrounding it in [[ ]]. */
280 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
281 cp_token
*start_token
, unsigned num
,
282 cp_token
*curr_token
)
284 unsigned i
, nprinted
;
288 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
294 num
= buffer
->length ();
296 if (start_token
== NULL
)
297 start_token
= buffer
->address ();
299 if (start_token
> buffer
->address ())
301 cp_lexer_print_token (file
, &(*buffer
)[0]);
302 fprintf (file
, " ... ");
307 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
309 if (token
== start_token
)
316 if (token
== curr_token
)
317 fprintf (file
, "[[");
319 cp_lexer_print_token (file
, token
);
321 if (token
== curr_token
)
322 fprintf (file
, "]]");
328 case CPP_CLOSE_BRACE
:
338 if (i
== num
&& i
< buffer
->length ())
340 fprintf (file
, " ... ");
341 cp_lexer_print_token (file
, &buffer
->last ());
344 fprintf (file
, "\n");
348 /* Dump all tokens in BUFFER to stderr. */
351 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
353 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
357 debug (vec
<cp_token
, va_gc
> &ref
)
359 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
363 debug (vec
<cp_token
, va_gc
> *ptr
)
368 fprintf (stderr
, "<nil>\n");
372 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
373 description for T. */
376 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
380 fprintf (file
, "%s: ", desc
);
381 print_node_brief (file
, "", t
, 0);
386 /* Dump parser context C to FILE. */
389 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
391 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
392 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
393 print_node_brief (file
, "", c
->object_type
, 0);
394 fprintf (file
, "}\n");
398 /* Print the stack of parsing contexts to FILE starting with FIRST. */
401 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
404 cp_parser_context
*c
;
406 fprintf (file
, "Parsing context stack:\n");
407 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
409 fprintf (file
, "\t#%u: ", i
);
410 cp_debug_print_context (file
, c
);
415 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
418 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
421 fprintf (file
, "%s: true\n", desc
);
425 /* Print an unparsed function entry UF to FILE. */
428 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
431 cp_default_arg_entry
*default_arg_fn
;
434 fprintf (file
, "\tFunctions with default args:\n");
436 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
439 fprintf (file
, "\t\tClass type: ");
440 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
441 fprintf (file
, "\t\tDeclaration: ");
442 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
443 fprintf (file
, "\n");
446 fprintf (file
, "\n\tFunctions with definitions that require "
447 "post-processing\n\t\t");
448 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
450 print_node_brief (file
, "", fn
, 0);
453 fprintf (file
, "\n");
455 fprintf (file
, "\n\tNon-static data members with initializers that require "
456 "post-processing\n\t\t");
457 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
459 print_node_brief (file
, "", fn
, 0);
462 fprintf (file
, "\n");
466 /* Print the stack of unparsed member functions S to FILE. */
469 cp_debug_print_unparsed_queues (FILE *file
,
470 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
473 cp_unparsed_functions_entry
*uf
;
475 fprintf (file
, "Unparsed functions\n");
476 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
478 fprintf (file
, "#%u:\n", i
);
479 cp_debug_print_unparsed_function (file
, uf
);
484 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
485 the given PARSER. If FILE is NULL, the output is printed on stderr. */
488 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
490 cp_token
*next_token
, *first_token
, *start_token
;
495 next_token
= parser
->lexer
->next_token
;
496 first_token
= parser
->lexer
->buffer
->address ();
497 start_token
= (next_token
> first_token
+ window_size
/ 2)
498 ? next_token
- window_size
/ 2
500 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
505 /* Dump debugging information for the given PARSER. If FILE is NULL,
506 the output is printed on stderr. */
509 cp_debug_parser (FILE *file
, cp_parser
*parser
)
511 const size_t window_size
= 20;
513 expanded_location eloc
;
518 fprintf (file
, "Parser state\n\n");
519 fprintf (file
, "Number of tokens: %u\n",
520 vec_safe_length (parser
->lexer
->buffer
));
521 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
522 cp_debug_print_tree_if_set (file
, "Object scope",
523 parser
->object_scope
);
524 cp_debug_print_tree_if_set (file
, "Qualifying scope",
525 parser
->qualifying_scope
);
526 cp_debug_print_context_stack (file
, parser
->context
);
527 cp_debug_print_flag (file
, "Allow GNU extensions",
528 parser
->allow_gnu_extensions_p
);
529 cp_debug_print_flag (file
, "'>' token is greater-than",
530 parser
->greater_than_is_operator_p
);
531 cp_debug_print_flag (file
, "Default args allowed in current "
532 "parameter list", parser
->default_arg_ok_p
);
533 cp_debug_print_flag (file
, "Parsing integral constant-expression",
534 parser
->integral_constant_expression_p
);
535 cp_debug_print_flag (file
, "Allow non-constant expression in current "
536 "constant-expression",
537 parser
->allow_non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Seen non-constant expression",
539 parser
->non_integral_constant_expression_p
);
540 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
542 parser
->local_variables_forbidden_p
);
543 cp_debug_print_flag (file
, "In unbraced linkage specification",
544 parser
->in_unbraced_linkage_specification_p
);
545 cp_debug_print_flag (file
, "Parsing a declarator",
546 parser
->in_declarator_p
);
547 cp_debug_print_flag (file
, "In template argument list",
548 parser
->in_template_argument_list_p
);
549 cp_debug_print_flag (file
, "Parsing an iteration statement",
550 parser
->in_statement
& IN_ITERATION_STMT
);
551 cp_debug_print_flag (file
, "Parsing a switch statement",
552 parser
->in_statement
& IN_SWITCH_STMT
);
553 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
554 parser
->in_statement
& IN_OMP_BLOCK
);
555 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
556 parser
->in_statement
& IN_CILK_SIMD_FOR
);
557 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
558 parser
->in_statement
& IN_OMP_FOR
);
559 cp_debug_print_flag (file
, "Parsing an if statement",
560 parser
->in_statement
& IN_IF_STMT
);
561 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
562 "context", parser
->in_type_id_in_expr_p
);
563 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
564 parser
->implicit_extern_c
);
565 cp_debug_print_flag (file
, "String expressions should be translated "
566 "to execution character set",
567 parser
->translate_strings_p
);
568 cp_debug_print_flag (file
, "Parsing function body outside of a "
569 "local class", parser
->in_function_body
);
570 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
571 parser
->colon_corrects_to_scope_p
);
572 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
573 parser
->colon_doesnt_start_class_def_p
);
574 if (parser
->type_definition_forbidden_message
)
575 fprintf (file
, "Error message for forbidden type definitions: %s\n",
576 parser
->type_definition_forbidden_message
);
577 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
578 fprintf (file
, "Number of class definitions in progress: %u\n",
579 parser
->num_classes_being_defined
);
580 fprintf (file
, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser
->num_template_parameter_lists
);
582 cp_debug_parser_tokens (file
, parser
, window_size
);
583 token
= parser
->lexer
->next_token
;
584 fprintf (file
, "Next token to parse:\n");
585 fprintf (file
, "\tToken: ");
586 cp_lexer_print_token (file
, token
);
587 eloc
= expand_location (token
->location
);
588 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
589 fprintf (file
, "\tLine: %d\n", eloc
.line
);
590 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
594 debug (cp_parser
&ref
)
596 cp_debug_parser (stderr
, &ref
);
600 debug (cp_parser
*ptr
)
605 fprintf (stderr
, "<nil>\n");
608 /* Allocate memory for a new lexer object and return it. */
611 cp_lexer_alloc (void)
615 c_common_no_more_pch ();
617 /* Allocate the memory. */
618 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
620 /* Initially we are not debugging. */
621 lexer
->debugging_p
= false;
623 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
625 /* Create the buffer. */
626 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 cp_lexer_new_main (void)
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token
);
646 lexer
= cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 lexer
->buffer
->quick_push (token
);
651 /* Get the remaining tokens from the preprocessor. */
652 while (token
.type
!= CPP_EOF
)
654 cp_lexer_get_preprocessor_token (lexer
, &token
);
655 vec_safe_push (lexer
->buffer
, token
);
658 lexer
->last_token
= lexer
->buffer
->address ()
659 + lexer
->buffer
->length ()
661 lexer
->next_token
= lexer
->buffer
->length ()
662 ? lexer
->buffer
->address ()
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
669 gcc_assert (!lexer
->next_token
->purged_p
);
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
677 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
679 cp_token
*first
= cache
->first
;
680 cp_token
*last
= cache
->last
;
681 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
683 /* We do not own the buffer. */
684 lexer
->buffer
= NULL
;
685 lexer
->next_token
= first
== last
? &eof_token
: first
;
686 lexer
->last_token
= last
;
688 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
690 /* Initially we are not debugging. */
691 lexer
->debugging_p
= false;
693 gcc_assert (!lexer
->next_token
->purged_p
);
697 /* Frees all resources associated with LEXER. */
700 cp_lexer_destroy (cp_lexer
*lexer
)
702 vec_free (lexer
->buffer
);
703 lexer
->saved_tokens
.release ();
707 /* Returns nonzero if debugging information should be output. */
710 cp_lexer_debugging_p (cp_lexer
*lexer
)
712 return lexer
->debugging_p
;
716 static inline cp_token_position
717 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
719 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
721 return lexer
->next_token
- previous_p
;
724 static inline cp_token
*
725 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
731 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
733 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
736 static inline cp_token_position
737 cp_lexer_previous_token_position (cp_lexer
*lexer
)
739 if (lexer
->next_token
== &eof_token
)
740 return lexer
->last_token
- 1;
742 return cp_lexer_token_position (lexer
, true);
745 static inline cp_token
*
746 cp_lexer_previous_token (cp_lexer
*lexer
)
748 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
750 return cp_lexer_token_at (lexer
, tp
);
753 /* nonzero if we are presently saving tokens. */
756 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
758 return lexer
->saved_tokens
.length () != 0;
761 /* Store the next token from the preprocessor in *TOKEN. Return true
762 if we reach EOF. If LEXER is NULL, assume we are handling an
763 initial #pragma pch_preprocess, and thus want the lexer to return
764 processed strings. */
767 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
769 static int is_extern_c
= 0;
771 /* Get a new token from the preprocessor. */
773 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
774 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
775 token
->keyword
= RID_MAX
;
776 token
->pragma_kind
= PRAGMA_NONE
;
777 token
->purged_p
= false;
778 token
->error_reported
= false;
780 /* On some systems, some header files are surrounded by an
781 implicit extern "C" block. Set a flag in the token if it
782 comes from such a header. */
783 is_extern_c
+= pending_lang_change
;
784 pending_lang_change
= 0;
785 token
->implicit_extern_c
= is_extern_c
> 0;
787 /* Check to see if this token is a keyword. */
788 if (token
->type
== CPP_NAME
)
790 if (C_IS_RESERVED_WORD (token
->u
.value
))
792 /* Mark this token as a keyword. */
793 token
->type
= CPP_KEYWORD
;
794 /* Record which keyword. */
795 token
->keyword
= C_RID_CODE (token
->u
.value
);
799 if (warn_cxx0x_compat
800 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
801 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
803 /* Warn about the C++0x keyword (but still treat it as
805 warning (OPT_Wc__0x_compat
,
806 "identifier %qE is a keyword in C++11",
809 /* Clear out the C_RID_CODE so we don't warn about this
810 particular identifier-turned-keyword again. */
811 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
814 token
->keyword
= RID_MAX
;
817 else if (token
->type
== CPP_AT_NAME
)
819 /* This only happens in Objective-C++; it must be a keyword. */
820 token
->type
= CPP_KEYWORD
;
821 switch (C_RID_CODE (token
->u
.value
))
823 /* Replace 'class' with '@class', 'private' with '@private',
824 etc. This prevents confusion with the C++ keyword
825 'class', and makes the tokens consistent with other
826 Objective-C 'AT' keywords. For example '@class' is
827 reported as RID_AT_CLASS which is consistent with
828 '@synchronized', which is reported as
831 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
832 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
833 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
834 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
835 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
836 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
837 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
838 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
841 else if (token
->type
== CPP_PRAGMA
)
843 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
844 token
->pragma_kind
= ((enum pragma_kind
)
845 TREE_INT_CST_LOW (token
->u
.value
));
846 token
->u
.value
= NULL_TREE
;
850 /* Update the globals input_location and the input file stack from TOKEN. */
852 cp_lexer_set_source_position_from_token (cp_token
*token
)
854 if (token
->type
!= CPP_EOF
)
856 input_location
= token
->location
;
860 /* Update the globals input_location and the input file stack from LEXER. */
862 cp_lexer_set_source_position (cp_lexer
*lexer
)
864 cp_token
*token
= cp_lexer_peek_token (lexer
);
865 cp_lexer_set_source_position_from_token (token
);
868 /* Return a pointer to the next token in the token stream, but do not
871 static inline cp_token
*
872 cp_lexer_peek_token (cp_lexer
*lexer
)
874 if (cp_lexer_debugging_p (lexer
))
876 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
877 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
878 putc ('\n', cp_lexer_debug_stream
);
880 return lexer
->next_token
;
883 /* Return true if the next token has the indicated TYPE. */
886 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
888 return cp_lexer_peek_token (lexer
)->type
== type
;
891 /* Return true if the next token does not have the indicated TYPE. */
894 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
896 return !cp_lexer_next_token_is (lexer
, type
);
899 /* Return true if the next token is the indicated KEYWORD. */
902 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
904 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
908 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
910 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
914 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
919 /* Return true if the next token is not the indicated KEYWORD. */
922 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
924 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
927 /* Return true if the next token is a keyword for a decl-specifier. */
930 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
934 token
= cp_lexer_peek_token (lexer
);
935 switch (token
->keyword
)
937 /* auto specifier: storage-class-specifier in C++,
938 simple-type-specifier in C++0x. */
940 /* Storage classes. */
946 /* Elaborated type specifiers. */
952 /* Simple type specifiers. */
966 /* GNU extensions. */
969 /* C++0x extensions. */
971 case RID_UNDERLYING_TYPE
:
975 if (token
->keyword
>= RID_FIRST_INT_N
976 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
977 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
983 /* Returns TRUE iff the token T begins a decltype type. */
986 token_is_decltype (cp_token
*t
)
988 return (t
->keyword
== RID_DECLTYPE
989 || t
->type
== CPP_DECLTYPE
);
992 /* Returns TRUE iff the next token begins a decltype type. */
995 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
997 cp_token
*t
= cp_lexer_peek_token (lexer
);
998 return token_is_decltype (t
);
1001 /* Return a pointer to the Nth token in the token stream. If N is 1,
1002 then this is precisely equivalent to cp_lexer_peek_token (except
1003 that it is not inline). One would like to disallow that case, but
1004 there is one case (cp_parser_nth_token_starts_template_id) where
1005 the caller passes a variable for N and it might be 1. */
1008 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1012 /* N is 1-based, not zero-based. */
1015 if (cp_lexer_debugging_p (lexer
))
1016 fprintf (cp_lexer_debug_stream
,
1017 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1020 token
= lexer
->next_token
;
1021 gcc_assert (!n
|| token
!= &eof_token
);
1025 if (token
== lexer
->last_token
)
1031 if (!token
->purged_p
)
1035 if (cp_lexer_debugging_p (lexer
))
1037 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1038 putc ('\n', cp_lexer_debug_stream
);
1044 /* Return the next token, and advance the lexer's next_token pointer
1045 to point to the next non-purged token. */
1048 cp_lexer_consume_token (cp_lexer
* lexer
)
1050 cp_token
*token
= lexer
->next_token
;
1052 gcc_assert (token
!= &eof_token
);
1053 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1057 lexer
->next_token
++;
1058 if (lexer
->next_token
== lexer
->last_token
)
1060 lexer
->next_token
= &eof_token
;
1065 while (lexer
->next_token
->purged_p
);
1067 cp_lexer_set_source_position_from_token (token
);
1069 /* Provide debugging output. */
1070 if (cp_lexer_debugging_p (lexer
))
1072 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1073 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1074 putc ('\n', cp_lexer_debug_stream
);
1080 /* Permanently remove the next token from the token stream, and
1081 advance the next_token pointer to refer to the next non-purged
1085 cp_lexer_purge_token (cp_lexer
*lexer
)
1087 cp_token
*tok
= lexer
->next_token
;
1089 gcc_assert (tok
!= &eof_token
);
1090 tok
->purged_p
= true;
1091 tok
->location
= UNKNOWN_LOCATION
;
1092 tok
->u
.value
= NULL_TREE
;
1093 tok
->keyword
= RID_MAX
;
1098 if (tok
== lexer
->last_token
)
1104 while (tok
->purged_p
);
1105 lexer
->next_token
= tok
;
1108 /* Permanently remove all tokens after TOK, up to, but not
1109 including, the token that will be returned next by
1110 cp_lexer_peek_token. */
1113 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1115 cp_token
*peek
= lexer
->next_token
;
1117 if (peek
== &eof_token
)
1118 peek
= lexer
->last_token
;
1120 gcc_assert (tok
< peek
);
1122 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1124 tok
->purged_p
= true;
1125 tok
->location
= UNKNOWN_LOCATION
;
1126 tok
->u
.value
= NULL_TREE
;
1127 tok
->keyword
= RID_MAX
;
1131 /* Begin saving tokens. All tokens consumed after this point will be
1135 cp_lexer_save_tokens (cp_lexer
* lexer
)
1137 /* Provide debugging output. */
1138 if (cp_lexer_debugging_p (lexer
))
1139 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1141 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1144 /* Commit to the portion of the token stream most recently saved. */
1147 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer
))
1151 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1153 lexer
->saved_tokens
.pop ();
1156 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1157 to the token stream. Stop saving tokens. */
1160 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1162 /* Provide debugging output. */
1163 if (cp_lexer_debugging_p (lexer
))
1164 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1166 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1169 /* RAII wrapper around the above functions, with sanity checking. Creating
1170 a variable saves tokens, which are committed when the variable is
1171 destroyed unless they are explicitly rolled back by calling the rollback
1174 struct saved_token_sentinel
1179 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1181 len
= lexer
->saved_tokens
.length ();
1182 cp_lexer_save_tokens (lexer
);
1186 cp_lexer_rollback_tokens (lexer
);
1189 ~saved_token_sentinel()
1192 cp_lexer_commit_tokens (lexer
);
1193 gcc_assert (lexer
->saved_tokens
.length () == len
);
1197 /* Print a representation of the TOKEN on the STREAM. */
1200 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1202 /* We don't use cpp_type2name here because the parser defines
1203 a few tokens of its own. */
1204 static const char *const token_names
[] = {
1205 /* cpplib-defined token types */
1206 #define OP(e, s) #e,
1207 #define TK(e, s) #e,
1211 /* C++ parser token types - see "Manifest constants", above. */
1214 "NESTED_NAME_SPECIFIER",
1217 /* For some tokens, print the associated data. */
1218 switch (token
->type
)
1221 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1222 For example, `struct' is mapped to an INTEGER_CST. */
1223 if (!identifier_p (token
->u
.value
))
1225 /* else fall through */
1227 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1234 case CPP_UTF8STRING
:
1235 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1239 print_generic_expr (stream
, token
->u
.value
, 0);
1243 /* If we have a name for the token, print it out. Otherwise, we
1244 simply give the numeric code. */
1245 if (token
->type
< ARRAY_SIZE(token_names
))
1246 fputs (token_names
[token
->type
], stream
);
1248 fprintf (stream
, "[%d]", token
->type
);
1254 debug (cp_token
&ref
)
1256 cp_lexer_print_token (stderr
, &ref
);
1257 fprintf (stderr
, "\n");
1261 debug (cp_token
*ptr
)
1266 fprintf (stderr
, "<nil>\n");
1270 /* Start emitting debugging information. */
1273 cp_lexer_start_debugging (cp_lexer
* lexer
)
1275 lexer
->debugging_p
= true;
1276 cp_lexer_debug_stream
= stderr
;
1279 /* Stop emitting debugging information. */
1282 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1284 lexer
->debugging_p
= false;
1285 cp_lexer_debug_stream
= NULL
;
1288 /* Create a new cp_token_cache, representing a range of tokens. */
1290 static cp_token_cache
*
1291 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1293 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1294 cache
->first
= first
;
1299 /* Diagnose if #pragma omp declare simd isn't followed immediately
1300 by function declaration or definition. */
1303 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1305 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1307 error ("%<#pragma omp declare simd%> not immediately followed by "
1308 "function declaration or definition");
1309 parser
->omp_declare_simd
= NULL
;
1313 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1314 and put that into "omp declare simd" attribute. */
1317 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1319 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1321 if (fndecl
== error_mark_node
)
1323 parser
->omp_declare_simd
= NULL
;
1326 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1328 cp_ensure_no_omp_declare_simd (parser
);
1334 /* Decl-specifiers. */
1336 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1339 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1341 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1346 /* Nothing other than the parser should be creating declarators;
1347 declarators are a semi-syntactic representation of C++ entities.
1348 Other parts of the front end that need to create entities (like
1349 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1351 static cp_declarator
*make_call_declarator
1352 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1353 static cp_declarator
*make_array_declarator
1354 (cp_declarator
*, tree
);
1355 static cp_declarator
*make_pointer_declarator
1356 (cp_cv_quals
, cp_declarator
*, tree
);
1357 static cp_declarator
*make_reference_declarator
1358 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1359 static cp_parameter_declarator
*make_parameter_declarator
1360 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1361 static cp_declarator
*make_ptrmem_declarator
1362 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1364 /* An erroneous declarator. */
1365 static cp_declarator
*cp_error_declarator
;
1367 /* The obstack on which declarators and related data structures are
1369 static struct obstack declarator_obstack
;
1371 /* Alloc BYTES from the declarator memory pool. */
1373 static inline void *
1374 alloc_declarator (size_t bytes
)
1376 return obstack_alloc (&declarator_obstack
, bytes
);
1379 /* Allocate a declarator of the indicated KIND. Clear fields that are
1380 common to all declarators. */
1382 static cp_declarator
*
1383 make_declarator (cp_declarator_kind kind
)
1385 cp_declarator
*declarator
;
1387 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1388 declarator
->kind
= kind
;
1389 declarator
->attributes
= NULL_TREE
;
1390 declarator
->std_attributes
= NULL_TREE
;
1391 declarator
->declarator
= NULL
;
1392 declarator
->parameter_pack_p
= false;
1393 declarator
->id_loc
= UNKNOWN_LOCATION
;
1398 /* Make a declarator for a generalized identifier. If
1399 QUALIFYING_SCOPE is non-NULL, the identifier is
1400 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1401 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1404 static cp_declarator
*
1405 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1406 special_function_kind sfk
)
1408 cp_declarator
*declarator
;
1410 /* It is valid to write:
1412 class C { void f(); };
1416 The standard is not clear about whether `typedef const C D' is
1417 legal; as of 2002-09-15 the committee is considering that
1418 question. EDG 3.0 allows that syntax. Therefore, we do as
1420 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1421 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1423 gcc_assert (identifier_p (unqualified_name
)
1424 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1425 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1427 declarator
= make_declarator (cdk_id
);
1428 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1429 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1430 declarator
->u
.id
.sfk
= sfk
;
1435 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1436 of modifiers such as const or volatile to apply to the pointer
1437 type, represented as identifiers. ATTRIBUTES represent the attributes that
1438 appertain to the pointer or reference. */
1441 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1444 cp_declarator
*declarator
;
1446 declarator
= make_declarator (cdk_pointer
);
1447 declarator
->declarator
= target
;
1448 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1449 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1452 declarator
->id_loc
= target
->id_loc
;
1453 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1454 target
->parameter_pack_p
= false;
1457 declarator
->parameter_pack_p
= false;
1459 declarator
->std_attributes
= attributes
;
1464 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1465 represent the attributes that appertain to the pointer or
1469 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1470 bool rvalue_ref
, tree attributes
)
1472 cp_declarator
*declarator
;
1474 declarator
= make_declarator (cdk_reference
);
1475 declarator
->declarator
= target
;
1476 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1477 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1480 declarator
->id_loc
= target
->id_loc
;
1481 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1482 target
->parameter_pack_p
= false;
1485 declarator
->parameter_pack_p
= false;
1487 declarator
->std_attributes
= attributes
;
1492 /* Like make_pointer_declarator -- but for a pointer to a non-static
1493 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1494 appertain to the pointer or reference. */
1497 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1498 cp_declarator
*pointee
,
1501 cp_declarator
*declarator
;
1503 declarator
= make_declarator (cdk_ptrmem
);
1504 declarator
->declarator
= pointee
;
1505 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1506 declarator
->u
.pointer
.class_type
= class_type
;
1510 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1511 pointee
->parameter_pack_p
= false;
1514 declarator
->parameter_pack_p
= false;
1516 declarator
->std_attributes
= attributes
;
1521 /* Make a declarator for the function given by TARGET, with the
1522 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1523 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1524 indicates what exceptions can be thrown. */
1527 make_call_declarator (cp_declarator
*target
,
1529 cp_cv_quals cv_qualifiers
,
1530 cp_virt_specifiers virt_specifiers
,
1531 cp_ref_qualifier ref_qualifier
,
1532 tree exception_specification
,
1533 tree late_return_type
)
1535 cp_declarator
*declarator
;
1537 declarator
= make_declarator (cdk_function
);
1538 declarator
->declarator
= target
;
1539 declarator
->u
.function
.parameters
= parms
;
1540 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1541 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1542 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1543 declarator
->u
.function
.exception_specification
= exception_specification
;
1544 declarator
->u
.function
.late_return_type
= late_return_type
;
1547 declarator
->id_loc
= target
->id_loc
;
1548 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1549 target
->parameter_pack_p
= false;
1552 declarator
->parameter_pack_p
= false;
1557 /* Make a declarator for an array of BOUNDS elements, each of which is
1558 defined by ELEMENT. */
1561 make_array_declarator (cp_declarator
*element
, tree bounds
)
1563 cp_declarator
*declarator
;
1565 declarator
= make_declarator (cdk_array
);
1566 declarator
->declarator
= element
;
1567 declarator
->u
.array
.bounds
= bounds
;
1570 declarator
->id_loc
= element
->id_loc
;
1571 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1572 element
->parameter_pack_p
= false;
1575 declarator
->parameter_pack_p
= false;
1580 /* Determine whether the declarator we've seen so far can be a
1581 parameter pack, when followed by an ellipsis. */
1583 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1585 /* Search for a declarator name, or any other declarator that goes
1586 after the point where the ellipsis could appear in a parameter
1587 pack. If we find any of these, then this declarator can not be
1588 made into a parameter pack. */
1590 while (declarator
&& !found
)
1592 switch ((int)declarator
->kind
)
1603 declarator
= declarator
->declarator
;
1611 cp_parameter_declarator
*no_parameters
;
1613 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1614 DECLARATOR and DEFAULT_ARGUMENT. */
1616 cp_parameter_declarator
*
1617 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1618 cp_declarator
*declarator
,
1619 tree default_argument
)
1621 cp_parameter_declarator
*parameter
;
1623 parameter
= ((cp_parameter_declarator
*)
1624 alloc_declarator (sizeof (cp_parameter_declarator
)));
1625 parameter
->next
= NULL
;
1626 if (decl_specifiers
)
1627 parameter
->decl_specifiers
= *decl_specifiers
;
1629 clear_decl_specs (¶meter
->decl_specifiers
);
1630 parameter
->declarator
= declarator
;
1631 parameter
->default_argument
= default_argument
;
1632 parameter
->ellipsis_p
= false;
1637 /* Returns true iff DECLARATOR is a declaration for a function. */
1640 function_declarator_p (const cp_declarator
*declarator
)
1644 if (declarator
->kind
== cdk_function
1645 && declarator
->declarator
->kind
== cdk_id
)
1647 if (declarator
->kind
== cdk_id
1648 || declarator
->kind
== cdk_error
)
1650 declarator
= declarator
->declarator
;
1660 A cp_parser parses the token stream as specified by the C++
1661 grammar. Its job is purely parsing, not semantic analysis. For
1662 example, the parser breaks the token stream into declarators,
1663 expressions, statements, and other similar syntactic constructs.
1664 It does not check that the types of the expressions on either side
1665 of an assignment-statement are compatible, or that a function is
1666 not declared with a parameter of type `void'.
1668 The parser invokes routines elsewhere in the compiler to perform
1669 semantic analysis and to build up the abstract syntax tree for the
1672 The parser (and the template instantiation code, which is, in a
1673 way, a close relative of parsing) are the only parts of the
1674 compiler that should be calling push_scope and pop_scope, or
1675 related functions. The parser (and template instantiation code)
1676 keeps track of what scope is presently active; everything else
1677 should simply honor that. (The code that generates static
1678 initializers may also need to set the scope, in order to check
1679 access control correctly when emitting the initializers.)
1684 The parser is of the standard recursive-descent variety. Upcoming
1685 tokens in the token stream are examined in order to determine which
1686 production to use when parsing a non-terminal. Some C++ constructs
1687 require arbitrary look ahead to disambiguate. For example, it is
1688 impossible, in the general case, to tell whether a statement is an
1689 expression or declaration without scanning the entire statement.
1690 Therefore, the parser is capable of "parsing tentatively." When the
1691 parser is not sure what construct comes next, it enters this mode.
1692 Then, while we attempt to parse the construct, the parser queues up
1693 error messages, rather than issuing them immediately, and saves the
1694 tokens it consumes. If the construct is parsed successfully, the
1695 parser "commits", i.e., it issues any queued error messages and
1696 the tokens that were being preserved are permanently discarded.
1697 If, however, the construct is not parsed successfully, the parser
1698 rolls back its state completely so that it can resume parsing using
1699 a different alternative.
1704 The performance of the parser could probably be improved substantially.
1705 We could often eliminate the need to parse tentatively by looking ahead
1706 a little bit. In some places, this approach might not entirely eliminate
1707 the need to parse tentatively, but it might still speed up the average
1710 /* Flags that are passed to some parsing functions. These values can
1711 be bitwise-ored together. */
1716 CP_PARSER_FLAGS_NONE
= 0x0,
1717 /* The construct is optional. If it is not present, then no error
1718 should be issued. */
1719 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1720 /* When parsing a type-specifier, treat user-defined type-names
1721 as non-type identifiers. */
1722 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1723 /* When parsing a type-specifier, do not try to parse a class-specifier
1724 or enum-specifier. */
1725 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1726 /* When parsing a decl-specifier-seq, only allow type-specifier or
1728 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1731 /* This type is used for parameters and variables which hold
1732 combinations of the above flags. */
1733 typedef int cp_parser_flags
;
1735 /* The different kinds of declarators we want to parse. */
1737 typedef enum cp_parser_declarator_kind
1739 /* We want an abstract declarator. */
1740 CP_PARSER_DECLARATOR_ABSTRACT
,
1741 /* We want a named declarator. */
1742 CP_PARSER_DECLARATOR_NAMED
,
1743 /* We don't mind, but the name must be an unqualified-id. */
1744 CP_PARSER_DECLARATOR_EITHER
1745 } cp_parser_declarator_kind
;
1747 /* The precedence values used to parse binary expressions. The minimum value
1748 of PREC must be 1, because zero is reserved to quickly discriminate
1749 binary operators from other tokens. */
1754 PREC_LOGICAL_OR_EXPRESSION
,
1755 PREC_LOGICAL_AND_EXPRESSION
,
1756 PREC_INCLUSIVE_OR_EXPRESSION
,
1757 PREC_EXCLUSIVE_OR_EXPRESSION
,
1758 PREC_AND_EXPRESSION
,
1759 PREC_EQUALITY_EXPRESSION
,
1760 PREC_RELATIONAL_EXPRESSION
,
1761 PREC_SHIFT_EXPRESSION
,
1762 PREC_ADDITIVE_EXPRESSION
,
1763 PREC_MULTIPLICATIVE_EXPRESSION
,
1765 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1768 /* A mapping from a token type to a corresponding tree node type, with a
1769 precedence value. */
1771 typedef struct cp_parser_binary_operations_map_node
1773 /* The token type. */
1774 enum cpp_ttype token_type
;
1775 /* The corresponding tree code. */
1776 enum tree_code tree_type
;
1777 /* The precedence of this operator. */
1778 enum cp_parser_prec prec
;
1779 } cp_parser_binary_operations_map_node
;
1781 typedef struct cp_parser_expression_stack_entry
1783 /* Left hand side of the binary operation we are currently
1786 /* Original tree code for left hand side, if it was a binary
1787 expression itself (used for -Wparentheses). */
1788 enum tree_code lhs_type
;
1789 /* Tree code for the binary operation we are parsing. */
1790 enum tree_code tree_type
;
1791 /* Precedence of the binary operation we are parsing. */
1792 enum cp_parser_prec prec
;
1793 /* Location of the binary operation we are parsing. */
1795 } cp_parser_expression_stack_entry
;
1797 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1798 entries because precedence levels on the stack are monotonically
1800 typedef struct cp_parser_expression_stack_entry
1801 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1805 /* Constructors and destructors. */
1807 static cp_parser_context
*cp_parser_context_new
1808 (cp_parser_context
*);
1810 /* Class variables. */
1812 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1814 /* The operator-precedence table used by cp_parser_binary_expression.
1815 Transformed into an associative array (binops_by_token) by
1818 static const cp_parser_binary_operations_map_node binops
[] = {
1819 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1820 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1822 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1823 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1824 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1826 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1827 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1829 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1830 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1832 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1833 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1834 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1835 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1837 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1838 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1840 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1842 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1844 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1846 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1848 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1851 /* The same as binops, but initialized by cp_parser_new so that
1852 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1854 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1856 /* Constructors and destructors. */
1858 /* Construct a new context. The context below this one on the stack
1859 is given by NEXT. */
1861 static cp_parser_context
*
1862 cp_parser_context_new (cp_parser_context
* next
)
1864 cp_parser_context
*context
;
1866 /* Allocate the storage. */
1867 if (cp_parser_context_free_list
!= NULL
)
1869 /* Pull the first entry from the free list. */
1870 context
= cp_parser_context_free_list
;
1871 cp_parser_context_free_list
= context
->next
;
1872 memset (context
, 0, sizeof (*context
));
1875 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1877 /* No errors have occurred yet in this context. */
1878 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1879 /* If this is not the bottommost context, copy information that we
1880 need from the previous context. */
1883 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1884 expression, then we are parsing one in this context, too. */
1885 context
->object_type
= next
->object_type
;
1886 /* Thread the stack. */
1887 context
->next
= next
;
1893 /* Managing the unparsed function queues. */
1895 #define unparsed_funs_with_default_args \
1896 parser->unparsed_queues->last ().funs_with_default_args
1897 #define unparsed_funs_with_definitions \
1898 parser->unparsed_queues->last ().funs_with_definitions
1899 #define unparsed_nsdmis \
1900 parser->unparsed_queues->last ().nsdmis
1901 #define unparsed_classes \
1902 parser->unparsed_queues->last ().classes
1905 push_unparsed_function_queues (cp_parser
*parser
)
1907 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1908 vec_safe_push (parser
->unparsed_queues
, e
);
1912 pop_unparsed_function_queues (cp_parser
*parser
)
1914 release_tree_vector (unparsed_funs_with_definitions
);
1915 parser
->unparsed_queues
->pop ();
1920 /* Constructors and destructors. */
1922 static cp_parser
*cp_parser_new
1925 /* Routines to parse various constructs.
1927 Those that return `tree' will return the error_mark_node (rather
1928 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1929 Sometimes, they will return an ordinary node if error-recovery was
1930 attempted, even though a parse error occurred. So, to check
1931 whether or not a parse error occurred, you should always use
1932 cp_parser_error_occurred. If the construct is optional (indicated
1933 either by an `_opt' in the name of the function that does the
1934 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1935 the construct is not present. */
1937 /* Lexical conventions [gram.lex] */
1939 static tree cp_parser_identifier
1941 static tree cp_parser_string_literal
1942 (cp_parser
*, bool, bool, bool);
1943 static tree cp_parser_userdef_char_literal
1945 static tree cp_parser_userdef_string_literal
1947 static tree cp_parser_userdef_numeric_literal
1950 /* Basic concepts [gram.basic] */
1952 static bool cp_parser_translation_unit
1955 /* Expressions [gram.expr] */
1957 static tree cp_parser_primary_expression
1958 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1959 static tree cp_parser_id_expression
1960 (cp_parser
*, bool, bool, bool *, bool, bool);
1961 static tree cp_parser_unqualified_id
1962 (cp_parser
*, bool, bool, bool, bool);
1963 static tree cp_parser_nested_name_specifier_opt
1964 (cp_parser
*, bool, bool, bool, bool);
1965 static tree cp_parser_nested_name_specifier
1966 (cp_parser
*, bool, bool, bool, bool);
1967 static tree cp_parser_qualifying_entity
1968 (cp_parser
*, bool, bool, bool, bool, bool);
1969 static tree cp_parser_postfix_expression
1970 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1971 static tree cp_parser_postfix_open_square_expression
1972 (cp_parser
*, tree
, bool, bool);
1973 static tree cp_parser_postfix_dot_deref_expression
1974 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1975 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1976 (cp_parser
*, int, bool, bool, bool *, bool = false);
1977 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1978 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1979 static void cp_parser_pseudo_destructor_name
1980 (cp_parser
*, tree
, tree
*, tree
*);
1981 static tree cp_parser_unary_expression
1982 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
1983 static enum tree_code cp_parser_unary_operator
1985 static tree cp_parser_new_expression
1987 static vec
<tree
, va_gc
> *cp_parser_new_placement
1989 static tree cp_parser_new_type_id
1990 (cp_parser
*, tree
*);
1991 static cp_declarator
*cp_parser_new_declarator_opt
1993 static cp_declarator
*cp_parser_direct_new_declarator
1995 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1997 static tree cp_parser_delete_expression
1999 static tree cp_parser_cast_expression
2000 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2001 static tree cp_parser_binary_expression
2002 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2003 static tree cp_parser_question_colon_clause
2004 (cp_parser
*, tree
);
2005 static tree cp_parser_assignment_expression
2006 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2007 static enum tree_code cp_parser_assignment_operator_opt
2009 static tree cp_parser_expression
2010 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2011 static tree cp_parser_constant_expression
2012 (cp_parser
*, bool = false, bool * = NULL
);
2013 static tree cp_parser_builtin_offsetof
2015 static tree cp_parser_lambda_expression
2017 static void cp_parser_lambda_introducer
2018 (cp_parser
*, tree
);
2019 static bool cp_parser_lambda_declarator_opt
2020 (cp_parser
*, tree
);
2021 static void cp_parser_lambda_body
2022 (cp_parser
*, tree
);
2024 /* Statements [gram.stmt.stmt] */
2026 static void cp_parser_statement
2027 (cp_parser
*, tree
, bool, bool *);
2028 static void cp_parser_label_for_labeled_statement
2029 (cp_parser
*, tree
);
2030 static tree cp_parser_expression_statement
2031 (cp_parser
*, tree
);
2032 static tree cp_parser_compound_statement
2033 (cp_parser
*, tree
, bool, bool);
2034 static void cp_parser_statement_seq_opt
2035 (cp_parser
*, tree
);
2036 static tree cp_parser_selection_statement
2037 (cp_parser
*, bool *);
2038 static tree cp_parser_condition
2040 static tree cp_parser_iteration_statement
2041 (cp_parser
*, bool);
2042 static bool cp_parser_for_init_statement
2043 (cp_parser
*, tree
*decl
);
2044 static tree cp_parser_for
2045 (cp_parser
*, bool);
2046 static tree cp_parser_c_for
2047 (cp_parser
*, tree
, tree
, bool);
2048 static tree cp_parser_range_for
2049 (cp_parser
*, tree
, tree
, tree
, bool);
2050 static void do_range_for_auto_deduction
2052 static tree cp_parser_perform_range_for_lookup
2053 (tree
, tree
*, tree
*);
2054 static tree cp_parser_range_for_member_function
2056 static tree cp_parser_jump_statement
2058 static void cp_parser_declaration_statement
2061 static tree cp_parser_implicitly_scoped_statement
2062 (cp_parser
*, bool *);
2063 static void cp_parser_already_scoped_statement
2066 /* Declarations [gram.dcl.dcl] */
2068 static void cp_parser_declaration_seq_opt
2070 static void cp_parser_declaration
2072 static void cp_parser_block_declaration
2073 (cp_parser
*, bool);
2074 static void cp_parser_simple_declaration
2075 (cp_parser
*, bool, tree
*);
2076 static void cp_parser_decl_specifier_seq
2077 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2078 static tree cp_parser_storage_class_specifier_opt
2080 static tree cp_parser_function_specifier_opt
2081 (cp_parser
*, cp_decl_specifier_seq
*);
2082 static tree cp_parser_type_specifier
2083 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2085 static tree cp_parser_simple_type_specifier
2086 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2087 static tree cp_parser_type_name
2089 static tree cp_parser_nonclass_name
2090 (cp_parser
* parser
);
2091 static tree cp_parser_elaborated_type_specifier
2092 (cp_parser
*, bool, bool);
2093 static tree cp_parser_enum_specifier
2095 static void cp_parser_enumerator_list
2096 (cp_parser
*, tree
);
2097 static void cp_parser_enumerator_definition
2098 (cp_parser
*, tree
);
2099 static tree cp_parser_namespace_name
2101 static void cp_parser_namespace_definition
2103 static void cp_parser_namespace_body
2105 static tree cp_parser_qualified_namespace_specifier
2107 static void cp_parser_namespace_alias_definition
2109 static bool cp_parser_using_declaration
2110 (cp_parser
*, bool);
2111 static void cp_parser_using_directive
2113 static tree cp_parser_alias_declaration
2115 static void cp_parser_asm_definition
2117 static void cp_parser_linkage_specification
2119 static void cp_parser_static_assert
2120 (cp_parser
*, bool);
2121 static tree cp_parser_decltype
2124 /* Declarators [gram.dcl.decl] */
2126 static tree cp_parser_init_declarator
2127 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*);
2128 static cp_declarator
*cp_parser_declarator
2129 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2130 static cp_declarator
*cp_parser_direct_declarator
2131 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2132 static enum tree_code cp_parser_ptr_operator
2133 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2134 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2136 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2138 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2140 static tree cp_parser_late_return_type_opt
2141 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2142 static tree cp_parser_declarator_id
2143 (cp_parser
*, bool);
2144 static tree cp_parser_type_id
2146 static tree cp_parser_template_type_arg
2148 static tree
cp_parser_trailing_type_id (cp_parser
*);
2149 static tree cp_parser_type_id_1
2150 (cp_parser
*, bool, bool);
2151 static void cp_parser_type_specifier_seq
2152 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2153 static tree cp_parser_parameter_declaration_clause
2155 static tree cp_parser_parameter_declaration_list
2156 (cp_parser
*, bool *);
2157 static cp_parameter_declarator
*cp_parser_parameter_declaration
2158 (cp_parser
*, bool, bool *);
2159 static tree cp_parser_default_argument
2160 (cp_parser
*, bool);
2161 static void cp_parser_function_body
2162 (cp_parser
*, bool);
2163 static tree cp_parser_initializer
2164 (cp_parser
*, bool *, bool *);
2165 static tree cp_parser_initializer_clause
2166 (cp_parser
*, bool *);
2167 static tree cp_parser_braced_list
2168 (cp_parser
*, bool*);
2169 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2170 (cp_parser
*, bool *);
2172 static bool cp_parser_ctor_initializer_opt_and_function_body
2173 (cp_parser
*, bool);
2175 static tree cp_parser_late_parsing_omp_declare_simd
2176 (cp_parser
*, tree
);
2178 static tree cp_parser_late_parsing_cilk_simd_fn_info
2179 (cp_parser
*, tree
);
2181 static tree synthesize_implicit_template_parm
2183 static tree finish_fully_implicit_template
2184 (cp_parser
*, tree
);
2186 /* Classes [gram.class] */
2188 static tree cp_parser_class_name
2189 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2190 static tree cp_parser_class_specifier
2192 static tree cp_parser_class_head
2193 (cp_parser
*, bool *);
2194 static enum tag_types cp_parser_class_key
2196 static void cp_parser_type_parameter_key
2197 (cp_parser
* parser
);
2198 static void cp_parser_member_specification_opt
2200 static void cp_parser_member_declaration
2202 static tree cp_parser_pure_specifier
2204 static tree cp_parser_constant_initializer
2207 /* Derived classes [gram.class.derived] */
2209 static tree cp_parser_base_clause
2211 static tree cp_parser_base_specifier
2214 /* Special member functions [gram.special] */
2216 static tree cp_parser_conversion_function_id
2218 static tree cp_parser_conversion_type_id
2220 static cp_declarator
*cp_parser_conversion_declarator_opt
2222 static bool cp_parser_ctor_initializer_opt
2224 static void cp_parser_mem_initializer_list
2226 static tree cp_parser_mem_initializer
2228 static tree cp_parser_mem_initializer_id
2231 /* Overloading [gram.over] */
2233 static tree cp_parser_operator_function_id
2235 static tree cp_parser_operator
2238 /* Templates [gram.temp] */
2240 static void cp_parser_template_declaration
2241 (cp_parser
*, bool);
2242 static tree cp_parser_template_parameter_list
2244 static tree cp_parser_template_parameter
2245 (cp_parser
*, bool *, bool *);
2246 static tree cp_parser_type_parameter
2247 (cp_parser
*, bool *);
2248 static tree cp_parser_template_id
2249 (cp_parser
*, bool, bool, enum tag_types
, bool);
2250 static tree cp_parser_template_name
2251 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2252 static tree cp_parser_template_argument_list
2254 static tree cp_parser_template_argument
2256 static void cp_parser_explicit_instantiation
2258 static void cp_parser_explicit_specialization
2261 /* Exception handling [gram.exception] */
2263 static tree cp_parser_try_block
2265 static bool cp_parser_function_try_block
2267 static void cp_parser_handler_seq
2269 static void cp_parser_handler
2271 static tree cp_parser_exception_declaration
2273 static tree cp_parser_throw_expression
2275 static tree cp_parser_exception_specification_opt
2277 static tree cp_parser_type_id_list
2280 /* GNU Extensions */
2282 static tree cp_parser_asm_specification_opt
2284 static tree cp_parser_asm_operand_list
2286 static tree cp_parser_asm_clobber_list
2288 static tree cp_parser_asm_label_list
2290 static bool cp_next_tokens_can_be_attribute_p
2292 static bool cp_next_tokens_can_be_gnu_attribute_p
2294 static bool cp_next_tokens_can_be_std_attribute_p
2296 static bool cp_nth_tokens_can_be_std_attribute_p
2297 (cp_parser
*, size_t);
2298 static bool cp_nth_tokens_can_be_gnu_attribute_p
2299 (cp_parser
*, size_t);
2300 static bool cp_nth_tokens_can_be_attribute_p
2301 (cp_parser
*, size_t);
2302 static tree cp_parser_attributes_opt
2304 static tree cp_parser_gnu_attributes_opt
2306 static tree cp_parser_gnu_attribute_list
2308 static tree cp_parser_std_attribute
2310 static tree cp_parser_std_attribute_spec
2312 static tree cp_parser_std_attribute_spec_seq
2314 static bool cp_parser_extension_opt
2315 (cp_parser
*, int *);
2316 static void cp_parser_label_declaration
2319 /* Transactional Memory Extensions */
2321 static tree cp_parser_transaction
2322 (cp_parser
*, enum rid
);
2323 static tree cp_parser_transaction_expression
2324 (cp_parser
*, enum rid
);
2325 static bool cp_parser_function_transaction
2326 (cp_parser
*, enum rid
);
2327 static tree cp_parser_transaction_cancel
2330 enum pragma_context
{
2337 static bool cp_parser_pragma
2338 (cp_parser
*, enum pragma_context
);
2340 /* Objective-C++ Productions */
2342 static tree cp_parser_objc_message_receiver
2344 static tree cp_parser_objc_message_args
2346 static tree cp_parser_objc_message_expression
2348 static tree cp_parser_objc_encode_expression
2350 static tree cp_parser_objc_defs_expression
2352 static tree cp_parser_objc_protocol_expression
2354 static tree cp_parser_objc_selector_expression
2356 static tree cp_parser_objc_expression
2358 static bool cp_parser_objc_selector_p
2360 static tree cp_parser_objc_selector
2362 static tree cp_parser_objc_protocol_refs_opt
2364 static void cp_parser_objc_declaration
2365 (cp_parser
*, tree
);
2366 static tree cp_parser_objc_statement
2368 static bool cp_parser_objc_valid_prefix_attributes
2369 (cp_parser
*, tree
*);
2370 static void cp_parser_objc_at_property_declaration
2372 static void cp_parser_objc_at_synthesize_declaration
2374 static void cp_parser_objc_at_dynamic_declaration
2376 static tree cp_parser_objc_struct_declaration
2379 /* Utility Routines */
2381 static tree cp_parser_lookup_name
2382 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2383 static tree cp_parser_lookup_name_simple
2384 (cp_parser
*, tree
, location_t
);
2385 static tree cp_parser_maybe_treat_template_as_class
2387 static bool cp_parser_check_declarator_template_parameters
2388 (cp_parser
*, cp_declarator
*, location_t
);
2389 static bool cp_parser_check_template_parameters
2390 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2391 static tree cp_parser_simple_cast_expression
2393 static tree cp_parser_global_scope_opt
2394 (cp_parser
*, bool);
2395 static bool cp_parser_constructor_declarator_p
2396 (cp_parser
*, bool);
2397 static tree cp_parser_function_definition_from_specifiers_and_declarator
2398 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2399 static tree cp_parser_function_definition_after_declarator
2400 (cp_parser
*, bool);
2401 static void cp_parser_template_declaration_after_export
2402 (cp_parser
*, bool);
2403 static void cp_parser_perform_template_parameter_access_checks
2404 (vec
<deferred_access_check
, va_gc
> *);
2405 static tree cp_parser_single_declaration
2406 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2407 static tree cp_parser_functional_cast
2408 (cp_parser
*, tree
);
2409 static tree cp_parser_save_member_function_body
2410 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2411 static tree cp_parser_save_nsdmi
2413 static tree cp_parser_enclosed_template_argument_list
2415 static void cp_parser_save_default_args
2416 (cp_parser
*, tree
);
2417 static void cp_parser_late_parsing_for_member
2418 (cp_parser
*, tree
);
2419 static tree cp_parser_late_parse_one_default_arg
2420 (cp_parser
*, tree
, tree
, tree
);
2421 static void cp_parser_late_parsing_nsdmi
2422 (cp_parser
*, tree
);
2423 static void cp_parser_late_parsing_default_args
2424 (cp_parser
*, tree
);
2425 static tree cp_parser_sizeof_operand
2426 (cp_parser
*, enum rid
);
2427 static tree cp_parser_trait_expr
2428 (cp_parser
*, enum rid
);
2429 static bool cp_parser_declares_only_class_p
2431 static void cp_parser_set_storage_class
2432 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2433 static void cp_parser_set_decl_spec_type
2434 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2435 static void set_and_check_decl_spec_loc
2436 (cp_decl_specifier_seq
*decl_specs
,
2437 cp_decl_spec ds
, cp_token
*);
2438 static bool cp_parser_friend_p
2439 (const cp_decl_specifier_seq
*);
2440 static void cp_parser_required_error
2441 (cp_parser
*, required_token
, bool);
2442 static cp_token
*cp_parser_require
2443 (cp_parser
*, enum cpp_ttype
, required_token
);
2444 static cp_token
*cp_parser_require_keyword
2445 (cp_parser
*, enum rid
, required_token
);
2446 static bool cp_parser_token_starts_function_definition_p
2448 static bool cp_parser_next_token_starts_class_definition_p
2450 static bool cp_parser_next_token_ends_template_argument_p
2452 static bool cp_parser_nth_token_starts_template_argument_list_p
2453 (cp_parser
*, size_t);
2454 static enum tag_types cp_parser_token_is_class_key
2456 static enum tag_types cp_parser_token_is_type_parameter_key
2458 static void cp_parser_check_class_key
2459 (enum tag_types
, tree type
);
2460 static void cp_parser_check_access_in_redeclaration
2461 (tree type
, location_t location
);
2462 static bool cp_parser_optional_template_keyword
2464 static void cp_parser_pre_parsed_nested_name_specifier
2466 static bool cp_parser_cache_group
2467 (cp_parser
*, enum cpp_ttype
, unsigned);
2468 static tree cp_parser_cache_defarg
2469 (cp_parser
*parser
, bool nsdmi
);
2470 static void cp_parser_parse_tentatively
2472 static void cp_parser_commit_to_tentative_parse
2474 static void cp_parser_commit_to_topmost_tentative_parse
2476 static void cp_parser_abort_tentative_parse
2478 static bool cp_parser_parse_definitely
2480 static inline bool cp_parser_parsing_tentatively
2482 static bool cp_parser_uncommitted_to_tentative_parse_p
2484 static void cp_parser_error
2485 (cp_parser
*, const char *);
2486 static void cp_parser_name_lookup_error
2487 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2488 static bool cp_parser_simulate_error
2490 static bool cp_parser_check_type_definition
2492 static void cp_parser_check_for_definition_in_return_type
2493 (cp_declarator
*, tree
, location_t type_location
);
2494 static void cp_parser_check_for_invalid_template_id
2495 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2496 static bool cp_parser_non_integral_constant_expression
2497 (cp_parser
*, non_integral_constant
);
2498 static void cp_parser_diagnose_invalid_type_name
2499 (cp_parser
*, tree
, location_t
);
2500 static bool cp_parser_parse_and_diagnose_invalid_type_name
2502 static int cp_parser_skip_to_closing_parenthesis
2503 (cp_parser
*, bool, bool, bool);
2504 static void cp_parser_skip_to_end_of_statement
2506 static void cp_parser_consume_semicolon_at_end_of_statement
2508 static void cp_parser_skip_to_end_of_block_or_statement
2510 static bool cp_parser_skip_to_closing_brace
2512 static void cp_parser_skip_to_end_of_template_parameter_list
2514 static void cp_parser_skip_to_pragma_eol
2515 (cp_parser
*, cp_token
*);
2516 static bool cp_parser_error_occurred
2518 static bool cp_parser_allow_gnu_extensions_p
2520 static bool cp_parser_is_pure_string_literal
2522 static bool cp_parser_is_string_literal
2524 static bool cp_parser_is_keyword
2525 (cp_token
*, enum rid
);
2526 static tree cp_parser_make_typename_type
2527 (cp_parser
*, tree
, location_t location
);
2528 static cp_declarator
* cp_parser_make_indirect_declarator
2529 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2530 static bool cp_parser_compound_literal_p
2532 static bool cp_parser_array_designator_p
2534 static bool cp_parser_skip_to_closing_square_bracket
2537 /* Returns nonzero if we are parsing tentatively. */
2540 cp_parser_parsing_tentatively (cp_parser
* parser
)
2542 return parser
->context
->next
!= NULL
;
2545 /* Returns nonzero if TOKEN is a string literal. */
2548 cp_parser_is_pure_string_literal (cp_token
* token
)
2550 return (token
->type
== CPP_STRING
||
2551 token
->type
== CPP_STRING16
||
2552 token
->type
== CPP_STRING32
||
2553 token
->type
== CPP_WSTRING
||
2554 token
->type
== CPP_UTF8STRING
);
2557 /* Returns nonzero if TOKEN is a string literal
2558 of a user-defined string literal. */
2561 cp_parser_is_string_literal (cp_token
* token
)
2563 return (cp_parser_is_pure_string_literal (token
) ||
2564 token
->type
== CPP_STRING_USERDEF
||
2565 token
->type
== CPP_STRING16_USERDEF
||
2566 token
->type
== CPP_STRING32_USERDEF
||
2567 token
->type
== CPP_WSTRING_USERDEF
||
2568 token
->type
== CPP_UTF8STRING_USERDEF
);
2571 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2574 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2576 return token
->keyword
== keyword
;
2579 /* If not parsing tentatively, issue a diagnostic of the form
2580 FILE:LINE: MESSAGE before TOKEN
2581 where TOKEN is the next token in the input stream. MESSAGE
2582 (specified by the caller) is usually of the form "expected
2586 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2588 if (!cp_parser_simulate_error (parser
))
2590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2591 /* This diagnostic makes more sense if it is tagged to the line
2592 of the token we just peeked at. */
2593 cp_lexer_set_source_position_from_token (token
);
2595 if (token
->type
== CPP_PRAGMA
)
2597 error_at (token
->location
,
2598 "%<#pragma%> is not allowed here");
2599 cp_parser_skip_to_pragma_eol (parser
, token
);
2603 c_parse_error (gmsgid
,
2604 /* Because c_parser_error does not understand
2605 CPP_KEYWORD, keywords are treated like
2607 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2608 token
->u
.value
, token
->flags
);
2612 /* Issue an error about name-lookup failing. NAME is the
2613 IDENTIFIER_NODE DECL is the result of
2614 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2615 the thing that we hoped to find. */
2618 cp_parser_name_lookup_error (cp_parser
* parser
,
2621 name_lookup_error desired
,
2622 location_t location
)
2624 /* If name lookup completely failed, tell the user that NAME was not
2626 if (decl
== error_mark_node
)
2628 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2629 error_at (location
, "%<%E::%E%> has not been declared",
2630 parser
->scope
, name
);
2631 else if (parser
->scope
== global_namespace
)
2632 error_at (location
, "%<::%E%> has not been declared", name
);
2633 else if (parser
->object_scope
2634 && !CLASS_TYPE_P (parser
->object_scope
))
2635 error_at (location
, "request for member %qE in non-class type %qT",
2636 name
, parser
->object_scope
);
2637 else if (parser
->object_scope
)
2638 error_at (location
, "%<%T::%E%> has not been declared",
2639 parser
->object_scope
, name
);
2641 error_at (location
, "%qE has not been declared", name
);
2643 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2648 error_at (location
, "%<%E::%E%> is not a type",
2649 parser
->scope
, name
);
2652 error_at (location
, "%<%E::%E%> is not a class or namespace",
2653 parser
->scope
, name
);
2657 "%<%E::%E%> is not a class, namespace, or enumeration",
2658 parser
->scope
, name
);
2665 else if (parser
->scope
== global_namespace
)
2670 error_at (location
, "%<::%E%> is not a type", name
);
2673 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2677 "%<::%E%> is not a class, namespace, or enumeration",
2689 error_at (location
, "%qE is not a type", name
);
2692 error_at (location
, "%qE is not a class or namespace", name
);
2696 "%qE is not a class, namespace, or enumeration", name
);
2704 /* If we are parsing tentatively, remember that an error has occurred
2705 during this tentative parse. Returns true if the error was
2706 simulated; false if a message should be issued by the caller. */
2709 cp_parser_simulate_error (cp_parser
* parser
)
2711 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2713 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2719 /* This function is called when a type is defined. If type
2720 definitions are forbidden at this point, an error message is
2724 cp_parser_check_type_definition (cp_parser
* parser
)
2726 /* If types are forbidden here, issue a message. */
2727 if (parser
->type_definition_forbidden_message
)
2729 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2730 in the message need to be interpreted. */
2731 error (parser
->type_definition_forbidden_message
);
2737 /* This function is called when the DECLARATOR is processed. The TYPE
2738 was a type defined in the decl-specifiers. If it is invalid to
2739 define a type in the decl-specifiers for DECLARATOR, an error is
2740 issued. TYPE_LOCATION is the location of TYPE and is used
2741 for error reporting. */
2744 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2745 tree type
, location_t type_location
)
2747 /* [dcl.fct] forbids type definitions in return types.
2748 Unfortunately, it's not easy to know whether or not we are
2749 processing a return type until after the fact. */
2751 && (declarator
->kind
== cdk_pointer
2752 || declarator
->kind
== cdk_reference
2753 || declarator
->kind
== cdk_ptrmem
))
2754 declarator
= declarator
->declarator
;
2756 && declarator
->kind
== cdk_function
)
2758 error_at (type_location
,
2759 "new types may not be defined in a return type");
2760 inform (type_location
,
2761 "(perhaps a semicolon is missing after the definition of %qT)",
2766 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2767 "<" in any valid C++ program. If the next token is indeed "<",
2768 issue a message warning the user about what appears to be an
2769 invalid attempt to form a template-id. LOCATION is the location
2770 of the type-specifier (TYPE) */
2773 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2775 enum tag_types tag_type
,
2776 location_t location
)
2778 cp_token_position start
= 0;
2780 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2783 error_at (location
, "%qT is not a template", type
);
2784 else if (identifier_p (type
))
2786 if (tag_type
!= none_type
)
2787 error_at (location
, "%qE is not a class template", type
);
2789 error_at (location
, "%qE is not a template", type
);
2792 error_at (location
, "invalid template-id");
2793 /* Remember the location of the invalid "<". */
2794 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2795 start
= cp_lexer_token_position (parser
->lexer
, true);
2796 /* Consume the "<". */
2797 cp_lexer_consume_token (parser
->lexer
);
2798 /* Parse the template arguments. */
2799 cp_parser_enclosed_template_argument_list (parser
);
2800 /* Permanently remove the invalid template arguments so that
2801 this error message is not issued again. */
2803 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2807 /* If parsing an integral constant-expression, issue an error message
2808 about the fact that THING appeared and return true. Otherwise,
2809 return false. In either case, set
2810 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2813 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2814 non_integral_constant thing
)
2816 parser
->non_integral_constant_expression_p
= true;
2817 if (parser
->integral_constant_expression_p
)
2819 if (!parser
->allow_non_integral_constant_expression_p
)
2821 const char *msg
= NULL
;
2825 error ("floating-point literal "
2826 "cannot appear in a constant-expression");
2829 error ("a cast to a type other than an integral or "
2830 "enumeration type cannot appear in a "
2831 "constant-expression");
2834 error ("%<typeid%> operator "
2835 "cannot appear in a constant-expression");
2838 error ("non-constant compound literals "
2839 "cannot appear in a constant-expression");
2842 error ("a function call "
2843 "cannot appear in a constant-expression");
2846 error ("an increment "
2847 "cannot appear in a constant-expression");
2850 error ("an decrement "
2851 "cannot appear in a constant-expression");
2854 error ("an array reference "
2855 "cannot appear in a constant-expression");
2857 case NIC_ADDR_LABEL
:
2858 error ("the address of a label "
2859 "cannot appear in a constant-expression");
2861 case NIC_OVERLOADED
:
2862 error ("calls to overloaded operators "
2863 "cannot appear in a constant-expression");
2865 case NIC_ASSIGNMENT
:
2866 error ("an assignment cannot appear in a constant-expression");
2869 error ("a comma operator "
2870 "cannot appear in a constant-expression");
2872 case NIC_CONSTRUCTOR
:
2873 error ("a call to a constructor "
2874 "cannot appear in a constant-expression");
2876 case NIC_TRANSACTION
:
2877 error ("a transaction expression "
2878 "cannot appear in a constant-expression");
2884 msg
= "__FUNCTION__";
2886 case NIC_PRETTY_FUNC
:
2887 msg
= "__PRETTY_FUNCTION__";
2907 case NIC_PREINCREMENT
:
2910 case NIC_PREDECREMENT
:
2923 error ("%qs cannot appear in a constant-expression", msg
);
2930 /* Emit a diagnostic for an invalid type name. This function commits
2931 to the current active tentative parse, if any. (Otherwise, the
2932 problematic construct might be encountered again later, resulting
2933 in duplicate error messages.) LOCATION is the location of ID. */
2936 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
2937 location_t location
)
2939 tree decl
, ambiguous_decls
;
2940 cp_parser_commit_to_tentative_parse (parser
);
2941 /* Try to lookup the identifier. */
2942 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
2943 /*is_template=*/false,
2944 /*is_namespace=*/false,
2945 /*check_dependency=*/true,
2946 &ambiguous_decls
, location
);
2947 if (ambiguous_decls
)
2948 /* If the lookup was ambiguous, an error will already have
2951 /* If the lookup found a template-name, it means that the user forgot
2952 to specify an argument list. Emit a useful error message. */
2953 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2955 "invalid use of template-name %qE without an argument list",
2957 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2958 error_at (location
, "invalid use of destructor %qD as a type", id
);
2959 else if (TREE_CODE (decl
) == TYPE_DECL
)
2960 /* Something like 'unsigned A a;' */
2961 error_at (location
, "invalid combination of multiple type-specifiers");
2962 else if (!parser
->scope
)
2964 /* Issue an error message. */
2965 error_at (location
, "%qE does not name a type", id
);
2966 /* If we're in a template class, it's possible that the user was
2967 referring to a type from a base class. For example:
2969 template <typename T> struct A { typedef T X; };
2970 template <typename T> struct B : public A<T> { X x; };
2972 The user should have said "typename A<T>::X". */
2973 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2974 inform (location
, "C++11 %<constexpr%> only available with "
2975 "-std=c++11 or -std=gnu++11");
2976 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
2977 inform (location
, "C++11 %<noexcept%> only available with "
2978 "-std=c++11 or -std=gnu++11");
2979 else if (cxx_dialect
< cxx11
2980 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
2981 inform (location
, "C++11 %<thread_local%> only available with "
2982 "-std=c++11 or -std=gnu++11");
2983 else if (processing_template_decl
&& current_class_type
2984 && TYPE_BINFO (current_class_type
))
2988 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2992 tree base_type
= BINFO_TYPE (b
);
2993 if (CLASS_TYPE_P (base_type
)
2994 && dependent_type_p (base_type
))
2997 /* Go from a particular instantiation of the
2998 template (which will have an empty TYPE_FIELDs),
2999 to the main version. */
3000 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3001 for (field
= TYPE_FIELDS (base_type
);
3003 field
= DECL_CHAIN (field
))
3004 if (TREE_CODE (field
) == TYPE_DECL
3005 && DECL_NAME (field
) == id
)
3008 "(perhaps %<typename %T::%E%> was intended)",
3009 BINFO_TYPE (b
), id
);
3018 /* Here we diagnose qualified-ids where the scope is actually correct,
3019 but the identifier does not resolve to a valid type name. */
3020 else if (parser
->scope
!= error_mark_node
)
3022 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3025 error_at (location_of (id
),
3026 "%qE in namespace %qE does not name a template type",
3029 error_at (location_of (id
),
3030 "%qE in namespace %qE does not name a type",
3033 else if (CLASS_TYPE_P (parser
->scope
)
3034 && constructor_name_p (id
, parser
->scope
))
3037 error_at (location
, "%<%T::%E%> names the constructor, not"
3038 " the type", parser
->scope
, id
);
3039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3040 error_at (location
, "and %qT has no template constructors",
3043 else if (TYPE_P (parser
->scope
)
3044 && dependent_scope_p (parser
->scope
))
3045 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3046 "%qT is a dependent scope",
3047 parser
->scope
, id
, parser
->scope
);
3048 else if (TYPE_P (parser
->scope
))
3050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3051 error_at (location_of (id
),
3052 "%qE in %q#T does not name a template type",
3055 error_at (location_of (id
),
3056 "%qE in %q#T does not name a type",
3064 /* Check for a common situation where a type-name should be present,
3065 but is not, and issue a sensible error message. Returns true if an
3066 invalid type-name was detected.
3068 The situation handled by this function are variable declarations of the
3069 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3070 Usually, `ID' should name a type, but if we got here it means that it
3071 does not. We try to emit the best possible error message depending on
3072 how exactly the id-expression looks like. */
3075 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3078 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3080 /* Avoid duplicate error about ambiguous lookup. */
3081 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3083 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3084 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3088 cp_parser_parse_tentatively (parser
);
3089 id
= cp_parser_id_expression (parser
,
3090 /*template_keyword_p=*/false,
3091 /*check_dependency_p=*/true,
3092 /*template_p=*/NULL
,
3093 /*declarator_p=*/true,
3094 /*optional_p=*/false);
3095 /* If the next token is a (, this is a function with no explicit return
3096 type, i.e. constructor, destructor or conversion op. */
3097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3098 || TREE_CODE (id
) == TYPE_DECL
)
3100 cp_parser_abort_tentative_parse (parser
);
3103 if (!cp_parser_parse_definitely (parser
))
3106 /* Emit a diagnostic for the invalid type. */
3107 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3109 /* If we aren't in the middle of a declarator (i.e. in a
3110 parameter-declaration-clause), skip to the end of the declaration;
3111 there's no point in trying to process it. */
3112 if (!parser
->in_declarator_p
)
3113 cp_parser_skip_to_end_of_block_or_statement (parser
);
3117 /* Consume tokens up to, and including, the next non-nested closing `)'.
3118 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3119 are doing error recovery. Returns -1 if OR_COMMA is true and we
3120 found an unnested comma. */
3123 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3128 unsigned paren_depth
= 0;
3129 unsigned brace_depth
= 0;
3130 unsigned square_depth
= 0;
3132 if (recovering
&& !or_comma
3133 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3138 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3140 switch (token
->type
)
3143 case CPP_PRAGMA_EOL
:
3144 /* If we've run out of tokens, then there is no closing `)'. */
3147 /* This is good for lambda expression capture-lists. */
3148 case CPP_OPEN_SQUARE
:
3151 case CPP_CLOSE_SQUARE
:
3152 if (!square_depth
--)
3157 /* This matches the processing in skip_to_end_of_statement. */
3162 case CPP_OPEN_BRACE
:
3165 case CPP_CLOSE_BRACE
:
3171 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3176 case CPP_OPEN_PAREN
:
3181 case CPP_CLOSE_PAREN
:
3182 if (!brace_depth
&& !paren_depth
--)
3185 cp_lexer_consume_token (parser
->lexer
);
3194 /* Consume the token. */
3195 cp_lexer_consume_token (parser
->lexer
);
3199 /* Consume tokens until we reach the end of the current statement.
3200 Normally, that will be just before consuming a `;'. However, if a
3201 non-nested `}' comes first, then we stop before consuming that. */
3204 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3206 unsigned nesting_depth
= 0;
3208 /* Unwind generic function template scope if necessary. */
3209 if (parser
->fully_implicit_function_template_p
)
3210 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3214 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3216 switch (token
->type
)
3219 case CPP_PRAGMA_EOL
:
3220 /* If we've run out of tokens, stop. */
3224 /* If the next token is a `;', we have reached the end of the
3230 case CPP_CLOSE_BRACE
:
3231 /* If this is a non-nested '}', stop before consuming it.
3232 That way, when confronted with something like:
3236 we stop before consuming the closing '}', even though we
3237 have not yet reached a `;'. */
3238 if (nesting_depth
== 0)
3241 /* If it is the closing '}' for a block that we have
3242 scanned, stop -- but only after consuming the token.
3248 we will stop after the body of the erroneously declared
3249 function, but before consuming the following `typedef'
3251 if (--nesting_depth
== 0)
3253 cp_lexer_consume_token (parser
->lexer
);
3257 case CPP_OPEN_BRACE
:
3265 /* Consume the token. */
3266 cp_lexer_consume_token (parser
->lexer
);
3270 /* This function is called at the end of a statement or declaration.
3271 If the next token is a semicolon, it is consumed; otherwise, error
3272 recovery is attempted. */
3275 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3277 /* Look for the trailing `;'. */
3278 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3280 /* If there is additional (erroneous) input, skip to the end of
3282 cp_parser_skip_to_end_of_statement (parser
);
3283 /* If the next token is now a `;', consume it. */
3284 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3285 cp_lexer_consume_token (parser
->lexer
);
3289 /* Skip tokens until we have consumed an entire block, or until we
3290 have consumed a non-nested `;'. */
3293 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3295 int nesting_depth
= 0;
3297 /* Unwind generic function template scope if necessary. */
3298 if (parser
->fully_implicit_function_template_p
)
3299 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3301 while (nesting_depth
>= 0)
3303 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3305 switch (token
->type
)
3308 case CPP_PRAGMA_EOL
:
3309 /* If we've run out of tokens, stop. */
3313 /* Stop if this is an unnested ';'. */
3318 case CPP_CLOSE_BRACE
:
3319 /* Stop if this is an unnested '}', or closes the outermost
3322 if (nesting_depth
< 0)
3328 case CPP_OPEN_BRACE
:
3337 /* Consume the token. */
3338 cp_lexer_consume_token (parser
->lexer
);
3342 /* Skip tokens until a non-nested closing curly brace is the next
3343 token, or there are no more tokens. Return true in the first case,
3347 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3349 unsigned nesting_depth
= 0;
3353 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3355 switch (token
->type
)
3358 case CPP_PRAGMA_EOL
:
3359 /* If we've run out of tokens, stop. */
3362 case CPP_CLOSE_BRACE
:
3363 /* If the next token is a non-nested `}', then we have reached
3364 the end of the current block. */
3365 if (nesting_depth
-- == 0)
3369 case CPP_OPEN_BRACE
:
3370 /* If it the next token is a `{', then we are entering a new
3371 block. Consume the entire block. */
3379 /* Consume the token. */
3380 cp_lexer_consume_token (parser
->lexer
);
3384 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3385 parameter is the PRAGMA token, allowing us to purge the entire pragma
3389 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3393 parser
->lexer
->in_pragma
= false;
3396 token
= cp_lexer_consume_token (parser
->lexer
);
3397 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3399 /* Ensure that the pragma is not parsed again. */
3400 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3403 /* Require pragma end of line, resyncing with it as necessary. The
3404 arguments are as for cp_parser_skip_to_pragma_eol. */
3407 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3409 parser
->lexer
->in_pragma
= false;
3410 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3411 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3414 /* This is a simple wrapper around make_typename_type. When the id is
3415 an unresolved identifier node, we can provide a superior diagnostic
3416 using cp_parser_diagnose_invalid_type_name. */
3419 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3420 location_t id_location
)
3423 if (identifier_p (id
))
3425 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3426 /*complain=*/tf_none
);
3427 if (result
== error_mark_node
)
3428 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3431 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3434 /* This is a wrapper around the
3435 make_{pointer,ptrmem,reference}_declarator functions that decides
3436 which one to call based on the CODE and CLASS_TYPE arguments. The
3437 CODE argument should be one of the values returned by
3438 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3439 appertain to the pointer or reference. */
3441 static cp_declarator
*
3442 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3443 cp_cv_quals cv_qualifiers
,
3444 cp_declarator
*target
,
3447 if (code
== ERROR_MARK
)
3448 return cp_error_declarator
;
3450 if (code
== INDIRECT_REF
)
3451 if (class_type
== NULL_TREE
)
3452 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3454 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3455 target
, attributes
);
3456 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3457 return make_reference_declarator (cv_qualifiers
, target
,
3459 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3460 return make_reference_declarator (cv_qualifiers
, target
,
3465 /* Create a new C++ parser. */
3468 cp_parser_new (void)
3474 /* cp_lexer_new_main is called before doing GC allocation because
3475 cp_lexer_new_main might load a PCH file. */
3476 lexer
= cp_lexer_new_main ();
3478 /* Initialize the binops_by_token so that we can get the tree
3479 directly from the token. */
3480 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3481 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3483 parser
= ggc_cleared_alloc
<cp_parser
> ();
3484 parser
->lexer
= lexer
;
3485 parser
->context
= cp_parser_context_new (NULL
);
3487 /* For now, we always accept GNU extensions. */
3488 parser
->allow_gnu_extensions_p
= 1;
3490 /* The `>' token is a greater-than operator, not the end of a
3492 parser
->greater_than_is_operator_p
= true;
3494 parser
->default_arg_ok_p
= true;
3496 /* We are not parsing a constant-expression. */
3497 parser
->integral_constant_expression_p
= false;
3498 parser
->allow_non_integral_constant_expression_p
= false;
3499 parser
->non_integral_constant_expression_p
= false;
3501 /* Local variable names are not forbidden. */
3502 parser
->local_variables_forbidden_p
= false;
3504 /* We are not processing an `extern "C"' declaration. */
3505 parser
->in_unbraced_linkage_specification_p
= false;
3507 /* We are not processing a declarator. */
3508 parser
->in_declarator_p
= false;
3510 /* We are not processing a template-argument-list. */
3511 parser
->in_template_argument_list_p
= false;
3513 /* We are not in an iteration statement. */
3514 parser
->in_statement
= 0;
3516 /* We are not in a switch statement. */
3517 parser
->in_switch_statement_p
= false;
3519 /* We are not parsing a type-id inside an expression. */
3520 parser
->in_type_id_in_expr_p
= false;
3522 /* Declarations aren't implicitly extern "C". */
3523 parser
->implicit_extern_c
= false;
3525 /* String literals should be translated to the execution character set. */
3526 parser
->translate_strings_p
= true;
3528 /* We are not parsing a function body. */
3529 parser
->in_function_body
= false;
3531 /* We can correct until told otherwise. */
3532 parser
->colon_corrects_to_scope_p
= true;
3534 /* The unparsed function queue is empty. */
3535 push_unparsed_function_queues (parser
);
3537 /* There are no classes being defined. */
3538 parser
->num_classes_being_defined
= 0;
3540 /* No template parameters apply. */
3541 parser
->num_template_parameter_lists
= 0;
3543 /* Not declaring an implicit function template. */
3544 parser
->auto_is_implicit_function_template_parm_p
= false;
3545 parser
->fully_implicit_function_template_p
= false;
3546 parser
->implicit_template_parms
= 0;
3547 parser
->implicit_template_scope
= 0;
3552 /* Create a cp_lexer structure which will emit the tokens in CACHE
3553 and push it onto the parser's lexer stack. This is used for delayed
3554 parsing of in-class method bodies and default arguments, and should
3555 not be confused with tentative parsing. */
3557 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3559 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3560 lexer
->next
= parser
->lexer
;
3561 parser
->lexer
= lexer
;
3563 /* Move the current source position to that of the first token in the
3565 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3568 /* Pop the top lexer off the parser stack. This is never used for the
3569 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3571 cp_parser_pop_lexer (cp_parser
*parser
)
3573 cp_lexer
*lexer
= parser
->lexer
;
3574 parser
->lexer
= lexer
->next
;
3575 cp_lexer_destroy (lexer
);
3577 /* Put the current source position back where it was before this
3578 lexer was pushed. */
3579 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3582 /* Lexical conventions [gram.lex] */
3584 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3588 cp_parser_identifier (cp_parser
* parser
)
3592 /* Look for the identifier. */
3593 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3594 /* Return the value. */
3595 return token
? token
->u
.value
: error_mark_node
;
3598 /* Parse a sequence of adjacent string constants. Returns a
3599 TREE_STRING representing the combined, nul-terminated string
3600 constant. If TRANSLATE is true, translate the string to the
3601 execution character set. If WIDE_OK is true, a wide string is
3604 C++98 [lex.string] says that if a narrow string literal token is
3605 adjacent to a wide string literal token, the behavior is undefined.
3606 However, C99 6.4.5p4 says that this results in a wide string literal.
3607 We follow C99 here, for consistency with the C front end.
3609 This code is largely lifted from lex_string() in c-lex.c.
3611 FUTURE: ObjC++ will need to handle @-strings here. */
3613 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3614 bool lookup_udlit
= true)
3618 struct obstack str_ob
;
3619 cpp_string str
, istr
, *strs
;
3621 enum cpp_ttype type
, curr_type
;
3622 int have_suffix_p
= 0;
3624 tree suffix_id
= NULL_TREE
;
3625 bool curr_tok_is_userdef_p
= false;
3627 tok
= cp_lexer_peek_token (parser
->lexer
);
3628 if (!cp_parser_is_string_literal (tok
))
3630 cp_parser_error (parser
, "expected string-literal");
3631 return error_mark_node
;
3634 if (cpp_userdef_string_p (tok
->type
))
3636 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3637 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3638 curr_tok_is_userdef_p
= true;
3642 string_tree
= tok
->u
.value
;
3643 curr_type
= tok
->type
;
3647 /* Try to avoid the overhead of creating and destroying an obstack
3648 for the common case of just one string. */
3649 if (!cp_parser_is_string_literal
3650 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3652 cp_lexer_consume_token (parser
->lexer
);
3654 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3655 str
.len
= TREE_STRING_LENGTH (string_tree
);
3658 if (curr_tok_is_userdef_p
)
3660 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3662 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3665 curr_type
= tok
->type
;
3671 gcc_obstack_init (&str_ob
);
3676 cp_lexer_consume_token (parser
->lexer
);
3678 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3679 str
.len
= TREE_STRING_LENGTH (string_tree
);
3681 if (curr_tok_is_userdef_p
)
3683 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3684 if (have_suffix_p
== 0)
3686 suffix_id
= curr_suffix_id
;
3689 else if (have_suffix_p
== 1
3690 && curr_suffix_id
!= suffix_id
)
3692 error ("inconsistent user-defined literal suffixes"
3693 " %qD and %qD in string literal",
3694 suffix_id
, curr_suffix_id
);
3697 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3700 curr_type
= tok
->type
;
3702 if (type
!= curr_type
)
3704 if (type
== CPP_STRING
)
3706 else if (curr_type
!= CPP_STRING
)
3707 error_at (tok
->location
,
3708 "unsupported non-standard concatenation "
3709 "of string literals");
3712 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3714 tok
= cp_lexer_peek_token (parser
->lexer
);
3715 if (cpp_userdef_string_p (tok
->type
))
3717 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3718 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3719 curr_tok_is_userdef_p
= true;
3723 string_tree
= tok
->u
.value
;
3724 curr_type
= tok
->type
;
3725 curr_tok_is_userdef_p
= false;
3728 while (cp_parser_is_string_literal (tok
));
3730 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3733 if (type
!= CPP_STRING
&& !wide_ok
)
3735 cp_parser_error (parser
, "a wide string is invalid in this context");
3739 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3740 (parse_in
, strs
, count
, &istr
, type
))
3742 value
= build_string (istr
.len
, (const char *)istr
.text
);
3743 free (CONST_CAST (unsigned char *, istr
.text
));
3749 case CPP_UTF8STRING
:
3750 TREE_TYPE (value
) = char_array_type_node
;
3753 TREE_TYPE (value
) = char16_array_type_node
;
3756 TREE_TYPE (value
) = char32_array_type_node
;
3759 TREE_TYPE (value
) = wchar_array_type_node
;
3763 value
= fix_string_type (value
);
3767 tree literal
= build_userdef_literal (suffix_id
, value
,
3768 OT_NONE
, NULL_TREE
);
3770 value
= cp_parser_userdef_string_literal (literal
);
3776 /* cpp_interpret_string has issued an error. */
3777 value
= error_mark_node
;
3780 obstack_free (&str_ob
, 0);
3785 /* Look up a literal operator with the name and the exact arguments. */
3788 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3791 decl
= lookup_name (name
);
3792 if (!decl
|| !is_overloaded_fn (decl
))
3793 return error_mark_node
;
3795 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3799 tree fn
= OVL_CURRENT (fns
);
3800 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3801 if (parmtypes
!= NULL_TREE
)
3803 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3804 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3806 tree tparm
= TREE_VALUE (parmtypes
);
3807 tree targ
= TREE_TYPE ((*args
)[ix
]);
3808 bool ptr
= TYPE_PTR_P (tparm
);
3809 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3810 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3812 || !same_type_p (TREE_TYPE (tparm
),
3817 && ix
== vec_safe_length (args
)
3818 /* May be this should be sufficient_parms_p instead,
3819 depending on how exactly should user-defined literals
3820 work in presence of default arguments on the literal
3821 operator parameters. */
3822 && parmtypes
== void_list_node
)
3827 return error_mark_node
;
3830 /* Parse a user-defined char constant. Returns a call to a user-defined
3831 literal operator taking the character as an argument. */
3834 cp_parser_userdef_char_literal (cp_parser
*parser
)
3836 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3837 tree literal
= token
->u
.value
;
3838 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3839 tree value
= USERDEF_LITERAL_VALUE (literal
);
3840 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3843 /* Build up a call to the user-defined operator */
3844 /* Lookup the name we got back from the id-expression. */
3845 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3846 vec_safe_push (args
, value
);
3847 decl
= lookup_literal_operator (name
, args
);
3848 if (!decl
|| decl
== error_mark_node
)
3850 error ("unable to find character literal operator %qD with %qT argument",
3851 name
, TREE_TYPE (value
));
3852 release_tree_vector (args
);
3853 return error_mark_node
;
3855 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3856 release_tree_vector (args
);
3857 if (result
!= error_mark_node
)
3860 error ("unable to find character literal operator %qD with %qT argument",
3861 name
, TREE_TYPE (value
));
3862 return error_mark_node
;
3865 /* A subroutine of cp_parser_userdef_numeric_literal to
3866 create a char... template parameter pack from a string node. */
3869 make_char_string_pack (tree value
)
3872 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3873 const char *str
= TREE_STRING_POINTER (value
);
3874 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3875 tree argvec
= make_tree_vec (1);
3877 /* Fill in CHARVEC with all of the parameters. */
3878 charvec
= make_tree_vec (len
);
3879 for (i
= 0; i
< len
; ++i
)
3880 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3882 /* Build the argument packs. */
3883 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3884 TREE_TYPE (argpack
) = char_type_node
;
3886 TREE_VEC_ELT (argvec
, 0) = argpack
;
3891 /* A subroutine of cp_parser_userdef_numeric_literal to
3892 create a char... template parameter pack from a string node. */
3895 make_string_pack (tree value
)
3898 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3899 const unsigned char *str
3900 = (const unsigned char *) TREE_STRING_POINTER (value
);
3901 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3902 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3903 tree argvec
= make_tree_vec (2);
3905 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3906 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3908 /* First template parm is character type. */
3909 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3911 /* Fill in CHARVEC with all of the parameters. */
3912 charvec
= make_tree_vec (len
);
3913 for (int i
= 0; i
< len
; ++i
)
3914 TREE_VEC_ELT (charvec
, i
)
3915 = double_int_to_tree (str_char_type_node
,
3916 double_int::from_buffer (str
+ i
* sz
, sz
));
3918 /* Build the argument packs. */
3919 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3920 TREE_TYPE (argpack
) = str_char_type_node
;
3922 TREE_VEC_ELT (argvec
, 1) = argpack
;
3927 /* Parse a user-defined numeric constant. returns a call to a user-defined
3928 literal operator. */
3931 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3933 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3934 tree literal
= token
->u
.value
;
3935 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3936 tree value
= USERDEF_LITERAL_VALUE (literal
);
3937 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3938 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3939 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3941 vec
<tree
, va_gc
> *args
;
3943 /* Look for a literal operator taking the exact type of numeric argument
3944 as the literal value. */
3945 args
= make_tree_vector ();
3946 vec_safe_push (args
, value
);
3947 decl
= lookup_literal_operator (name
, args
);
3948 if (decl
&& decl
!= error_mark_node
)
3950 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3951 if (result
!= error_mark_node
)
3953 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3954 warning_at (token
->location
, OPT_Woverflow
,
3955 "integer literal exceeds range of %qT type",
3956 long_long_unsigned_type_node
);
3960 warning_at (token
->location
, OPT_Woverflow
,
3961 "floating literal exceeds range of %qT type",
3962 long_double_type_node
);
3963 else if (overflow
< 0)
3964 warning_at (token
->location
, OPT_Woverflow
,
3965 "floating literal truncated to zero");
3967 release_tree_vector (args
);
3971 release_tree_vector (args
);
3973 /* If the numeric argument didn't work, look for a raw literal
3974 operator taking a const char* argument consisting of the number
3975 in string format. */
3976 args
= make_tree_vector ();
3977 vec_safe_push (args
, num_string
);
3978 decl
= lookup_literal_operator (name
, args
);
3979 if (decl
&& decl
!= error_mark_node
)
3981 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3982 if (result
!= error_mark_node
)
3984 release_tree_vector (args
);
3988 release_tree_vector (args
);
3990 /* If the raw literal didn't work, look for a non-type template
3991 function with parameter pack char.... Call the function with
3992 template parameter characters representing the number. */
3993 args
= make_tree_vector ();
3994 decl
= lookup_literal_operator (name
, args
);
3995 if (decl
&& decl
!= error_mark_node
)
3997 tree tmpl_args
= make_char_string_pack (num_string
);
3998 decl
= lookup_template_function (decl
, tmpl_args
);
3999 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4000 if (result
!= error_mark_node
)
4002 release_tree_vector (args
);
4006 release_tree_vector (args
);
4008 error ("unable to find numeric literal operator %qD", name
);
4009 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4010 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4011 "to enable more built-in suffixes");
4012 return error_mark_node
;
4015 /* Parse a user-defined string constant. Returns a call to a user-defined
4016 literal operator taking a character pointer and the length of the string
4020 cp_parser_userdef_string_literal (tree literal
)
4022 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4023 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4024 tree value
= USERDEF_LITERAL_VALUE (literal
);
4025 int len
= TREE_STRING_LENGTH (value
)
4026 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4028 vec
<tree
, va_gc
> *args
;
4030 /* Look for a template function with typename parameter CharT
4031 and parameter pack CharT... Call the function with
4032 template parameter characters representing the string. */
4033 args
= make_tree_vector ();
4034 decl
= lookup_literal_operator (name
, args
);
4035 if (decl
&& decl
!= error_mark_node
)
4037 tree tmpl_args
= make_string_pack (value
);
4038 decl
= lookup_template_function (decl
, tmpl_args
);
4039 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4040 if (result
!= error_mark_node
)
4042 release_tree_vector (args
);
4046 release_tree_vector (args
);
4048 /* Build up a call to the user-defined operator */
4049 /* Lookup the name we got back from the id-expression. */
4050 args
= make_tree_vector ();
4051 vec_safe_push (args
, value
);
4052 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4053 decl
= lookup_name (name
);
4054 if (!decl
|| decl
== error_mark_node
)
4056 error ("unable to find string literal operator %qD", name
);
4057 release_tree_vector (args
);
4058 return error_mark_node
;
4060 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4061 release_tree_vector (args
);
4062 if (result
!= error_mark_node
)
4065 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4066 name
, TREE_TYPE (value
), size_type_node
);
4067 return error_mark_node
;
4071 /* Basic concepts [gram.basic] */
4073 /* Parse a translation-unit.
4076 declaration-seq [opt]
4078 Returns TRUE if all went well. */
4081 cp_parser_translation_unit (cp_parser
* parser
)
4083 /* The address of the first non-permanent object on the declarator
4085 static void *declarator_obstack_base
;
4089 /* Create the declarator obstack, if necessary. */
4090 if (!cp_error_declarator
)
4092 gcc_obstack_init (&declarator_obstack
);
4093 /* Create the error declarator. */
4094 cp_error_declarator
= make_declarator (cdk_error
);
4095 /* Create the empty parameter list. */
4096 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4097 /* Remember where the base of the declarator obstack lies. */
4098 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4101 cp_parser_declaration_seq_opt (parser
);
4103 /* If there are no tokens left then all went well. */
4104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4106 /* Get rid of the token array; we don't need it any more. */
4107 cp_lexer_destroy (parser
->lexer
);
4108 parser
->lexer
= NULL
;
4110 /* This file might have been a context that's implicitly extern
4111 "C". If so, pop the lang context. (Only relevant for PCH.) */
4112 if (parser
->implicit_extern_c
)
4114 pop_lang_context ();
4115 parser
->implicit_extern_c
= false;
4119 finish_translation_unit ();
4125 cp_parser_error (parser
, "expected declaration");
4129 /* Make sure the declarator obstack was fully cleaned up. */
4130 gcc_assert (obstack_next_free (&declarator_obstack
)
4131 == declarator_obstack_base
);
4133 /* All went well. */
4137 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4138 decltype context. */
4140 static inline tsubst_flags_t
4141 complain_flags (bool decltype_p
)
4143 tsubst_flags_t complain
= tf_warning_or_error
;
4145 complain
|= tf_decltype
;
4149 /* We're about to parse a collection of statements. If we're currently
4150 parsing tentatively, set up a firewall so that any nested
4151 cp_parser_commit_to_tentative_parse won't affect the current context. */
4153 static cp_token_position
4154 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4156 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4159 cp_parser_parse_tentatively (parser
);
4160 cp_parser_commit_to_topmost_tentative_parse (parser
);
4161 return cp_lexer_token_position (parser
->lexer
, false);
4164 /* We've finished parsing the collection of statements. Wrap up the
4165 firewall and replace the relevant tokens with the parsed form. */
4168 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4174 /* Finish the firewall level. */
4175 cp_parser_parse_definitely (parser
);
4176 /* And remember the result of the parse for when we try again. */
4177 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4178 token
->type
= CPP_PREPARSED_EXPR
;
4179 token
->u
.value
= expr
;
4180 token
->keyword
= RID_MAX
;
4181 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4184 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4185 enclosing parentheses. */
4188 cp_parser_statement_expr (cp_parser
*parser
)
4190 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4192 /* Consume the '('. */
4193 cp_lexer_consume_token (parser
->lexer
);
4194 /* Start the statement-expression. */
4195 tree expr
= begin_stmt_expr ();
4196 /* Parse the compound-statement. */
4197 cp_parser_compound_statement (parser
, expr
, false, false);
4199 expr
= finish_stmt_expr (expr
, false);
4200 /* Consume the ')'. */
4201 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4202 cp_parser_skip_to_end_of_statement (parser
);
4204 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4208 /* Expressions [gram.expr] */
4210 /* Parse a primary-expression.
4217 lambda-expression (C++11)
4222 ( compound-statement )
4223 __builtin_va_arg ( assignment-expression , type-id )
4224 __builtin_offsetof ( type-id , offsetof-expression )
4227 __has_nothrow_assign ( type-id )
4228 __has_nothrow_constructor ( type-id )
4229 __has_nothrow_copy ( type-id )
4230 __has_trivial_assign ( type-id )
4231 __has_trivial_constructor ( type-id )
4232 __has_trivial_copy ( type-id )
4233 __has_trivial_destructor ( type-id )
4234 __has_virtual_destructor ( type-id )
4235 __is_abstract ( type-id )
4236 __is_base_of ( type-id , type-id )
4237 __is_class ( type-id )
4238 __is_empty ( type-id )
4239 __is_enum ( type-id )
4240 __is_final ( type-id )
4241 __is_literal_type ( type-id )
4242 __is_pod ( type-id )
4243 __is_polymorphic ( type-id )
4244 __is_std_layout ( type-id )
4245 __is_trivial ( type-id )
4246 __is_union ( type-id )
4248 Objective-C++ Extension:
4256 ADDRESS_P is true iff this expression was immediately preceded by
4257 "&" and therefore might denote a pointer-to-member. CAST_P is true
4258 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4259 true iff this expression is a template argument.
4261 Returns a representation of the expression. Upon return, *IDK
4262 indicates what kind of id-expression (if any) was present. */
4265 cp_parser_primary_expression (cp_parser
*parser
,
4268 bool template_arg_p
,
4272 cp_token
*token
= NULL
;
4274 /* Assume the primary expression is not an id-expression. */
4275 *idk
= CP_ID_KIND_NONE
;
4277 /* Peek at the next token. */
4278 token
= cp_lexer_peek_token (parser
->lexer
);
4279 switch ((int) token
->type
)
4288 user-defined-literal */
4294 case CPP_PREPARSED_EXPR
:
4295 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4296 return cp_parser_userdef_numeric_literal (parser
);
4297 token
= cp_lexer_consume_token (parser
->lexer
);
4298 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4300 error_at (token
->location
,
4301 "fixed-point types not supported in C++");
4302 return error_mark_node
;
4304 /* Floating-point literals are only allowed in an integral
4305 constant expression if they are cast to an integral or
4306 enumeration type. */
4307 if (TREE_CODE (token
->u
.value
) == REAL_CST
4308 && parser
->integral_constant_expression_p
4311 /* CAST_P will be set even in invalid code like "int(2.7 +
4312 ...)". Therefore, we have to check that the next token
4313 is sure to end the cast. */
4316 cp_token
*next_token
;
4318 next_token
= cp_lexer_peek_token (parser
->lexer
);
4319 if (/* The comma at the end of an
4320 enumerator-definition. */
4321 next_token
->type
!= CPP_COMMA
4322 /* The curly brace at the end of an enum-specifier. */
4323 && next_token
->type
!= CPP_CLOSE_BRACE
4324 /* The end of a statement. */
4325 && next_token
->type
!= CPP_SEMICOLON
4326 /* The end of the cast-expression. */
4327 && next_token
->type
!= CPP_CLOSE_PAREN
4328 /* The end of an array bound. */
4329 && next_token
->type
!= CPP_CLOSE_SQUARE
4330 /* The closing ">" in a template-argument-list. */
4331 && (next_token
->type
!= CPP_GREATER
4332 || parser
->greater_than_is_operator_p
)
4333 /* C++0x only: A ">>" treated like two ">" tokens,
4334 in a template-argument-list. */
4335 && (next_token
->type
!= CPP_RSHIFT
4336 || (cxx_dialect
== cxx98
)
4337 || parser
->greater_than_is_operator_p
))
4341 /* If we are within a cast, then the constraint that the
4342 cast is to an integral or enumeration type will be
4343 checked at that point. If we are not within a cast, then
4344 this code is invalid. */
4346 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4348 return token
->u
.value
;
4350 case CPP_CHAR_USERDEF
:
4351 case CPP_CHAR16_USERDEF
:
4352 case CPP_CHAR32_USERDEF
:
4353 case CPP_WCHAR_USERDEF
:
4354 return cp_parser_userdef_char_literal (parser
);
4360 case CPP_UTF8STRING
:
4361 case CPP_STRING_USERDEF
:
4362 case CPP_STRING16_USERDEF
:
4363 case CPP_STRING32_USERDEF
:
4364 case CPP_WSTRING_USERDEF
:
4365 case CPP_UTF8STRING_USERDEF
:
4366 /* ??? Should wide strings be allowed when parser->translate_strings_p
4367 is false (i.e. in attributes)? If not, we can kill the third
4368 argument to cp_parser_string_literal. */
4369 return cp_parser_string_literal (parser
,
4370 parser
->translate_strings_p
,
4373 case CPP_OPEN_PAREN
:
4374 /* If we see `( { ' then we are looking at the beginning of
4375 a GNU statement-expression. */
4376 if (cp_parser_allow_gnu_extensions_p (parser
)
4377 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4379 /* Statement-expressions are not allowed by the standard. */
4380 pedwarn (token
->location
, OPT_Wpedantic
,
4381 "ISO C++ forbids braced-groups within expressions");
4383 /* And they're not allowed outside of a function-body; you
4384 cannot, for example, write:
4386 int i = ({ int j = 3; j + 1; });
4388 at class or namespace scope. */
4389 if (!parser
->in_function_body
4390 || parser
->in_template_argument_list_p
)
4392 error_at (token
->location
,
4393 "statement-expressions are not allowed outside "
4394 "functions nor in template-argument lists");
4395 cp_parser_skip_to_end_of_block_or_statement (parser
);
4396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4397 cp_lexer_consume_token (parser
->lexer
);
4398 return error_mark_node
;
4401 return cp_parser_statement_expr (parser
);
4403 /* Otherwise it's a normal parenthesized expression. */
4406 bool saved_greater_than_is_operator_p
;
4408 /* Consume the `('. */
4409 cp_lexer_consume_token (parser
->lexer
);
4410 /* Within a parenthesized expression, a `>' token is always
4411 the greater-than operator. */
4412 saved_greater_than_is_operator_p
4413 = parser
->greater_than_is_operator_p
;
4414 parser
->greater_than_is_operator_p
= true;
4416 /* Parse the parenthesized expression. */
4417 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4418 /* Let the front end know that this expression was
4419 enclosed in parentheses. This matters in case, for
4420 example, the expression is of the form `A::B', since
4421 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4423 expr
= finish_parenthesized_expr (expr
);
4424 /* DR 705: Wrapping an unqualified name in parentheses
4425 suppresses arg-dependent lookup. We want to pass back
4426 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4427 (c++/37862), but none of the others. */
4428 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4429 *idk
= CP_ID_KIND_NONE
;
4431 /* The `>' token might be the end of a template-id or
4432 template-parameter-list now. */
4433 parser
->greater_than_is_operator_p
4434 = saved_greater_than_is_operator_p
;
4435 /* Consume the `)'. */
4436 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4437 cp_parser_skip_to_end_of_statement (parser
);
4442 case CPP_OPEN_SQUARE
:
4443 if (c_dialect_objc ())
4444 /* We have an Objective-C++ message. */
4445 return cp_parser_objc_expression (parser
);
4447 tree lam
= cp_parser_lambda_expression (parser
);
4448 /* Don't warn about a failed tentative parse. */
4449 if (cp_parser_error_occurred (parser
))
4450 return error_mark_node
;
4451 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4455 case CPP_OBJC_STRING
:
4456 if (c_dialect_objc ())
4457 /* We have an Objective-C++ string literal. */
4458 return cp_parser_objc_expression (parser
);
4459 cp_parser_error (parser
, "expected primary-expression");
4460 return error_mark_node
;
4463 switch (token
->keyword
)
4465 /* These two are the boolean literals. */
4467 cp_lexer_consume_token (parser
->lexer
);
4468 return boolean_true_node
;
4470 cp_lexer_consume_token (parser
->lexer
);
4471 return boolean_false_node
;
4473 /* The `__null' literal. */
4475 cp_lexer_consume_token (parser
->lexer
);
4478 /* The `nullptr' literal. */
4480 cp_lexer_consume_token (parser
->lexer
);
4481 return nullptr_node
;
4483 /* Recognize the `this' keyword. */
4485 cp_lexer_consume_token (parser
->lexer
);
4486 if (parser
->local_variables_forbidden_p
)
4488 error_at (token
->location
,
4489 "%<this%> may not be used in this context");
4490 return error_mark_node
;
4492 /* Pointers cannot appear in constant-expressions. */
4493 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4494 return error_mark_node
;
4495 return finish_this_expr ();
4497 /* The `operator' keyword can be the beginning of an
4502 case RID_FUNCTION_NAME
:
4503 case RID_PRETTY_FUNCTION_NAME
:
4504 case RID_C99_FUNCTION_NAME
:
4506 non_integral_constant name
;
4508 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4509 __func__ are the names of variables -- but they are
4510 treated specially. Therefore, they are handled here,
4511 rather than relying on the generic id-expression logic
4512 below. Grammatically, these names are id-expressions.
4514 Consume the token. */
4515 token
= cp_lexer_consume_token (parser
->lexer
);
4517 switch (token
->keyword
)
4519 case RID_FUNCTION_NAME
:
4520 name
= NIC_FUNC_NAME
;
4522 case RID_PRETTY_FUNCTION_NAME
:
4523 name
= NIC_PRETTY_FUNC
;
4525 case RID_C99_FUNCTION_NAME
:
4526 name
= NIC_C99_FUNC
;
4532 if (cp_parser_non_integral_constant_expression (parser
, name
))
4533 return error_mark_node
;
4535 /* Look up the name. */
4536 return finish_fname (token
->u
.value
);
4543 source_location type_location
;
4545 /* The `__builtin_va_arg' construct is used to handle
4546 `va_arg'. Consume the `__builtin_va_arg' token. */
4547 cp_lexer_consume_token (parser
->lexer
);
4548 /* Look for the opening `('. */
4549 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4550 /* Now, parse the assignment-expression. */
4551 expression
= cp_parser_assignment_expression (parser
);
4552 /* Look for the `,'. */
4553 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4554 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4555 /* Parse the type-id. */
4556 type
= cp_parser_type_id (parser
);
4557 /* Look for the closing `)'. */
4558 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4559 /* Using `va_arg' in a constant-expression is not
4561 if (cp_parser_non_integral_constant_expression (parser
,
4563 return error_mark_node
;
4564 return build_x_va_arg (type_location
, expression
, type
);
4568 return cp_parser_builtin_offsetof (parser
);
4570 case RID_HAS_NOTHROW_ASSIGN
:
4571 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4572 case RID_HAS_NOTHROW_COPY
:
4573 case RID_HAS_TRIVIAL_ASSIGN
:
4574 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4575 case RID_HAS_TRIVIAL_COPY
:
4576 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4577 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4578 case RID_IS_ABSTRACT
:
4579 case RID_IS_BASE_OF
:
4584 case RID_IS_LITERAL_TYPE
:
4586 case RID_IS_POLYMORPHIC
:
4587 case RID_IS_STD_LAYOUT
:
4588 case RID_IS_TRIVIAL
:
4589 case RID_IS_TRIVIALLY_ASSIGNABLE
:
4590 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
4591 case RID_IS_TRIVIALLY_COPYABLE
:
4593 return cp_parser_trait_expr (parser
, token
->keyword
);
4595 /* Objective-C++ expressions. */
4597 case RID_AT_PROTOCOL
:
4598 case RID_AT_SELECTOR
:
4599 return cp_parser_objc_expression (parser
);
4602 if (parser
->in_function_body
4603 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4606 error_at (token
->location
,
4607 "a template declaration cannot appear at block scope");
4608 cp_parser_skip_to_end_of_block_or_statement (parser
);
4609 return error_mark_node
;
4612 cp_parser_error (parser
, "expected primary-expression");
4613 return error_mark_node
;
4616 /* An id-expression can start with either an identifier, a
4617 `::' as the beginning of a qualified-id, or the "operator"
4621 case CPP_TEMPLATE_ID
:
4622 case CPP_NESTED_NAME_SPECIFIER
:
4626 const char *error_msg
;
4629 cp_token
*id_expr_token
;
4632 /* Parse the id-expression. */
4634 = cp_parser_id_expression (parser
,
4635 /*template_keyword_p=*/false,
4636 /*check_dependency_p=*/true,
4638 /*declarator_p=*/false,
4639 /*optional_p=*/false);
4640 if (id_expression
== error_mark_node
)
4641 return error_mark_node
;
4642 id_expr_token
= token
;
4643 token
= cp_lexer_peek_token (parser
->lexer
);
4644 done
= (token
->type
!= CPP_OPEN_SQUARE
4645 && token
->type
!= CPP_OPEN_PAREN
4646 && token
->type
!= CPP_DOT
4647 && token
->type
!= CPP_DEREF
4648 && token
->type
!= CPP_PLUS_PLUS
4649 && token
->type
!= CPP_MINUS_MINUS
);
4650 /* If we have a template-id, then no further lookup is
4651 required. If the template-id was for a template-class, we
4652 will sometimes have a TYPE_DECL at this point. */
4653 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4654 || TREE_CODE (id_expression
) == TYPE_DECL
)
4655 decl
= id_expression
;
4656 /* Look up the name. */
4659 tree ambiguous_decls
;
4661 /* If we already know that this lookup is ambiguous, then
4662 we've already issued an error message; there's no reason
4664 if (id_expr_token
->type
== CPP_NAME
4665 && id_expr_token
->error_reported
)
4667 cp_parser_simulate_error (parser
);
4668 return error_mark_node
;
4671 decl
= cp_parser_lookup_name (parser
, id_expression
,
4674 /*is_namespace=*/false,
4675 /*check_dependency=*/true,
4677 id_expr_token
->location
);
4678 /* If the lookup was ambiguous, an error will already have
4680 if (ambiguous_decls
)
4681 return error_mark_node
;
4683 /* In Objective-C++, we may have an Objective-C 2.0
4684 dot-syntax for classes here. */
4685 if (c_dialect_objc ()
4686 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4687 && TREE_CODE (decl
) == TYPE_DECL
4688 && objc_is_class_name (decl
))
4691 cp_lexer_consume_token (parser
->lexer
);
4692 component
= cp_parser_identifier (parser
);
4693 if (component
== error_mark_node
)
4694 return error_mark_node
;
4696 return objc_build_class_component_ref (id_expression
, component
);
4699 /* In Objective-C++, an instance variable (ivar) may be preferred
4700 to whatever cp_parser_lookup_name() found. */
4701 decl
= objc_lookup_ivar (decl
, id_expression
);
4703 /* If name lookup gives us a SCOPE_REF, then the
4704 qualifying scope was dependent. */
4705 if (TREE_CODE (decl
) == SCOPE_REF
)
4707 /* At this point, we do not know if DECL is a valid
4708 integral constant expression. We assume that it is
4709 in fact such an expression, so that code like:
4711 template <int N> struct A {
4715 is accepted. At template-instantiation time, we
4716 will check that B<N>::i is actually a constant. */
4719 /* Check to see if DECL is a local variable in a context
4720 where that is forbidden. */
4721 if (parser
->local_variables_forbidden_p
4722 && local_variable_p (decl
))
4724 /* It might be that we only found DECL because we are
4725 trying to be generous with pre-ISO scoping rules.
4726 For example, consider:
4730 for (int i = 0; i < 10; ++i) {}
4731 extern void f(int j = i);
4734 Here, name look up will originally find the out
4735 of scope `i'. We need to issue a warning message,
4736 but then use the global `i'. */
4737 decl
= check_for_out_of_scope_variable (decl
);
4738 if (local_variable_p (decl
))
4740 error_at (id_expr_token
->location
,
4741 "local variable %qD may not appear in this context",
4743 return error_mark_node
;
4748 decl
= (finish_id_expression
4749 (id_expression
, decl
, parser
->scope
,
4751 parser
->integral_constant_expression_p
,
4752 parser
->allow_non_integral_constant_expression_p
,
4753 &parser
->non_integral_constant_expression_p
,
4754 template_p
, done
, address_p
,
4757 id_expr_token
->location
));
4759 cp_parser_error (parser
, error_msg
);
4763 /* Anything else is an error. */
4765 cp_parser_error (parser
, "expected primary-expression");
4766 return error_mark_node
;
4771 cp_parser_primary_expression (cp_parser
*parser
,
4774 bool template_arg_p
,
4777 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4778 /*decltype*/false, idk
);
4781 /* Parse an id-expression.
4788 :: [opt] nested-name-specifier template [opt] unqualified-id
4790 :: operator-function-id
4793 Return a representation of the unqualified portion of the
4794 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4795 a `::' or nested-name-specifier.
4797 Often, if the id-expression was a qualified-id, the caller will
4798 want to make a SCOPE_REF to represent the qualified-id. This
4799 function does not do this in order to avoid wastefully creating
4800 SCOPE_REFs when they are not required.
4802 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4805 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4806 uninstantiated templates.
4808 If *TEMPLATE_P is non-NULL, it is set to true iff the
4809 `template' keyword is used to explicitly indicate that the entity
4810 named is a template.
4812 If DECLARATOR_P is true, the id-expression is appearing as part of
4813 a declarator, rather than as part of an expression. */
4816 cp_parser_id_expression (cp_parser
*parser
,
4817 bool template_keyword_p
,
4818 bool check_dependency_p
,
4823 bool global_scope_p
;
4824 bool nested_name_specifier_p
;
4826 /* Assume the `template' keyword was not used. */
4828 *template_p
= template_keyword_p
;
4830 /* Look for the optional `::' operator. */
4832 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4834 /* Look for the optional nested-name-specifier. */
4835 nested_name_specifier_p
4836 = (cp_parser_nested_name_specifier_opt (parser
,
4837 /*typename_keyword_p=*/false,
4842 /* If there is a nested-name-specifier, then we are looking at
4843 the first qualified-id production. */
4844 if (nested_name_specifier_p
)
4847 tree saved_object_scope
;
4848 tree saved_qualifying_scope
;
4849 tree unqualified_id
;
4852 /* See if the next token is the `template' keyword. */
4854 template_p
= &is_template
;
4855 *template_p
= cp_parser_optional_template_keyword (parser
);
4856 /* Name lookup we do during the processing of the
4857 unqualified-id might obliterate SCOPE. */
4858 saved_scope
= parser
->scope
;
4859 saved_object_scope
= parser
->object_scope
;
4860 saved_qualifying_scope
= parser
->qualifying_scope
;
4861 /* Process the final unqualified-id. */
4862 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4865 /*optional_p=*/false);
4866 /* Restore the SAVED_SCOPE for our caller. */
4867 parser
->scope
= saved_scope
;
4868 parser
->object_scope
= saved_object_scope
;
4869 parser
->qualifying_scope
= saved_qualifying_scope
;
4871 return unqualified_id
;
4873 /* Otherwise, if we are in global scope, then we are looking at one
4874 of the other qualified-id productions. */
4875 else if (global_scope_p
)
4880 /* Peek at the next token. */
4881 token
= cp_lexer_peek_token (parser
->lexer
);
4883 /* If it's an identifier, and the next token is not a "<", then
4884 we can avoid the template-id case. This is an optimization
4885 for this common case. */
4886 if (token
->type
== CPP_NAME
4887 && !cp_parser_nth_token_starts_template_argument_list_p
4889 return cp_parser_identifier (parser
);
4891 cp_parser_parse_tentatively (parser
);
4892 /* Try a template-id. */
4893 id
= cp_parser_template_id (parser
,
4894 /*template_keyword_p=*/false,
4895 /*check_dependency_p=*/true,
4898 /* If that worked, we're done. */
4899 if (cp_parser_parse_definitely (parser
))
4902 /* Peek at the next token. (Changes in the token buffer may
4903 have invalidated the pointer obtained above.) */
4904 token
= cp_lexer_peek_token (parser
->lexer
);
4906 switch (token
->type
)
4909 return cp_parser_identifier (parser
);
4912 if (token
->keyword
== RID_OPERATOR
)
4913 return cp_parser_operator_function_id (parser
);
4917 cp_parser_error (parser
, "expected id-expression");
4918 return error_mark_node
;
4922 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4923 /*check_dependency_p=*/true,
4928 /* Parse an unqualified-id.
4932 operator-function-id
4933 conversion-function-id
4937 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4938 keyword, in a construct like `A::template ...'.
4940 Returns a representation of unqualified-id. For the `identifier'
4941 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4942 production a BIT_NOT_EXPR is returned; the operand of the
4943 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4944 other productions, see the documentation accompanying the
4945 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4946 names are looked up in uninstantiated templates. If DECLARATOR_P
4947 is true, the unqualified-id is appearing as part of a declarator,
4948 rather than as part of an expression. */
4951 cp_parser_unqualified_id (cp_parser
* parser
,
4952 bool template_keyword_p
,
4953 bool check_dependency_p
,
4959 /* Peek at the next token. */
4960 token
= cp_lexer_peek_token (parser
->lexer
);
4962 switch ((int) token
->type
)
4968 /* We don't know yet whether or not this will be a
4970 cp_parser_parse_tentatively (parser
);
4971 /* Try a template-id. */
4972 id
= cp_parser_template_id (parser
, template_keyword_p
,
4976 /* If it worked, we're done. */
4977 if (cp_parser_parse_definitely (parser
))
4979 /* Otherwise, it's an ordinary identifier. */
4980 return cp_parser_identifier (parser
);
4983 case CPP_TEMPLATE_ID
:
4984 return cp_parser_template_id (parser
, template_keyword_p
,
4992 tree qualifying_scope
;
4997 /* Consume the `~' token. */
4998 cp_lexer_consume_token (parser
->lexer
);
4999 /* Parse the class-name. The standard, as written, seems to
5002 template <typename T> struct S { ~S (); };
5003 template <typename T> S<T>::~S() {}
5005 is invalid, since `~' must be followed by a class-name, but
5006 `S<T>' is dependent, and so not known to be a class.
5007 That's not right; we need to look in uninstantiated
5008 templates. A further complication arises from:
5010 template <typename T> void f(T t) {
5014 Here, it is not possible to look up `T' in the scope of `T'
5015 itself. We must look in both the current scope, and the
5016 scope of the containing complete expression.
5018 Yet another issue is:
5027 The standard does not seem to say that the `S' in `~S'
5028 should refer to the type `S' and not the data member
5031 /* DR 244 says that we look up the name after the "~" in the
5032 same scope as we looked up the qualifying name. That idea
5033 isn't fully worked out; it's more complicated than that. */
5034 scope
= parser
->scope
;
5035 object_scope
= parser
->object_scope
;
5036 qualifying_scope
= parser
->qualifying_scope
;
5038 /* Check for invalid scopes. */
5039 if (scope
== error_mark_node
)
5041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5042 cp_lexer_consume_token (parser
->lexer
);
5043 return error_mark_node
;
5045 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5047 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5048 error_at (token
->location
,
5049 "scope %qT before %<~%> is not a class-name",
5051 cp_parser_simulate_error (parser
);
5052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5053 cp_lexer_consume_token (parser
->lexer
);
5054 return error_mark_node
;
5056 gcc_assert (!scope
|| TYPE_P (scope
));
5058 /* If the name is of the form "X::~X" it's OK even if X is a
5060 token
= cp_lexer_peek_token (parser
->lexer
);
5062 && token
->type
== CPP_NAME
5063 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5065 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5066 || (CLASS_TYPE_P (scope
)
5067 && constructor_name_p (token
->u
.value
, scope
))))
5069 cp_lexer_consume_token (parser
->lexer
);
5070 return build_nt (BIT_NOT_EXPR
, scope
);
5073 /* ~auto means the destructor of whatever the object is. */
5074 if (cp_parser_is_keyword (token
, RID_AUTO
))
5076 if (cxx_dialect
< cxx14
)
5077 pedwarn (input_location
, 0,
5078 "%<~auto%> only available with "
5079 "-std=c++14 or -std=gnu++14");
5080 cp_lexer_consume_token (parser
->lexer
);
5081 return build_nt (BIT_NOT_EXPR
, make_auto ());
5084 /* If there was an explicit qualification (S::~T), first look
5085 in the scope given by the qualification (i.e., S).
5087 Note: in the calls to cp_parser_class_name below we pass
5088 typename_type so that lookup finds the injected-class-name
5089 rather than the constructor. */
5091 type_decl
= NULL_TREE
;
5094 cp_parser_parse_tentatively (parser
);
5095 type_decl
= cp_parser_class_name (parser
,
5096 /*typename_keyword_p=*/false,
5097 /*template_keyword_p=*/false,
5099 /*check_dependency=*/false,
5100 /*class_head_p=*/false,
5102 if (cp_parser_parse_definitely (parser
))
5105 /* In "N::S::~S", look in "N" as well. */
5106 if (!done
&& scope
&& qualifying_scope
)
5108 cp_parser_parse_tentatively (parser
);
5109 parser
->scope
= qualifying_scope
;
5110 parser
->object_scope
= NULL_TREE
;
5111 parser
->qualifying_scope
= NULL_TREE
;
5113 = cp_parser_class_name (parser
,
5114 /*typename_keyword_p=*/false,
5115 /*template_keyword_p=*/false,
5117 /*check_dependency=*/false,
5118 /*class_head_p=*/false,
5120 if (cp_parser_parse_definitely (parser
))
5123 /* In "p->S::~T", look in the scope given by "*p" as well. */
5124 else if (!done
&& object_scope
)
5126 cp_parser_parse_tentatively (parser
);
5127 parser
->scope
= object_scope
;
5128 parser
->object_scope
= NULL_TREE
;
5129 parser
->qualifying_scope
= NULL_TREE
;
5131 = cp_parser_class_name (parser
,
5132 /*typename_keyword_p=*/false,
5133 /*template_keyword_p=*/false,
5135 /*check_dependency=*/false,
5136 /*class_head_p=*/false,
5138 if (cp_parser_parse_definitely (parser
))
5141 /* Look in the surrounding context. */
5144 parser
->scope
= NULL_TREE
;
5145 parser
->object_scope
= NULL_TREE
;
5146 parser
->qualifying_scope
= NULL_TREE
;
5147 if (processing_template_decl
)
5148 cp_parser_parse_tentatively (parser
);
5150 = cp_parser_class_name (parser
,
5151 /*typename_keyword_p=*/false,
5152 /*template_keyword_p=*/false,
5154 /*check_dependency=*/false,
5155 /*class_head_p=*/false,
5157 if (processing_template_decl
5158 && ! cp_parser_parse_definitely (parser
))
5160 /* We couldn't find a type with this name, so just accept
5161 it and check for a match at instantiation time. */
5162 type_decl
= cp_parser_identifier (parser
);
5163 if (type_decl
!= error_mark_node
)
5164 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5168 /* If an error occurred, assume that the name of the
5169 destructor is the same as the name of the qualifying
5170 class. That allows us to keep parsing after running
5171 into ill-formed destructor names. */
5172 if (type_decl
== error_mark_node
&& scope
)
5173 return build_nt (BIT_NOT_EXPR
, scope
);
5174 else if (type_decl
== error_mark_node
)
5175 return error_mark_node
;
5177 /* Check that destructor name and scope match. */
5178 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5180 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5181 error_at (token
->location
,
5182 "declaration of %<~%T%> as member of %qT",
5184 cp_parser_simulate_error (parser
);
5185 return error_mark_node
;
5190 A typedef-name that names a class shall not be used as the
5191 identifier in the declarator for a destructor declaration. */
5193 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5194 && !DECL_SELF_REFERENCE_P (type_decl
)
5195 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5196 error_at (token
->location
,
5197 "typedef-name %qD used as destructor declarator",
5200 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5204 if (token
->keyword
== RID_OPERATOR
)
5208 /* This could be a template-id, so we try that first. */
5209 cp_parser_parse_tentatively (parser
);
5210 /* Try a template-id. */
5211 id
= cp_parser_template_id (parser
, template_keyword_p
,
5212 /*check_dependency_p=*/true,
5215 /* If that worked, we're done. */
5216 if (cp_parser_parse_definitely (parser
))
5218 /* We still don't know whether we're looking at an
5219 operator-function-id or a conversion-function-id. */
5220 cp_parser_parse_tentatively (parser
);
5221 /* Try an operator-function-id. */
5222 id
= cp_parser_operator_function_id (parser
);
5223 /* If that didn't work, try a conversion-function-id. */
5224 if (!cp_parser_parse_definitely (parser
))
5225 id
= cp_parser_conversion_function_id (parser
);
5226 else if (UDLIT_OPER_P (id
))
5229 const char *name
= UDLIT_OP_SUFFIX (id
);
5230 if (name
[0] != '_' && !in_system_header_at (input_location
)
5232 warning (0, "literal operator suffixes not preceded by %<_%>"
5233 " are reserved for future standardization");
5243 cp_parser_error (parser
, "expected unqualified-id");
5244 return error_mark_node
;
5248 /* Parse an (optional) nested-name-specifier.
5250 nested-name-specifier: [C++98]
5251 class-or-namespace-name :: nested-name-specifier [opt]
5252 class-or-namespace-name :: template nested-name-specifier [opt]
5254 nested-name-specifier: [C++0x]
5257 nested-name-specifier identifier ::
5258 nested-name-specifier template [opt] simple-template-id ::
5260 PARSER->SCOPE should be set appropriately before this function is
5261 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5262 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5265 Sets PARSER->SCOPE to the class (TYPE) or namespace
5266 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5267 it unchanged if there is no nested-name-specifier. Returns the new
5268 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5270 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5271 part of a declaration and/or decl-specifier. */
5274 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5275 bool typename_keyword_p
,
5276 bool check_dependency_p
,
5278 bool is_declaration
)
5280 bool success
= false;
5281 cp_token_position start
= 0;
5284 /* Remember where the nested-name-specifier starts. */
5285 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5287 start
= cp_lexer_token_position (parser
->lexer
, false);
5288 push_deferring_access_checks (dk_deferred
);
5295 tree saved_qualifying_scope
;
5296 bool template_keyword_p
;
5298 /* Spot cases that cannot be the beginning of a
5299 nested-name-specifier. */
5300 token
= cp_lexer_peek_token (parser
->lexer
);
5302 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5303 the already parsed nested-name-specifier. */
5304 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5306 /* Grab the nested-name-specifier and continue the loop. */
5307 cp_parser_pre_parsed_nested_name_specifier (parser
);
5308 /* If we originally encountered this nested-name-specifier
5309 with IS_DECLARATION set to false, we will not have
5310 resolved TYPENAME_TYPEs, so we must do so here. */
5312 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5314 new_scope
= resolve_typename_type (parser
->scope
,
5315 /*only_current_p=*/false);
5316 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5317 parser
->scope
= new_scope
;
5323 /* Spot cases that cannot be the beginning of a
5324 nested-name-specifier. On the second and subsequent times
5325 through the loop, we look for the `template' keyword. */
5326 if (success
&& token
->keyword
== RID_TEMPLATE
)
5328 /* A template-id can start a nested-name-specifier. */
5329 else if (token
->type
== CPP_TEMPLATE_ID
)
5331 /* DR 743: decltype can be used in a nested-name-specifier. */
5332 else if (token_is_decltype (token
))
5336 /* If the next token is not an identifier, then it is
5337 definitely not a type-name or namespace-name. */
5338 if (token
->type
!= CPP_NAME
)
5340 /* If the following token is neither a `<' (to begin a
5341 template-id), nor a `::', then we are not looking at a
5342 nested-name-specifier. */
5343 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5345 if (token
->type
== CPP_COLON
5346 && parser
->colon_corrects_to_scope_p
5347 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5349 error_at (token
->location
,
5350 "found %<:%> in nested-name-specifier, expected %<::%>");
5351 token
->type
= CPP_SCOPE
;
5354 if (token
->type
!= CPP_SCOPE
5355 && !cp_parser_nth_token_starts_template_argument_list_p
5360 /* The nested-name-specifier is optional, so we parse
5362 cp_parser_parse_tentatively (parser
);
5364 /* Look for the optional `template' keyword, if this isn't the
5365 first time through the loop. */
5367 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5369 template_keyword_p
= false;
5371 /* Save the old scope since the name lookup we are about to do
5372 might destroy it. */
5373 old_scope
= parser
->scope
;
5374 saved_qualifying_scope
= parser
->qualifying_scope
;
5375 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5376 look up names in "X<T>::I" in order to determine that "Y" is
5377 a template. So, if we have a typename at this point, we make
5378 an effort to look through it. */
5380 && !typename_keyword_p
5382 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5383 parser
->scope
= resolve_typename_type (parser
->scope
,
5384 /*only_current_p=*/false);
5385 /* Parse the qualifying entity. */
5387 = cp_parser_qualifying_entity (parser
,
5393 /* Look for the `::' token. */
5394 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5396 /* If we found what we wanted, we keep going; otherwise, we're
5398 if (!cp_parser_parse_definitely (parser
))
5400 bool error_p
= false;
5402 /* Restore the OLD_SCOPE since it was valid before the
5403 failed attempt at finding the last
5404 class-or-namespace-name. */
5405 parser
->scope
= old_scope
;
5406 parser
->qualifying_scope
= saved_qualifying_scope
;
5408 /* If the next token is a decltype, and the one after that is a
5409 `::', then the decltype has failed to resolve to a class or
5410 enumeration type. Give this error even when parsing
5411 tentatively since it can't possibly be valid--and we're going
5412 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5413 won't get another chance.*/
5414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5415 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5418 token
= cp_lexer_consume_token (parser
->lexer
);
5419 error_at (token
->location
, "decltype evaluates to %qT, "
5420 "which is not a class or enumeration type",
5422 parser
->scope
= error_mark_node
;
5426 cp_lexer_consume_token (parser
->lexer
);
5429 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5431 /* If the next token is an identifier, and the one after
5432 that is a `::', then any valid interpretation would have
5433 found a class-or-namespace-name. */
5434 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5435 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5437 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5440 token
= cp_lexer_consume_token (parser
->lexer
);
5443 if (!token
->error_reported
)
5446 tree ambiguous_decls
;
5448 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5450 /*is_template=*/false,
5451 /*is_namespace=*/false,
5452 /*check_dependency=*/true,
5455 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5456 error_at (token
->location
,
5457 "%qD used without template parameters",
5459 else if (ambiguous_decls
)
5461 // cp_parser_lookup_name has the same diagnostic,
5462 // thus make sure to emit it at most once.
5463 if (cp_parser_uncommitted_to_tentative_parse_p
5466 error_at (token
->location
,
5467 "reference to %qD is ambiguous",
5469 print_candidates (ambiguous_decls
);
5471 decl
= error_mark_node
;
5475 if (cxx_dialect
!= cxx98
)
5476 cp_parser_name_lookup_error
5477 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5480 cp_parser_name_lookup_error
5481 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5485 parser
->scope
= error_mark_node
;
5487 /* Treat this as a successful nested-name-specifier
5492 If the name found is not a class-name (clause
5493 _class_) or namespace-name (_namespace.def_), the
5494 program is ill-formed. */
5497 cp_lexer_consume_token (parser
->lexer
);
5501 /* We've found one valid nested-name-specifier. */
5503 /* Name lookup always gives us a DECL. */
5504 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5505 new_scope
= TREE_TYPE (new_scope
);
5506 /* Uses of "template" must be followed by actual templates. */
5507 if (template_keyword_p
5508 && !(CLASS_TYPE_P (new_scope
)
5509 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5510 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5511 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5512 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5513 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5514 == TEMPLATE_ID_EXPR
)))
5515 permerror (input_location
, TYPE_P (new_scope
)
5516 ? G_("%qT is not a template")
5517 : G_("%qD is not a template"),
5519 /* If it is a class scope, try to complete it; we are about to
5520 be looking up names inside the class. */
5521 if (TYPE_P (new_scope
)
5522 /* Since checking types for dependency can be expensive,
5523 avoid doing it if the type is already complete. */
5524 && !COMPLETE_TYPE_P (new_scope
)
5525 /* Do not try to complete dependent types. */
5526 && !dependent_type_p (new_scope
))
5528 new_scope
= complete_type (new_scope
);
5529 /* If it is a typedef to current class, use the current
5530 class instead, as the typedef won't have any names inside
5532 if (!COMPLETE_TYPE_P (new_scope
)
5533 && currently_open_class (new_scope
))
5534 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5536 /* Make sure we look in the right scope the next time through
5538 parser
->scope
= new_scope
;
5541 /* If parsing tentatively, replace the sequence of tokens that makes
5542 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5543 token. That way, should we re-parse the token stream, we will
5544 not have to repeat the effort required to do the parse, nor will
5545 we issue duplicate error messages. */
5546 if (success
&& start
)
5550 token
= cp_lexer_token_at (parser
->lexer
, start
);
5551 /* Reset the contents of the START token. */
5552 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5553 /* Retrieve any deferred checks. Do not pop this access checks yet
5554 so the memory will not be reclaimed during token replacing below. */
5555 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
5556 token
->u
.tree_check_value
->value
= parser
->scope
;
5557 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5558 token
->u
.tree_check_value
->qualifying_scope
=
5559 parser
->qualifying_scope
;
5560 token
->keyword
= RID_MAX
;
5562 /* Purge all subsequent tokens. */
5563 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5567 pop_to_parent_deferring_access_checks ();
5569 return success
? parser
->scope
: NULL_TREE
;
5572 /* Parse a nested-name-specifier. See
5573 cp_parser_nested_name_specifier_opt for details. This function
5574 behaves identically, except that it will an issue an error if no
5575 nested-name-specifier is present. */
5578 cp_parser_nested_name_specifier (cp_parser
*parser
,
5579 bool typename_keyword_p
,
5580 bool check_dependency_p
,
5582 bool is_declaration
)
5586 /* Look for the nested-name-specifier. */
5587 scope
= cp_parser_nested_name_specifier_opt (parser
,
5592 /* If it was not present, issue an error message. */
5595 cp_parser_error (parser
, "expected nested-name-specifier");
5596 parser
->scope
= NULL_TREE
;
5602 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5603 this is either a class-name or a namespace-name (which corresponds
5604 to the class-or-namespace-name production in the grammar). For
5605 C++0x, it can also be a type-name that refers to an enumeration
5606 type or a simple-template-id.
5608 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5609 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5610 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5611 TYPE_P is TRUE iff the next name should be taken as a class-name,
5612 even the same name is declared to be another entity in the same
5615 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5616 specified by the class-or-namespace-name. If neither is found the
5617 ERROR_MARK_NODE is returned. */
5620 cp_parser_qualifying_entity (cp_parser
*parser
,
5621 bool typename_keyword_p
,
5622 bool template_keyword_p
,
5623 bool check_dependency_p
,
5625 bool is_declaration
)
5628 tree saved_qualifying_scope
;
5629 tree saved_object_scope
;
5632 bool successful_parse_p
;
5634 /* DR 743: decltype can appear in a nested-name-specifier. */
5635 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5637 scope
= cp_parser_decltype (parser
);
5638 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5639 && !MAYBE_CLASS_TYPE_P (scope
))
5641 cp_parser_simulate_error (parser
);
5642 return error_mark_node
;
5644 if (TYPE_NAME (scope
))
5645 scope
= TYPE_NAME (scope
);
5649 /* Before we try to parse the class-name, we must save away the
5650 current PARSER->SCOPE since cp_parser_class_name will destroy
5652 saved_scope
= parser
->scope
;
5653 saved_qualifying_scope
= parser
->qualifying_scope
;
5654 saved_object_scope
= parser
->object_scope
;
5655 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5656 there is no need to look for a namespace-name. */
5657 only_class_p
= template_keyword_p
5658 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5660 cp_parser_parse_tentatively (parser
);
5661 scope
= cp_parser_class_name (parser
,
5664 type_p
? class_type
: none_type
,
5666 /*class_head_p=*/false,
5668 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5669 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5671 && cxx_dialect
!= cxx98
5672 && !successful_parse_p
)
5674 /* Restore the saved scope. */
5675 parser
->scope
= saved_scope
;
5676 parser
->qualifying_scope
= saved_qualifying_scope
;
5677 parser
->object_scope
= saved_object_scope
;
5679 /* Parse tentatively. */
5680 cp_parser_parse_tentatively (parser
);
5682 /* Parse a type-name */
5683 scope
= cp_parser_type_name (parser
);
5685 /* "If the name found does not designate a namespace or a class,
5686 enumeration, or dependent type, the program is ill-formed."
5688 We cover classes and dependent types above and namespaces below,
5689 so this code is only looking for enums. */
5690 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5691 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5692 cp_parser_simulate_error (parser
);
5694 successful_parse_p
= cp_parser_parse_definitely (parser
);
5696 /* If that didn't work, try for a namespace-name. */
5697 if (!only_class_p
&& !successful_parse_p
)
5699 /* Restore the saved scope. */
5700 parser
->scope
= saved_scope
;
5701 parser
->qualifying_scope
= saved_qualifying_scope
;
5702 parser
->object_scope
= saved_object_scope
;
5703 /* If we are not looking at an identifier followed by the scope
5704 resolution operator, then this is not part of a
5705 nested-name-specifier. (Note that this function is only used
5706 to parse the components of a nested-name-specifier.) */
5707 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5708 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5709 return error_mark_node
;
5710 scope
= cp_parser_namespace_name (parser
);
5716 /* Return true if we are looking at a compound-literal, false otherwise. */
5719 cp_parser_compound_literal_p (cp_parser
*parser
)
5721 /* Consume the `('. */
5722 cp_lexer_consume_token (parser
->lexer
);
5724 cp_lexer_save_tokens (parser
->lexer
);
5726 /* Skip tokens until the next token is a closing parenthesis.
5727 If we find the closing `)', and the next token is a `{', then
5728 we are looking at a compound-literal. */
5729 bool compound_literal_p
5730 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5731 /*consume_paren=*/true)
5732 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5734 /* Roll back the tokens we skipped. */
5735 cp_lexer_rollback_tokens (parser
->lexer
);
5737 return compound_literal_p
;
5740 /* Parse a postfix-expression.
5744 postfix-expression [ expression ]
5745 postfix-expression ( expression-list [opt] )
5746 simple-type-specifier ( expression-list [opt] )
5747 typename :: [opt] nested-name-specifier identifier
5748 ( expression-list [opt] )
5749 typename :: [opt] nested-name-specifier template [opt] template-id
5750 ( expression-list [opt] )
5751 postfix-expression . template [opt] id-expression
5752 postfix-expression -> template [opt] id-expression
5753 postfix-expression . pseudo-destructor-name
5754 postfix-expression -> pseudo-destructor-name
5755 postfix-expression ++
5756 postfix-expression --
5757 dynamic_cast < type-id > ( expression )
5758 static_cast < type-id > ( expression )
5759 reinterpret_cast < type-id > ( expression )
5760 const_cast < type-id > ( expression )
5761 typeid ( expression )
5767 ( type-id ) { initializer-list , [opt] }
5769 This extension is a GNU version of the C99 compound-literal
5770 construct. (The C99 grammar uses `type-name' instead of `type-id',
5771 but they are essentially the same concept.)
5773 If ADDRESS_P is true, the postfix expression is the operand of the
5774 `&' operator. CAST_P is true if this expression is the target of a
5777 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5778 class member access expressions [expr.ref].
5780 Returns a representation of the expression. */
5783 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5784 bool member_access_only_p
, bool decltype_p
,
5785 cp_id_kind
* pidk_return
)
5790 cp_id_kind idk
= CP_ID_KIND_NONE
;
5791 tree postfix_expression
= NULL_TREE
;
5792 bool is_member_access
= false;
5793 int saved_in_statement
= -1;
5795 /* Peek at the next token. */
5796 token
= cp_lexer_peek_token (parser
->lexer
);
5797 loc
= token
->location
;
5798 /* Some of the productions are determined by keywords. */
5799 keyword
= token
->keyword
;
5809 const char *saved_message
;
5810 bool saved_in_type_id_in_expr_p
;
5812 /* All of these can be handled in the same way from the point
5813 of view of parsing. Begin by consuming the token
5814 identifying the cast. */
5815 cp_lexer_consume_token (parser
->lexer
);
5817 /* New types cannot be defined in the cast. */
5818 saved_message
= parser
->type_definition_forbidden_message
;
5819 parser
->type_definition_forbidden_message
5820 = G_("types may not be defined in casts");
5822 /* Look for the opening `<'. */
5823 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5824 /* Parse the type to which we are casting. */
5825 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5826 parser
->in_type_id_in_expr_p
= true;
5827 type
= cp_parser_type_id (parser
);
5828 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5829 /* Look for the closing `>'. */
5830 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5831 /* Restore the old message. */
5832 parser
->type_definition_forbidden_message
= saved_message
;
5834 bool saved_greater_than_is_operator_p
5835 = parser
->greater_than_is_operator_p
;
5836 parser
->greater_than_is_operator_p
= true;
5838 /* And the expression which is being cast. */
5839 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5840 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
5841 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5843 parser
->greater_than_is_operator_p
5844 = saved_greater_than_is_operator_p
;
5846 /* Only type conversions to integral or enumeration types
5847 can be used in constant-expressions. */
5848 if (!cast_valid_in_integral_constant_expression_p (type
)
5849 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5850 return error_mark_node
;
5856 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5860 = build_static_cast (type
, expression
, tf_warning_or_error
);
5864 = build_reinterpret_cast (type
, expression
,
5865 tf_warning_or_error
);
5869 = build_const_cast (type
, expression
, tf_warning_or_error
);
5880 const char *saved_message
;
5881 bool saved_in_type_id_in_expr_p
;
5883 /* Consume the `typeid' token. */
5884 cp_lexer_consume_token (parser
->lexer
);
5885 /* Look for the `(' token. */
5886 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5887 /* Types cannot be defined in a `typeid' expression. */
5888 saved_message
= parser
->type_definition_forbidden_message
;
5889 parser
->type_definition_forbidden_message
5890 = G_("types may not be defined in a %<typeid%> expression");
5891 /* We can't be sure yet whether we're looking at a type-id or an
5893 cp_parser_parse_tentatively (parser
);
5894 /* Try a type-id first. */
5895 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5896 parser
->in_type_id_in_expr_p
= true;
5897 type
= cp_parser_type_id (parser
);
5898 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5899 /* Look for the `)' token. Otherwise, we can't be sure that
5900 we're not looking at an expression: consider `typeid (int
5901 (3))', for example. */
5902 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5903 /* If all went well, simply lookup the type-id. */
5904 if (cp_parser_parse_definitely (parser
))
5905 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5906 /* Otherwise, fall back to the expression variant. */
5911 /* Look for an expression. */
5912 expression
= cp_parser_expression (parser
, & idk
);
5913 /* Compute its typeid. */
5914 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5915 /* Look for the `)' token. */
5916 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5918 /* Restore the saved message. */
5919 parser
->type_definition_forbidden_message
= saved_message
;
5920 /* `typeid' may not appear in an integral constant expression. */
5921 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5922 return error_mark_node
;
5929 /* The syntax permitted here is the same permitted for an
5930 elaborated-type-specifier. */
5931 type
= cp_parser_elaborated_type_specifier (parser
,
5932 /*is_friend=*/false,
5933 /*is_declaration=*/false);
5934 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5938 case RID_CILK_SPAWN
:
5940 cp_lexer_consume_token (parser
->lexer
);
5941 token
= cp_lexer_peek_token (parser
->lexer
);
5942 if (token
->type
== CPP_SEMICOLON
)
5944 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
5946 postfix_expression
= error_mark_node
;
5949 else if (!current_function_decl
)
5951 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
5952 "inside a function");
5953 postfix_expression
= error_mark_node
;
5958 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5959 saved_in_statement
= parser
->in_statement
;
5960 parser
->in_statement
|= IN_CILK_SPAWN
;
5962 cfun
->calls_cilk_spawn
= 1;
5963 postfix_expression
=
5964 cp_parser_postfix_expression (parser
, false, false,
5965 false, false, &idk
);
5968 error_at (token
->location
, "-fcilkplus must be enabled to use"
5969 " %<_Cilk_spawn%>");
5970 cfun
->calls_cilk_spawn
= 0;
5972 else if (saved_in_statement
& IN_CILK_SPAWN
)
5974 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
5975 "are not permitted");
5976 postfix_expression
= error_mark_node
;
5977 cfun
->calls_cilk_spawn
= 0;
5981 postfix_expression
= build_cilk_spawn (token
->location
,
5982 postfix_expression
);
5983 if (postfix_expression
!= error_mark_node
)
5984 SET_EXPR_LOCATION (postfix_expression
, input_location
);
5985 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
5990 case RID_BUILTIN_SHUFFLE
:
5992 vec
<tree
, va_gc
> *vec
;
5996 cp_lexer_consume_token (parser
->lexer
);
5997 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5998 /*cast_p=*/false, /*allow_expansion_p=*/true,
5999 /*non_constant_p=*/NULL
);
6001 return error_mark_node
;
6003 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6006 if (vec
->length () == 2)
6007 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
6008 tf_warning_or_error
);
6009 else if (vec
->length () == 3)
6010 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6011 tf_warning_or_error
);
6014 error_at (loc
, "wrong number of arguments to "
6015 "%<__builtin_shuffle%>");
6016 return error_mark_node
;
6025 /* If the next thing is a simple-type-specifier, we may be
6026 looking at a functional cast. We could also be looking at
6027 an id-expression. So, we try the functional cast, and if
6028 that doesn't work we fall back to the primary-expression. */
6029 cp_parser_parse_tentatively (parser
);
6030 /* Look for the simple-type-specifier. */
6031 type
= cp_parser_simple_type_specifier (parser
,
6032 /*decl_specs=*/NULL
,
6033 CP_PARSER_FLAGS_NONE
);
6034 /* Parse the cast itself. */
6035 if (!cp_parser_error_occurred (parser
))
6037 = cp_parser_functional_cast (parser
, type
);
6038 /* If that worked, we're done. */
6039 if (cp_parser_parse_definitely (parser
))
6042 /* If the functional-cast didn't work out, try a
6043 compound-literal. */
6044 if (cp_parser_allow_gnu_extensions_p (parser
)
6045 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6047 tree initializer
= NULL_TREE
;
6049 cp_parser_parse_tentatively (parser
);
6051 /* Avoid calling cp_parser_type_id pointlessly, see comment
6052 in cp_parser_cast_expression about c++/29234. */
6053 if (!cp_parser_compound_literal_p (parser
))
6054 cp_parser_simulate_error (parser
);
6057 /* Parse the type. */
6058 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6059 parser
->in_type_id_in_expr_p
= true;
6060 type
= cp_parser_type_id (parser
);
6061 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6062 /* Look for the `)'. */
6063 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6066 /* If things aren't going well, there's no need to
6068 if (!cp_parser_error_occurred (parser
))
6070 bool non_constant_p
;
6071 /* Parse the brace-enclosed initializer list. */
6072 initializer
= cp_parser_braced_list (parser
,
6075 /* If that worked, we're definitely looking at a
6076 compound-literal expression. */
6077 if (cp_parser_parse_definitely (parser
))
6079 /* Warn the user that a compound literal is not
6080 allowed in standard C++. */
6081 pedwarn (input_location
, OPT_Wpedantic
,
6082 "ISO C++ forbids compound-literals");
6083 /* For simplicity, we disallow compound literals in
6084 constant-expressions. We could
6085 allow compound literals of integer type, whose
6086 initializer was a constant, in constant
6087 expressions. Permitting that usage, as a further
6088 extension, would not change the meaning of any
6089 currently accepted programs. (Of course, as
6090 compound literals are not part of ISO C++, the
6091 standard has nothing to say.) */
6092 if (cp_parser_non_integral_constant_expression (parser
,
6095 postfix_expression
= error_mark_node
;
6098 /* Form the representation of the compound-literal. */
6100 = finish_compound_literal (type
, initializer
,
6101 tf_warning_or_error
);
6106 /* It must be a primary-expression. */
6108 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6109 /*template_arg_p=*/false,
6116 /* Note that we don't need to worry about calling build_cplus_new on a
6117 class-valued CALL_EXPR in decltype when it isn't the end of the
6118 postfix-expression; unary_complex_lvalue will take care of that for
6121 /* Keep looping until the postfix-expression is complete. */
6124 if (idk
== CP_ID_KIND_UNQUALIFIED
6125 && identifier_p (postfix_expression
)
6126 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6127 /* It is not a Koenig lookup function call. */
6129 = unqualified_name_lookup_error (postfix_expression
);
6131 /* Peek at the next token. */
6132 token
= cp_lexer_peek_token (parser
->lexer
);
6134 switch (token
->type
)
6136 case CPP_OPEN_SQUARE
:
6137 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6139 cp_parser_error (parser
,
6140 "two consecutive %<[%> shall "
6141 "only introduce an attribute");
6142 return error_mark_node
;
6145 = cp_parser_postfix_open_square_expression (parser
,
6149 idk
= CP_ID_KIND_NONE
;
6150 is_member_access
= false;
6153 case CPP_OPEN_PAREN
:
6154 /* postfix-expression ( expression-list [opt] ) */
6157 bool is_builtin_constant_p
;
6158 bool saved_integral_constant_expression_p
= false;
6159 bool saved_non_integral_constant_expression_p
= false;
6160 tsubst_flags_t complain
= complain_flags (decltype_p
);
6161 vec
<tree
, va_gc
> *args
;
6163 is_member_access
= false;
6165 is_builtin_constant_p
6166 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6167 if (is_builtin_constant_p
)
6169 /* The whole point of __builtin_constant_p is to allow
6170 non-constant expressions to appear as arguments. */
6171 saved_integral_constant_expression_p
6172 = parser
->integral_constant_expression_p
;
6173 saved_non_integral_constant_expression_p
6174 = parser
->non_integral_constant_expression_p
;
6175 parser
->integral_constant_expression_p
= false;
6177 args
= (cp_parser_parenthesized_expression_list
6179 /*cast_p=*/false, /*allow_expansion_p=*/true,
6180 /*non_constant_p=*/NULL
,
6181 /*want_literal_zero_p=*/warn_memset_transposed_args
));
6182 if (is_builtin_constant_p
)
6184 parser
->integral_constant_expression_p
6185 = saved_integral_constant_expression_p
;
6186 parser
->non_integral_constant_expression_p
6187 = saved_non_integral_constant_expression_p
;
6192 postfix_expression
= error_mark_node
;
6196 /* Function calls are not permitted in
6197 constant-expressions. */
6198 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6199 && cp_parser_non_integral_constant_expression (parser
,
6202 postfix_expression
= error_mark_node
;
6203 release_tree_vector (args
);
6208 if (idk
== CP_ID_KIND_UNQUALIFIED
6209 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6211 if (identifier_p (postfix_expression
))
6213 if (!args
->is_empty ())
6216 if (!any_type_dependent_arguments_p (args
))
6218 = perform_koenig_lookup (postfix_expression
, args
,
6223 = unqualified_fn_lookup_error (postfix_expression
);
6225 /* We do not perform argument-dependent lookup if
6226 normal lookup finds a non-function, in accordance
6227 with the expected resolution of DR 218. */
6228 else if (!args
->is_empty ()
6229 && is_overloaded_fn (postfix_expression
))
6231 tree fn
= get_first_fn (postfix_expression
);
6232 fn
= STRIP_TEMPLATE (fn
);
6234 /* Do not do argument dependent lookup if regular
6235 lookup finds a member function or a block-scope
6236 function declaration. [basic.lookup.argdep]/3 */
6237 if (!DECL_FUNCTION_MEMBER_P (fn
)
6238 && !DECL_LOCAL_FUNCTION_P (fn
))
6241 if (!any_type_dependent_arguments_p (args
))
6243 = perform_koenig_lookup (postfix_expression
, args
,
6249 if (warn_memset_transposed_args
)
6251 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6252 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6253 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6254 && vec_safe_length (args
) == 3
6255 && integer_zerop ((*args
)[2])
6256 && LITERAL_ZERO_P ((*args
)[2])
6257 && !(integer_zerop ((*args
)[1])
6258 && LITERAL_ZERO_P ((*args
)[1])))
6259 warning (OPT_Wmemset_transposed_args
,
6260 "%<memset%> used with constant zero length "
6261 "parameter; this could be due to transposed "
6264 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6265 to avoid leaking those into folder and middle-end. */
6268 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6269 if (TREE_CODE (arg
) == INTEGER_CST
&& LITERAL_ZERO_P (arg
))
6270 (*args
)[i
] = build_int_cst (TREE_TYPE (arg
), 0);
6273 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6275 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6276 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6278 if (processing_template_decl
6279 && (type_dependent_expression_p (instance
)
6280 || (!BASELINK_P (fn
)
6281 && TREE_CODE (fn
) != FIELD_DECL
)
6282 || type_dependent_expression_p (fn
)
6283 || any_type_dependent_arguments_p (args
)))
6286 = build_nt_call_vec (postfix_expression
, args
);
6287 release_tree_vector (args
);
6291 if (BASELINK_P (fn
))
6294 = (build_new_method_call
6295 (instance
, fn
, &args
, NULL_TREE
,
6296 (idk
== CP_ID_KIND_QUALIFIED
6297 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6304 = finish_call_expr (postfix_expression
, &args
,
6305 /*disallow_virtual=*/false,
6309 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6310 || TREE_CODE (postfix_expression
) == MEMBER_REF
6311 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6312 postfix_expression
= (build_offset_ref_call_from_tree
6313 (postfix_expression
, &args
,
6315 else if (idk
== CP_ID_KIND_QUALIFIED
)
6316 /* A call to a static class member, or a namespace-scope
6319 = finish_call_expr (postfix_expression
, &args
,
6320 /*disallow_virtual=*/true,
6324 /* All other function calls. */
6326 = finish_call_expr (postfix_expression
, &args
,
6327 /*disallow_virtual=*/false,
6331 protected_set_expr_location (postfix_expression
, token
->location
);
6333 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6334 idk
= CP_ID_KIND_NONE
;
6336 release_tree_vector (args
);
6342 /* postfix-expression . template [opt] id-expression
6343 postfix-expression . pseudo-destructor-name
6344 postfix-expression -> template [opt] id-expression
6345 postfix-expression -> pseudo-destructor-name */
6347 /* Consume the `.' or `->' operator. */
6348 cp_lexer_consume_token (parser
->lexer
);
6351 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6355 is_member_access
= true;
6359 /* postfix-expression ++ */
6360 /* Consume the `++' token. */
6361 cp_lexer_consume_token (parser
->lexer
);
6362 /* Generate a representation for the complete expression. */
6364 = finish_increment_expr (postfix_expression
,
6365 POSTINCREMENT_EXPR
);
6366 /* Increments may not appear in constant-expressions. */
6367 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6368 postfix_expression
= error_mark_node
;
6369 idk
= CP_ID_KIND_NONE
;
6370 is_member_access
= false;
6373 case CPP_MINUS_MINUS
:
6374 /* postfix-expression -- */
6375 /* Consume the `--' token. */
6376 cp_lexer_consume_token (parser
->lexer
);
6377 /* Generate a representation for the complete expression. */
6379 = finish_increment_expr (postfix_expression
,
6380 POSTDECREMENT_EXPR
);
6381 /* Decrements may not appear in constant-expressions. */
6382 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6383 postfix_expression
= error_mark_node
;
6384 idk
= CP_ID_KIND_NONE
;
6385 is_member_access
= false;
6389 if (pidk_return
!= NULL
)
6390 * pidk_return
= idk
;
6391 if (member_access_only_p
)
6392 return is_member_access
? postfix_expression
: error_mark_node
;
6394 return postfix_expression
;
6398 /* We should never get here. */
6400 return error_mark_node
;
6403 /* This function parses Cilk Plus array notations. If a normal array expr. is
6404 parsed then the array index is passed back to the caller through *INIT_INDEX
6405 and the function returns a NULL_TREE. If array notation expr. is parsed,
6406 then *INIT_INDEX is ignored by the caller and the function returns
6407 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6411 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6414 cp_token
*token
= NULL
;
6415 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6416 if (!array_value
|| array_value
== error_mark_node
)
6418 cp_parser_skip_to_end_of_statement (parser
);
6419 return error_mark_node
;
6422 array_type
= TREE_TYPE (array_value
);
6424 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6425 parser
->colon_corrects_to_scope_p
= false;
6426 token
= cp_lexer_peek_token (parser
->lexer
);
6430 cp_parser_error (parser
, "expected %<:%> or numeral");
6431 return error_mark_node
;
6433 else if (token
->type
== CPP_COLON
)
6435 /* Consume the ':'. */
6436 cp_lexer_consume_token (parser
->lexer
);
6438 /* If we are here, then we have a case like this A[:]. */
6439 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6441 cp_parser_error (parser
, "expected %<]%>");
6442 cp_parser_skip_to_end_of_statement (parser
);
6443 return error_mark_node
;
6445 *init_index
= NULL_TREE
;
6447 length_index
= NULL_TREE
;
6451 /* If we are here, then there are three valid possibilities:
6453 2. ARRAY [ EXP : EXP ]
6454 3. ARRAY [ EXP : EXP : EXP ] */
6456 *init_index
= cp_parser_expression (parser
);
6457 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6459 /* This indicates that we have a normal array expression. */
6460 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6464 /* Consume the ':'. */
6465 cp_lexer_consume_token (parser
->lexer
);
6466 length_index
= cp_parser_expression (parser
);
6467 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6469 cp_lexer_consume_token (parser
->lexer
);
6470 stride
= cp_parser_expression (parser
);
6473 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6475 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6476 || stride
== error_mark_node
|| array_type
== error_mark_node
)
6478 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6479 cp_lexer_consume_token (parser
->lexer
);
6480 return error_mark_node
;
6482 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6484 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6485 length_index
, stride
, array_type
);
6489 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6490 by cp_parser_builtin_offsetof. We're looking for
6492 postfix-expression [ expression ]
6493 postfix-expression [ braced-init-list ] (C++11)
6495 FOR_OFFSETOF is set if we're being called in that context, which
6496 changes how we deal with integer constant expressions. */
6499 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6500 tree postfix_expression
,
6504 tree index
= NULL_TREE
;
6505 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6506 bool saved_greater_than_is_operator_p
;
6508 /* Consume the `[' token. */
6509 cp_lexer_consume_token (parser
->lexer
);
6511 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6512 parser
->greater_than_is_operator_p
= true;
6514 /* Parse the index expression. */
6515 /* ??? For offsetof, there is a question of what to allow here. If
6516 offsetof is not being used in an integral constant expression context,
6517 then we *could* get the right answer by computing the value at runtime.
6518 If we are in an integral constant expression context, then we might
6519 could accept any constant expression; hard to say without analysis.
6520 Rather than open the barn door too wide right away, allow only integer
6521 constant expressions here. */
6523 index
= cp_parser_constant_expression (parser
);
6526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6528 bool expr_nonconst_p
;
6529 cp_lexer_set_source_position (parser
->lexer
);
6530 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6531 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6533 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6535 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6536 "braced list index is not allowed with array "
6538 cp_parser_skip_to_end_of_statement (parser
);
6539 return error_mark_node
;
6542 else if (flag_cilkplus
)
6544 /* Here are have these two options:
6545 ARRAY[EXP : EXP] - Array notation expr with default
6547 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6549 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6550 postfix_expression
);
6555 index
= cp_parser_expression (parser
);
6558 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6560 /* Look for the closing `]'. */
6561 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6563 /* Build the ARRAY_REF. */
6564 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6567 /* When not doing offsetof, array references are not permitted in
6568 constant-expressions. */
6570 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6571 postfix_expression
= error_mark_node
;
6573 return postfix_expression
;
6576 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6577 by cp_parser_builtin_offsetof. We're looking for
6579 postfix-expression . template [opt] id-expression
6580 postfix-expression . pseudo-destructor-name
6581 postfix-expression -> template [opt] id-expression
6582 postfix-expression -> pseudo-destructor-name
6584 FOR_OFFSETOF is set if we're being called in that context. That sorta
6585 limits what of the above we'll actually accept, but nevermind.
6586 TOKEN_TYPE is the "." or "->" token, which will already have been
6587 removed from the stream. */
6590 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6591 enum cpp_ttype token_type
,
6592 tree postfix_expression
,
6593 bool for_offsetof
, cp_id_kind
*idk
,
6594 location_t location
)
6598 bool pseudo_destructor_p
;
6599 tree scope
= NULL_TREE
;
6601 /* If this is a `->' operator, dereference the pointer. */
6602 if (token_type
== CPP_DEREF
)
6603 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6604 tf_warning_or_error
);
6605 /* Check to see whether or not the expression is type-dependent. */
6606 dependent_p
= type_dependent_expression_p (postfix_expression
);
6607 /* The identifier following the `->' or `.' is not qualified. */
6608 parser
->scope
= NULL_TREE
;
6609 parser
->qualifying_scope
= NULL_TREE
;
6610 parser
->object_scope
= NULL_TREE
;
6611 *idk
= CP_ID_KIND_NONE
;
6613 /* Enter the scope corresponding to the type of the object
6614 given by the POSTFIX_EXPRESSION. */
6615 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6617 scope
= TREE_TYPE (postfix_expression
);
6618 /* According to the standard, no expression should ever have
6619 reference type. Unfortunately, we do not currently match
6620 the standard in this respect in that our internal representation
6621 of an expression may have reference type even when the standard
6622 says it does not. Therefore, we have to manually obtain the
6623 underlying type here. */
6624 scope
= non_reference (scope
);
6625 /* The type of the POSTFIX_EXPRESSION must be complete. */
6626 if (scope
== unknown_type_node
)
6628 error_at (location
, "%qE does not have class type",
6629 postfix_expression
);
6632 /* Unlike the object expression in other contexts, *this is not
6633 required to be of complete type for purposes of class member
6634 access (5.2.5) outside the member function body. */
6635 else if (postfix_expression
!= current_class_ref
6636 && !(processing_template_decl
&& scope
== current_class_type
))
6637 scope
= complete_type_or_else (scope
, NULL_TREE
);
6638 /* Let the name lookup machinery know that we are processing a
6639 class member access expression. */
6640 parser
->context
->object_type
= scope
;
6641 /* If something went wrong, we want to be able to discern that case,
6642 as opposed to the case where there was no SCOPE due to the type
6643 of expression being dependent. */
6645 scope
= error_mark_node
;
6646 /* If the SCOPE was erroneous, make the various semantic analysis
6647 functions exit quickly -- and without issuing additional error
6649 if (scope
== error_mark_node
)
6650 postfix_expression
= error_mark_node
;
6653 /* Assume this expression is not a pseudo-destructor access. */
6654 pseudo_destructor_p
= false;
6656 /* If the SCOPE is a scalar type, then, if this is a valid program,
6657 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6658 is type dependent, it can be pseudo-destructor-name or something else.
6659 Try to parse it as pseudo-destructor-name first. */
6660 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6665 cp_parser_parse_tentatively (parser
);
6666 /* Parse the pseudo-destructor-name. */
6668 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6671 && (cp_parser_error_occurred (parser
)
6672 || !SCALAR_TYPE_P (type
)))
6673 cp_parser_abort_tentative_parse (parser
);
6674 else if (cp_parser_parse_definitely (parser
))
6676 pseudo_destructor_p
= true;
6678 = finish_pseudo_destructor_expr (postfix_expression
,
6683 if (!pseudo_destructor_p
)
6685 /* If the SCOPE is not a scalar type, we are looking at an
6686 ordinary class member access expression, rather than a
6687 pseudo-destructor-name. */
6689 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6690 /* Parse the id-expression. */
6691 name
= (cp_parser_id_expression
6693 cp_parser_optional_template_keyword (parser
),
6694 /*check_dependency_p=*/true,
6696 /*declarator_p=*/false,
6697 /*optional_p=*/false));
6698 /* In general, build a SCOPE_REF if the member name is qualified.
6699 However, if the name was not dependent and has already been
6700 resolved; there is no need to build the SCOPE_REF. For example;
6702 struct X { void f(); };
6703 template <typename T> void f(T* t) { t->X::f(); }
6705 Even though "t" is dependent, "X::f" is not and has been resolved
6706 to a BASELINK; there is no need to include scope information. */
6708 /* But we do need to remember that there was an explicit scope for
6709 virtual function calls. */
6711 *idk
= CP_ID_KIND_QUALIFIED
;
6713 /* If the name is a template-id that names a type, we will get a
6714 TYPE_DECL here. That is invalid code. */
6715 if (TREE_CODE (name
) == TYPE_DECL
)
6717 error_at (token
->location
, "invalid use of %qD", name
);
6718 postfix_expression
= error_mark_node
;
6722 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6724 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6726 error_at (token
->location
, "%<%D::%D%> is not a class member",
6727 parser
->scope
, name
);
6728 postfix_expression
= error_mark_node
;
6731 name
= build_qualified_name (/*type=*/NULL_TREE
,
6735 parser
->scope
= NULL_TREE
;
6736 parser
->qualifying_scope
= NULL_TREE
;
6737 parser
->object_scope
= NULL_TREE
;
6739 if (parser
->scope
&& name
&& BASELINK_P (name
))
6740 adjust_result_of_qualified_name_lookup
6741 (name
, parser
->scope
, scope
);
6743 = finish_class_member_access_expr (postfix_expression
, name
,
6745 tf_warning_or_error
);
6749 /* We no longer need to look up names in the scope of the object on
6750 the left-hand side of the `.' or `->' operator. */
6751 parser
->context
->object_type
= NULL_TREE
;
6753 /* Outside of offsetof, these operators may not appear in
6754 constant-expressions. */
6756 && (cp_parser_non_integral_constant_expression
6757 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6758 postfix_expression
= error_mark_node
;
6760 return postfix_expression
;
6763 /* Cache of LITERAL_ZERO_P constants. */
6765 static GTY(()) tree literal_zeros
[itk_none
];
6767 /* Parse a parenthesized expression-list.
6770 assignment-expression
6771 expression-list, assignment-expression
6776 identifier, expression-list
6778 CAST_P is true if this expression is the target of a cast.
6780 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6783 Returns a vector of trees. Each element is a representation of an
6784 assignment-expression. NULL is returned if the ( and or ) are
6785 missing. An empty, but allocated, vector is returned on no
6786 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6787 if we are parsing an attribute list for an attribute that wants a
6788 plain identifier argument, normal_attr for an attribute that wants
6789 an expression, or non_attr if we aren't parsing an attribute list. If
6790 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6791 not all of the expressions in the list were constant.
6792 WANT_LITERAL_ZERO_P is true if the caller is interested in
6793 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6794 immediately, this can be removed. */
6796 static vec
<tree
, va_gc
> *
6797 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6798 int is_attribute_list
,
6800 bool allow_expansion_p
,
6801 bool *non_constant_p
,
6802 bool want_literal_zero_p
)
6804 vec
<tree
, va_gc
> *expression_list
;
6805 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6806 tree identifier
= NULL_TREE
;
6807 bool saved_greater_than_is_operator_p
;
6809 /* Assume all the expressions will be constant. */
6811 *non_constant_p
= false;
6813 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6816 expression_list
= make_tree_vector ();
6818 /* Within a parenthesized expression, a `>' token is always
6819 the greater-than operator. */
6820 saved_greater_than_is_operator_p
6821 = parser
->greater_than_is_operator_p
;
6822 parser
->greater_than_is_operator_p
= true;
6824 /* Consume expressions until there are no more. */
6825 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6830 /* At the beginning of attribute lists, check to see if the
6831 next token is an identifier. */
6832 if (is_attribute_list
== id_attr
6833 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6837 /* Consume the identifier. */
6838 token
= cp_lexer_consume_token (parser
->lexer
);
6839 /* Save the identifier. */
6840 identifier
= token
->u
.value
;
6844 bool expr_non_constant_p
;
6846 /* Parse the next assignment-expression. */
6847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6849 /* A braced-init-list. */
6850 cp_lexer_set_source_position (parser
->lexer
);
6851 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6852 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6853 if (non_constant_p
&& expr_non_constant_p
)
6854 *non_constant_p
= true;
6856 else if (non_constant_p
)
6858 expr
= (cp_parser_constant_expression
6859 (parser
, /*allow_non_constant_p=*/true,
6860 &expr_non_constant_p
));
6861 if (expr_non_constant_p
)
6862 *non_constant_p
= true;
6867 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
6875 /* If a parameter is literal zero alone, remember it
6876 for -Wmemset-transposed-args warning. */
6877 if (integer_zerop (tok
->u
.value
)
6878 && !TREE_OVERFLOW (tok
->u
.value
)
6879 && want_literal_zero_p
6880 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6882 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6883 == CPP_CLOSE_PAREN
))
6886 for (i
= 0; i
< itk_none
; ++i
)
6887 if (TREE_TYPE (tok
->u
.value
) == integer_types
[i
])
6889 if (i
< itk_none
&& literal_zeros
[i
])
6890 expr
= literal_zeros
[i
];
6893 expr
= copy_node (tok
->u
.value
);
6894 LITERAL_ZERO_P (expr
) = 1;
6896 literal_zeros
[i
] = expr
;
6898 /* Consume the 0 token (or '\0', 0LL etc.). */
6899 cp_lexer_consume_token (parser
->lexer
);
6905 if (expr
== NULL_TREE
)
6906 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
6911 expr
= fold_non_dependent_expr (expr
);
6913 /* If we have an ellipsis, then this is an expression
6915 if (allow_expansion_p
6916 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6918 /* Consume the `...'. */
6919 cp_lexer_consume_token (parser
->lexer
);
6921 /* Build the argument pack. */
6922 expr
= make_pack_expansion (expr
);
6925 /* Add it to the list. We add error_mark_node
6926 expressions to the list, so that we can still tell if
6927 the correct form for a parenthesized expression-list
6928 is found. That gives better errors. */
6929 vec_safe_push (expression_list
, expr
);
6931 if (expr
== error_mark_node
)
6935 /* After the first item, attribute lists look the same as
6936 expression lists. */
6937 is_attribute_list
= non_attr
;
6940 /* If the next token isn't a `,', then we are done. */
6941 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6944 /* Otherwise, consume the `,' and keep going. */
6945 cp_lexer_consume_token (parser
->lexer
);
6948 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6953 /* We try and resync to an unnested comma, as that will give the
6954 user better diagnostics. */
6955 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6956 /*recovering=*/true,
6958 /*consume_paren=*/true);
6963 parser
->greater_than_is_operator_p
6964 = saved_greater_than_is_operator_p
;
6969 parser
->greater_than_is_operator_p
6970 = saved_greater_than_is_operator_p
;
6973 vec_safe_insert (expression_list
, 0, identifier
);
6975 return expression_list
;
6978 /* Parse a pseudo-destructor-name.
6980 pseudo-destructor-name:
6981 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6982 :: [opt] nested-name-specifier template template-id :: ~ type-name
6983 :: [opt] nested-name-specifier [opt] ~ type-name
6985 If either of the first two productions is used, sets *SCOPE to the
6986 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6987 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6988 or ERROR_MARK_NODE if the parse fails. */
6991 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6996 bool nested_name_specifier_p
;
6999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7000 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7001 && !type_dependent_expression_p (object
))
7003 if (cxx_dialect
< cxx14
)
7004 pedwarn (input_location
, 0,
7005 "%<~auto%> only available with "
7006 "-std=c++14 or -std=gnu++14");
7007 cp_lexer_consume_token (parser
->lexer
);
7008 cp_lexer_consume_token (parser
->lexer
);
7010 *type
= TREE_TYPE (object
);
7014 /* Assume that things will not work out. */
7015 *type
= error_mark_node
;
7017 /* Look for the optional `::' operator. */
7018 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7019 /* Look for the optional nested-name-specifier. */
7020 nested_name_specifier_p
7021 = (cp_parser_nested_name_specifier_opt (parser
,
7022 /*typename_keyword_p=*/false,
7023 /*check_dependency_p=*/true,
7025 /*is_declaration=*/false)
7027 /* Now, if we saw a nested-name-specifier, we might be doing the
7028 second production. */
7029 if (nested_name_specifier_p
7030 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7032 /* Consume the `template' keyword. */
7033 cp_lexer_consume_token (parser
->lexer
);
7034 /* Parse the template-id. */
7035 cp_parser_template_id (parser
,
7036 /*template_keyword_p=*/true,
7037 /*check_dependency_p=*/false,
7039 /*is_declaration=*/true);
7040 /* Look for the `::' token. */
7041 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7043 /* If the next token is not a `~', then there might be some
7044 additional qualification. */
7045 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7047 /* At this point, we're looking for "type-name :: ~". The type-name
7048 must not be a class-name, since this is a pseudo-destructor. So,
7049 it must be either an enum-name, or a typedef-name -- both of which
7050 are just identifiers. So, we peek ahead to check that the "::"
7051 and "~" tokens are present; if they are not, then we can avoid
7052 calling type_name. */
7053 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7054 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7055 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7057 cp_parser_error (parser
, "non-scalar type");
7061 /* Look for the type-name. */
7062 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7063 if (*scope
== error_mark_node
)
7066 /* Look for the `::' token. */
7067 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7072 /* Look for the `~'. */
7073 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7075 /* Once we see the ~, this has to be a pseudo-destructor. */
7076 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7077 cp_parser_commit_to_topmost_tentative_parse (parser
);
7079 /* Look for the type-name again. We are not responsible for
7080 checking that it matches the first type-name. */
7081 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7084 /* Parse a unary-expression.
7090 unary-operator cast-expression
7091 sizeof unary-expression
7093 alignof ( type-id ) [C++0x]
7100 __extension__ cast-expression
7101 __alignof__ unary-expression
7102 __alignof__ ( type-id )
7103 alignof unary-expression [C++0x]
7104 __real__ cast-expression
7105 __imag__ cast-expression
7107 sizeof ( type-id ) { initializer-list , [opt] }
7108 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7109 __alignof__ ( type-id ) { initializer-list , [opt] }
7111 ADDRESS_P is true iff the unary-expression is appearing as the
7112 operand of the `&' operator. CAST_P is true if this expression is
7113 the target of a cast.
7115 Returns a representation of the expression. */
7118 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7119 bool address_p
, bool cast_p
, bool decltype_p
)
7122 enum tree_code unary_operator
;
7124 /* Peek at the next token. */
7125 token
= cp_lexer_peek_token (parser
->lexer
);
7126 /* Some keywords give away the kind of expression. */
7127 if (token
->type
== CPP_KEYWORD
)
7129 enum rid keyword
= token
->keyword
;
7138 location_t first_loc
;
7140 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7141 /* Consume the token. */
7142 cp_lexer_consume_token (parser
->lexer
);
7143 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7144 /* Parse the operand. */
7145 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7147 if (TYPE_P (operand
))
7148 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7151 /* ISO C++ defines alignof only with types, not with
7152 expressions. So pedwarn if alignof is used with a non-
7153 type expression. However, __alignof__ is ok. */
7154 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7155 pedwarn (token
->location
, OPT_Wpedantic
,
7156 "ISO C++ does not allow %<alignof%> "
7159 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7161 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7162 SIZEOF_EXPR with the original operand. */
7163 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7165 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7167 if (!processing_template_decl
&& TYPE_P (operand
))
7169 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7170 build1 (NOP_EXPR
, operand
,
7172 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7175 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7176 TREE_SIDE_EFFECTS (ret
) = 0;
7177 TREE_READONLY (ret
) = 1;
7179 SET_EXPR_LOCATION (ret
, first_loc
);
7185 return cp_parser_new_expression (parser
);
7188 return cp_parser_delete_expression (parser
);
7192 /* The saved value of the PEDANTIC flag. */
7196 /* Save away the PEDANTIC flag. */
7197 cp_parser_extension_opt (parser
, &saved_pedantic
);
7198 /* Parse the cast-expression. */
7199 expr
= cp_parser_simple_cast_expression (parser
);
7200 /* Restore the PEDANTIC flag. */
7201 pedantic
= saved_pedantic
;
7211 /* Consume the `__real__' or `__imag__' token. */
7212 cp_lexer_consume_token (parser
->lexer
);
7213 /* Parse the cast-expression. */
7214 expression
= cp_parser_simple_cast_expression (parser
);
7215 /* Create the complete representation. */
7216 return build_x_unary_op (token
->location
,
7217 (keyword
== RID_REALPART
7218 ? REALPART_EXPR
: IMAGPART_EXPR
),
7220 tf_warning_or_error
);
7224 case RID_TRANSACTION_ATOMIC
:
7225 case RID_TRANSACTION_RELAXED
:
7226 return cp_parser_transaction_expression (parser
, keyword
);
7231 const char *saved_message
;
7232 bool saved_integral_constant_expression_p
;
7233 bool saved_non_integral_constant_expression_p
;
7234 bool saved_greater_than_is_operator_p
;
7236 cp_lexer_consume_token (parser
->lexer
);
7237 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7239 saved_message
= parser
->type_definition_forbidden_message
;
7240 parser
->type_definition_forbidden_message
7241 = G_("types may not be defined in %<noexcept%> expressions");
7243 saved_integral_constant_expression_p
7244 = parser
->integral_constant_expression_p
;
7245 saved_non_integral_constant_expression_p
7246 = parser
->non_integral_constant_expression_p
;
7247 parser
->integral_constant_expression_p
= false;
7249 saved_greater_than_is_operator_p
7250 = parser
->greater_than_is_operator_p
;
7251 parser
->greater_than_is_operator_p
= true;
7253 ++cp_unevaluated_operand
;
7254 ++c_inhibit_evaluation_warnings
;
7255 ++cp_noexcept_operand
;
7256 expr
= cp_parser_expression (parser
);
7257 --cp_noexcept_operand
;
7258 --c_inhibit_evaluation_warnings
;
7259 --cp_unevaluated_operand
;
7261 parser
->greater_than_is_operator_p
7262 = saved_greater_than_is_operator_p
;
7264 parser
->integral_constant_expression_p
7265 = saved_integral_constant_expression_p
;
7266 parser
->non_integral_constant_expression_p
7267 = saved_non_integral_constant_expression_p
;
7269 parser
->type_definition_forbidden_message
= saved_message
;
7271 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7272 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7280 /* Look for the `:: new' and `:: delete', which also signal the
7281 beginning of a new-expression, or delete-expression,
7282 respectively. If the next token is `::', then it might be one of
7284 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7288 /* See if the token after the `::' is one of the keywords in
7289 which we're interested. */
7290 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7291 /* If it's `new', we have a new-expression. */
7292 if (keyword
== RID_NEW
)
7293 return cp_parser_new_expression (parser
);
7294 /* Similarly, for `delete'. */
7295 else if (keyword
== RID_DELETE
)
7296 return cp_parser_delete_expression (parser
);
7299 /* Look for a unary operator. */
7300 unary_operator
= cp_parser_unary_operator (token
);
7301 /* The `++' and `--' operators can be handled similarly, even though
7302 they are not technically unary-operators in the grammar. */
7303 if (unary_operator
== ERROR_MARK
)
7305 if (token
->type
== CPP_PLUS_PLUS
)
7306 unary_operator
= PREINCREMENT_EXPR
;
7307 else if (token
->type
== CPP_MINUS_MINUS
)
7308 unary_operator
= PREDECREMENT_EXPR
;
7309 /* Handle the GNU address-of-label extension. */
7310 else if (cp_parser_allow_gnu_extensions_p (parser
)
7311 && token
->type
== CPP_AND_AND
)
7315 location_t loc
= token
->location
;
7317 /* Consume the '&&' token. */
7318 cp_lexer_consume_token (parser
->lexer
);
7319 /* Look for the identifier. */
7320 identifier
= cp_parser_identifier (parser
);
7321 /* Create an expression representing the address. */
7322 expression
= finish_label_address_expr (identifier
, loc
);
7323 if (cp_parser_non_integral_constant_expression (parser
,
7325 expression
= error_mark_node
;
7329 if (unary_operator
!= ERROR_MARK
)
7331 tree cast_expression
;
7332 tree expression
= error_mark_node
;
7333 non_integral_constant non_constant_p
= NIC_NONE
;
7334 location_t loc
= token
->location
;
7335 tsubst_flags_t complain
= complain_flags (decltype_p
);
7337 /* Consume the operator token. */
7338 token
= cp_lexer_consume_token (parser
->lexer
);
7339 /* Parse the cast-expression. */
7341 = cp_parser_cast_expression (parser
,
7342 unary_operator
== ADDR_EXPR
,
7346 /* Now, build an appropriate representation. */
7347 switch (unary_operator
)
7350 non_constant_p
= NIC_STAR
;
7351 expression
= build_x_indirect_ref (loc
, cast_expression
,
7357 non_constant_p
= NIC_ADDR
;
7360 expression
= build_x_unary_op (loc
, unary_operator
,
7365 case PREINCREMENT_EXPR
:
7366 case PREDECREMENT_EXPR
:
7367 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7368 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7370 case UNARY_PLUS_EXPR
:
7372 case TRUTH_NOT_EXPR
:
7373 expression
= finish_unary_op_expr (loc
, unary_operator
,
7374 cast_expression
, complain
);
7381 if (non_constant_p
!= NIC_NONE
7382 && cp_parser_non_integral_constant_expression (parser
,
7384 expression
= error_mark_node
;
7389 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7390 /*member_access_only_p=*/false,
7395 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7396 unary-operator, the corresponding tree code is returned. */
7398 static enum tree_code
7399 cp_parser_unary_operator (cp_token
* token
)
7401 switch (token
->type
)
7404 return INDIRECT_REF
;
7410 return UNARY_PLUS_EXPR
;
7416 return TRUTH_NOT_EXPR
;
7419 return BIT_NOT_EXPR
;
7426 /* Parse a new-expression.
7429 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7430 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7432 Returns a representation of the expression. */
7435 cp_parser_new_expression (cp_parser
* parser
)
7437 bool global_scope_p
;
7438 vec
<tree
, va_gc
> *placement
;
7440 vec
<tree
, va_gc
> *initializer
;
7441 tree nelts
= NULL_TREE
;
7444 /* Look for the optional `::' operator. */
7446 = (cp_parser_global_scope_opt (parser
,
7447 /*current_scope_valid_p=*/false)
7449 /* Look for the `new' operator. */
7450 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7451 /* There's no easy way to tell a new-placement from the
7452 `( type-id )' construct. */
7453 cp_parser_parse_tentatively (parser
);
7454 /* Look for a new-placement. */
7455 placement
= cp_parser_new_placement (parser
);
7456 /* If that didn't work out, there's no new-placement. */
7457 if (!cp_parser_parse_definitely (parser
))
7459 if (placement
!= NULL
)
7460 release_tree_vector (placement
);
7464 /* If the next token is a `(', then we have a parenthesized
7466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7469 const char *saved_message
= parser
->type_definition_forbidden_message
;
7471 /* Consume the `('. */
7472 cp_lexer_consume_token (parser
->lexer
);
7474 /* Parse the type-id. */
7475 parser
->type_definition_forbidden_message
7476 = G_("types may not be defined in a new-expression");
7477 type
= cp_parser_type_id (parser
);
7478 parser
->type_definition_forbidden_message
= saved_message
;
7480 /* Look for the closing `)'. */
7481 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7482 token
= cp_lexer_peek_token (parser
->lexer
);
7483 /* There should not be a direct-new-declarator in this production,
7484 but GCC used to allowed this, so we check and emit a sensible error
7485 message for this case. */
7486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7488 error_at (token
->location
,
7489 "array bound forbidden after parenthesized type-id");
7490 inform (token
->location
,
7491 "try removing the parentheses around the type-id");
7492 cp_parser_direct_new_declarator (parser
);
7495 /* Otherwise, there must be a new-type-id. */
7497 type
= cp_parser_new_type_id (parser
, &nelts
);
7499 /* If the next token is a `(' or '{', then we have a new-initializer. */
7500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7501 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7502 initializer
= cp_parser_new_initializer (parser
);
7506 /* A new-expression may not appear in an integral constant
7508 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7509 ret
= error_mark_node
;
7512 /* Create a representation of the new-expression. */
7513 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7514 tf_warning_or_error
);
7517 if (placement
!= NULL
)
7518 release_tree_vector (placement
);
7519 if (initializer
!= NULL
)
7520 release_tree_vector (initializer
);
7525 /* Parse a new-placement.
7530 Returns the same representation as for an expression-list. */
7532 static vec
<tree
, va_gc
> *
7533 cp_parser_new_placement (cp_parser
* parser
)
7535 vec
<tree
, va_gc
> *expression_list
;
7537 /* Parse the expression-list. */
7538 expression_list
= (cp_parser_parenthesized_expression_list
7539 (parser
, non_attr
, /*cast_p=*/false,
7540 /*allow_expansion_p=*/true,
7541 /*non_constant_p=*/NULL
));
7543 return expression_list
;
7546 /* Parse a new-type-id.
7549 type-specifier-seq new-declarator [opt]
7551 Returns the TYPE allocated. If the new-type-id indicates an array
7552 type, *NELTS is set to the number of elements in the last array
7553 bound; the TYPE will not include the last array bound. */
7556 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7558 cp_decl_specifier_seq type_specifier_seq
;
7559 cp_declarator
*new_declarator
;
7560 cp_declarator
*declarator
;
7561 cp_declarator
*outer_declarator
;
7562 const char *saved_message
;
7564 /* The type-specifier sequence must not contain type definitions.
7565 (It cannot contain declarations of new types either, but if they
7566 are not definitions we will catch that because they are not
7568 saved_message
= parser
->type_definition_forbidden_message
;
7569 parser
->type_definition_forbidden_message
7570 = G_("types may not be defined in a new-type-id");
7571 /* Parse the type-specifier-seq. */
7572 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7573 /*is_trailing_return=*/false,
7574 &type_specifier_seq
);
7575 /* Restore the old message. */
7576 parser
->type_definition_forbidden_message
= saved_message
;
7578 if (type_specifier_seq
.type
== error_mark_node
)
7579 return error_mark_node
;
7581 /* Parse the new-declarator. */
7582 new_declarator
= cp_parser_new_declarator_opt (parser
);
7584 /* Determine the number of elements in the last array dimension, if
7587 /* Skip down to the last array dimension. */
7588 declarator
= new_declarator
;
7589 outer_declarator
= NULL
;
7590 while (declarator
&& (declarator
->kind
== cdk_pointer
7591 || declarator
->kind
== cdk_ptrmem
))
7593 outer_declarator
= declarator
;
7594 declarator
= declarator
->declarator
;
7597 && declarator
->kind
== cdk_array
7598 && declarator
->declarator
7599 && declarator
->declarator
->kind
== cdk_array
)
7601 outer_declarator
= declarator
;
7602 declarator
= declarator
->declarator
;
7605 if (declarator
&& declarator
->kind
== cdk_array
)
7607 *nelts
= declarator
->u
.array
.bounds
;
7608 if (*nelts
== error_mark_node
)
7609 *nelts
= integer_one_node
;
7611 if (outer_declarator
)
7612 outer_declarator
->declarator
= declarator
->declarator
;
7614 new_declarator
= NULL
;
7617 return groktypename (&type_specifier_seq
, new_declarator
, false);
7620 /* Parse an (optional) new-declarator.
7623 ptr-operator new-declarator [opt]
7624 direct-new-declarator
7626 Returns the declarator. */
7628 static cp_declarator
*
7629 cp_parser_new_declarator_opt (cp_parser
* parser
)
7631 enum tree_code code
;
7632 tree type
, std_attributes
= NULL_TREE
;
7633 cp_cv_quals cv_quals
;
7635 /* We don't know if there's a ptr-operator next, or not. */
7636 cp_parser_parse_tentatively (parser
);
7637 /* Look for a ptr-operator. */
7638 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7639 /* If that worked, look for more new-declarators. */
7640 if (cp_parser_parse_definitely (parser
))
7642 cp_declarator
*declarator
;
7644 /* Parse another optional declarator. */
7645 declarator
= cp_parser_new_declarator_opt (parser
);
7647 declarator
= cp_parser_make_indirect_declarator
7648 (code
, type
, cv_quals
, declarator
, std_attributes
);
7653 /* If the next token is a `[', there is a direct-new-declarator. */
7654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7655 return cp_parser_direct_new_declarator (parser
);
7660 /* Parse a direct-new-declarator.
7662 direct-new-declarator:
7664 direct-new-declarator [constant-expression]
7668 static cp_declarator
*
7669 cp_parser_direct_new_declarator (cp_parser
* parser
)
7671 cp_declarator
*declarator
= NULL
;
7678 /* Look for the opening `['. */
7679 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7681 token
= cp_lexer_peek_token (parser
->lexer
);
7682 expression
= cp_parser_expression (parser
);
7683 /* The standard requires that the expression have integral
7684 type. DR 74 adds enumeration types. We believe that the
7685 real intent is that these expressions be handled like the
7686 expression in a `switch' condition, which also allows
7687 classes with a single conversion to integral or
7688 enumeration type. */
7689 if (!processing_template_decl
)
7692 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7697 error_at (token
->location
,
7698 "expression in new-declarator must have integral "
7699 "or enumeration type");
7700 expression
= error_mark_node
;
7704 /* Look for the closing `]'. */
7705 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7707 /* Add this bound to the declarator. */
7708 declarator
= make_array_declarator (declarator
, expression
);
7710 /* If the next token is not a `[', then there are no more
7712 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7719 /* Parse a new-initializer.
7722 ( expression-list [opt] )
7725 Returns a representation of the expression-list. */
7727 static vec
<tree
, va_gc
> *
7728 cp_parser_new_initializer (cp_parser
* parser
)
7730 vec
<tree
, va_gc
> *expression_list
;
7732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7735 bool expr_non_constant_p
;
7736 cp_lexer_set_source_position (parser
->lexer
);
7737 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7738 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7739 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7740 expression_list
= make_tree_vector_single (t
);
7743 expression_list
= (cp_parser_parenthesized_expression_list
7744 (parser
, non_attr
, /*cast_p=*/false,
7745 /*allow_expansion_p=*/true,
7746 /*non_constant_p=*/NULL
));
7748 return expression_list
;
7751 /* Parse a delete-expression.
7754 :: [opt] delete cast-expression
7755 :: [opt] delete [ ] cast-expression
7757 Returns a representation of the expression. */
7760 cp_parser_delete_expression (cp_parser
* parser
)
7762 bool global_scope_p
;
7766 /* Look for the optional `::' operator. */
7768 = (cp_parser_global_scope_opt (parser
,
7769 /*current_scope_valid_p=*/false)
7771 /* Look for the `delete' keyword. */
7772 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7773 /* See if the array syntax is in use. */
7774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7776 /* Consume the `[' token. */
7777 cp_lexer_consume_token (parser
->lexer
);
7778 /* Look for the `]' token. */
7779 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7780 /* Remember that this is the `[]' construct. */
7786 /* Parse the cast-expression. */
7787 expression
= cp_parser_simple_cast_expression (parser
);
7789 /* A delete-expression may not appear in an integral constant
7791 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7792 return error_mark_node
;
7794 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7795 tf_warning_or_error
);
7798 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7799 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7803 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7805 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7806 switch (token
->type
)
7812 case CPP_CLOSE_SQUARE
:
7813 case CPP_CLOSE_PAREN
:
7814 case CPP_CLOSE_BRACE
:
7815 case CPP_OPEN_BRACE
:
7819 case CPP_DEREF_STAR
:
7827 case CPP_GREATER_EQ
:
7848 case CPP_OPEN_PAREN
:
7849 /* In ((type ()) () the last () isn't a valid cast-expression,
7850 so the whole must be parsed as postfix-expression. */
7851 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7854 case CPP_OPEN_SQUARE
:
7855 /* '[' may start a primary-expression in obj-c++ and in C++11,
7856 as a lambda-expression, eg, '(void)[]{}'. */
7857 if (cxx_dialect
>= cxx11
)
7859 return c_dialect_objc ();
7862 case CPP_MINUS_MINUS
:
7863 /* '++' and '--' may or may not start a cast-expression:
7865 struct T { void operator++(int); };
7866 void f() { (T())++; }
7879 /* Parse a cast-expression.
7883 ( type-id ) cast-expression
7885 ADDRESS_P is true iff the unary-expression is appearing as the
7886 operand of the `&' operator. CAST_P is true if this expression is
7887 the target of a cast.
7889 Returns a representation of the expression. */
7892 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7893 bool decltype_p
, cp_id_kind
* pidk
)
7895 /* If it's a `(', then we might be looking at a cast. */
7896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7898 tree type
= NULL_TREE
;
7899 tree expr
= NULL_TREE
;
7900 int cast_expression
= 0;
7901 const char *saved_message
;
7903 /* There's no way to know yet whether or not this is a cast.
7904 For example, `(int (3))' is a unary-expression, while `(int)
7905 3' is a cast. So, we resort to parsing tentatively. */
7906 cp_parser_parse_tentatively (parser
);
7907 /* Types may not be defined in a cast. */
7908 saved_message
= parser
->type_definition_forbidden_message
;
7909 parser
->type_definition_forbidden_message
7910 = G_("types may not be defined in casts");
7911 /* Consume the `('. */
7912 cp_lexer_consume_token (parser
->lexer
);
7913 /* A very tricky bit is that `(struct S) { 3 }' is a
7914 compound-literal (which we permit in C++ as an extension).
7915 But, that construct is not a cast-expression -- it is a
7916 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7917 is legal; if the compound-literal were a cast-expression,
7918 you'd need an extra set of parentheses.) But, if we parse
7919 the type-id, and it happens to be a class-specifier, then we
7920 will commit to the parse at that point, because we cannot
7921 undo the action that is done when creating a new class. So,
7922 then we cannot back up and do a postfix-expression.
7924 Another tricky case is the following (c++/29234):
7926 struct S { void operator () (); };
7933 As a type-id we parse the parenthesized S()() as a function
7934 returning a function, groktypename complains and we cannot
7935 back up in this case either.
7937 Therefore, we scan ahead to the closing `)', and check to see
7938 if the tokens after the `)' can start a cast-expression. Otherwise
7939 we are dealing with an unary-expression, a postfix-expression
7942 Yet another tricky case, in C++11, is the following (c++/54891):
7946 The issue is that usually, besides the case of lambda-expressions,
7947 the parenthesized type-id cannot be followed by '[', and, eg, we
7948 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7949 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7950 we don't commit, we try a cast-expression, then an unary-expression.
7952 Save tokens so that we can put them back. */
7953 cp_lexer_save_tokens (parser
->lexer
);
7955 /* We may be looking at a cast-expression. */
7956 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7957 /*consume_paren=*/true))
7959 = cp_parser_tokens_start_cast_expression (parser
);
7961 /* Roll back the tokens we skipped. */
7962 cp_lexer_rollback_tokens (parser
->lexer
);
7963 /* If we aren't looking at a cast-expression, simulate an error so
7964 that the call to cp_parser_error_occurred below returns true. */
7965 if (!cast_expression
)
7966 cp_parser_simulate_error (parser
);
7969 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7970 parser
->in_type_id_in_expr_p
= true;
7971 /* Look for the type-id. */
7972 type
= cp_parser_type_id (parser
);
7973 /* Look for the closing `)'. */
7974 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7975 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7978 /* Restore the saved message. */
7979 parser
->type_definition_forbidden_message
= saved_message
;
7981 /* At this point this can only be either a cast or a
7982 parenthesized ctor such as `(T ())' that looks like a cast to
7983 function returning T. */
7984 if (!cp_parser_error_occurred (parser
))
7986 /* Only commit if the cast-expression doesn't start with
7987 '++', '--', or '[' in C++11. */
7988 if (cast_expression
> 0)
7989 cp_parser_commit_to_topmost_tentative_parse (parser
);
7991 expr
= cp_parser_cast_expression (parser
,
7992 /*address_p=*/false,
7994 /*decltype_p=*/false,
7997 if (cp_parser_parse_definitely (parser
))
7999 /* Warn about old-style casts, if so requested. */
8000 if (warn_old_style_cast
8001 && !in_system_header_at (input_location
)
8002 && !VOID_TYPE_P (type
)
8003 && current_lang_name
!= lang_name_c
)
8004 warning (OPT_Wold_style_cast
, "use of old-style cast");
8006 /* Only type conversions to integral or enumeration types
8007 can be used in constant-expressions. */
8008 if (!cast_valid_in_integral_constant_expression_p (type
)
8009 && cp_parser_non_integral_constant_expression (parser
,
8011 return error_mark_node
;
8013 /* Perform the cast. */
8014 expr
= build_c_cast (input_location
, type
, expr
);
8019 cp_parser_abort_tentative_parse (parser
);
8022 /* If we get here, then it's not a cast, so it must be a
8023 unary-expression. */
8024 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8025 cast_p
, decltype_p
);
8028 /* Parse a binary expression of the general form:
8032 pm-expression .* cast-expression
8033 pm-expression ->* cast-expression
8035 multiplicative-expression:
8037 multiplicative-expression * pm-expression
8038 multiplicative-expression / pm-expression
8039 multiplicative-expression % pm-expression
8041 additive-expression:
8042 multiplicative-expression
8043 additive-expression + multiplicative-expression
8044 additive-expression - multiplicative-expression
8048 shift-expression << additive-expression
8049 shift-expression >> additive-expression
8051 relational-expression:
8053 relational-expression < shift-expression
8054 relational-expression > shift-expression
8055 relational-expression <= shift-expression
8056 relational-expression >= shift-expression
8060 relational-expression:
8061 relational-expression <? shift-expression
8062 relational-expression >? shift-expression
8064 equality-expression:
8065 relational-expression
8066 equality-expression == relational-expression
8067 equality-expression != relational-expression
8071 and-expression & equality-expression
8073 exclusive-or-expression:
8075 exclusive-or-expression ^ and-expression
8077 inclusive-or-expression:
8078 exclusive-or-expression
8079 inclusive-or-expression | exclusive-or-expression
8081 logical-and-expression:
8082 inclusive-or-expression
8083 logical-and-expression && inclusive-or-expression
8085 logical-or-expression:
8086 logical-and-expression
8087 logical-or-expression || logical-and-expression
8089 All these are implemented with a single function like:
8092 simple-cast-expression
8093 binary-expression <token> binary-expression
8095 CAST_P is true if this expression is the target of a cast.
8097 The binops_by_token map is used to get the tree codes for each <token> type.
8098 binary-expressions are associated according to a precedence table. */
8100 #define TOKEN_PRECEDENCE(token) \
8101 (((token->type == CPP_GREATER \
8102 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8103 && !parser->greater_than_is_operator_p) \
8104 ? PREC_NOT_OPERATOR \
8105 : binops_by_token[token->type].prec)
8108 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8109 bool no_toplevel_fold_p
,
8111 enum cp_parser_prec prec
,
8114 cp_parser_expression_stack stack
;
8115 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8116 cp_parser_expression_stack_entry current
;
8119 enum tree_code rhs_type
;
8120 enum cp_parser_prec new_prec
, lookahead_prec
;
8123 /* Parse the first expression. */
8124 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8125 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8126 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8127 cast_p
, decltype_p
, pidk
);
8128 current
.prec
= prec
;
8130 if (cp_parser_error_occurred (parser
))
8131 return error_mark_node
;
8135 /* Get an operator token. */
8136 token
= cp_lexer_peek_token (parser
->lexer
);
8138 if (warn_cxx0x_compat
8139 && token
->type
== CPP_RSHIFT
8140 && !parser
->greater_than_is_operator_p
)
8142 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
8143 "%<>>%> operator is treated"
8144 " as two right angle brackets in C++11"))
8145 inform (token
->location
,
8146 "suggest parentheses around %<>>%> expression");
8149 new_prec
= TOKEN_PRECEDENCE (token
);
8151 /* Popping an entry off the stack means we completed a subexpression:
8152 - either we found a token which is not an operator (`>' where it is not
8153 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8154 will happen repeatedly;
8155 - or, we found an operator which has lower priority. This is the case
8156 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8158 if (new_prec
<= current
.prec
)
8167 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8168 current
.loc
= token
->location
;
8170 /* We used the operator token. */
8171 cp_lexer_consume_token (parser
->lexer
);
8173 /* For "false && x" or "true || x", x will never be executed;
8174 disable warnings while evaluating it. */
8175 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8176 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
8177 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8178 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
8180 /* Extract another operand. It may be the RHS of this expression
8181 or the LHS of a new, higher priority expression. */
8182 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8183 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8184 rhs
= cp_parser_simple_cast_expression (parser
);
8186 /* Get another operator token. Look up its precedence to avoid
8187 building a useless (immediately popped) stack entry for common
8188 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8189 token
= cp_lexer_peek_token (parser
->lexer
);
8190 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8191 if (lookahead_prec
> new_prec
)
8193 /* ... and prepare to parse the RHS of the new, higher priority
8194 expression. Since precedence levels on the stack are
8195 monotonically increasing, we do not have to care about
8200 current
.lhs_type
= rhs_type
;
8201 current
.prec
= new_prec
;
8202 new_prec
= lookahead_prec
;
8206 lookahead_prec
= new_prec
;
8207 /* If the stack is not empty, we have parsed into LHS the right side
8208 (`4' in the example above) of an expression we had suspended.
8209 We can use the information on the stack to recover the LHS (`3')
8210 from the stack together with the tree code (`MULT_EXPR'), and
8211 the precedence of the higher level subexpression
8212 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8213 which will be used to actually build the additive expression. */
8215 rhs_type
= current
.lhs_type
;
8220 /* Undo the disabling of warnings done above. */
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 if (warn_logical_not_paren
8227 && current
.lhs_type
== TRUTH_NOT_EXPR
)
8228 warn_logical_not_parentheses (current
.loc
, current
.tree_type
, rhs
);
8231 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8232 ERROR_MARK for everything that is not a binary expression.
8233 This makes warn_about_parentheses miss some warnings that
8234 involve unary operators. For unary expressions we should
8235 pass the correct tree_code unless the unary expression was
8236 surrounded by parentheses.
8238 if (no_toplevel_fold_p
8239 && lookahead_prec
<= current
.prec
8241 current
.lhs
= build2 (current
.tree_type
,
8242 TREE_CODE_CLASS (current
.tree_type
)
8244 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8247 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8248 current
.lhs
, current
.lhs_type
,
8249 rhs
, rhs_type
, &overload
,
8250 complain_flags (decltype_p
));
8251 current
.lhs_type
= current
.tree_type
;
8252 if (EXPR_P (current
.lhs
))
8253 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
8255 /* If the binary operator required the use of an overloaded operator,
8256 then this expression cannot be an integral constant-expression.
8257 An overloaded operator can be used even if both operands are
8258 otherwise permissible in an integral constant-expression if at
8259 least one of the operands is of enumeration type. */
8262 && cp_parser_non_integral_constant_expression (parser
,
8264 return error_mark_node
;
8271 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8272 bool no_toplevel_fold_p
,
8273 enum cp_parser_prec prec
,
8276 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8277 /*decltype*/false, prec
, pidk
);
8280 /* Parse the `? expression : assignment-expression' part of a
8281 conditional-expression. The LOGICAL_OR_EXPR is the
8282 logical-or-expression that started the conditional-expression.
8283 Returns a representation of the entire conditional-expression.
8285 This routine is used by cp_parser_assignment_expression.
8287 ? expression : assignment-expression
8291 ? : assignment-expression */
8294 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8297 tree assignment_expr
;
8298 struct cp_token
*token
;
8299 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8301 /* Consume the `?' token. */
8302 cp_lexer_consume_token (parser
->lexer
);
8303 token
= cp_lexer_peek_token (parser
->lexer
);
8304 if (cp_parser_allow_gnu_extensions_p (parser
)
8305 && token
->type
== CPP_COLON
)
8307 pedwarn (token
->location
, OPT_Wpedantic
,
8308 "ISO C++ does not allow ?: with omitted middle operand");
8309 /* Implicit true clause. */
8311 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8312 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8316 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8317 parser
->colon_corrects_to_scope_p
= false;
8318 /* Parse the expression. */
8319 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8320 expr
= cp_parser_expression (parser
);
8321 c_inhibit_evaluation_warnings
+=
8322 ((logical_or_expr
== truthvalue_true_node
)
8323 - (logical_or_expr
== truthvalue_false_node
));
8324 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8327 /* The next token should be a `:'. */
8328 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8329 /* Parse the assignment-expression. */
8330 assignment_expr
= cp_parser_assignment_expression (parser
);
8331 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8333 /* Build the conditional-expression. */
8334 return build_x_conditional_expr (loc
, logical_or_expr
,
8337 tf_warning_or_error
);
8340 /* Parse an assignment-expression.
8342 assignment-expression:
8343 conditional-expression
8344 logical-or-expression assignment-operator assignment_expression
8347 CAST_P is true if this expression is the target of a cast.
8348 DECLTYPE_P is true if this expression is the operand of decltype.
8350 Returns a representation for the expression. */
8353 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8354 bool cast_p
, bool decltype_p
)
8358 /* If the next token is the `throw' keyword, then we're looking at
8359 a throw-expression. */
8360 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8361 expr
= cp_parser_throw_expression (parser
);
8362 /* Otherwise, it must be that we are looking at a
8363 logical-or-expression. */
8366 /* Parse the binary expressions (logical-or-expression). */
8367 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8369 PREC_NOT_OPERATOR
, pidk
);
8370 /* If the next token is a `?' then we're actually looking at a
8371 conditional-expression. */
8372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8373 return cp_parser_question_colon_clause (parser
, expr
);
8376 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8378 /* If it's an assignment-operator, we're using the second
8380 enum tree_code assignment_operator
8381 = cp_parser_assignment_operator_opt (parser
);
8382 if (assignment_operator
!= ERROR_MARK
)
8384 bool non_constant_p
;
8385 location_t saved_input_location
;
8387 /* Parse the right-hand side of the assignment. */
8388 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8390 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8391 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8393 /* An assignment may not appear in a
8394 constant-expression. */
8395 if (cp_parser_non_integral_constant_expression (parser
,
8397 return error_mark_node
;
8398 /* Build the assignment expression. Its default
8399 location is the location of the '=' token. */
8400 saved_input_location
= input_location
;
8401 input_location
= loc
;
8402 expr
= build_x_modify_expr (loc
, expr
,
8403 assignment_operator
,
8405 complain_flags (decltype_p
));
8406 input_location
= saved_input_location
;
8414 /* Parse an (optional) assignment-operator.
8416 assignment-operator: one of
8417 = *= /= %= += -= >>= <<= &= ^= |=
8421 assignment-operator: one of
8424 If the next token is an assignment operator, the corresponding tree
8425 code is returned, and the token is consumed. For example, for
8426 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8427 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8428 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8429 operator, ERROR_MARK is returned. */
8431 static enum tree_code
8432 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8437 /* Peek at the next token. */
8438 token
= cp_lexer_peek_token (parser
->lexer
);
8440 switch (token
->type
)
8451 op
= TRUNC_DIV_EXPR
;
8455 op
= TRUNC_MOD_EXPR
;
8487 /* Nothing else is an assignment operator. */
8491 /* If it was an assignment operator, consume it. */
8492 if (op
!= ERROR_MARK
)
8493 cp_lexer_consume_token (parser
->lexer
);
8498 /* Parse an expression.
8501 assignment-expression
8502 expression , assignment-expression
8504 CAST_P is true if this expression is the target of a cast.
8505 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8506 except possibly parenthesized or on the RHS of a comma (N3276).
8508 Returns a representation of the expression. */
8511 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8512 bool cast_p
, bool decltype_p
)
8514 tree expression
= NULL_TREE
;
8515 location_t loc
= UNKNOWN_LOCATION
;
8519 tree assignment_expression
;
8521 /* Parse the next assignment-expression. */
8522 assignment_expression
8523 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
8525 /* We don't create a temporary for a call that is the immediate operand
8526 of decltype or on the RHS of a comma. But when we see a comma, we
8527 need to create a temporary for a call on the LHS. */
8528 if (decltype_p
&& !processing_template_decl
8529 && TREE_CODE (assignment_expression
) == CALL_EXPR
8530 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8531 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8532 assignment_expression
8533 = build_cplus_new (TREE_TYPE (assignment_expression
),
8534 assignment_expression
, tf_warning_or_error
);
8536 /* If this is the first assignment-expression, we can just
8539 expression
= assignment_expression
;
8541 expression
= build_x_compound_expr (loc
, expression
,
8542 assignment_expression
,
8543 complain_flags (decltype_p
));
8544 /* If the next token is not a comma, then we are done with the
8546 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8548 /* Consume the `,'. */
8549 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8550 cp_lexer_consume_token (parser
->lexer
);
8551 /* A comma operator cannot appear in a constant-expression. */
8552 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8553 expression
= error_mark_node
;
8559 /* Parse a constant-expression.
8561 constant-expression:
8562 conditional-expression
8564 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8565 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8566 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8567 is false, NON_CONSTANT_P should be NULL. */
8570 cp_parser_constant_expression (cp_parser
* parser
,
8571 bool allow_non_constant_p
,
8572 bool *non_constant_p
)
8574 bool saved_integral_constant_expression_p
;
8575 bool saved_allow_non_integral_constant_expression_p
;
8576 bool saved_non_integral_constant_expression_p
;
8579 /* It might seem that we could simply parse the
8580 conditional-expression, and then check to see if it were
8581 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8582 one that the compiler can figure out is constant, possibly after
8583 doing some simplifications or optimizations. The standard has a
8584 precise definition of constant-expression, and we must honor
8585 that, even though it is somewhat more restrictive.
8591 is not a legal declaration, because `(2, 3)' is not a
8592 constant-expression. The `,' operator is forbidden in a
8593 constant-expression. However, GCC's constant-folding machinery
8594 will fold this operation to an INTEGER_CST for `3'. */
8596 /* Save the old settings. */
8597 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8598 saved_allow_non_integral_constant_expression_p
8599 = parser
->allow_non_integral_constant_expression_p
;
8600 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8601 /* We are now parsing a constant-expression. */
8602 parser
->integral_constant_expression_p
= true;
8603 parser
->allow_non_integral_constant_expression_p
8604 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8605 parser
->non_integral_constant_expression_p
= false;
8606 /* Although the grammar says "conditional-expression", we parse an
8607 "assignment-expression", which also permits "throw-expression"
8608 and the use of assignment operators. In the case that
8609 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8610 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8611 actually essential that we look for an assignment-expression.
8612 For example, cp_parser_initializer_clauses uses this function to
8613 determine whether a particular assignment-expression is in fact
8615 expression
= cp_parser_assignment_expression (parser
);
8616 /* Restore the old settings. */
8617 parser
->integral_constant_expression_p
8618 = saved_integral_constant_expression_p
;
8619 parser
->allow_non_integral_constant_expression_p
8620 = saved_allow_non_integral_constant_expression_p
;
8621 if (cxx_dialect
>= cxx11
)
8623 /* Require an rvalue constant expression here; that's what our
8624 callers expect. Reference constant expressions are handled
8625 separately in e.g. cp_parser_template_argument. */
8626 bool is_const
= potential_rvalue_constant_expression (expression
);
8627 parser
->non_integral_constant_expression_p
= !is_const
;
8628 if (!is_const
&& !allow_non_constant_p
)
8629 require_potential_rvalue_constant_expression (expression
);
8631 if (allow_non_constant_p
)
8632 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8633 parser
->non_integral_constant_expression_p
8634 = saved_non_integral_constant_expression_p
;
8639 /* Parse __builtin_offsetof.
8641 offsetof-expression:
8642 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8644 offsetof-member-designator:
8646 | offsetof-member-designator "." id-expression
8647 | offsetof-member-designator "[" expression "]"
8648 | offsetof-member-designator "->" id-expression */
8651 cp_parser_builtin_offsetof (cp_parser
*parser
)
8653 int save_ice_p
, save_non_ice_p
;
8658 /* We're about to accept non-integral-constant things, but will
8659 definitely yield an integral constant expression. Save and
8660 restore these values around our local parsing. */
8661 save_ice_p
= parser
->integral_constant_expression_p
;
8662 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8664 /* Consume the "__builtin_offsetof" token. */
8665 cp_lexer_consume_token (parser
->lexer
);
8666 /* Consume the opening `('. */
8667 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8668 /* Parse the type-id. */
8669 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8670 type
= cp_parser_type_id (parser
);
8671 /* Look for the `,'. */
8672 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8673 token
= cp_lexer_peek_token (parser
->lexer
);
8675 /* Build the (type *)null that begins the traditional offsetof macro. */
8676 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8677 tf_warning_or_error
);
8679 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8680 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8681 true, &dummy
, token
->location
);
8684 token
= cp_lexer_peek_token (parser
->lexer
);
8685 switch (token
->type
)
8687 case CPP_OPEN_SQUARE
:
8688 /* offsetof-member-designator "[" expression "]" */
8689 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8694 /* offsetof-member-designator "->" identifier */
8695 expr
= grok_array_decl (token
->location
, expr
,
8696 integer_zero_node
, false);
8700 /* offsetof-member-designator "." identifier */
8701 cp_lexer_consume_token (parser
->lexer
);
8702 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8707 case CPP_CLOSE_PAREN
:
8708 /* Consume the ")" token. */
8709 cp_lexer_consume_token (parser
->lexer
);
8713 /* Error. We know the following require will fail, but
8714 that gives the proper error message. */
8715 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8716 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8717 expr
= error_mark_node
;
8723 /* If we're processing a template, we can't finish the semantics yet.
8724 Otherwise we can fold the entire expression now. */
8725 if (processing_template_decl
)
8727 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
8728 SET_EXPR_LOCATION (expr
, loc
);
8731 expr
= finish_offsetof (expr
, loc
);
8734 parser
->integral_constant_expression_p
= save_ice_p
;
8735 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8740 /* Parse a trait expression.
8742 Returns a representation of the expression, the underlying type
8743 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8746 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8749 tree type1
, type2
= NULL_TREE
;
8750 bool binary
= false;
8751 bool variadic
= false;
8755 case RID_HAS_NOTHROW_ASSIGN
:
8756 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8758 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8759 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8761 case RID_HAS_NOTHROW_COPY
:
8762 kind
= CPTK_HAS_NOTHROW_COPY
;
8764 case RID_HAS_TRIVIAL_ASSIGN
:
8765 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8767 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8768 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8770 case RID_HAS_TRIVIAL_COPY
:
8771 kind
= CPTK_HAS_TRIVIAL_COPY
;
8773 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8774 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8776 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8777 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8779 case RID_IS_ABSTRACT
:
8780 kind
= CPTK_IS_ABSTRACT
;
8782 case RID_IS_BASE_OF
:
8783 kind
= CPTK_IS_BASE_OF
;
8787 kind
= CPTK_IS_CLASS
;
8790 kind
= CPTK_IS_EMPTY
;
8793 kind
= CPTK_IS_ENUM
;
8796 kind
= CPTK_IS_FINAL
;
8798 case RID_IS_LITERAL_TYPE
:
8799 kind
= CPTK_IS_LITERAL_TYPE
;
8804 case RID_IS_POLYMORPHIC
:
8805 kind
= CPTK_IS_POLYMORPHIC
;
8807 case RID_IS_STD_LAYOUT
:
8808 kind
= CPTK_IS_STD_LAYOUT
;
8810 case RID_IS_TRIVIAL
:
8811 kind
= CPTK_IS_TRIVIAL
;
8813 case RID_IS_TRIVIALLY_ASSIGNABLE
:
8814 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
8817 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
8818 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
8821 case RID_IS_TRIVIALLY_COPYABLE
:
8822 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
8825 kind
= CPTK_IS_UNION
;
8827 case RID_UNDERLYING_TYPE
:
8828 kind
= CPTK_UNDERLYING_TYPE
;
8833 case RID_DIRECT_BASES
:
8834 kind
= CPTK_DIRECT_BASES
;
8840 /* Consume the token. */
8841 cp_lexer_consume_token (parser
->lexer
);
8843 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8845 type1
= cp_parser_type_id (parser
);
8847 if (type1
== error_mark_node
)
8848 return error_mark_node
;
8852 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8854 type2
= cp_parser_type_id (parser
);
8856 if (type2
== error_mark_node
)
8857 return error_mark_node
;
8861 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8863 cp_lexer_consume_token (parser
->lexer
);
8864 tree elt
= cp_parser_type_id (parser
);
8865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8867 cp_lexer_consume_token (parser
->lexer
);
8868 elt
= make_pack_expansion (elt
);
8870 if (elt
== error_mark_node
)
8871 return error_mark_node
;
8872 type2
= tree_cons (NULL_TREE
, elt
, type2
);
8876 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8878 /* Complete the trait expression, which may mean either processing
8879 the trait expr now or saving it for template instantiation. */
8882 case CPTK_UNDERLYING_TYPE
:
8883 return finish_underlying_type (type1
);
8885 return finish_bases (type1
, false);
8886 case CPTK_DIRECT_BASES
:
8887 return finish_bases (type1
, true);
8889 return finish_trait_expr (kind
, type1
, type2
);
8893 /* Lambdas that appear in variable initializer or default argument scope
8894 get that in their mangling, so we need to record it. We might as well
8895 use the count for function and namespace scopes as well. */
8896 static GTY(()) tree lambda_scope
;
8897 static GTY(()) int lambda_count
;
8898 typedef struct GTY(()) tree_int
8903 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8906 start_lambda_scope (tree decl
)
8910 /* Once we're inside a function, we ignore other scopes and just push
8911 the function again so that popping works properly. */
8912 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8913 decl
= current_function_decl
;
8914 ti
.t
= lambda_scope
;
8915 ti
.i
= lambda_count
;
8916 vec_safe_push (lambda_scope_stack
, ti
);
8917 if (lambda_scope
!= decl
)
8919 /* Don't reset the count if we're still in the same function. */
8920 lambda_scope
= decl
;
8926 record_lambda_scope (tree lambda
)
8928 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8929 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8933 finish_lambda_scope (void)
8935 tree_int
*p
= &lambda_scope_stack
->last ();
8936 if (lambda_scope
!= p
->t
)
8938 lambda_scope
= p
->t
;
8939 lambda_count
= p
->i
;
8941 lambda_scope_stack
->pop ();
8944 /* Parse a lambda expression.
8947 lambda-introducer lambda-declarator [opt] compound-statement
8949 Returns a representation of the expression. */
8952 cp_parser_lambda_expression (cp_parser
* parser
)
8954 tree lambda_expr
= build_lambda_expr ();
8957 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8958 cp_token_position start
= 0;
8960 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
8962 if (cp_unevaluated_operand
)
8964 if (!token
->error_reported
)
8966 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8967 "lambda-expression in unevaluated context");
8968 token
->error_reported
= true;
8972 else if (parser
->in_template_argument_list_p
)
8974 if (!token
->error_reported
)
8976 error_at (token
->location
, "lambda-expression in template-argument");
8977 token
->error_reported
= true;
8982 /* We may be in the middle of deferred access check. Disable
8984 push_deferring_access_checks (dk_no_deferred
);
8986 cp_parser_lambda_introducer (parser
, lambda_expr
);
8988 type
= begin_lambda_type (lambda_expr
);
8989 if (type
== error_mark_node
)
8990 return error_mark_node
;
8992 record_lambda_scope (lambda_expr
);
8994 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8995 determine_visibility (TYPE_NAME (type
));
8997 /* Now that we've started the type, add the capture fields for any
8998 explicit captures. */
8999 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9002 /* Inside the class, surrounding template-parameter-lists do not apply. */
9003 unsigned int saved_num_template_parameter_lists
9004 = parser
->num_template_parameter_lists
;
9005 unsigned char in_statement
= parser
->in_statement
;
9006 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9007 bool fully_implicit_function_template_p
9008 = parser
->fully_implicit_function_template_p
;
9009 tree implicit_template_parms
= parser
->implicit_template_parms
;
9010 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9011 bool auto_is_implicit_function_template_parm_p
9012 = parser
->auto_is_implicit_function_template_parm_p
;
9014 parser
->num_template_parameter_lists
= 0;
9015 parser
->in_statement
= 0;
9016 parser
->in_switch_statement_p
= false;
9017 parser
->fully_implicit_function_template_p
= false;
9018 parser
->implicit_template_parms
= 0;
9019 parser
->implicit_template_scope
= 0;
9020 parser
->auto_is_implicit_function_template_parm_p
= false;
9022 /* By virtue of defining a local class, a lambda expression has access to
9023 the private variables of enclosing classes. */
9025 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9029 if (!cp_parser_error_occurred (parser
)
9030 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9031 && cp_parser_start_tentative_firewall (parser
))
9033 cp_parser_lambda_body (parser
, lambda_expr
);
9035 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9037 if (cp_parser_skip_to_closing_brace (parser
))
9038 cp_lexer_consume_token (parser
->lexer
);
9041 /* The capture list was built up in reverse order; fix that now. */
9042 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9043 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9046 maybe_add_lambda_conv_op (type
);
9048 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9050 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9051 parser
->in_statement
= in_statement
;
9052 parser
->in_switch_statement_p
= in_switch_statement_p
;
9053 parser
->fully_implicit_function_template_p
9054 = fully_implicit_function_template_p
;
9055 parser
->implicit_template_parms
= implicit_template_parms
;
9056 parser
->implicit_template_scope
= implicit_template_scope
;
9057 parser
->auto_is_implicit_function_template_parm_p
9058 = auto_is_implicit_function_template_parm_p
;
9061 pop_deferring_access_checks ();
9063 /* This field is only used during parsing of the lambda. */
9064 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9066 /* This lambda shouldn't have any proxies left at this point. */
9067 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9068 /* And now that we're done, push proxies for an enclosing lambda. */
9069 insert_pending_capture_proxies ();
9072 lambda_expr
= build_lambda_object (lambda_expr
);
9074 lambda_expr
= error_mark_node
;
9076 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9081 /* Parse the beginning of a lambda expression.
9084 [ lambda-capture [opt] ]
9086 LAMBDA_EXPR is the current representation of the lambda expression. */
9089 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9091 /* Need commas after the first capture. */
9094 /* Eat the leading `['. */
9095 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9097 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9099 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9100 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9101 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9104 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9106 cp_lexer_consume_token (parser
->lexer
);
9110 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9112 cp_token
* capture_token
;
9114 tree capture_init_expr
;
9115 cp_id_kind idk
= CP_ID_KIND_NONE
;
9116 bool explicit_init_p
= false;
9118 enum capture_kind_type
9123 enum capture_kind_type capture_kind
= BY_COPY
;
9125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9127 error ("expected end of capture-list");
9134 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9136 /* Possibly capture `this'. */
9137 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9139 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9140 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9141 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9142 "with by-copy capture default");
9143 cp_lexer_consume_token (parser
->lexer
);
9144 add_capture (lambda_expr
,
9145 /*id=*/this_identifier
,
9146 /*initializer=*/finish_this_expr(),
9147 /*by_reference_p=*/false,
9152 /* Remember whether we want to capture as a reference or not. */
9153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9155 capture_kind
= BY_REFERENCE
;
9156 cp_lexer_consume_token (parser
->lexer
);
9159 /* Get the identifier. */
9160 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9161 capture_id
= cp_parser_identifier (parser
);
9163 if (capture_id
== error_mark_node
)
9164 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9165 delimiters, but I modified this to stop on unnested ']' as well. It
9166 was already changed to stop on unnested '}', so the
9167 "closing_parenthesis" name is no more misleading with my change. */
9169 cp_parser_skip_to_closing_parenthesis (parser
,
9170 /*recovering=*/true,
9172 /*consume_paren=*/true);
9176 /* Find the initializer for this capture. */
9177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9178 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9179 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9181 bool direct
, non_constant
;
9182 /* An explicit initializer exists. */
9183 if (cxx_dialect
< cxx14
)
9184 pedwarn (input_location
, 0,
9185 "lambda capture initializers "
9186 "only available with -std=c++14 or -std=gnu++14");
9187 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9189 explicit_init_p
= true;
9190 if (capture_init_expr
== NULL_TREE
)
9192 error ("empty initializer for lambda init-capture");
9193 capture_init_expr
= error_mark_node
;
9198 const char* error_msg
;
9200 /* Turn the identifier into an id-expression. */
9202 = cp_parser_lookup_name_simple (parser
, capture_id
,
9203 capture_token
->location
);
9205 if (capture_init_expr
== error_mark_node
)
9207 unqualified_name_lookup_error (capture_id
);
9210 else if (DECL_P (capture_init_expr
)
9211 && (!VAR_P (capture_init_expr
)
9212 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9214 error_at (capture_token
->location
,
9215 "capture of non-variable %qD ",
9217 inform (0, "%q+#D declared here", capture_init_expr
);
9220 if (VAR_P (capture_init_expr
)
9221 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9223 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9224 "%qD with non-automatic storage duration",
9226 inform (0, "%q+#D declared here", capture_init_expr
);
9231 = finish_id_expression
9236 /*integral_constant_expression_p=*/false,
9237 /*allow_non_integral_constant_expression_p=*/false,
9238 /*non_integral_constant_expression_p=*/NULL
,
9239 /*template_p=*/false,
9241 /*address_p=*/false,
9242 /*template_arg_p=*/false,
9244 capture_token
->location
);
9246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9248 cp_lexer_consume_token (parser
->lexer
);
9249 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9252 check_for_bare_parameter_packs (capture_init_expr
);
9255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9256 && !explicit_init_p
)
9258 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9259 && capture_kind
== BY_COPY
)
9260 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9261 "of %qD redundant with by-copy capture default",
9263 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9264 && capture_kind
== BY_REFERENCE
)
9265 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9266 "capture of %qD redundant with by-reference capture "
9267 "default", capture_id
);
9270 add_capture (lambda_expr
,
9273 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9277 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9280 /* Parse the (optional) middle of a lambda expression.
9283 < template-parameter-list [opt] >
9284 ( parameter-declaration-clause [opt] )
9285 attribute-specifier [opt]
9287 exception-specification [opt]
9288 lambda-return-type-clause [opt]
9290 LAMBDA_EXPR is the current representation of the lambda expression. */
9293 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9295 /* 5.1.1.4 of the standard says:
9296 If a lambda-expression does not include a lambda-declarator, it is as if
9297 the lambda-declarator were ().
9298 This means an empty parameter list, no attributes, and no exception
9300 tree param_list
= void_list_node
;
9301 tree attributes
= NULL_TREE
;
9302 tree exception_spec
= NULL_TREE
;
9303 tree template_param_list
= NULL_TREE
;
9305 /* The template-parameter-list is optional, but must begin with
9306 an opening angle if present. */
9307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9309 if (cxx_dialect
< cxx14
)
9310 pedwarn (parser
->lexer
->next_token
->location
, 0,
9311 "lambda templates are only available with "
9312 "-std=c++14 or -std=gnu++14");
9314 cp_lexer_consume_token (parser
->lexer
);
9316 template_param_list
= cp_parser_template_parameter_list (parser
);
9318 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9320 /* We just processed one more parameter list. */
9321 ++parser
->num_template_parameter_lists
;
9324 /* The parameter-declaration-clause is optional (unless
9325 template-parameter-list was given), but must begin with an
9326 opening parenthesis if present. */
9327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9329 cp_lexer_consume_token (parser
->lexer
);
9331 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9333 /* Parse parameters. */
9334 param_list
= cp_parser_parameter_declaration_clause (parser
);
9336 /* Default arguments shall not be specified in the
9337 parameter-declaration-clause of a lambda-declarator. */
9338 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9339 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
9340 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9341 "default argument specified for lambda parameter");
9343 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9345 attributes
= cp_parser_attributes_opt (parser
);
9347 /* Parse optional `mutable' keyword. */
9348 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9350 cp_lexer_consume_token (parser
->lexer
);
9351 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9354 /* Parse optional exception specification. */
9355 exception_spec
= cp_parser_exception_specification_opt (parser
);
9357 /* Parse optional trailing return type. */
9358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9360 cp_lexer_consume_token (parser
->lexer
);
9361 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9362 = cp_parser_trailing_type_id (parser
);
9365 /* The function parameters must be in scope all the way until after the
9366 trailing-return-type in case of decltype. */
9367 pop_bindings_and_leave_scope ();
9369 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9370 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9372 /* Create the function call operator.
9374 Messing with declarators like this is no uglier than building up the
9375 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9378 cp_decl_specifier_seq return_type_specs
;
9379 cp_declarator
* declarator
;
9384 clear_decl_specs (&return_type_specs
);
9385 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9386 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9388 /* Maybe we will deduce the return type later. */
9389 return_type_specs
.type
= make_auto ();
9391 p
= obstack_alloc (&declarator_obstack
, 0);
9393 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9396 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9397 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9398 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9399 VIRT_SPEC_UNSPECIFIED
,
9402 /*late_return_type=*/NULL_TREE
);
9403 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9405 fco
= grokmethod (&return_type_specs
,
9408 if (fco
!= error_mark_node
)
9410 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9411 DECL_ARTIFICIAL (fco
) = 1;
9412 /* Give the object parameter a different name. */
9413 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9414 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9415 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
9417 if (template_param_list
)
9419 fco
= finish_member_template_decl (fco
);
9420 finish_template_decl (template_param_list
);
9421 --parser
->num_template_parameter_lists
;
9423 else if (parser
->fully_implicit_function_template_p
)
9424 fco
= finish_fully_implicit_template (parser
, fco
);
9426 finish_member_declaration (fco
);
9428 obstack_free (&declarator_obstack
, p
);
9430 return (fco
!= error_mark_node
);
9434 /* Parse the body of a lambda expression, which is simply
9438 but which requires special handling.
9439 LAMBDA_EXPR is the current representation of the lambda expression. */
9442 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9444 bool nested
= (current_function_decl
!= NULL_TREE
);
9445 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9447 push_function_context ();
9449 /* Still increment function_depth so that we don't GC in the
9450 middle of an expression. */
9452 /* Clear this in case we're in the middle of a default argument. */
9453 parser
->local_variables_forbidden_p
= false;
9455 /* Finish the function call operator
9457 + late_parsing_for_member
9458 + function_definition_after_declarator
9459 + ctor_initializer_opt_and_function_body */
9461 tree fco
= lambda_function (lambda_expr
);
9467 /* Let the front end know that we are going to be defining this
9469 start_preparsed_function (fco
,
9471 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9473 start_lambda_scope (fco
);
9474 body
= begin_function_body ();
9476 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9479 /* Push the proxies for any explicit captures. */
9480 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9481 cap
= TREE_CHAIN (cap
))
9482 build_capture_proxy (TREE_PURPOSE (cap
));
9484 compound_stmt
= begin_compound_stmt (0);
9486 /* 5.1.1.4 of the standard says:
9487 If a lambda-expression does not include a trailing-return-type, it
9488 is as if the trailing-return-type denotes the following type:
9489 * if the compound-statement is of the form
9490 { return attribute-specifier [opt] expression ; }
9491 the type of the returned expression after lvalue-to-rvalue
9492 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9493 (_conv.array_ 4.2), and function-to-pointer conversion
9495 * otherwise, void. */
9497 /* In a lambda that has neither a lambda-return-type-clause
9498 nor a deducible form, errors should be reported for return statements
9499 in the body. Since we used void as the placeholder return type, parsing
9500 the body as usual will give such desired behavior. */
9501 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9502 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9503 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9505 tree expr
= NULL_TREE
;
9506 cp_id_kind idk
= CP_ID_KIND_NONE
;
9508 /* Parse tentatively in case there's more after the initial return
9510 cp_parser_parse_tentatively (parser
);
9512 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9514 expr
= cp_parser_expression (parser
, &idk
);
9516 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9517 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9519 if (cp_parser_parse_definitely (parser
))
9521 if (!processing_template_decl
)
9522 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9524 /* Will get error here if type not deduced yet. */
9525 finish_return_stmt (expr
);
9533 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9534 cp_parser_label_declaration (parser
);
9535 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9536 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9539 finish_compound_stmt (compound_stmt
);
9542 finish_function_body (body
);
9543 finish_lambda_scope ();
9545 /* Finish the function and generate code for it if necessary. */
9546 tree fn
= finish_function (/*inline*/2);
9548 /* Only expand if the call op is not a template. */
9549 if (!DECL_TEMPLATE_INFO (fco
))
9550 expand_or_defer_fn (fn
);
9553 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9555 pop_function_context();
9560 /* Statements [gram.stmt.stmt] */
9562 /* Parse a statement.
9566 expression-statement
9571 declaration-statement
9578 attribute-specifier-seq (opt) expression-statement
9579 attribute-specifier-seq (opt) compound-statement
9580 attribute-specifier-seq (opt) selection-statement
9581 attribute-specifier-seq (opt) iteration-statement
9582 attribute-specifier-seq (opt) jump-statement
9583 declaration-statement
9584 attribute-specifier-seq (opt) try-block
9591 IN_COMPOUND is true when the statement is nested inside a
9592 cp_parser_compound_statement; this matters for certain pragmas.
9594 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9595 is a (possibly labeled) if statement which is not enclosed in braces
9596 and has an else clause. This is used to implement -Wparentheses. */
9599 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9600 bool in_compound
, bool *if_p
)
9602 tree statement
, std_attrs
= NULL_TREE
;
9604 location_t statement_location
, attrs_location
;
9609 /* There is no statement yet. */
9610 statement
= NULL_TREE
;
9612 saved_token_sentinel
saved_tokens (parser
->lexer
);
9613 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9614 if (c_dialect_objc ())
9615 /* In obj-c++, seeing '[[' might be the either the beginning of
9616 c++11 attributes, or a nested objc-message-expression. So
9617 let's parse the c++11 attributes tentatively. */
9618 cp_parser_parse_tentatively (parser
);
9619 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9620 if (c_dialect_objc ())
9622 if (!cp_parser_parse_definitely (parser
))
9623 std_attrs
= NULL_TREE
;
9626 /* Peek at the next token. */
9627 token
= cp_lexer_peek_token (parser
->lexer
);
9628 /* Remember the location of the first token in the statement. */
9629 statement_location
= token
->location
;
9630 /* If this is a keyword, then that will often determine what kind of
9631 statement we have. */
9632 if (token
->type
== CPP_KEYWORD
)
9634 enum rid keyword
= token
->keyword
;
9640 /* Looks like a labeled-statement with a case label.
9641 Parse the label, and then use tail recursion to parse
9643 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9648 statement
= cp_parser_selection_statement (parser
, if_p
);
9654 statement
= cp_parser_iteration_statement (parser
, false);
9660 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
9661 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9662 cp_lexer_consume_token (parser
->lexer
);
9663 statement
= error_mark_node
;
9666 statement
= cp_parser_cilk_for (parser
, integer_zero_node
);
9673 statement
= cp_parser_jump_statement (parser
);
9677 cp_lexer_consume_token (parser
->lexer
);
9680 tree sync_expr
= build_cilk_sync ();
9681 SET_EXPR_LOCATION (sync_expr
,
9683 statement
= finish_expr_stmt (sync_expr
);
9687 error_at (token
->location
, "-fcilkplus must be enabled to use"
9689 statement
= error_mark_node
;
9691 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9694 /* Objective-C++ exception-handling constructs. */
9697 case RID_AT_FINALLY
:
9698 case RID_AT_SYNCHRONIZED
:
9700 statement
= cp_parser_objc_statement (parser
);
9704 statement
= cp_parser_try_block (parser
);
9708 /* This must be a namespace alias definition. */
9709 cp_parser_declaration_statement (parser
);
9712 case RID_TRANSACTION_ATOMIC
:
9713 case RID_TRANSACTION_RELAXED
:
9714 statement
= cp_parser_transaction (parser
, keyword
);
9716 case RID_TRANSACTION_CANCEL
:
9717 statement
= cp_parser_transaction_cancel (parser
);
9721 /* It might be a keyword like `int' that can start a
9722 declaration-statement. */
9726 else if (token
->type
== CPP_NAME
)
9728 /* If the next token is a `:', then we are looking at a
9729 labeled-statement. */
9730 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9731 if (token
->type
== CPP_COLON
)
9733 /* Looks like a labeled-statement with an ordinary label.
9734 Parse the label, and then use tail recursion to parse
9737 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9741 /* Anything that starts with a `{' must be a compound-statement. */
9742 else if (token
->type
== CPP_OPEN_BRACE
)
9743 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9744 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9745 a statement all its own. */
9746 else if (token
->type
== CPP_PRAGMA
)
9748 /* Only certain OpenMP pragmas are attached to statements, and thus
9749 are considered statements themselves. All others are not. In
9750 the context of a compound, accept the pragma as a "statement" and
9751 return so that we can check for a close brace. Otherwise we
9752 require a real statement and must go back and read one. */
9754 cp_parser_pragma (parser
, pragma_compound
);
9755 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9759 else if (token
->type
== CPP_EOF
)
9761 cp_parser_error (parser
, "expected statement");
9765 /* Everything else must be a declaration-statement or an
9766 expression-statement. Try for the declaration-statement
9767 first, unless we are looking at a `;', in which case we know that
9768 we have an expression-statement. */
9771 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9773 if (std_attrs
!= NULL_TREE
)
9775 /* Attributes should be parsed as part of the the
9776 declaration, so let's un-parse them. */
9777 saved_tokens
.rollback();
9778 std_attrs
= NULL_TREE
;
9781 cp_parser_parse_tentatively (parser
);
9782 /* Try to parse the declaration-statement. */
9783 cp_parser_declaration_statement (parser
);
9784 /* If that worked, we're done. */
9785 if (cp_parser_parse_definitely (parser
))
9788 /* Look for an expression-statement instead. */
9789 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9792 /* Set the line number for the statement. */
9793 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9794 SET_EXPR_LOCATION (statement
, statement_location
);
9796 /* Note that for now, we don't do anything with c++11 statements
9797 parsed at this level. */
9798 if (std_attrs
!= NULL_TREE
)
9799 warning_at (attrs_location
,
9801 "attributes at the beginning of statement are ignored");
9804 /* Parse the label for a labeled-statement, i.e.
9807 case constant-expression :
9811 case constant-expression ... constant-expression : statement
9813 When a label is parsed without errors, the label is added to the
9814 parse tree by the finish_* functions, so this function doesn't
9815 have to return the label. */
9818 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9821 tree label
= NULL_TREE
;
9822 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9824 /* The next token should be an identifier. */
9825 token
= cp_lexer_peek_token (parser
->lexer
);
9826 if (token
->type
!= CPP_NAME
9827 && token
->type
!= CPP_KEYWORD
)
9829 cp_parser_error (parser
, "expected labeled-statement");
9833 parser
->colon_corrects_to_scope_p
= false;
9834 switch (token
->keyword
)
9841 /* Consume the `case' token. */
9842 cp_lexer_consume_token (parser
->lexer
);
9843 /* Parse the constant-expression. */
9844 expr
= cp_parser_constant_expression (parser
);
9846 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9847 if (ellipsis
->type
== CPP_ELLIPSIS
)
9849 /* Consume the `...' token. */
9850 cp_lexer_consume_token (parser
->lexer
);
9852 cp_parser_constant_expression (parser
);
9854 /* We don't need to emit warnings here, as the common code
9855 will do this for us. */
9858 expr_hi
= NULL_TREE
;
9860 if (parser
->in_switch_statement_p
)
9861 finish_case_label (token
->location
, expr
, expr_hi
);
9863 error_at (token
->location
,
9864 "case label %qE not within a switch statement",
9870 /* Consume the `default' token. */
9871 cp_lexer_consume_token (parser
->lexer
);
9873 if (parser
->in_switch_statement_p
)
9874 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9876 error_at (token
->location
, "case label not within a switch statement");
9880 /* Anything else must be an ordinary label. */
9881 label
= finish_label_stmt (cp_parser_identifier (parser
));
9885 /* Require the `:' token. */
9886 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9888 /* An ordinary label may optionally be followed by attributes.
9889 However, this is only permitted if the attributes are then
9890 followed by a semicolon. This is because, for backward
9891 compatibility, when parsing
9892 lab: __attribute__ ((unused)) int i;
9893 we want the attribute to attach to "i", not "lab". */
9894 if (label
!= NULL_TREE
9895 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9898 cp_parser_parse_tentatively (parser
);
9899 attrs
= cp_parser_gnu_attributes_opt (parser
);
9900 if (attrs
== NULL_TREE
9901 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9902 cp_parser_abort_tentative_parse (parser
);
9903 else if (!cp_parser_parse_definitely (parser
))
9906 attributes
= chainon (attributes
, attrs
);
9909 if (attributes
!= NULL_TREE
)
9910 cplus_decl_attributes (&label
, attributes
, 0);
9912 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9915 /* Parse an expression-statement.
9917 expression-statement:
9920 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9921 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9922 indicates whether this expression-statement is part of an
9923 expression statement. */
9926 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9928 tree statement
= NULL_TREE
;
9929 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9931 /* If the next token is a ';', then there is no expression
9933 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9935 statement
= cp_parser_expression (parser
);
9936 if (statement
== error_mark_node
9937 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9939 cp_parser_skip_to_end_of_block_or_statement (parser
);
9940 return error_mark_node
;
9944 /* Give a helpful message for "A<T>::type t;" and the like. */
9945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9946 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9948 if (TREE_CODE (statement
) == SCOPE_REF
)
9949 error_at (token
->location
, "need %<typename%> before %qE because "
9950 "%qT is a dependent scope",
9951 statement
, TREE_OPERAND (statement
, 0));
9952 else if (is_overloaded_fn (statement
)
9953 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9956 tree fn
= get_first_fn (statement
);
9957 error_at (token
->location
,
9958 "%<%T::%D%> names the constructor, not the type",
9959 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9963 /* Consume the final `;'. */
9964 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9966 if (in_statement_expr
9967 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9968 /* This is the final expression statement of a statement
9970 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9972 statement
= finish_expr_stmt (statement
);
9977 /* Parse a compound-statement.
9980 { statement-seq [opt] }
9985 { label-declaration-seq [opt] statement-seq [opt] }
9987 label-declaration-seq:
9989 label-declaration-seq label-declaration
9991 Returns a tree representing the statement. */
9994 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
9995 bool in_try
, bool function_body
)
9999 /* Consume the `{'. */
10000 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10001 return error_mark_node
;
10002 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10003 && !function_body
&& cxx_dialect
< cxx14
)
10004 pedwarn (input_location
, OPT_Wpedantic
,
10005 "compound-statement in constexpr function");
10006 /* Begin the compound-statement. */
10007 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
10008 /* If the next keyword is `__label__' we have a label declaration. */
10009 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10010 cp_parser_label_declaration (parser
);
10011 /* Parse an (optional) statement-seq. */
10012 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10013 /* Finish the compound-statement. */
10014 finish_compound_stmt (compound_stmt
);
10015 /* Consume the `}'. */
10016 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10018 return compound_stmt
;
10021 /* Parse an (optional) statement-seq.
10025 statement-seq [opt] statement */
10028 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10030 /* Scan statements until there aren't any more. */
10033 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10035 /* If we are looking at a `}', then we have run out of
10036 statements; the same is true if we have reached the end
10037 of file, or have stumbled upon a stray '@end'. */
10038 if (token
->type
== CPP_CLOSE_BRACE
10039 || token
->type
== CPP_EOF
10040 || token
->type
== CPP_PRAGMA_EOL
10041 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10044 /* If we are in a compound statement and find 'else' then
10045 something went wrong. */
10046 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10048 if (parser
->in_statement
& IN_IF_STMT
)
10052 token
= cp_lexer_consume_token (parser
->lexer
);
10053 error_at (token
->location
, "%<else%> without a previous %<if%>");
10057 /* Parse the statement. */
10058 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10062 /* Parse a selection-statement.
10064 selection-statement:
10065 if ( condition ) statement
10066 if ( condition ) statement else statement
10067 switch ( condition ) statement
10069 Returns the new IF_STMT or SWITCH_STMT.
10071 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10072 is a (possibly labeled) if statement which is not enclosed in
10073 braces and has an else clause. This is used to implement
10077 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
10085 /* Peek at the next token. */
10086 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10088 /* See what kind of keyword it is. */
10089 keyword
= token
->keyword
;
10098 /* Look for the `('. */
10099 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10101 cp_parser_skip_to_end_of_statement (parser
);
10102 return error_mark_node
;
10105 /* Begin the selection-statement. */
10106 if (keyword
== RID_IF
)
10107 statement
= begin_if_stmt ();
10109 statement
= begin_switch_stmt ();
10111 /* Parse the condition. */
10112 condition
= cp_parser_condition (parser
);
10113 /* Look for the `)'. */
10114 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10115 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10116 /*consume_paren=*/true);
10118 if (keyword
== RID_IF
)
10121 unsigned char in_statement
;
10123 /* Add the condition. */
10124 finish_if_stmt_cond (condition
, statement
);
10126 /* Parse the then-clause. */
10127 in_statement
= parser
->in_statement
;
10128 parser
->in_statement
|= IN_IF_STMT
;
10129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10131 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10132 add_stmt (build_empty_stmt (loc
));
10133 cp_lexer_consume_token (parser
->lexer
);
10134 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
10135 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
10136 "empty body in an %<if%> statement");
10140 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
10141 parser
->in_statement
= in_statement
;
10143 finish_then_clause (statement
);
10145 /* If the next token is `else', parse the else-clause. */
10146 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10149 /* Consume the `else' keyword. */
10150 cp_lexer_consume_token (parser
->lexer
);
10151 begin_else_clause (statement
);
10152 /* Parse the else-clause. */
10153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10156 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10158 OPT_Wempty_body
, "suggest braces around "
10159 "empty body in an %<else%> statement");
10160 add_stmt (build_empty_stmt (loc
));
10161 cp_lexer_consume_token (parser
->lexer
);
10164 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10166 finish_else_clause (statement
);
10168 /* If we are currently parsing a then-clause, then
10169 IF_P will not be NULL. We set it to true to
10170 indicate that this if statement has an else clause.
10171 This may trigger the Wparentheses warning below
10172 when we get back up to the parent if statement. */
10178 /* This if statement does not have an else clause. If
10179 NESTED_IF is true, then the then-clause is an if
10180 statement which does have an else clause. We warn
10181 about the potential ambiguity. */
10183 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
10184 "suggest explicit braces to avoid ambiguous"
10188 /* Now we're all done with the if-statement. */
10189 finish_if_stmt (statement
);
10193 bool in_switch_statement_p
;
10194 unsigned char in_statement
;
10196 /* Add the condition. */
10197 finish_switch_cond (condition
, statement
);
10199 /* Parse the body of the switch-statement. */
10200 in_switch_statement_p
= parser
->in_switch_statement_p
;
10201 in_statement
= parser
->in_statement
;
10202 parser
->in_switch_statement_p
= true;
10203 parser
->in_statement
|= IN_SWITCH_STMT
;
10204 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10205 parser
->in_switch_statement_p
= in_switch_statement_p
;
10206 parser
->in_statement
= in_statement
;
10208 /* Now we're all done with the switch-statement. */
10209 finish_switch_stmt (statement
);
10217 cp_parser_error (parser
, "expected selection-statement");
10218 return error_mark_node
;
10222 /* Parse a condition.
10226 type-specifier-seq declarator = initializer-clause
10227 type-specifier-seq declarator braced-init-list
10232 type-specifier-seq declarator asm-specification [opt]
10233 attributes [opt] = assignment-expression
10235 Returns the expression that should be tested. */
10238 cp_parser_condition (cp_parser
* parser
)
10240 cp_decl_specifier_seq type_specifiers
;
10241 const char *saved_message
;
10242 int declares_class_or_enum
;
10244 /* Try the declaration first. */
10245 cp_parser_parse_tentatively (parser
);
10246 /* New types are not allowed in the type-specifier-seq for a
10248 saved_message
= parser
->type_definition_forbidden_message
;
10249 parser
->type_definition_forbidden_message
10250 = G_("types may not be defined in conditions");
10251 /* Parse the type-specifier-seq. */
10252 cp_parser_decl_specifier_seq (parser
,
10253 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
10255 &declares_class_or_enum
);
10256 /* Restore the saved message. */
10257 parser
->type_definition_forbidden_message
= saved_message
;
10258 /* If all is well, we might be looking at a declaration. */
10259 if (!cp_parser_error_occurred (parser
))
10262 tree asm_specification
;
10264 cp_declarator
*declarator
;
10265 tree initializer
= NULL_TREE
;
10267 /* Parse the declarator. */
10268 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10269 /*ctor_dtor_or_conv_p=*/NULL
,
10270 /*parenthesized_p=*/NULL
,
10271 /*member_p=*/false,
10272 /*friend_p=*/false);
10273 /* Parse the attributes. */
10274 attributes
= cp_parser_attributes_opt (parser
);
10275 /* Parse the asm-specification. */
10276 asm_specification
= cp_parser_asm_specification_opt (parser
);
10277 /* If the next token is not an `=' or '{', then we might still be
10278 looking at an expression. For example:
10282 looks like a decl-specifier-seq and a declarator -- but then
10283 there is no `=', so this is an expression. */
10284 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10285 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10286 cp_parser_simulate_error (parser
);
10288 /* If we did see an `=' or '{', then we are looking at a declaration
10290 if (cp_parser_parse_definitely (parser
))
10293 bool non_constant_p
;
10294 bool flags
= LOOKUP_ONLYCONVERTING
;
10296 /* Create the declaration. */
10297 decl
= start_decl (declarator
, &type_specifiers
,
10298 /*initialized_p=*/true,
10299 attributes
, /*prefix_attributes=*/NULL_TREE
,
10302 /* Parse the initializer. */
10303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10305 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10306 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10311 /* Consume the `='. */
10312 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10313 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10315 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10316 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10318 /* Process the initializer. */
10319 cp_finish_decl (decl
,
10320 initializer
, !non_constant_p
,
10325 pop_scope (pushed_scope
);
10327 return convert_from_reference (decl
);
10330 /* If we didn't even get past the declarator successfully, we are
10331 definitely not looking at a declaration. */
10333 cp_parser_abort_tentative_parse (parser
);
10335 /* Otherwise, we are looking at an expression. */
10336 return cp_parser_expression (parser
);
10339 /* Parses a for-statement or range-for-statement until the closing ')',
10343 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10345 tree init
, scope
, decl
;
10348 /* Begin the for-statement. */
10349 scope
= begin_for_scope (&init
);
10351 /* Parse the initialization. */
10352 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10355 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10357 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10361 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10363 /* Normal for loop */
10364 tree condition
= NULL_TREE
;
10365 tree expression
= NULL_TREE
;
10368 stmt
= begin_for_stmt (scope
, init
);
10369 /* The for-init-statement has already been parsed in
10370 cp_parser_for_init_statement, so no work is needed here. */
10371 finish_for_init_stmt (stmt
);
10373 /* If there's a condition, process it. */
10374 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10375 condition
= cp_parser_condition (parser
);
10378 cp_parser_error (parser
, "missing loop condition in loop with "
10379 "%<GCC ivdep%> pragma");
10380 condition
= error_mark_node
;
10382 finish_for_cond (condition
, stmt
, ivdep
);
10383 /* Look for the `;'. */
10384 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10386 /* If there's an expression, process it. */
10387 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10388 expression
= cp_parser_expression (parser
);
10389 finish_for_expr (expression
, stmt
);
10394 /* Tries to parse a range-based for-statement:
10397 decl-specifier-seq declarator : expression
10399 The decl-specifier-seq declarator and the `:' are already parsed by
10400 cp_parser_for_init_statement. If processing_template_decl it returns a
10401 newly created RANGE_FOR_STMT; if not, it is converted to a
10402 regular FOR_STMT. */
10405 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10408 tree stmt
, range_expr
;
10410 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10412 bool expr_non_constant_p
;
10413 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10416 range_expr
= cp_parser_expression (parser
);
10418 /* If in template, STMT is converted to a normal for-statement
10419 at instantiation. If not, it is done just ahead. */
10420 if (processing_template_decl
)
10422 if (check_for_bare_parameter_packs (range_expr
))
10423 range_expr
= error_mark_node
;
10424 stmt
= begin_range_for_stmt (scope
, init
);
10426 RANGE_FOR_IVDEP (stmt
) = 1;
10427 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10428 if (!type_dependent_expression_p (range_expr
)
10429 /* do_auto_deduction doesn't mess with template init-lists. */
10430 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10431 do_range_for_auto_deduction (range_decl
, range_expr
);
10435 stmt
= begin_for_stmt (scope
, init
);
10436 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10441 /* Subroutine of cp_convert_range_for: given the initializer expression,
10442 builds up the range temporary. */
10445 build_range_temp (tree range_expr
)
10447 tree range_type
, range_temp
;
10449 /* Find out the type deduced by the declaration
10450 `auto &&__range = range_expr'. */
10451 range_type
= cp_build_reference_type (make_auto (), true);
10452 range_type
= do_auto_deduction (range_type
, range_expr
,
10453 type_uses_auto (range_type
));
10455 /* Create the __range variable. */
10456 range_temp
= build_decl (input_location
, VAR_DECL
,
10457 get_identifier ("__for_range"), range_type
);
10458 TREE_USED (range_temp
) = 1;
10459 DECL_ARTIFICIAL (range_temp
) = 1;
10464 /* Used by cp_parser_range_for in template context: we aren't going to
10465 do a full conversion yet, but we still need to resolve auto in the
10466 type of the for-range-declaration if present. This is basically
10467 a shortcut version of cp_convert_range_for. */
10470 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10472 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10475 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10476 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10477 iter_type
= (cp_parser_perform_range_for_lookup
10478 (range_temp
, &begin_dummy
, &end_dummy
));
10481 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10483 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10484 tf_warning_or_error
);
10485 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10486 iter_decl
, auto_node
);
10491 /* Converts a range-based for-statement into a normal
10492 for-statement, as per the definition.
10494 for (RANGE_DECL : RANGE_EXPR)
10497 should be equivalent to:
10500 auto &&__range = RANGE_EXPR;
10501 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10505 RANGE_DECL = *__begin;
10510 If RANGE_EXPR is an array:
10511 BEGIN_EXPR = __range
10512 END_EXPR = __range + ARRAY_SIZE(__range)
10513 Else if RANGE_EXPR has a member 'begin' or 'end':
10514 BEGIN_EXPR = __range.begin()
10515 END_EXPR = __range.end()
10517 BEGIN_EXPR = begin(__range)
10518 END_EXPR = end(__range);
10520 If __range has a member 'begin' but not 'end', or vice versa, we must
10521 still use the second alternative (it will surely fail, however).
10522 When calling begin()/end() in the third alternative we must use
10523 argument dependent lookup, but always considering 'std' as an associated
10527 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10531 tree iter_type
, begin_expr
, end_expr
;
10532 tree condition
, expression
;
10534 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10535 /* If an error happened previously do nothing or else a lot of
10536 unhelpful errors would be issued. */
10537 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10542 if (TREE_CODE (range_expr
) == VAR_DECL
10543 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10544 /* Can't bind a reference to an array of runtime bound. */
10545 range_temp
= range_expr
;
10548 range_temp
= build_range_temp (range_expr
);
10549 pushdecl (range_temp
);
10550 cp_finish_decl (range_temp
, range_expr
,
10551 /*is_constant_init*/false, NULL_TREE
,
10552 LOOKUP_ONLYCONVERTING
);
10553 range_temp
= convert_from_reference (range_temp
);
10555 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10556 &begin_expr
, &end_expr
);
10559 /* The new for initialization statement. */
10560 begin
= build_decl (input_location
, VAR_DECL
,
10561 get_identifier ("__for_begin"), iter_type
);
10562 TREE_USED (begin
) = 1;
10563 DECL_ARTIFICIAL (begin
) = 1;
10565 cp_finish_decl (begin
, begin_expr
,
10566 /*is_constant_init*/false, NULL_TREE
,
10567 LOOKUP_ONLYCONVERTING
);
10569 end
= build_decl (input_location
, VAR_DECL
,
10570 get_identifier ("__for_end"), iter_type
);
10571 TREE_USED (end
) = 1;
10572 DECL_ARTIFICIAL (end
) = 1;
10574 cp_finish_decl (end
, end_expr
,
10575 /*is_constant_init*/false, NULL_TREE
,
10576 LOOKUP_ONLYCONVERTING
);
10578 finish_for_init_stmt (statement
);
10580 /* The new for condition. */
10581 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10584 NULL
, tf_warning_or_error
);
10585 finish_for_cond (condition
, statement
, ivdep
);
10587 /* The new increment expression. */
10588 expression
= finish_unary_op_expr (input_location
,
10589 PREINCREMENT_EXPR
, begin
,
10590 tf_warning_or_error
);
10591 finish_for_expr (expression
, statement
);
10593 /* The declaration is initialized with *__begin inside the loop body. */
10594 cp_finish_decl (range_decl
,
10595 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10596 tf_warning_or_error
),
10597 /*is_constant_init*/false, NULL_TREE
,
10598 LOOKUP_ONLYCONVERTING
);
10603 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10604 We need to solve both at the same time because the method used
10605 depends on the existence of members begin or end.
10606 Returns the type deduced for the iterator expression. */
10609 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10611 if (error_operand_p (range
))
10613 *begin
= *end
= error_mark_node
;
10614 return error_mark_node
;
10617 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10619 error ("range-based %<for%> expression of type %qT "
10620 "has incomplete type", TREE_TYPE (range
));
10621 *begin
= *end
= error_mark_node
;
10622 return error_mark_node
;
10624 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10626 /* If RANGE is an array, we will use pointer arithmetic. */
10628 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10630 array_type_nelts_top (TREE_TYPE (range
)),
10632 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10636 /* If it is not an array, we must do a bit of magic. */
10637 tree id_begin
, id_end
;
10638 tree member_begin
, member_end
;
10640 *begin
= *end
= error_mark_node
;
10642 id_begin
= get_identifier ("begin");
10643 id_end
= get_identifier ("end");
10644 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10645 /*protect=*/2, /*want_type=*/false,
10646 tf_warning_or_error
);
10647 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10648 /*protect=*/2, /*want_type=*/false,
10649 tf_warning_or_error
);
10651 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10653 /* Use the member functions. */
10654 if (member_begin
!= NULL_TREE
)
10655 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10657 error ("range-based %<for%> expression of type %qT has an "
10658 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10660 if (member_end
!= NULL_TREE
)
10661 *end
= cp_parser_range_for_member_function (range
, id_end
);
10663 error ("range-based %<for%> expression of type %qT has a "
10664 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10668 /* Use global functions with ADL. */
10669 vec
<tree
, va_gc
> *vec
;
10670 vec
= make_tree_vector ();
10672 vec_safe_push (vec
, range
);
10674 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10675 tf_warning_or_error
);
10676 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10677 tf_warning_or_error
);
10678 member_end
= perform_koenig_lookup (id_end
, vec
,
10679 tf_warning_or_error
);
10680 *end
= finish_call_expr (member_end
, &vec
, false, true,
10681 tf_warning_or_error
);
10683 release_tree_vector (vec
);
10686 /* Last common checks. */
10687 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10689 /* If one of the expressions is an error do no more checks. */
10690 *begin
= *end
= error_mark_node
;
10691 return error_mark_node
;
10693 else if (type_dependent_expression_p (*begin
)
10694 || type_dependent_expression_p (*end
))
10695 /* Can happen, when, eg, in a template context, Koenig lookup
10696 can't resolve begin/end (c++/58503). */
10700 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10701 /* The unqualified type of the __begin and __end temporaries should
10702 be the same, as required by the multiple auto declaration. */
10703 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10704 error ("inconsistent begin/end types in range-based %<for%> "
10705 "statement: %qT and %qT",
10706 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10712 /* Helper function for cp_parser_perform_range_for_lookup.
10713 Builds a tree for RANGE.IDENTIFIER(). */
10716 cp_parser_range_for_member_function (tree range
, tree identifier
)
10719 vec
<tree
, va_gc
> *vec
;
10721 member
= finish_class_member_access_expr (range
, identifier
,
10722 false, tf_warning_or_error
);
10723 if (member
== error_mark_node
)
10724 return error_mark_node
;
10726 vec
= make_tree_vector ();
10727 res
= finish_call_expr (member
, &vec
,
10728 /*disallow_virtual=*/false,
10729 /*koenig_p=*/false,
10730 tf_warning_or_error
);
10731 release_tree_vector (vec
);
10735 /* Parse an iteration-statement.
10737 iteration-statement:
10738 while ( condition ) statement
10739 do statement while ( expression ) ;
10740 for ( for-init-statement condition [opt] ; expression [opt] )
10743 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10746 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10751 unsigned char in_statement
;
10753 /* Peek at the next token. */
10754 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10756 return error_mark_node
;
10758 /* Remember whether or not we are already within an iteration
10760 in_statement
= parser
->in_statement
;
10762 /* See what kind of keyword it is. */
10763 keyword
= token
->keyword
;
10770 /* Begin the while-statement. */
10771 statement
= begin_while_stmt ();
10772 /* Look for the `('. */
10773 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10774 /* Parse the condition. */
10775 condition
= cp_parser_condition (parser
);
10776 finish_while_stmt_cond (condition
, statement
, ivdep
);
10777 /* Look for the `)'. */
10778 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10779 /* Parse the dependent statement. */
10780 parser
->in_statement
= IN_ITERATION_STMT
;
10781 cp_parser_already_scoped_statement (parser
);
10782 parser
->in_statement
= in_statement
;
10783 /* We're done with the while-statement. */
10784 finish_while_stmt (statement
);
10792 /* Begin the do-statement. */
10793 statement
= begin_do_stmt ();
10794 /* Parse the body of the do-statement. */
10795 parser
->in_statement
= IN_ITERATION_STMT
;
10796 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10797 parser
->in_statement
= in_statement
;
10798 finish_do_body (statement
);
10799 /* Look for the `while' keyword. */
10800 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10801 /* Look for the `('. */
10802 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10803 /* Parse the expression. */
10804 expression
= cp_parser_expression (parser
);
10805 /* We're done with the do-statement. */
10806 finish_do_stmt (expression
, statement
, ivdep
);
10807 /* Look for the `)'. */
10808 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10809 /* Look for the `;'. */
10810 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10816 /* Look for the `('. */
10817 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10819 statement
= cp_parser_for (parser
, ivdep
);
10821 /* Look for the `)'. */
10822 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10824 /* Parse the body of the for-statement. */
10825 parser
->in_statement
= IN_ITERATION_STMT
;
10826 cp_parser_already_scoped_statement (parser
);
10827 parser
->in_statement
= in_statement
;
10829 /* We're done with the for-statement. */
10830 finish_for_stmt (statement
);
10835 cp_parser_error (parser
, "expected iteration-statement");
10836 statement
= error_mark_node
;
10843 /* Parse a for-init-statement or the declarator of a range-based-for.
10844 Returns true if a range-based-for declaration is seen.
10846 for-init-statement:
10847 expression-statement
10848 simple-declaration */
10851 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10853 /* If the next token is a `;', then we have an empty
10854 expression-statement. Grammatically, this is also a
10855 simple-declaration, but an invalid one, because it does not
10856 declare anything. Therefore, if we did not handle this case
10857 specially, we would issue an error message about an invalid
10859 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10861 bool is_range_for
= false;
10862 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
10865 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
10867 /* N3994 -- for (id : init) ... */
10868 if (cxx_dialect
< cxx1z
)
10869 pedwarn (input_location
, 0, "range-based for loop without a "
10870 "type-specifier only available with "
10871 "-std=c++1z or -std=gnu++1z");
10872 tree name
= cp_parser_identifier (parser
);
10873 tree type
= cp_build_reference_type (make_auto (), /*rval*/true);
10874 *decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
10876 cp_lexer_consume_token (parser
->lexer
);
10880 /* A colon is used in range-based for. */
10881 parser
->colon_corrects_to_scope_p
= false;
10883 /* We're going to speculatively look for a declaration, falling back
10884 to an expression, if necessary. */
10885 cp_parser_parse_tentatively (parser
);
10886 /* Parse the declaration. */
10887 cp_parser_simple_declaration (parser
,
10888 /*function_definition_allowed_p=*/false,
10890 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10893 /* It is a range-for, consume the ':' */
10894 cp_lexer_consume_token (parser
->lexer
);
10895 is_range_for
= true;
10896 if (cxx_dialect
< cxx11
)
10898 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
10899 "range-based %<for%> loops only available with "
10900 "-std=c++11 or -std=gnu++11");
10901 *decl
= error_mark_node
;
10905 /* The ';' is not consumed yet because we told
10906 cp_parser_simple_declaration not to. */
10907 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10909 if (cp_parser_parse_definitely (parser
))
10910 return is_range_for
;
10911 /* If the tentative parse failed, then we shall need to look for an
10912 expression-statement. */
10914 /* If we are here, it is an expression-statement. */
10915 cp_parser_expression_statement (parser
, NULL_TREE
);
10919 /* Parse a jump-statement.
10924 return expression [opt] ;
10925 return braced-init-list ;
10931 goto * expression ;
10933 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10936 cp_parser_jump_statement (cp_parser
* parser
)
10938 tree statement
= error_mark_node
;
10941 unsigned char in_statement
;
10943 /* Peek at the next token. */
10944 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10946 return error_mark_node
;
10948 /* See what kind of keyword it is. */
10949 keyword
= token
->keyword
;
10953 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
10954 switch (in_statement
)
10957 error_at (token
->location
, "break statement not within loop or switch");
10960 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
10961 || in_statement
== IN_ITERATION_STMT
);
10962 statement
= finish_break_stmt ();
10963 if (in_statement
== IN_ITERATION_STMT
)
10964 break_maybe_infinite_loop ();
10967 error_at (token
->location
, "invalid exit from OpenMP structured block");
10970 error_at (token
->location
, "break statement used with OpenMP for loop");
10972 case IN_CILK_SIMD_FOR
:
10973 error_at (token
->location
, "break statement used with Cilk Plus for loop");
10976 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10980 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10983 error_at (token
->location
, "continue statement not within a loop");
10985 case IN_CILK_SIMD_FOR
:
10986 error_at (token
->location
,
10987 "continue statement within %<#pragma simd%> loop body");
10988 /* Fall through. */
10989 case IN_ITERATION_STMT
:
10991 statement
= finish_continue_stmt ();
10994 error_at (token
->location
, "invalid exit from OpenMP structured block");
10997 gcc_unreachable ();
10999 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11005 bool expr_non_constant_p
;
11007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11009 cp_lexer_set_source_position (parser
->lexer
);
11010 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11011 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11013 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11014 expr
= cp_parser_expression (parser
);
11016 /* If the next token is a `;', then there is no
11019 /* Build the return-statement. */
11020 statement
= finish_return_stmt (expr
);
11021 /* Look for the final `;'. */
11022 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11027 if (parser
->in_function_body
11028 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11029 error ("%<goto%> in %<constexpr%> function");
11031 /* Create the goto-statement. */
11032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11034 /* Issue a warning about this use of a GNU extension. */
11035 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11036 /* Consume the '*' token. */
11037 cp_lexer_consume_token (parser
->lexer
);
11038 /* Parse the dependent expression. */
11039 finish_goto_stmt (cp_parser_expression (parser
));
11042 finish_goto_stmt (cp_parser_identifier (parser
));
11043 /* Look for the final `;'. */
11044 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11048 cp_parser_error (parser
, "expected jump-statement");
11055 /* Parse a declaration-statement.
11057 declaration-statement:
11058 block-declaration */
11061 cp_parser_declaration_statement (cp_parser
* parser
)
11065 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11066 p
= obstack_alloc (&declarator_obstack
, 0);
11068 /* Parse the block-declaration. */
11069 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11071 /* Free any declarators allocated. */
11072 obstack_free (&declarator_obstack
, p
);
11075 /* Some dependent statements (like `if (cond) statement'), are
11076 implicitly in their own scope. In other words, if the statement is
11077 a single statement (as opposed to a compound-statement), it is
11078 none-the-less treated as if it were enclosed in braces. Any
11079 declarations appearing in the dependent statement are out of scope
11080 after control passes that point. This function parses a statement,
11081 but ensures that is in its own scope, even if it is not a
11082 compound-statement.
11084 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11085 is a (possibly labeled) if statement which is not enclosed in
11086 braces and has an else clause. This is used to implement
11089 Returns the new statement. */
11092 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
11099 /* Mark if () ; with a special NOP_EXPR. */
11100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11102 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11103 cp_lexer_consume_token (parser
->lexer
);
11104 statement
= add_stmt (build_empty_stmt (loc
));
11106 /* if a compound is opened, we simply parse the statement directly. */
11107 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11108 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
11109 /* If the token is not a `{', then we must take special action. */
11112 /* Create a compound-statement. */
11113 statement
= begin_compound_stmt (0);
11114 /* Parse the dependent-statement. */
11115 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
11116 /* Finish the dummy compound-statement. */
11117 finish_compound_stmt (statement
);
11120 /* Return the statement. */
11124 /* For some dependent statements (like `while (cond) statement'), we
11125 have already created a scope. Therefore, even if the dependent
11126 statement is a compound-statement, we do not want to create another
11130 cp_parser_already_scoped_statement (cp_parser
* parser
)
11132 /* If the token is a `{', then we must take special action. */
11133 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11134 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
11137 /* Avoid calling cp_parser_compound_statement, so that we
11138 don't create a new scope. Do everything else by hand. */
11139 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11140 /* If the next keyword is `__label__' we have a label declaration. */
11141 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11142 cp_parser_label_declaration (parser
);
11143 /* Parse an (optional) statement-seq. */
11144 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11145 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11149 /* Declarations [gram.dcl.dcl] */
11151 /* Parse an optional declaration-sequence.
11155 declaration-seq declaration */
11158 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11164 token
= cp_lexer_peek_token (parser
->lexer
);
11166 if (token
->type
== CPP_CLOSE_BRACE
11167 || token
->type
== CPP_EOF
11168 || token
->type
== CPP_PRAGMA_EOL
)
11171 if (token
->type
== CPP_SEMICOLON
)
11173 /* A declaration consisting of a single semicolon is
11174 invalid. Allow it unless we're being pedantic. */
11175 cp_lexer_consume_token (parser
->lexer
);
11176 if (!in_system_header_at (input_location
))
11177 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11181 /* If we're entering or exiting a region that's implicitly
11182 extern "C", modify the lang context appropriately. */
11183 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11185 push_lang_context (lang_name_c
);
11186 parser
->implicit_extern_c
= true;
11188 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
11190 pop_lang_context ();
11191 parser
->implicit_extern_c
= false;
11194 if (token
->type
== CPP_PRAGMA
)
11196 /* A top-level declaration can consist solely of a #pragma.
11197 A nested declaration cannot, so this is done here and not
11198 in cp_parser_declaration. (A #pragma at block scope is
11199 handled in cp_parser_statement.) */
11200 cp_parser_pragma (parser
, pragma_external
);
11204 /* Parse the declaration itself. */
11205 cp_parser_declaration (parser
);
11209 /* Parse a declaration.
11213 function-definition
11214 template-declaration
11215 explicit-instantiation
11216 explicit-specialization
11217 linkage-specification
11218 namespace-definition
11223 __extension__ declaration */
11226 cp_parser_declaration (cp_parser
* parser
)
11230 int saved_pedantic
;
11232 tree attributes
= NULL_TREE
;
11234 /* Check for the `__extension__' keyword. */
11235 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11237 /* Parse the qualified declaration. */
11238 cp_parser_declaration (parser
);
11239 /* Restore the PEDANTIC flag. */
11240 pedantic
= saved_pedantic
;
11245 /* Try to figure out what kind of declaration is present. */
11246 token1
= *cp_lexer_peek_token (parser
->lexer
);
11248 if (token1
.type
!= CPP_EOF
)
11249 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
11252 token2
.type
= CPP_EOF
;
11253 token2
.keyword
= RID_MAX
;
11256 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11257 p
= obstack_alloc (&declarator_obstack
, 0);
11259 /* If the next token is `extern' and the following token is a string
11260 literal, then we have a linkage specification. */
11261 if (token1
.keyword
== RID_EXTERN
11262 && cp_parser_is_pure_string_literal (&token2
))
11263 cp_parser_linkage_specification (parser
);
11264 /* If the next token is `template', then we have either a template
11265 declaration, an explicit instantiation, or an explicit
11267 else if (token1
.keyword
== RID_TEMPLATE
)
11269 /* `template <>' indicates a template specialization. */
11270 if (token2
.type
== CPP_LESS
11271 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
11272 cp_parser_explicit_specialization (parser
);
11273 /* `template <' indicates a template declaration. */
11274 else if (token2
.type
== CPP_LESS
)
11275 cp_parser_template_declaration (parser
, /*member_p=*/false);
11276 /* Anything else must be an explicit instantiation. */
11278 cp_parser_explicit_instantiation (parser
);
11280 /* If the next token is `export', then we have a template
11282 else if (token1
.keyword
== RID_EXPORT
)
11283 cp_parser_template_declaration (parser
, /*member_p=*/false);
11284 /* If the next token is `extern', 'static' or 'inline' and the one
11285 after that is `template', we have a GNU extended explicit
11286 instantiation directive. */
11287 else if (cp_parser_allow_gnu_extensions_p (parser
)
11288 && (token1
.keyword
== RID_EXTERN
11289 || token1
.keyword
== RID_STATIC
11290 || token1
.keyword
== RID_INLINE
)
11291 && token2
.keyword
== RID_TEMPLATE
)
11292 cp_parser_explicit_instantiation (parser
);
11293 /* If the next token is `namespace', check for a named or unnamed
11294 namespace definition. */
11295 else if (token1
.keyword
== RID_NAMESPACE
11296 && (/* A named namespace definition. */
11297 (token2
.type
== CPP_NAME
11298 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11300 /* An unnamed namespace definition. */
11301 || token2
.type
== CPP_OPEN_BRACE
11302 || token2
.keyword
== RID_ATTRIBUTE
))
11303 cp_parser_namespace_definition (parser
);
11304 /* An inline (associated) namespace definition. */
11305 else if (token1
.keyword
== RID_INLINE
11306 && token2
.keyword
== RID_NAMESPACE
)
11307 cp_parser_namespace_definition (parser
);
11308 /* Objective-C++ declaration/definition. */
11309 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11310 cp_parser_objc_declaration (parser
, NULL_TREE
);
11311 else if (c_dialect_objc ()
11312 && token1
.keyword
== RID_ATTRIBUTE
11313 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11314 cp_parser_objc_declaration (parser
, attributes
);
11315 /* We must have either a block declaration or a function
11318 /* Try to parse a block-declaration, or a function-definition. */
11319 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11321 /* Free any declarators allocated. */
11322 obstack_free (&declarator_obstack
, p
);
11325 /* Parse a block-declaration.
11330 namespace-alias-definition
11337 __extension__ block-declaration
11342 static_assert-declaration
11344 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11345 part of a declaration-statement. */
11348 cp_parser_block_declaration (cp_parser
*parser
,
11352 int saved_pedantic
;
11354 /* Check for the `__extension__' keyword. */
11355 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11357 /* Parse the qualified declaration. */
11358 cp_parser_block_declaration (parser
, statement_p
);
11359 /* Restore the PEDANTIC flag. */
11360 pedantic
= saved_pedantic
;
11365 /* Peek at the next token to figure out which kind of declaration is
11367 token1
= cp_lexer_peek_token (parser
->lexer
);
11369 /* If the next keyword is `asm', we have an asm-definition. */
11370 if (token1
->keyword
== RID_ASM
)
11373 cp_parser_commit_to_tentative_parse (parser
);
11374 cp_parser_asm_definition (parser
);
11376 /* If the next keyword is `namespace', we have a
11377 namespace-alias-definition. */
11378 else if (token1
->keyword
== RID_NAMESPACE
)
11379 cp_parser_namespace_alias_definition (parser
);
11380 /* If the next keyword is `using', we have a
11381 using-declaration, a using-directive, or an alias-declaration. */
11382 else if (token1
->keyword
== RID_USING
)
11387 cp_parser_commit_to_tentative_parse (parser
);
11388 /* If the token after `using' is `namespace', then we have a
11389 using-directive. */
11390 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11391 if (token2
->keyword
== RID_NAMESPACE
)
11392 cp_parser_using_directive (parser
);
11393 /* If the second token after 'using' is '=', then we have an
11394 alias-declaration. */
11395 else if (cxx_dialect
>= cxx11
11396 && token2
->type
== CPP_NAME
11397 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11398 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11399 cp_parser_alias_declaration (parser
);
11400 /* Otherwise, it's a using-declaration. */
11402 cp_parser_using_declaration (parser
,
11403 /*access_declaration_p=*/false);
11405 /* If the next keyword is `__label__' we have a misplaced label
11407 else if (token1
->keyword
== RID_LABEL
)
11409 cp_lexer_consume_token (parser
->lexer
);
11410 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11411 cp_parser_skip_to_end_of_statement (parser
);
11412 /* If the next token is now a `;', consume it. */
11413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11414 cp_lexer_consume_token (parser
->lexer
);
11416 /* If the next token is `static_assert' we have a static assertion. */
11417 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11418 cp_parser_static_assert (parser
, /*member_p=*/false);
11419 /* Anything else must be a simple-declaration. */
11421 cp_parser_simple_declaration (parser
, !statement_p
,
11422 /*maybe_range_for_decl*/NULL
);
11425 /* Parse a simple-declaration.
11427 simple-declaration:
11428 decl-specifier-seq [opt] init-declarator-list [opt] ;
11430 init-declarator-list:
11432 init-declarator-list , init-declarator
11434 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11435 function-definition as a simple-declaration.
11437 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11438 parsed declaration if it is an uninitialized single declarator not followed
11439 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11440 if present, will not be consumed. */
11443 cp_parser_simple_declaration (cp_parser
* parser
,
11444 bool function_definition_allowed_p
,
11445 tree
*maybe_range_for_decl
)
11447 cp_decl_specifier_seq decl_specifiers
;
11448 int declares_class_or_enum
;
11449 bool saw_declarator
;
11451 if (maybe_range_for_decl
)
11452 *maybe_range_for_decl
= NULL_TREE
;
11454 /* Defer access checks until we know what is being declared; the
11455 checks for names appearing in the decl-specifier-seq should be
11456 done as if we were in the scope of the thing being declared. */
11457 push_deferring_access_checks (dk_deferred
);
11459 /* Parse the decl-specifier-seq. We have to keep track of whether
11460 or not the decl-specifier-seq declares a named class or
11461 enumeration type, since that is the only case in which the
11462 init-declarator-list is allowed to be empty.
11466 In a simple-declaration, the optional init-declarator-list can be
11467 omitted only when declaring a class or enumeration, that is when
11468 the decl-specifier-seq contains either a class-specifier, an
11469 elaborated-type-specifier, or an enum-specifier. */
11470 cp_parser_decl_specifier_seq (parser
,
11471 CP_PARSER_FLAGS_OPTIONAL
,
11473 &declares_class_or_enum
);
11474 /* We no longer need to defer access checks. */
11475 stop_deferring_access_checks ();
11477 /* In a block scope, a valid declaration must always have a
11478 decl-specifier-seq. By not trying to parse declarators, we can
11479 resolve the declaration/expression ambiguity more quickly. */
11480 if (!function_definition_allowed_p
11481 && !decl_specifiers
.any_specifiers_p
)
11483 cp_parser_error (parser
, "expected declaration");
11487 /* If the next two tokens are both identifiers, the code is
11488 erroneous. The usual cause of this situation is code like:
11492 where "T" should name a type -- but does not. */
11493 if (!decl_specifiers
.any_type_specifiers_p
11494 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11496 /* If parsing tentatively, we should commit; we really are
11497 looking at a declaration. */
11498 cp_parser_commit_to_tentative_parse (parser
);
11503 /* If we have seen at least one decl-specifier, and the next token
11504 is not a parenthesis, then we must be looking at a declaration.
11505 (After "int (" we might be looking at a functional cast.) */
11506 if (decl_specifiers
.any_specifiers_p
11507 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11508 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11509 && !cp_parser_error_occurred (parser
))
11510 cp_parser_commit_to_tentative_parse (parser
);
11512 /* Keep going until we hit the `;' at the end of the simple
11514 saw_declarator
= false;
11515 while (cp_lexer_next_token_is_not (parser
->lexer
,
11519 bool function_definition_p
;
11522 if (saw_declarator
)
11524 /* If we are processing next declarator, coma is expected */
11525 token
= cp_lexer_peek_token (parser
->lexer
);
11526 gcc_assert (token
->type
== CPP_COMMA
);
11527 cp_lexer_consume_token (parser
->lexer
);
11528 if (maybe_range_for_decl
)
11529 *maybe_range_for_decl
= error_mark_node
;
11532 saw_declarator
= true;
11534 /* Parse the init-declarator. */
11535 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11537 function_definition_allowed_p
,
11538 /*member_p=*/false,
11539 declares_class_or_enum
,
11540 &function_definition_p
,
11541 maybe_range_for_decl
);
11542 /* If an error occurred while parsing tentatively, exit quickly.
11543 (That usually happens when in the body of a function; each
11544 statement is treated as a declaration-statement until proven
11546 if (cp_parser_error_occurred (parser
))
11548 /* Handle function definitions specially. */
11549 if (function_definition_p
)
11551 /* If the next token is a `,', then we are probably
11552 processing something like:
11556 which is erroneous. */
11557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11559 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11560 error_at (token
->location
,
11562 " declarations and function-definitions is forbidden");
11564 /* Otherwise, we're done with the list of declarators. */
11567 pop_deferring_access_checks ();
11571 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11572 *maybe_range_for_decl
= decl
;
11573 /* The next token should be either a `,' or a `;'. */
11574 token
= cp_lexer_peek_token (parser
->lexer
);
11575 /* If it's a `,', there are more declarators to come. */
11576 if (token
->type
== CPP_COMMA
)
11577 /* will be consumed next time around */;
11578 /* If it's a `;', we are done. */
11579 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11581 /* Anything else is an error. */
11584 /* If we have already issued an error message we don't need
11585 to issue another one. */
11586 if (decl
!= error_mark_node
11587 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11588 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11589 /* Skip tokens until we reach the end of the statement. */
11590 cp_parser_skip_to_end_of_statement (parser
);
11591 /* If the next token is now a `;', consume it. */
11592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11593 cp_lexer_consume_token (parser
->lexer
);
11596 /* After the first time around, a function-definition is not
11597 allowed -- even if it was OK at first. For example:
11602 function_definition_allowed_p
= false;
11605 /* Issue an error message if no declarators are present, and the
11606 decl-specifier-seq does not itself declare a class or
11607 enumeration: [dcl.dcl]/3. */
11608 if (!saw_declarator
)
11610 if (cp_parser_declares_only_class_p (parser
))
11612 if (!declares_class_or_enum
11613 && decl_specifiers
.type
11614 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11615 /* Ensure an error is issued anyway when finish_decltype_type,
11616 called via cp_parser_decl_specifier_seq, returns a class or
11617 an enumeration (c++/51786). */
11618 decl_specifiers
.type
= NULL_TREE
;
11619 shadow_tag (&decl_specifiers
);
11621 /* Perform any deferred access checks. */
11622 perform_deferred_access_checks (tf_warning_or_error
);
11625 /* Consume the `;'. */
11626 if (!maybe_range_for_decl
)
11627 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11630 pop_deferring_access_checks ();
11633 /* Parse a decl-specifier-seq.
11635 decl-specifier-seq:
11636 decl-specifier-seq [opt] decl-specifier
11637 decl-specifier attribute-specifier-seq [opt] (C++11)
11640 storage-class-specifier
11651 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11653 The parser flags FLAGS is used to control type-specifier parsing.
11655 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11658 1: one of the decl-specifiers is an elaborated-type-specifier
11659 (i.e., a type declaration)
11660 2: one of the decl-specifiers is an enum-specifier or a
11661 class-specifier (i.e., a type definition)
11666 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11667 cp_parser_flags flags
,
11668 cp_decl_specifier_seq
*decl_specs
,
11669 int* declares_class_or_enum
)
11671 bool constructor_possible_p
= !parser
->in_declarator_p
;
11672 bool found_decl_spec
= false;
11673 cp_token
*start_token
= NULL
;
11676 /* Clear DECL_SPECS. */
11677 clear_decl_specs (decl_specs
);
11679 /* Assume no class or enumeration type is declared. */
11680 *declares_class_or_enum
= 0;
11682 /* Keep reading specifiers until there are no more to read. */
11685 bool constructor_p
;
11689 /* Peek at the next token. */
11690 token
= cp_lexer_peek_token (parser
->lexer
);
11692 /* Save the first token of the decl spec list for error
11695 start_token
= token
;
11696 /* Handle attributes. */
11697 if (cp_next_tokens_can_be_attribute_p (parser
))
11699 /* Parse the attributes. */
11700 tree attrs
= cp_parser_attributes_opt (parser
);
11702 /* In a sequence of declaration specifiers, c++11 attributes
11703 appertain to the type that precede them. In that case
11706 The attribute-specifier-seq affects the type only for
11707 the declaration it appears in, not other declarations
11708 involving the same type.
11710 But for now let's force the user to position the
11711 attribute either at the beginning of the declaration or
11712 after the declarator-id, which would clearly mean that it
11713 applies to the declarator. */
11714 if (cxx11_attribute_p (attrs
))
11716 if (!found_decl_spec
)
11717 /* The c++11 attribute is at the beginning of the
11718 declaration. It appertains to the entity being
11722 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11724 /* This is an attribute following a
11725 class-specifier. */
11726 if (decl_specs
->type_definition_p
)
11727 warn_misplaced_attr_for_class_type (token
->location
,
11733 decl_specs
->std_attributes
11734 = chainon (decl_specs
->std_attributes
,
11736 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11737 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11743 decl_specs
->attributes
11744 = chainon (decl_specs
->attributes
,
11746 if (decl_specs
->locations
[ds_attribute
] == 0)
11747 decl_specs
->locations
[ds_attribute
] = token
->location
;
11750 /* Assume we will find a decl-specifier keyword. */
11751 found_decl_spec
= true;
11752 /* If the next token is an appropriate keyword, we can simply
11753 add it to the list. */
11754 switch (token
->keyword
)
11760 if (!at_class_scope_p ())
11762 error_at (token
->location
, "%<friend%> used outside of class");
11763 cp_lexer_purge_token (parser
->lexer
);
11768 /* Consume the token. */
11769 cp_lexer_consume_token (parser
->lexer
);
11773 case RID_CONSTEXPR
:
11775 cp_lexer_consume_token (parser
->lexer
);
11778 /* function-specifier:
11785 cp_parser_function_specifier_opt (parser
, decl_specs
);
11792 /* Consume the token. */
11793 cp_lexer_consume_token (parser
->lexer
);
11794 /* A constructor declarator cannot appear in a typedef. */
11795 constructor_possible_p
= false;
11796 /* The "typedef" keyword can only occur in a declaration; we
11797 may as well commit at this point. */
11798 cp_parser_commit_to_tentative_parse (parser
);
11800 if (decl_specs
->storage_class
!= sc_none
)
11801 decl_specs
->conflicting_specifiers_p
= true;
11804 /* storage-class-specifier:
11814 if (cxx_dialect
== cxx98
)
11816 /* Consume the token. */
11817 cp_lexer_consume_token (parser
->lexer
);
11819 /* Complain about `auto' as a storage specifier, if
11820 we're complaining about C++0x compatibility. */
11821 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11822 " changes meaning in C++11; please remove it");
11824 /* Set the storage class anyway. */
11825 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11829 /* C++0x auto type-specifier. */
11830 found_decl_spec
= false;
11837 /* Consume the token. */
11838 cp_lexer_consume_token (parser
->lexer
);
11839 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11843 /* Consume the token. */
11845 cp_lexer_consume_token (parser
->lexer
);
11849 /* We did not yet find a decl-specifier yet. */
11850 found_decl_spec
= false;
11854 if (found_decl_spec
11855 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11856 && token
->keyword
!= RID_CONSTEXPR
)
11857 error ("decl-specifier invalid in condition");
11860 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11862 /* Constructors are a special case. The `S' in `S()' is not a
11863 decl-specifier; it is the beginning of the declarator. */
11865 = (!found_decl_spec
11866 && constructor_possible_p
11867 && (cp_parser_constructor_declarator_p
11868 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11870 /* If we don't have a DECL_SPEC yet, then we must be looking at
11871 a type-specifier. */
11872 if (!found_decl_spec
&& !constructor_p
)
11874 int decl_spec_declares_class_or_enum
;
11875 bool is_cv_qualifier
;
11879 = cp_parser_type_specifier (parser
, flags
,
11881 /*is_declaration=*/true,
11882 &decl_spec_declares_class_or_enum
,
11884 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11886 /* If this type-specifier referenced a user-defined type
11887 (a typedef, class-name, etc.), then we can't allow any
11888 more such type-specifiers henceforth.
11892 The longest sequence of decl-specifiers that could
11893 possibly be a type name is taken as the
11894 decl-specifier-seq of a declaration. The sequence shall
11895 be self-consistent as described below.
11899 As a general rule, at most one type-specifier is allowed
11900 in the complete decl-specifier-seq of a declaration. The
11901 only exceptions are the following:
11903 -- const or volatile can be combined with any other
11906 -- signed or unsigned can be combined with char, long,
11914 void g (const int Pc);
11916 Here, Pc is *not* part of the decl-specifier seq; it's
11917 the declarator. Therefore, once we see a type-specifier
11918 (other than a cv-qualifier), we forbid any additional
11919 user-defined types. We *do* still allow things like `int
11920 int' to be considered a decl-specifier-seq, and issue the
11921 error message later. */
11922 if (type_spec
&& !is_cv_qualifier
)
11923 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11924 /* A constructor declarator cannot follow a type-specifier. */
11927 constructor_possible_p
= false;
11928 found_decl_spec
= true;
11929 if (!is_cv_qualifier
)
11930 decl_specs
->any_type_specifiers_p
= true;
11934 /* If we still do not have a DECL_SPEC, then there are no more
11935 decl-specifiers. */
11936 if (!found_decl_spec
)
11939 decl_specs
->any_specifiers_p
= true;
11940 /* After we see one decl-specifier, further decl-specifiers are
11941 always optional. */
11942 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
11945 /* Don't allow a friend specifier with a class definition. */
11946 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
11947 && (*declares_class_or_enum
& 2))
11948 error_at (decl_specs
->locations
[ds_friend
],
11949 "class definition may not be declared a friend");
11952 /* Parse an (optional) storage-class-specifier.
11954 storage-class-specifier:
11963 storage-class-specifier:
11966 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11969 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
11971 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
11974 if (cxx_dialect
!= cxx98
)
11976 /* Fall through for C++98. */
11983 /* Consume the token. */
11984 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11991 /* Parse an (optional) function-specifier.
11993 function-specifier:
11998 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11999 Updates DECL_SPECS, if it is non-NULL. */
12002 cp_parser_function_specifier_opt (cp_parser
* parser
,
12003 cp_decl_specifier_seq
*decl_specs
)
12005 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12006 switch (token
->keyword
)
12009 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12013 /* 14.5.2.3 [temp.mem]
12015 A member function template shall not be virtual. */
12016 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12017 error_at (token
->location
, "templates may not be %<virtual%>");
12019 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12023 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12030 /* Consume the token. */
12031 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12034 /* Parse a linkage-specification.
12036 linkage-specification:
12037 extern string-literal { declaration-seq [opt] }
12038 extern string-literal declaration */
12041 cp_parser_linkage_specification (cp_parser
* parser
)
12045 /* Look for the `extern' keyword. */
12046 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12048 /* Look for the string-literal. */
12049 linkage
= cp_parser_string_literal (parser
, false, false);
12051 /* Transform the literal into an identifier. If the literal is a
12052 wide-character string, or contains embedded NULs, then we can't
12053 handle it as the user wants. */
12054 if (strlen (TREE_STRING_POINTER (linkage
))
12055 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12057 cp_parser_error (parser
, "invalid linkage-specification");
12058 /* Assume C++ linkage. */
12059 linkage
= lang_name_cplusplus
;
12062 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12064 /* We're now using the new linkage. */
12065 push_lang_context (linkage
);
12067 /* If the next token is a `{', then we're using the first
12069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12071 cp_ensure_no_omp_declare_simd (parser
);
12073 /* Consume the `{' token. */
12074 cp_lexer_consume_token (parser
->lexer
);
12075 /* Parse the declarations. */
12076 cp_parser_declaration_seq_opt (parser
);
12077 /* Look for the closing `}'. */
12078 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12080 /* Otherwise, there's just one declaration. */
12083 bool saved_in_unbraced_linkage_specification_p
;
12085 saved_in_unbraced_linkage_specification_p
12086 = parser
->in_unbraced_linkage_specification_p
;
12087 parser
->in_unbraced_linkage_specification_p
= true;
12088 cp_parser_declaration (parser
);
12089 parser
->in_unbraced_linkage_specification_p
12090 = saved_in_unbraced_linkage_specification_p
;
12093 /* We're done with the linkage-specification. */
12094 pop_lang_context ();
12097 /* Parse a static_assert-declaration.
12099 static_assert-declaration:
12100 static_assert ( constant-expression , string-literal ) ;
12102 If MEMBER_P, this static_assert is a class member. */
12105 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12110 location_t saved_loc
;
12113 /* Peek at the `static_assert' token so we can keep track of exactly
12114 where the static assertion started. */
12115 token
= cp_lexer_peek_token (parser
->lexer
);
12116 saved_loc
= token
->location
;
12118 /* Look for the `static_assert' keyword. */
12119 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12123 /* We know we are in a static assertion; commit to any tentative
12125 if (cp_parser_parsing_tentatively (parser
))
12126 cp_parser_commit_to_tentative_parse (parser
);
12128 /* Parse the `(' starting the static assertion condition. */
12129 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
12131 /* Parse the constant-expression. Allow a non-constant expression
12132 here in order to give better diagnostics in finish_static_assert. */
12134 cp_parser_constant_expression (parser
,
12135 /*allow_non_constant_p=*/true,
12136 /*non_constant_p=*/&dummy
);
12138 /* Parse the separating `,'. */
12139 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
12141 /* Parse the string-literal message. */
12142 message
= cp_parser_string_literal (parser
,
12143 /*translate=*/false,
12146 /* A `)' completes the static assertion. */
12147 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12148 cp_parser_skip_to_closing_parenthesis (parser
,
12149 /*recovering=*/true,
12150 /*or_comma=*/false,
12151 /*consume_paren=*/true);
12153 /* A semicolon terminates the declaration. */
12154 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12156 /* Complete the static assertion, which may mean either processing
12157 the static assert now or saving it for template instantiation. */
12158 finish_static_assert (condition
, message
, saved_loc
, member_p
);
12161 /* Parse the expression in decltype ( expression ). */
12164 cp_parser_decltype_expr (cp_parser
*parser
,
12165 bool &id_expression_or_member_access_p
)
12167 cp_token
*id_expr_start_token
;
12170 /* First, try parsing an id-expression. */
12171 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
12172 cp_parser_parse_tentatively (parser
);
12173 expr
= cp_parser_id_expression (parser
,
12174 /*template_keyword_p=*/false,
12175 /*check_dependency_p=*/true,
12176 /*template_p=*/NULL
,
12177 /*declarator_p=*/false,
12178 /*optional_p=*/false);
12180 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
12182 bool non_integral_constant_expression_p
= false;
12183 tree id_expression
= expr
;
12185 const char *error_msg
;
12187 if (identifier_p (expr
))
12188 /* Lookup the name we got back from the id-expression. */
12189 expr
= cp_parser_lookup_name_simple (parser
, expr
,
12190 id_expr_start_token
->location
);
12193 && expr
!= error_mark_node
12194 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
12195 && TREE_CODE (expr
) != TYPE_DECL
12196 && (TREE_CODE (expr
) != BIT_NOT_EXPR
12197 || !TYPE_P (TREE_OPERAND (expr
, 0)))
12198 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12200 /* Complete lookup of the id-expression. */
12201 expr
= (finish_id_expression
12202 (id_expression
, expr
, parser
->scope
, &idk
,
12203 /*integral_constant_expression_p=*/false,
12204 /*allow_non_integral_constant_expression_p=*/true,
12205 &non_integral_constant_expression_p
,
12206 /*template_p=*/false,
12208 /*address_p=*/false,
12209 /*template_arg_p=*/false,
12211 id_expr_start_token
->location
));
12213 if (expr
== error_mark_node
)
12214 /* We found an id-expression, but it was something that we
12215 should not have found. This is an error, not something
12216 we can recover from, so note that we found an
12217 id-expression and we'll recover as gracefully as
12219 id_expression_or_member_access_p
= true;
12223 && expr
!= error_mark_node
12224 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12225 /* We have an id-expression. */
12226 id_expression_or_member_access_p
= true;
12229 if (!id_expression_or_member_access_p
)
12231 /* Abort the id-expression parse. */
12232 cp_parser_abort_tentative_parse (parser
);
12234 /* Parsing tentatively, again. */
12235 cp_parser_parse_tentatively (parser
);
12237 /* Parse a class member access. */
12238 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
12239 /*cast_p=*/false, /*decltype*/true,
12240 /*member_access_only_p=*/true, NULL
);
12243 && expr
!= error_mark_node
12244 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12245 /* We have an id-expression. */
12246 id_expression_or_member_access_p
= true;
12249 if (id_expression_or_member_access_p
)
12250 /* We have parsed the complete id-expression or member access. */
12251 cp_parser_parse_definitely (parser
);
12254 /* Abort our attempt to parse an id-expression or member access
12256 cp_parser_abort_tentative_parse (parser
);
12258 /* Parse a full expression. */
12259 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
12260 /*decltype_p=*/true);
12266 /* Parse a `decltype' type. Returns the type.
12268 simple-type-specifier:
12269 decltype ( expression )
12271 decltype ( auto ) */
12274 cp_parser_decltype (cp_parser
*parser
)
12277 bool id_expression_or_member_access_p
= false;
12278 const char *saved_message
;
12279 bool saved_integral_constant_expression_p
;
12280 bool saved_non_integral_constant_expression_p
;
12281 bool saved_greater_than_is_operator_p
;
12282 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12284 if (start_token
->type
== CPP_DECLTYPE
)
12286 /* Already parsed. */
12287 cp_lexer_consume_token (parser
->lexer
);
12288 return start_token
->u
.value
;
12291 /* Look for the `decltype' token. */
12292 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
12293 return error_mark_node
;
12295 /* Parse the opening `('. */
12296 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12297 return error_mark_node
;
12299 /* decltype (auto) */
12300 if (cxx_dialect
>= cxx14
12301 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12303 cp_lexer_consume_token (parser
->lexer
);
12304 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12305 return error_mark_node
;
12306 expr
= make_decltype_auto ();
12307 AUTO_IS_DECLTYPE (expr
) = true;
12311 /* Types cannot be defined in a `decltype' expression. Save away the
12313 saved_message
= parser
->type_definition_forbidden_message
;
12315 /* And create the new one. */
12316 parser
->type_definition_forbidden_message
12317 = G_("types may not be defined in %<decltype%> expressions");
12319 /* The restrictions on constant-expressions do not apply inside
12320 decltype expressions. */
12321 saved_integral_constant_expression_p
12322 = parser
->integral_constant_expression_p
;
12323 saved_non_integral_constant_expression_p
12324 = parser
->non_integral_constant_expression_p
;
12325 parser
->integral_constant_expression_p
= false;
12327 /* Within a parenthesized expression, a `>' token is always
12328 the greater-than operator. */
12329 saved_greater_than_is_operator_p
12330 = parser
->greater_than_is_operator_p
;
12331 parser
->greater_than_is_operator_p
= true;
12333 /* Do not actually evaluate the expression. */
12334 ++cp_unevaluated_operand
;
12336 /* Do not warn about problems with the expression. */
12337 ++c_inhibit_evaluation_warnings
;
12339 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12341 /* Go back to evaluating expressions. */
12342 --cp_unevaluated_operand
;
12343 --c_inhibit_evaluation_warnings
;
12345 /* The `>' token might be the end of a template-id or
12346 template-parameter-list now. */
12347 parser
->greater_than_is_operator_p
12348 = saved_greater_than_is_operator_p
;
12350 /* Restore the old message and the integral constant expression
12352 parser
->type_definition_forbidden_message
= saved_message
;
12353 parser
->integral_constant_expression_p
12354 = saved_integral_constant_expression_p
;
12355 parser
->non_integral_constant_expression_p
12356 = saved_non_integral_constant_expression_p
;
12358 /* Parse to the closing `)'. */
12359 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12361 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12362 /*consume_paren=*/true);
12363 return error_mark_node
;
12366 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12367 tf_warning_or_error
);
12370 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12372 start_token
->type
= CPP_DECLTYPE
;
12373 start_token
->u
.value
= expr
;
12374 start_token
->keyword
= RID_MAX
;
12375 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12380 /* Special member functions [gram.special] */
12382 /* Parse a conversion-function-id.
12384 conversion-function-id:
12385 operator conversion-type-id
12387 Returns an IDENTIFIER_NODE representing the operator. */
12390 cp_parser_conversion_function_id (cp_parser
* parser
)
12394 tree saved_qualifying_scope
;
12395 tree saved_object_scope
;
12396 tree pushed_scope
= NULL_TREE
;
12398 /* Look for the `operator' token. */
12399 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12400 return error_mark_node
;
12401 /* When we parse the conversion-type-id, the current scope will be
12402 reset. However, we need that information in able to look up the
12403 conversion function later, so we save it here. */
12404 saved_scope
= parser
->scope
;
12405 saved_qualifying_scope
= parser
->qualifying_scope
;
12406 saved_object_scope
= parser
->object_scope
;
12407 /* We must enter the scope of the class so that the names of
12408 entities declared within the class are available in the
12409 conversion-type-id. For example, consider:
12416 S::operator I() { ... }
12418 In order to see that `I' is a type-name in the definition, we
12419 must be in the scope of `S'. */
12421 pushed_scope
= push_scope (saved_scope
);
12422 /* Parse the conversion-type-id. */
12423 type
= cp_parser_conversion_type_id (parser
);
12424 /* Leave the scope of the class, if any. */
12426 pop_scope (pushed_scope
);
12427 /* Restore the saved scope. */
12428 parser
->scope
= saved_scope
;
12429 parser
->qualifying_scope
= saved_qualifying_scope
;
12430 parser
->object_scope
= saved_object_scope
;
12431 /* If the TYPE is invalid, indicate failure. */
12432 if (type
== error_mark_node
)
12433 return error_mark_node
;
12434 return mangle_conv_op_name_for_type (type
);
12437 /* Parse a conversion-type-id:
12439 conversion-type-id:
12440 type-specifier-seq conversion-declarator [opt]
12442 Returns the TYPE specified. */
12445 cp_parser_conversion_type_id (cp_parser
* parser
)
12448 cp_decl_specifier_seq type_specifiers
;
12449 cp_declarator
*declarator
;
12450 tree type_specified
;
12451 const char *saved_message
;
12453 /* Parse the attributes. */
12454 attributes
= cp_parser_attributes_opt (parser
);
12456 saved_message
= parser
->type_definition_forbidden_message
;
12457 parser
->type_definition_forbidden_message
12458 = G_("types may not be defined in a conversion-type-id");
12460 /* Parse the type-specifiers. */
12461 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12462 /*is_trailing_return=*/false,
12465 parser
->type_definition_forbidden_message
= saved_message
;
12467 /* If that didn't work, stop. */
12468 if (type_specifiers
.type
== error_mark_node
)
12469 return error_mark_node
;
12470 /* Parse the conversion-declarator. */
12471 declarator
= cp_parser_conversion_declarator_opt (parser
);
12473 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12474 /*initialized=*/0, &attributes
);
12476 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12478 /* Don't give this error when parsing tentatively. This happens to
12479 work because we always parse this definitively once. */
12480 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12481 && type_uses_auto (type_specified
))
12483 if (cxx_dialect
< cxx14
)
12485 error ("invalid use of %<auto%> in conversion operator");
12486 return error_mark_node
;
12488 else if (template_parm_scope_p ())
12489 warning (0, "use of %<auto%> in member template "
12490 "conversion operator can never be deduced");
12493 return type_specified
;
12496 /* Parse an (optional) conversion-declarator.
12498 conversion-declarator:
12499 ptr-operator conversion-declarator [opt]
12503 static cp_declarator
*
12504 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12506 enum tree_code code
;
12507 tree class_type
, std_attributes
= NULL_TREE
;
12508 cp_cv_quals cv_quals
;
12510 /* We don't know if there's a ptr-operator next, or not. */
12511 cp_parser_parse_tentatively (parser
);
12512 /* Try the ptr-operator. */
12513 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12515 /* If it worked, look for more conversion-declarators. */
12516 if (cp_parser_parse_definitely (parser
))
12518 cp_declarator
*declarator
;
12520 /* Parse another optional declarator. */
12521 declarator
= cp_parser_conversion_declarator_opt (parser
);
12523 declarator
= cp_parser_make_indirect_declarator
12524 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12532 /* Parse an (optional) ctor-initializer.
12535 : mem-initializer-list
12537 Returns TRUE iff the ctor-initializer was actually present. */
12540 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12542 /* If the next token is not a `:', then there is no
12543 ctor-initializer. */
12544 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12546 /* Do default initialization of any bases and members. */
12547 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12548 finish_mem_initializers (NULL_TREE
);
12553 /* Consume the `:' token. */
12554 cp_lexer_consume_token (parser
->lexer
);
12555 /* And the mem-initializer-list. */
12556 cp_parser_mem_initializer_list (parser
);
12561 /* Parse a mem-initializer-list.
12563 mem-initializer-list:
12564 mem-initializer ... [opt]
12565 mem-initializer ... [opt] , mem-initializer-list */
12568 cp_parser_mem_initializer_list (cp_parser
* parser
)
12570 tree mem_initializer_list
= NULL_TREE
;
12571 tree target_ctor
= error_mark_node
;
12572 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12574 /* Let the semantic analysis code know that we are starting the
12575 mem-initializer-list. */
12576 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12577 error_at (token
->location
,
12578 "only constructors take member initializers");
12580 /* Loop through the list. */
12583 tree mem_initializer
;
12585 token
= cp_lexer_peek_token (parser
->lexer
);
12586 /* Parse the mem-initializer. */
12587 mem_initializer
= cp_parser_mem_initializer (parser
);
12588 /* If the next token is a `...', we're expanding member initializers. */
12589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12591 /* Consume the `...'. */
12592 cp_lexer_consume_token (parser
->lexer
);
12594 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12595 can be expanded but members cannot. */
12596 if (mem_initializer
!= error_mark_node
12597 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12599 error_at (token
->location
,
12600 "cannot expand initializer for member %<%D%>",
12601 TREE_PURPOSE (mem_initializer
));
12602 mem_initializer
= error_mark_node
;
12605 /* Construct the pack expansion type. */
12606 if (mem_initializer
!= error_mark_node
)
12607 mem_initializer
= make_pack_expansion (mem_initializer
);
12609 if (target_ctor
!= error_mark_node
12610 && mem_initializer
!= error_mark_node
)
12612 error ("mem-initializer for %qD follows constructor delegation",
12613 TREE_PURPOSE (mem_initializer
));
12614 mem_initializer
= error_mark_node
;
12616 /* Look for a target constructor. */
12617 if (mem_initializer
!= error_mark_node
12618 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12619 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12621 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12622 if (mem_initializer_list
)
12624 error ("constructor delegation follows mem-initializer for %qD",
12625 TREE_PURPOSE (mem_initializer_list
));
12626 mem_initializer
= error_mark_node
;
12628 target_ctor
= mem_initializer
;
12630 /* Add it to the list, unless it was erroneous. */
12631 if (mem_initializer
!= error_mark_node
)
12633 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12634 mem_initializer_list
= mem_initializer
;
12636 /* If the next token is not a `,', we're done. */
12637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12639 /* Consume the `,' token. */
12640 cp_lexer_consume_token (parser
->lexer
);
12643 /* Perform semantic analysis. */
12644 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12645 finish_mem_initializers (mem_initializer_list
);
12648 /* Parse a mem-initializer.
12651 mem-initializer-id ( expression-list [opt] )
12652 mem-initializer-id braced-init-list
12657 ( expression-list [opt] )
12659 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12660 class) or FIELD_DECL (for a non-static data member) to initialize;
12661 the TREE_VALUE is the expression-list. An empty initialization
12662 list is represented by void_list_node. */
12665 cp_parser_mem_initializer (cp_parser
* parser
)
12667 tree mem_initializer_id
;
12668 tree expression_list
;
12670 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12672 /* Find out what is being initialized. */
12673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12675 permerror (token
->location
,
12676 "anachronistic old-style base class initializer");
12677 mem_initializer_id
= NULL_TREE
;
12681 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12682 if (mem_initializer_id
== error_mark_node
)
12683 return mem_initializer_id
;
12685 member
= expand_member_init (mem_initializer_id
);
12686 if (member
&& !DECL_P (member
))
12687 in_base_initializer
= 1;
12689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12691 bool expr_non_constant_p
;
12692 cp_lexer_set_source_position (parser
->lexer
);
12693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12694 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12695 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12696 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12700 vec
<tree
, va_gc
> *vec
;
12701 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12703 /*allow_expansion_p=*/true,
12704 /*non_constant_p=*/NULL
);
12706 return error_mark_node
;
12707 expression_list
= build_tree_list_vec (vec
);
12708 release_tree_vector (vec
);
12711 if (expression_list
== error_mark_node
)
12712 return error_mark_node
;
12713 if (!expression_list
)
12714 expression_list
= void_type_node
;
12716 in_base_initializer
= 0;
12718 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12721 /* Parse a mem-initializer-id.
12723 mem-initializer-id:
12724 :: [opt] nested-name-specifier [opt] class-name
12727 Returns a TYPE indicating the class to be initializer for the first
12728 production. Returns an IDENTIFIER_NODE indicating the data member
12729 to be initialized for the second production. */
12732 cp_parser_mem_initializer_id (cp_parser
* parser
)
12734 bool global_scope_p
;
12735 bool nested_name_specifier_p
;
12736 bool template_p
= false;
12739 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12741 /* `typename' is not allowed in this context ([temp.res]). */
12742 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12744 error_at (token
->location
,
12745 "keyword %<typename%> not allowed in this context (a qualified "
12746 "member initializer is implicitly a type)");
12747 cp_lexer_consume_token (parser
->lexer
);
12749 /* Look for the optional `::' operator. */
12751 = (cp_parser_global_scope_opt (parser
,
12752 /*current_scope_valid_p=*/false)
12754 /* Look for the optional nested-name-specifier. The simplest way to
12759 The keyword `typename' is not permitted in a base-specifier or
12760 mem-initializer; in these contexts a qualified name that
12761 depends on a template-parameter is implicitly assumed to be a
12764 is to assume that we have seen the `typename' keyword at this
12766 nested_name_specifier_p
12767 = (cp_parser_nested_name_specifier_opt (parser
,
12768 /*typename_keyword_p=*/true,
12769 /*check_dependency_p=*/true,
12771 /*is_declaration=*/true)
12773 if (nested_name_specifier_p
)
12774 template_p
= cp_parser_optional_template_keyword (parser
);
12775 /* If there is a `::' operator or a nested-name-specifier, then we
12776 are definitely looking for a class-name. */
12777 if (global_scope_p
|| nested_name_specifier_p
)
12778 return cp_parser_class_name (parser
,
12779 /*typename_keyword_p=*/true,
12780 /*template_keyword_p=*/template_p
,
12782 /*check_dependency_p=*/true,
12783 /*class_head_p=*/false,
12784 /*is_declaration=*/true);
12785 /* Otherwise, we could also be looking for an ordinary identifier. */
12786 cp_parser_parse_tentatively (parser
);
12787 /* Try a class-name. */
12788 id
= cp_parser_class_name (parser
,
12789 /*typename_keyword_p=*/true,
12790 /*template_keyword_p=*/false,
12792 /*check_dependency_p=*/true,
12793 /*class_head_p=*/false,
12794 /*is_declaration=*/true);
12795 /* If we found one, we're done. */
12796 if (cp_parser_parse_definitely (parser
))
12798 /* Otherwise, look for an ordinary identifier. */
12799 return cp_parser_identifier (parser
);
12802 /* Overloading [gram.over] */
12804 /* Parse an operator-function-id.
12806 operator-function-id:
12809 Returns an IDENTIFIER_NODE for the operator which is a
12810 human-readable spelling of the identifier, e.g., `operator +'. */
12813 cp_parser_operator_function_id (cp_parser
* parser
)
12815 /* Look for the `operator' keyword. */
12816 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12817 return error_mark_node
;
12818 /* And then the name of the operator itself. */
12819 return cp_parser_operator (parser
);
12822 /* Return an identifier node for a user-defined literal operator.
12823 The suffix identifier is chained to the operator name identifier. */
12826 cp_literal_operator_id (const char* name
)
12829 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12830 + strlen (name
) + 10);
12831 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12832 identifier
= get_identifier (buffer
);
12837 /* Parse an operator.
12840 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12841 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12842 || ++ -- , ->* -> () []
12849 Returns an IDENTIFIER_NODE for the operator which is a
12850 human-readable spelling of the identifier, e.g., `operator +'. */
12853 cp_parser_operator (cp_parser
* parser
)
12855 tree id
= NULL_TREE
;
12859 /* Peek at the next token. */
12860 token
= cp_lexer_peek_token (parser
->lexer
);
12861 /* Figure out which operator we have. */
12862 switch (token
->type
)
12868 /* The keyword should be either `new' or `delete'. */
12869 if (token
->keyword
== RID_NEW
)
12871 else if (token
->keyword
== RID_DELETE
)
12876 /* Consume the `new' or `delete' token. */
12877 cp_lexer_consume_token (parser
->lexer
);
12879 /* Peek at the next token. */
12880 token
= cp_lexer_peek_token (parser
->lexer
);
12881 /* If it's a `[' token then this is the array variant of the
12883 if (token
->type
== CPP_OPEN_SQUARE
)
12885 /* Consume the `[' token. */
12886 cp_lexer_consume_token (parser
->lexer
);
12887 /* Look for the `]' token. */
12888 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12889 id
= ansi_opname (op
== NEW_EXPR
12890 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12892 /* Otherwise, we have the non-array variant. */
12894 id
= ansi_opname (op
);
12900 id
= ansi_opname (PLUS_EXPR
);
12904 id
= ansi_opname (MINUS_EXPR
);
12908 id
= ansi_opname (MULT_EXPR
);
12912 id
= ansi_opname (TRUNC_DIV_EXPR
);
12916 id
= ansi_opname (TRUNC_MOD_EXPR
);
12920 id
= ansi_opname (BIT_XOR_EXPR
);
12924 id
= ansi_opname (BIT_AND_EXPR
);
12928 id
= ansi_opname (BIT_IOR_EXPR
);
12932 id
= ansi_opname (BIT_NOT_EXPR
);
12936 id
= ansi_opname (TRUTH_NOT_EXPR
);
12940 id
= ansi_assopname (NOP_EXPR
);
12944 id
= ansi_opname (LT_EXPR
);
12948 id
= ansi_opname (GT_EXPR
);
12952 id
= ansi_assopname (PLUS_EXPR
);
12956 id
= ansi_assopname (MINUS_EXPR
);
12960 id
= ansi_assopname (MULT_EXPR
);
12964 id
= ansi_assopname (TRUNC_DIV_EXPR
);
12968 id
= ansi_assopname (TRUNC_MOD_EXPR
);
12972 id
= ansi_assopname (BIT_XOR_EXPR
);
12976 id
= ansi_assopname (BIT_AND_EXPR
);
12980 id
= ansi_assopname (BIT_IOR_EXPR
);
12984 id
= ansi_opname (LSHIFT_EXPR
);
12988 id
= ansi_opname (RSHIFT_EXPR
);
12991 case CPP_LSHIFT_EQ
:
12992 id
= ansi_assopname (LSHIFT_EXPR
);
12995 case CPP_RSHIFT_EQ
:
12996 id
= ansi_assopname (RSHIFT_EXPR
);
13000 id
= ansi_opname (EQ_EXPR
);
13004 id
= ansi_opname (NE_EXPR
);
13008 id
= ansi_opname (LE_EXPR
);
13011 case CPP_GREATER_EQ
:
13012 id
= ansi_opname (GE_EXPR
);
13016 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13020 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13023 case CPP_PLUS_PLUS
:
13024 id
= ansi_opname (POSTINCREMENT_EXPR
);
13027 case CPP_MINUS_MINUS
:
13028 id
= ansi_opname (PREDECREMENT_EXPR
);
13032 id
= ansi_opname (COMPOUND_EXPR
);
13035 case CPP_DEREF_STAR
:
13036 id
= ansi_opname (MEMBER_REF
);
13040 id
= ansi_opname (COMPONENT_REF
);
13043 case CPP_OPEN_PAREN
:
13044 /* Consume the `('. */
13045 cp_lexer_consume_token (parser
->lexer
);
13046 /* Look for the matching `)'. */
13047 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13048 return ansi_opname (CALL_EXPR
);
13050 case CPP_OPEN_SQUARE
:
13051 /* Consume the `['. */
13052 cp_lexer_consume_token (parser
->lexer
);
13053 /* Look for the matching `]'. */
13054 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13055 return ansi_opname (ARRAY_REF
);
13057 case CPP_UTF8STRING
:
13058 case CPP_UTF8STRING_USERDEF
:
13064 case CPP_STRING_USERDEF
:
13065 case CPP_WSTRING_USERDEF
:
13066 case CPP_STRING16_USERDEF
:
13067 case CPP_STRING32_USERDEF
:
13069 tree str
, string_tree
;
13072 if (cxx_dialect
== cxx98
)
13073 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13075 /* Consume the string. */
13076 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13077 /*wide_ok=*/true, /*lookup_udlit=*/false);
13078 if (str
== error_mark_node
)
13079 return error_mark_node
;
13080 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13082 string_tree
= USERDEF_LITERAL_VALUE (str
);
13083 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13088 /* Look for the suffix identifier. */
13089 token
= cp_lexer_peek_token (parser
->lexer
);
13090 if (token
->type
== CPP_NAME
)
13091 id
= cp_parser_identifier (parser
);
13092 else if (token
->type
== CPP_KEYWORD
)
13094 error ("unexpected keyword;"
13095 " remove space between quotes and suffix identifier");
13096 return error_mark_node
;
13100 error ("expected suffix identifier");
13101 return error_mark_node
;
13104 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13105 (TREE_TYPE (TREE_TYPE (string_tree
))));
13106 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
13109 error ("expected empty string after %<operator%> keyword");
13110 return error_mark_node
;
13112 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
13115 error ("invalid encoding prefix in literal operator");
13116 return error_mark_node
;
13118 if (id
!= error_mark_node
)
13120 const char *name
= IDENTIFIER_POINTER (id
);
13121 id
= cp_literal_operator_id (name
);
13127 /* Anything else is an error. */
13131 /* If we have selected an identifier, we need to consume the
13134 cp_lexer_consume_token (parser
->lexer
);
13135 /* Otherwise, no valid operator name was present. */
13138 cp_parser_error (parser
, "expected operator");
13139 id
= error_mark_node
;
13145 /* Parse a template-declaration.
13147 template-declaration:
13148 export [opt] template < template-parameter-list > declaration
13150 If MEMBER_P is TRUE, this template-declaration occurs within a
13153 The grammar rule given by the standard isn't correct. What
13154 is really meant is:
13156 template-declaration:
13157 export [opt] template-parameter-list-seq
13158 decl-specifier-seq [opt] init-declarator [opt] ;
13159 export [opt] template-parameter-list-seq
13160 function-definition
13162 template-parameter-list-seq:
13163 template-parameter-list-seq [opt]
13164 template < template-parameter-list > */
13167 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
13169 /* Check for `export'. */
13170 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
13172 /* Consume the `export' token. */
13173 cp_lexer_consume_token (parser
->lexer
);
13174 /* Warn that we do not support `export'. */
13175 warning (0, "keyword %<export%> not implemented, and will be ignored");
13178 cp_parser_template_declaration_after_export (parser
, member_p
);
13181 /* Parse a template-parameter-list.
13183 template-parameter-list:
13185 template-parameter-list , template-parameter
13187 Returns a TREE_LIST. Each node represents a template parameter.
13188 The nodes are connected via their TREE_CHAINs. */
13191 cp_parser_template_parameter_list (cp_parser
* parser
)
13193 tree parameter_list
= NULL_TREE
;
13195 begin_template_parm_list ();
13197 /* The loop below parses the template parms. We first need to know
13198 the total number of template parms to be able to compute proper
13199 canonical types of each dependent type. So after the loop, when
13200 we know the total number of template parms,
13201 end_template_parm_list computes the proper canonical types and
13202 fixes up the dependent types accordingly. */
13207 bool is_parameter_pack
;
13208 location_t parm_loc
;
13210 /* Parse the template-parameter. */
13211 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13212 parameter
= cp_parser_template_parameter (parser
,
13214 &is_parameter_pack
);
13215 /* Add it to the list. */
13216 if (parameter
!= error_mark_node
)
13217 parameter_list
= process_template_parm (parameter_list
,
13221 is_parameter_pack
);
13224 tree err_parm
= build_tree_list (parameter
, parameter
);
13225 parameter_list
= chainon (parameter_list
, err_parm
);
13228 /* If the next token is not a `,', we're done. */
13229 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13231 /* Otherwise, consume the `,' token. */
13232 cp_lexer_consume_token (parser
->lexer
);
13235 return end_template_parm_list (parameter_list
);
13238 /* Parse a template-parameter.
13240 template-parameter:
13242 parameter-declaration
13244 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13245 the parameter. The TREE_PURPOSE is the default value, if any.
13246 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13247 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13248 set to true iff this parameter is a parameter pack. */
13251 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
13252 bool *is_parameter_pack
)
13255 cp_parameter_declarator
*parameter_declarator
;
13256 cp_declarator
*id_declarator
;
13259 /* Assume it is a type parameter or a template parameter. */
13260 *is_non_type
= false;
13261 /* Assume it not a parameter pack. */
13262 *is_parameter_pack
= false;
13263 /* Peek at the next token. */
13264 token
= cp_lexer_peek_token (parser
->lexer
);
13265 /* If it is `class' or `template', we have a type-parameter. */
13266 if (token
->keyword
== RID_TEMPLATE
)
13267 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13268 /* If it is `class' or `typename' we do not know yet whether it is a
13269 type parameter or a non-type parameter. Consider:
13271 template <typename T, typename T::X X> ...
13275 template <class C, class D*> ...
13277 Here, the first parameter is a type parameter, and the second is
13278 a non-type parameter. We can tell by looking at the token after
13279 the identifier -- if it is a `,', `=', or `>' then we have a type
13281 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
13283 /* Peek at the token after `class' or `typename'. */
13284 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13285 /* If it's an ellipsis, we have a template type parameter
13287 if (token
->type
== CPP_ELLIPSIS
)
13288 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13289 /* If it's an identifier, skip it. */
13290 if (token
->type
== CPP_NAME
)
13291 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
13292 /* Now, see if the token looks like the end of a template
13294 if (token
->type
== CPP_COMMA
13295 || token
->type
== CPP_EQ
13296 || token
->type
== CPP_GREATER
)
13297 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13300 /* Otherwise, it is a non-type parameter.
13304 When parsing a default template-argument for a non-type
13305 template-parameter, the first non-nested `>' is taken as the end
13306 of the template parameter-list rather than a greater-than
13308 *is_non_type
= true;
13309 parameter_declarator
13310 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
13311 /*parenthesized_p=*/NULL
);
13313 if (!parameter_declarator
)
13314 return error_mark_node
;
13316 /* If the parameter declaration is marked as a parameter pack, set
13317 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13318 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13320 if (parameter_declarator
->declarator
13321 && parameter_declarator
->declarator
->parameter_pack_p
)
13323 *is_parameter_pack
= true;
13324 parameter_declarator
->declarator
->parameter_pack_p
= false;
13327 if (parameter_declarator
->default_argument
)
13329 /* Can happen in some cases of erroneous input (c++/34892). */
13330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13331 /* Consume the `...' for better error recovery. */
13332 cp_lexer_consume_token (parser
->lexer
);
13334 /* If the next token is an ellipsis, and we don't already have it
13335 marked as a parameter pack, then we have a parameter pack (that
13336 has no declarator). */
13337 else if (!*is_parameter_pack
13338 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13339 && (declarator_can_be_parameter_pack
13340 (parameter_declarator
->declarator
)))
13342 /* Consume the `...'. */
13343 cp_lexer_consume_token (parser
->lexer
);
13344 maybe_warn_variadic_templates ();
13346 *is_parameter_pack
= true;
13348 /* We might end up with a pack expansion as the type of the non-type
13349 template parameter, in which case this is a non-type template
13351 else if (parameter_declarator
->decl_specifiers
.type
13352 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
13354 *is_parameter_pack
= true;
13355 parameter_declarator
->decl_specifiers
.type
=
13356 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
13359 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13361 /* Parameter packs cannot have default arguments. However, a
13362 user may try to do so, so we'll parse them and give an
13363 appropriate diagnostic here. */
13365 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13367 /* Find the name of the parameter pack. */
13368 id_declarator
= parameter_declarator
->declarator
;
13369 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
13370 id_declarator
= id_declarator
->declarator
;
13372 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
13373 error_at (start_token
->location
,
13374 "template parameter pack %qD cannot have a default argument",
13375 id_declarator
->u
.id
.unqualified_name
);
13377 error_at (start_token
->location
,
13378 "template parameter pack cannot have a default argument");
13380 /* Parse the default argument, but throw away the result. */
13381 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
13384 parm
= grokdeclarator (parameter_declarator
->declarator
,
13385 ¶meter_declarator
->decl_specifiers
,
13386 TPARM
, /*initialized=*/0,
13387 /*attrlist=*/NULL
);
13388 if (parm
== error_mark_node
)
13389 return error_mark_node
;
13391 return build_tree_list (parameter_declarator
->default_argument
, parm
);
13394 /* Parse a type-parameter.
13397 class identifier [opt]
13398 class identifier [opt] = type-id
13399 typename identifier [opt]
13400 typename identifier [opt] = type-id
13401 template < template-parameter-list > class identifier [opt]
13402 template < template-parameter-list > class identifier [opt]
13405 GNU Extension (variadic templates):
13408 class ... identifier [opt]
13409 typename ... identifier [opt]
13411 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13412 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13413 the declaration of the parameter.
13415 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13418 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13423 /* Look for a keyword to tell us what kind of parameter this is. */
13424 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13426 return error_mark_node
;
13428 switch (token
->keyword
)
13434 tree default_argument
;
13436 /* If the next token is an ellipsis, we have a template
13438 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13440 /* Consume the `...' token. */
13441 cp_lexer_consume_token (parser
->lexer
);
13442 maybe_warn_variadic_templates ();
13444 *is_parameter_pack
= true;
13447 /* If the next token is an identifier, then it names the
13449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13450 identifier
= cp_parser_identifier (parser
);
13452 identifier
= NULL_TREE
;
13454 /* Create the parameter. */
13455 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13457 /* If the next token is an `=', we have a default argument. */
13458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13460 /* Consume the `=' token. */
13461 cp_lexer_consume_token (parser
->lexer
);
13462 /* Parse the default-argument. */
13463 push_deferring_access_checks (dk_no_deferred
);
13464 default_argument
= cp_parser_type_id (parser
);
13466 /* Template parameter packs cannot have default
13468 if (*is_parameter_pack
)
13471 error_at (token
->location
,
13472 "template parameter pack %qD cannot have a "
13473 "default argument", identifier
);
13475 error_at (token
->location
,
13476 "template parameter packs cannot have "
13477 "default arguments");
13478 default_argument
= NULL_TREE
;
13480 pop_deferring_access_checks ();
13483 default_argument
= NULL_TREE
;
13485 /* Create the combined representation of the parameter and the
13486 default argument. */
13487 parameter
= build_tree_list (default_argument
, parameter
);
13494 tree default_argument
;
13496 /* Look for the `<'. */
13497 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13498 /* Parse the template-parameter-list. */
13499 cp_parser_template_parameter_list (parser
);
13500 /* Look for the `>'. */
13501 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13502 /* Look for the `class' or 'typename' keywords. */
13503 cp_parser_type_parameter_key (parser
);
13504 /* If the next token is an ellipsis, we have a template
13506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13508 /* Consume the `...' token. */
13509 cp_lexer_consume_token (parser
->lexer
);
13510 maybe_warn_variadic_templates ();
13512 *is_parameter_pack
= true;
13514 /* If the next token is an `=', then there is a
13515 default-argument. If the next token is a `>', we are at
13516 the end of the parameter-list. If the next token is a `,',
13517 then we are at the end of this parameter. */
13518 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13519 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13520 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13522 identifier
= cp_parser_identifier (parser
);
13523 /* Treat invalid names as if the parameter were nameless. */
13524 if (identifier
== error_mark_node
)
13525 identifier
= NULL_TREE
;
13528 identifier
= NULL_TREE
;
13530 /* Create the template parameter. */
13531 parameter
= finish_template_template_parm (class_type_node
,
13534 /* If the next token is an `=', then there is a
13535 default-argument. */
13536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13540 /* Consume the `='. */
13541 cp_lexer_consume_token (parser
->lexer
);
13542 /* Parse the id-expression. */
13543 push_deferring_access_checks (dk_no_deferred
);
13544 /* save token before parsing the id-expression, for error
13546 token
= cp_lexer_peek_token (parser
->lexer
);
13548 = cp_parser_id_expression (parser
,
13549 /*template_keyword_p=*/false,
13550 /*check_dependency_p=*/true,
13551 /*template_p=*/&is_template
,
13552 /*declarator_p=*/false,
13553 /*optional_p=*/false);
13554 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13555 /* If the id-expression was a template-id that refers to
13556 a template-class, we already have the declaration here,
13557 so no further lookup is needed. */
13560 /* Look up the name. */
13562 = cp_parser_lookup_name (parser
, default_argument
,
13564 /*is_template=*/is_template
,
13565 /*is_namespace=*/false,
13566 /*check_dependency=*/true,
13567 /*ambiguous_decls=*/NULL
,
13569 /* See if the default argument is valid. */
13571 = check_template_template_default_arg (default_argument
);
13573 /* Template parameter packs cannot have default
13575 if (*is_parameter_pack
)
13578 error_at (token
->location
,
13579 "template parameter pack %qD cannot "
13580 "have a default argument",
13583 error_at (token
->location
, "template parameter packs cannot "
13584 "have default arguments");
13585 default_argument
= NULL_TREE
;
13587 pop_deferring_access_checks ();
13590 default_argument
= NULL_TREE
;
13592 /* Create the combined representation of the parameter and the
13593 default argument. */
13594 parameter
= build_tree_list (default_argument
, parameter
);
13599 gcc_unreachable ();
13606 /* Parse a template-id.
13609 template-name < template-argument-list [opt] >
13611 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13612 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13613 returned. Otherwise, if the template-name names a function, or set
13614 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13615 names a class, returns a TYPE_DECL for the specialization.
13617 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13618 uninstantiated templates. */
13621 cp_parser_template_id (cp_parser
*parser
,
13622 bool template_keyword_p
,
13623 bool check_dependency_p
,
13624 enum tag_types tag_type
,
13625 bool is_declaration
)
13631 cp_token_position start_of_id
= 0;
13632 deferred_access_check
*chk
;
13633 vec
<deferred_access_check
, va_gc
> *access_check
;
13634 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13635 bool is_identifier
;
13637 /* If the next token corresponds to a template-id, there is no need
13639 next_token
= cp_lexer_peek_token (parser
->lexer
);
13640 if (next_token
->type
== CPP_TEMPLATE_ID
)
13642 struct tree_check
*check_value
;
13644 /* Get the stored value. */
13645 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13646 /* Perform any access checks that were deferred. */
13647 access_check
= check_value
->checks
;
13650 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13651 perform_or_defer_access_check (chk
->binfo
,
13654 tf_warning_or_error
);
13656 /* Return the stored value. */
13657 return check_value
->value
;
13660 /* Avoid performing name lookup if there is no possibility of
13661 finding a template-id. */
13662 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13663 || (next_token
->type
== CPP_NAME
13664 && !cp_parser_nth_token_starts_template_argument_list_p
13667 cp_parser_error (parser
, "expected template-id");
13668 return error_mark_node
;
13671 /* Remember where the template-id starts. */
13672 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13673 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13675 push_deferring_access_checks (dk_deferred
);
13677 /* Parse the template-name. */
13678 is_identifier
= false;
13679 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13680 check_dependency_p
,
13684 if (templ
== error_mark_node
|| is_identifier
)
13686 pop_deferring_access_checks ();
13690 /* If we find the sequence `[:' after a template-name, it's probably
13691 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13692 parse correctly the argument list. */
13693 next_token
= cp_lexer_peek_token (parser
->lexer
);
13694 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13695 if (next_token
->type
== CPP_OPEN_SQUARE
13696 && next_token
->flags
& DIGRAPH
13697 && next_token_2
->type
== CPP_COLON
13698 && !(next_token_2
->flags
& PREV_WHITE
))
13700 cp_parser_parse_tentatively (parser
);
13701 /* Change `:' into `::'. */
13702 next_token_2
->type
= CPP_SCOPE
;
13703 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13705 cp_lexer_consume_token (parser
->lexer
);
13707 /* Parse the arguments. */
13708 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13709 if (!cp_parser_parse_definitely (parser
))
13711 /* If we couldn't parse an argument list, then we revert our changes
13712 and return simply an error. Maybe this is not a template-id
13714 next_token_2
->type
= CPP_COLON
;
13715 cp_parser_error (parser
, "expected %<<%>");
13716 pop_deferring_access_checks ();
13717 return error_mark_node
;
13719 /* Otherwise, emit an error about the invalid digraph, but continue
13720 parsing because we got our argument list. */
13721 if (permerror (next_token
->location
,
13722 "%<<::%> cannot begin a template-argument list"))
13724 static bool hint
= false;
13725 inform (next_token
->location
,
13726 "%<<:%> is an alternate spelling for %<[%>."
13727 " Insert whitespace between %<<%> and %<::%>");
13728 if (!hint
&& !flag_permissive
)
13730 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13731 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13732 "accept your code)");
13739 /* Look for the `<' that starts the template-argument-list. */
13740 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13742 pop_deferring_access_checks ();
13743 return error_mark_node
;
13745 /* Parse the arguments. */
13746 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13749 /* Build a representation of the specialization. */
13750 if (identifier_p (templ
))
13751 template_id
= build_min_nt_loc (next_token
->location
,
13754 else if (DECL_TYPE_TEMPLATE_P (templ
)
13755 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13757 bool entering_scope
;
13758 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13759 template (rather than some instantiation thereof) only if
13760 is not nested within some other construct. For example, in
13761 "template <typename T> void f(T) { A<T>::", A<T> is just an
13762 instantiation of A. */
13763 entering_scope
= (template_parm_scope_p ()
13764 && cp_lexer_next_token_is (parser
->lexer
,
13767 = finish_template_type (templ
, arguments
, entering_scope
);
13769 else if (variable_template_p (templ
))
13771 template_id
= lookup_template_variable (templ
, arguments
);
13775 /* If it's not a class-template or a template-template, it should be
13776 a function-template. */
13777 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13778 || TREE_CODE (templ
) == OVERLOAD
13779 || BASELINK_P (templ
)));
13781 template_id
= lookup_template_function (templ
, arguments
);
13784 /* If parsing tentatively, replace the sequence of tokens that makes
13785 up the template-id with a CPP_TEMPLATE_ID token. That way,
13786 should we re-parse the token stream, we will not have to repeat
13787 the effort required to do the parse, nor will we issue duplicate
13788 error messages about problems during instantiation of the
13791 /* Don't do this if we had a parse error in a declarator; re-parsing
13792 might succeed if a name changes meaning (60361). */
13793 && !(cp_parser_error_occurred (parser
)
13794 && cp_parser_parsing_tentatively (parser
)
13795 && parser
->in_declarator_p
))
13797 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13799 /* Reset the contents of the START_OF_ID token. */
13800 token
->type
= CPP_TEMPLATE_ID
;
13801 /* Retrieve any deferred checks. Do not pop this access checks yet
13802 so the memory will not be reclaimed during token replacing below. */
13803 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13804 token
->u
.tree_check_value
->value
= template_id
;
13805 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13806 token
->keyword
= RID_MAX
;
13808 /* Purge all subsequent tokens. */
13809 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13811 /* ??? Can we actually assume that, if template_id ==
13812 error_mark_node, we will have issued a diagnostic to the
13813 user, as opposed to simply marking the tentative parse as
13815 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13816 error_at (token
->location
, "parse error in template argument list");
13819 pop_to_parent_deferring_access_checks ();
13820 return template_id
;
13823 /* Parse a template-name.
13828 The standard should actually say:
13832 operator-function-id
13834 A defect report has been filed about this issue.
13836 A conversion-function-id cannot be a template name because they cannot
13837 be part of a template-id. In fact, looking at this code:
13839 a.operator K<int>()
13841 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13842 It is impossible to call a templated conversion-function-id with an
13843 explicit argument list, since the only allowed template parameter is
13844 the type to which it is converting.
13846 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13847 `template' keyword, in a construction like:
13851 In that case `f' is taken to be a template-name, even though there
13852 is no way of knowing for sure.
13854 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13855 name refers to a set of overloaded functions, at least one of which
13856 is a template, or an IDENTIFIER_NODE with the name of the template,
13857 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13858 names are looked up inside uninstantiated templates. */
13861 cp_parser_template_name (cp_parser
* parser
,
13862 bool template_keyword_p
,
13863 bool check_dependency_p
,
13864 bool is_declaration
,
13865 enum tag_types tag_type
,
13866 bool *is_identifier
)
13871 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13873 /* If the next token is `operator', then we have either an
13874 operator-function-id or a conversion-function-id. */
13875 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13877 /* We don't know whether we're looking at an
13878 operator-function-id or a conversion-function-id. */
13879 cp_parser_parse_tentatively (parser
);
13880 /* Try an operator-function-id. */
13881 identifier
= cp_parser_operator_function_id (parser
);
13882 /* If that didn't work, try a conversion-function-id. */
13883 if (!cp_parser_parse_definitely (parser
))
13885 cp_parser_error (parser
, "expected template-name");
13886 return error_mark_node
;
13889 /* Look for the identifier. */
13891 identifier
= cp_parser_identifier (parser
);
13893 /* If we didn't find an identifier, we don't have a template-id. */
13894 if (identifier
== error_mark_node
)
13895 return error_mark_node
;
13897 /* If the name immediately followed the `template' keyword, then it
13898 is a template-name. However, if the next token is not `<', then
13899 we do not treat it as a template-name, since it is not being used
13900 as part of a template-id. This enables us to handle constructs
13903 template <typename T> struct S { S(); };
13904 template <typename T> S<T>::S();
13906 correctly. We would treat `S' as a template -- if it were `S<T>'
13907 -- but we do not if there is no `<'. */
13909 if (processing_template_decl
13910 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13912 /* In a declaration, in a dependent context, we pretend that the
13913 "template" keyword was present in order to improve error
13914 recovery. For example, given:
13916 template <typename T> void f(T::X<int>);
13918 we want to treat "X<int>" as a template-id. */
13920 && !template_keyword_p
13921 && parser
->scope
&& TYPE_P (parser
->scope
)
13922 && check_dependency_p
13923 && dependent_scope_p (parser
->scope
)
13924 /* Do not do this for dtors (or ctors), since they never
13925 need the template keyword before their name. */
13926 && !constructor_name_p (identifier
, parser
->scope
))
13928 cp_token_position start
= 0;
13930 /* Explain what went wrong. */
13931 error_at (token
->location
, "non-template %qD used as template",
13933 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
13934 parser
->scope
, identifier
);
13935 /* If parsing tentatively, find the location of the "<" token. */
13936 if (cp_parser_simulate_error (parser
))
13937 start
= cp_lexer_token_position (parser
->lexer
, true);
13938 /* Parse the template arguments so that we can issue error
13939 messages about them. */
13940 cp_lexer_consume_token (parser
->lexer
);
13941 cp_parser_enclosed_template_argument_list (parser
);
13942 /* Skip tokens until we find a good place from which to
13943 continue parsing. */
13944 cp_parser_skip_to_closing_parenthesis (parser
,
13945 /*recovering=*/true,
13947 /*consume_paren=*/false);
13948 /* If parsing tentatively, permanently remove the
13949 template argument list. That will prevent duplicate
13950 error messages from being issued about the missing
13951 "template" keyword. */
13953 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
13955 *is_identifier
= true;
13959 /* If the "template" keyword is present, then there is generally
13960 no point in doing name-lookup, so we just return IDENTIFIER.
13961 But, if the qualifying scope is non-dependent then we can
13962 (and must) do name-lookup normally. */
13963 if (template_keyword_p
13965 || (TYPE_P (parser
->scope
)
13966 && dependent_type_p (parser
->scope
))))
13970 /* Look up the name. */
13971 decl
= cp_parser_lookup_name (parser
, identifier
,
13973 /*is_template=*/true,
13974 /*is_namespace=*/false,
13975 check_dependency_p
,
13976 /*ambiguous_decls=*/NULL
,
13979 /* If DECL is a template, then the name was a template-name. */
13980 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
13984 tree fn
= NULL_TREE
;
13986 /* The standard does not explicitly indicate whether a name that
13987 names a set of overloaded declarations, some of which are
13988 templates, is a template-name. However, such a name should
13989 be a template-name; otherwise, there is no way to form a
13990 template-id for the overloaded templates. */
13991 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
13992 if (TREE_CODE (fns
) == OVERLOAD
)
13993 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
13994 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
13999 /* The name does not name a template. */
14000 cp_parser_error (parser
, "expected template-name");
14001 return error_mark_node
;
14005 /* If DECL is dependent, and refers to a function, then just return
14006 its name; we will look it up again during template instantiation. */
14007 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
14009 tree scope
= ovl_scope (decl
);
14010 if (TYPE_P (scope
) && dependent_type_p (scope
))
14017 /* Parse a template-argument-list.
14019 template-argument-list:
14020 template-argument ... [opt]
14021 template-argument-list , template-argument ... [opt]
14023 Returns a TREE_VEC containing the arguments. */
14026 cp_parser_template_argument_list (cp_parser
* parser
)
14028 tree fixed_args
[10];
14029 unsigned n_args
= 0;
14030 unsigned alloced
= 10;
14031 tree
*arg_ary
= fixed_args
;
14033 bool saved_in_template_argument_list_p
;
14035 bool saved_non_ice_p
;
14037 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
14038 parser
->in_template_argument_list_p
= true;
14039 /* Even if the template-id appears in an integral
14040 constant-expression, the contents of the argument list do
14042 saved_ice_p
= parser
->integral_constant_expression_p
;
14043 parser
->integral_constant_expression_p
= false;
14044 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
14045 parser
->non_integral_constant_expression_p
= false;
14047 /* Parse the arguments. */
14053 /* Consume the comma. */
14054 cp_lexer_consume_token (parser
->lexer
);
14056 /* Parse the template-argument. */
14057 argument
= cp_parser_template_argument (parser
);
14059 /* If the next token is an ellipsis, we're expanding a template
14061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14063 if (argument
== error_mark_node
)
14065 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14066 error_at (token
->location
,
14067 "expected parameter pack before %<...%>");
14069 /* Consume the `...' token. */
14070 cp_lexer_consume_token (parser
->lexer
);
14072 /* Make the argument into a TYPE_PACK_EXPANSION or
14073 EXPR_PACK_EXPANSION. */
14074 argument
= make_pack_expansion (argument
);
14077 if (n_args
== alloced
)
14081 if (arg_ary
== fixed_args
)
14083 arg_ary
= XNEWVEC (tree
, alloced
);
14084 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
14087 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
14089 arg_ary
[n_args
++] = argument
;
14091 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14093 vec
= make_tree_vec (n_args
);
14096 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
14098 if (arg_ary
!= fixed_args
)
14100 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
14101 parser
->integral_constant_expression_p
= saved_ice_p
;
14102 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
14103 #ifdef ENABLE_CHECKING
14104 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
14109 /* Parse a template-argument.
14112 assignment-expression
14116 The representation is that of an assignment-expression, type-id, or
14117 id-expression -- except that the qualified id-expression is
14118 evaluated, so that the value returned is either a DECL or an
14121 Although the standard says "assignment-expression", it forbids
14122 throw-expressions or assignments in the template argument.
14123 Therefore, we use "conditional-expression" instead. */
14126 cp_parser_template_argument (cp_parser
* parser
)
14131 bool maybe_type_id
= false;
14132 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
14133 location_t loc
= 0;
14136 /* There's really no way to know what we're looking at, so we just
14137 try each alternative in order.
14141 In a template-argument, an ambiguity between a type-id and an
14142 expression is resolved to a type-id, regardless of the form of
14143 the corresponding template-parameter.
14145 Therefore, we try a type-id first. */
14146 cp_parser_parse_tentatively (parser
);
14147 argument
= cp_parser_template_type_arg (parser
);
14148 /* If there was no error parsing the type-id but the next token is a
14149 '>>', our behavior depends on which dialect of C++ we're
14150 parsing. In C++98, we probably found a typo for '> >'. But there
14151 are type-id which are also valid expressions. For instance:
14153 struct X { int operator >> (int); };
14154 template <int V> struct Foo {};
14157 Here 'X()' is a valid type-id of a function type, but the user just
14158 wanted to write the expression "X() >> 5". Thus, we remember that we
14159 found a valid type-id, but we still try to parse the argument as an
14160 expression to see what happens.
14162 In C++0x, the '>>' will be considered two separate '>'
14164 if (!cp_parser_error_occurred (parser
)
14165 && cxx_dialect
== cxx98
14166 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14168 maybe_type_id
= true;
14169 cp_parser_abort_tentative_parse (parser
);
14173 /* If the next token isn't a `,' or a `>', then this argument wasn't
14174 really finished. This means that the argument is not a valid
14176 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14177 cp_parser_error (parser
, "expected template-argument");
14178 /* If that worked, we're done. */
14179 if (cp_parser_parse_definitely (parser
))
14182 /* We're still not sure what the argument will be. */
14183 cp_parser_parse_tentatively (parser
);
14184 /* Try a template. */
14185 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
14186 argument
= cp_parser_id_expression (parser
,
14187 /*template_keyword_p=*/false,
14188 /*check_dependency_p=*/true,
14190 /*declarator_p=*/false,
14191 /*optional_p=*/false);
14192 /* If the next token isn't a `,' or a `>', then this argument wasn't
14193 really finished. */
14194 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14195 cp_parser_error (parser
, "expected template-argument");
14196 if (!cp_parser_error_occurred (parser
))
14198 /* Figure out what is being referred to. If the id-expression
14199 was for a class template specialization, then we will have a
14200 TYPE_DECL at this point. There is no need to do name lookup
14201 at this point in that case. */
14202 if (TREE_CODE (argument
) != TYPE_DECL
)
14203 argument
= cp_parser_lookup_name (parser
, argument
,
14205 /*is_template=*/template_p
,
14206 /*is_namespace=*/false,
14207 /*check_dependency=*/true,
14208 /*ambiguous_decls=*/NULL
,
14209 argument_start_token
->location
);
14210 if (TREE_CODE (argument
) != TEMPLATE_DECL
14211 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
14212 cp_parser_error (parser
, "expected template-name");
14214 if (cp_parser_parse_definitely (parser
))
14216 /* It must be a non-type argument. There permitted cases are given
14217 in [temp.arg.nontype]:
14219 -- an integral constant-expression of integral or enumeration
14222 -- the name of a non-type template-parameter; or
14224 -- the name of an object or function with external linkage...
14226 -- the address of an object or function with external linkage...
14228 -- a pointer to member... */
14229 /* Look for a non-type template parameter. */
14230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14232 cp_parser_parse_tentatively (parser
);
14233 argument
= cp_parser_primary_expression (parser
,
14234 /*address_p=*/false,
14236 /*template_arg_p=*/true,
14238 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
14239 || !cp_parser_next_token_ends_template_argument_p (parser
))
14240 cp_parser_simulate_error (parser
);
14241 if (cp_parser_parse_definitely (parser
))
14245 /* If the next token is "&", the argument must be the address of an
14246 object or function with external linkage. */
14247 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
14250 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14251 cp_lexer_consume_token (parser
->lexer
);
14253 /* See if we might have an id-expression. */
14254 token
= cp_lexer_peek_token (parser
->lexer
);
14255 if (token
->type
== CPP_NAME
14256 || token
->keyword
== RID_OPERATOR
14257 || token
->type
== CPP_SCOPE
14258 || token
->type
== CPP_TEMPLATE_ID
14259 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
14261 cp_parser_parse_tentatively (parser
);
14262 argument
= cp_parser_primary_expression (parser
,
14265 /*template_arg_p=*/true,
14267 if (cp_parser_error_occurred (parser
)
14268 || !cp_parser_next_token_ends_template_argument_p (parser
))
14269 cp_parser_abort_tentative_parse (parser
);
14274 if (INDIRECT_REF_P (argument
))
14276 /* Strip the dereference temporarily. */
14277 gcc_assert (REFERENCE_REF_P (argument
));
14278 argument
= TREE_OPERAND (argument
, 0);
14281 /* If we're in a template, we represent a qualified-id referring
14282 to a static data member as a SCOPE_REF even if the scope isn't
14283 dependent so that we can check access control later. */
14285 if (TREE_CODE (probe
) == SCOPE_REF
)
14286 probe
= TREE_OPERAND (probe
, 1);
14289 /* A variable without external linkage might still be a
14290 valid constant-expression, so no error is issued here
14291 if the external-linkage check fails. */
14292 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
14293 cp_parser_simulate_error (parser
);
14295 else if (is_overloaded_fn (argument
))
14296 /* All overloaded functions are allowed; if the external
14297 linkage test does not pass, an error will be issued
14301 && (TREE_CODE (argument
) == OFFSET_REF
14302 || TREE_CODE (argument
) == SCOPE_REF
))
14303 /* A pointer-to-member. */
14305 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
14308 cp_parser_simulate_error (parser
);
14310 if (cp_parser_parse_definitely (parser
))
14313 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
14314 tf_warning_or_error
);
14316 argument
= convert_from_reference (argument
);
14321 /* If the argument started with "&", there are no other valid
14322 alternatives at this point. */
14325 cp_parser_error (parser
, "invalid non-type template argument");
14326 return error_mark_node
;
14329 /* If the argument wasn't successfully parsed as a type-id followed
14330 by '>>', the argument can only be a constant expression now.
14331 Otherwise, we try parsing the constant-expression tentatively,
14332 because the argument could really be a type-id. */
14334 cp_parser_parse_tentatively (parser
);
14335 argument
= cp_parser_constant_expression (parser
);
14337 if (!maybe_type_id
)
14339 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14340 cp_parser_error (parser
, "expected template-argument");
14341 if (cp_parser_parse_definitely (parser
))
14343 /* We did our best to parse the argument as a non type-id, but that
14344 was the only alternative that matched (albeit with a '>' after
14345 it). We can assume it's just a typo from the user, and a
14346 diagnostic will then be issued. */
14347 return cp_parser_template_type_arg (parser
);
14350 /* Parse an explicit-instantiation.
14352 explicit-instantiation:
14353 template declaration
14355 Although the standard says `declaration', what it really means is:
14357 explicit-instantiation:
14358 template decl-specifier-seq [opt] declarator [opt] ;
14360 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14361 supposed to be allowed. A defect report has been filed about this
14366 explicit-instantiation:
14367 storage-class-specifier template
14368 decl-specifier-seq [opt] declarator [opt] ;
14369 function-specifier template
14370 decl-specifier-seq [opt] declarator [opt] ; */
14373 cp_parser_explicit_instantiation (cp_parser
* parser
)
14375 int declares_class_or_enum
;
14376 cp_decl_specifier_seq decl_specifiers
;
14377 tree extension_specifier
= NULL_TREE
;
14379 timevar_push (TV_TEMPLATE_INST
);
14381 /* Look for an (optional) storage-class-specifier or
14382 function-specifier. */
14383 if (cp_parser_allow_gnu_extensions_p (parser
))
14385 extension_specifier
14386 = cp_parser_storage_class_specifier_opt (parser
);
14387 if (!extension_specifier
)
14388 extension_specifier
14389 = cp_parser_function_specifier_opt (parser
,
14390 /*decl_specs=*/NULL
);
14393 /* Look for the `template' keyword. */
14394 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14395 /* Let the front end know that we are processing an explicit
14397 begin_explicit_instantiation ();
14398 /* [temp.explicit] says that we are supposed to ignore access
14399 control while processing explicit instantiation directives. */
14400 push_deferring_access_checks (dk_no_check
);
14401 /* Parse a decl-specifier-seq. */
14402 cp_parser_decl_specifier_seq (parser
,
14403 CP_PARSER_FLAGS_OPTIONAL
,
14405 &declares_class_or_enum
);
14406 /* If there was exactly one decl-specifier, and it declared a class,
14407 and there's no declarator, then we have an explicit type
14409 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
14413 type
= check_tag_decl (&decl_specifiers
,
14414 /*explicit_type_instantiation_p=*/true);
14415 /* Turn access control back on for names used during
14416 template instantiation. */
14417 pop_deferring_access_checks ();
14419 do_type_instantiation (type
, extension_specifier
,
14420 /*complain=*/tf_error
);
14424 cp_declarator
*declarator
;
14427 /* Parse the declarator. */
14429 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14430 /*ctor_dtor_or_conv_p=*/NULL
,
14431 /*parenthesized_p=*/NULL
,
14432 /*member_p=*/false,
14433 /*friend_p=*/false);
14434 if (declares_class_or_enum
& 2)
14435 cp_parser_check_for_definition_in_return_type (declarator
,
14436 decl_specifiers
.type
,
14437 decl_specifiers
.locations
[ds_type_spec
]);
14438 if (declarator
!= cp_error_declarator
)
14440 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14441 permerror (decl_specifiers
.locations
[ds_inline
],
14442 "explicit instantiation shall not use"
14443 " %<inline%> specifier");
14444 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14445 permerror (decl_specifiers
.locations
[ds_constexpr
],
14446 "explicit instantiation shall not use"
14447 " %<constexpr%> specifier");
14449 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14450 NORMAL
, 0, &decl_specifiers
.attributes
);
14451 /* Turn access control back on for names used during
14452 template instantiation. */
14453 pop_deferring_access_checks ();
14454 /* Do the explicit instantiation. */
14455 do_decl_instantiation (decl
, extension_specifier
);
14459 pop_deferring_access_checks ();
14460 /* Skip the body of the explicit instantiation. */
14461 cp_parser_skip_to_end_of_statement (parser
);
14464 /* We're done with the instantiation. */
14465 end_explicit_instantiation ();
14467 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14469 timevar_pop (TV_TEMPLATE_INST
);
14472 /* Parse an explicit-specialization.
14474 explicit-specialization:
14475 template < > declaration
14477 Although the standard says `declaration', what it really means is:
14479 explicit-specialization:
14480 template <> decl-specifier [opt] init-declarator [opt] ;
14481 template <> function-definition
14482 template <> explicit-specialization
14483 template <> template-declaration */
14486 cp_parser_explicit_specialization (cp_parser
* parser
)
14488 bool need_lang_pop
;
14489 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14491 /* Look for the `template' keyword. */
14492 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14493 /* Look for the `<'. */
14494 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14495 /* Look for the `>'. */
14496 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14497 /* We have processed another parameter list. */
14498 ++parser
->num_template_parameter_lists
;
14501 A template ... explicit specialization ... shall not have C
14503 if (current_lang_name
== lang_name_c
)
14505 error_at (token
->location
, "template specialization with C linkage");
14506 /* Give it C++ linkage to avoid confusing other parts of the
14508 push_lang_context (lang_name_cplusplus
);
14509 need_lang_pop
= true;
14512 need_lang_pop
= false;
14513 /* Let the front end know that we are beginning a specialization. */
14514 if (!begin_specialization ())
14516 end_specialization ();
14520 /* If the next keyword is `template', we need to figure out whether
14521 or not we're looking a template-declaration. */
14522 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14524 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14525 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14526 cp_parser_template_declaration_after_export (parser
,
14527 /*member_p=*/false);
14529 cp_parser_explicit_specialization (parser
);
14532 /* Parse the dependent declaration. */
14533 cp_parser_single_declaration (parser
,
14535 /*member_p=*/false,
14536 /*explicit_specialization_p=*/true,
14537 /*friend_p=*/NULL
);
14538 /* We're done with the specialization. */
14539 end_specialization ();
14540 /* For the erroneous case of a template with C linkage, we pushed an
14541 implicit C++ linkage scope; exit that scope now. */
14543 pop_lang_context ();
14544 /* We're done with this parameter list. */
14545 --parser
->num_template_parameter_lists
;
14548 /* Parse a type-specifier.
14551 simple-type-specifier
14554 elaborated-type-specifier
14562 Returns a representation of the type-specifier. For a
14563 class-specifier, enum-specifier, or elaborated-type-specifier, a
14564 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14566 The parser flags FLAGS is used to control type-specifier parsing.
14568 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14569 in a decl-specifier-seq.
14571 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14572 class-specifier, enum-specifier, or elaborated-type-specifier, then
14573 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14574 if a type is declared; 2 if it is defined. Otherwise, it is set to
14577 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14578 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14579 is set to FALSE. */
14582 cp_parser_type_specifier (cp_parser
* parser
,
14583 cp_parser_flags flags
,
14584 cp_decl_specifier_seq
*decl_specs
,
14585 bool is_declaration
,
14586 int* declares_class_or_enum
,
14587 bool* is_cv_qualifier
)
14589 tree type_spec
= NULL_TREE
;
14592 cp_decl_spec ds
= ds_last
;
14594 /* Assume this type-specifier does not declare a new type. */
14595 if (declares_class_or_enum
)
14596 *declares_class_or_enum
= 0;
14597 /* And that it does not specify a cv-qualifier. */
14598 if (is_cv_qualifier
)
14599 *is_cv_qualifier
= false;
14600 /* Peek at the next token. */
14601 token
= cp_lexer_peek_token (parser
->lexer
);
14603 /* If we're looking at a keyword, we can use that to guide the
14604 production we choose. */
14605 keyword
= token
->keyword
;
14609 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14610 goto elaborated_type_specifier
;
14612 /* Look for the enum-specifier. */
14613 type_spec
= cp_parser_enum_specifier (parser
);
14614 /* If that worked, we're done. */
14617 if (declares_class_or_enum
)
14618 *declares_class_or_enum
= 2;
14620 cp_parser_set_decl_spec_type (decl_specs
,
14623 /*type_definition_p=*/true);
14627 goto elaborated_type_specifier
;
14629 /* Any of these indicate either a class-specifier, or an
14630 elaborated-type-specifier. */
14634 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14635 goto elaborated_type_specifier
;
14637 /* Parse tentatively so that we can back up if we don't find a
14638 class-specifier. */
14639 cp_parser_parse_tentatively (parser
);
14640 /* Look for the class-specifier. */
14641 type_spec
= cp_parser_class_specifier (parser
);
14642 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14643 /* If that worked, we're done. */
14644 if (cp_parser_parse_definitely (parser
))
14646 if (declares_class_or_enum
)
14647 *declares_class_or_enum
= 2;
14649 cp_parser_set_decl_spec_type (decl_specs
,
14652 /*type_definition_p=*/true);
14656 /* Fall through. */
14657 elaborated_type_specifier
:
14658 /* We're declaring (not defining) a class or enum. */
14659 if (declares_class_or_enum
)
14660 *declares_class_or_enum
= 1;
14662 /* Fall through. */
14664 /* Look for an elaborated-type-specifier. */
14666 = (cp_parser_elaborated_type_specifier
14668 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14671 cp_parser_set_decl_spec_type (decl_specs
,
14674 /*type_definition_p=*/false);
14679 if (is_cv_qualifier
)
14680 *is_cv_qualifier
= true;
14685 if (is_cv_qualifier
)
14686 *is_cv_qualifier
= true;
14691 if (is_cv_qualifier
)
14692 *is_cv_qualifier
= true;
14696 /* The `__complex__' keyword is a GNU extension. */
14704 /* Handle simple keywords. */
14709 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14710 decl_specs
->any_specifiers_p
= true;
14712 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14715 /* If we do not already have a type-specifier, assume we are looking
14716 at a simple-type-specifier. */
14717 type_spec
= cp_parser_simple_type_specifier (parser
,
14721 /* If we didn't find a type-specifier, and a type-specifier was not
14722 optional in this context, issue an error message. */
14723 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14725 cp_parser_error (parser
, "expected type specifier");
14726 return error_mark_node
;
14732 /* Parse a simple-type-specifier.
14734 simple-type-specifier:
14735 :: [opt] nested-name-specifier [opt] type-name
14736 :: [opt] nested-name-specifier template template-id
14751 simple-type-specifier:
14753 decltype ( expression )
14756 __underlying_type ( type-id )
14760 simple-type-specifier:
14762 __typeof__ unary-expression
14763 __typeof__ ( type-id )
14764 __typeof__ ( type-id ) { initializer-list , [opt] }
14766 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14767 appropriately updated. */
14770 cp_parser_simple_type_specifier (cp_parser
* parser
,
14771 cp_decl_specifier_seq
*decl_specs
,
14772 cp_parser_flags flags
)
14774 tree type
= NULL_TREE
;
14778 /* Peek at the next token. */
14779 token
= cp_lexer_peek_token (parser
->lexer
);
14781 /* If we're looking at a keyword, things are easy. */
14782 switch (token
->keyword
)
14786 decl_specs
->explicit_char_p
= true;
14787 type
= char_type_node
;
14790 type
= char16_type_node
;
14793 type
= char32_type_node
;
14796 type
= wchar_type_node
;
14799 type
= boolean_type_node
;
14802 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14803 type
= short_integer_type_node
;
14807 decl_specs
->explicit_int_p
= true;
14808 type
= integer_type_node
;
14814 idx
= token
->keyword
- RID_INT_N_0
;
14815 if (! int_n_enabled_p
[idx
])
14819 decl_specs
->explicit_intN_p
= true;
14820 decl_specs
->int_n_idx
= idx
;
14822 type
= int_n_trees
[idx
].signed_type
;
14826 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14827 type
= long_integer_type_node
;
14830 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14831 type
= integer_type_node
;
14834 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14835 type
= unsigned_type_node
;
14838 type
= float_type_node
;
14841 type
= double_type_node
;
14844 type
= void_type_node
;
14848 maybe_warn_cpp0x (CPP0X_AUTO
);
14849 if (parser
->auto_is_implicit_function_template_parm_p
)
14851 type
= synthesize_implicit_template_parm (parser
);
14853 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14855 if (cxx_dialect
< cxx14
)
14856 pedwarn (location_of (type
), 0,
14857 "use of %<auto%> in lambda parameter declaration "
14858 "only available with "
14859 "-std=c++14 or -std=gnu++14");
14861 else if (cxx_dialect
< cxx14
)
14862 pedwarn (location_of (type
), 0,
14863 "use of %<auto%> in parameter declaration "
14864 "only available with "
14865 "-std=c++14 or -std=gnu++14");
14867 pedwarn (location_of (type
), OPT_Wpedantic
,
14868 "ISO C++ forbids use of %<auto%> in parameter "
14872 type
= make_auto ();
14876 /* Since DR 743, decltype can either be a simple-type-specifier by
14877 itself or begin a nested-name-specifier. Parsing it will replace
14878 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14879 handling below decide what to do. */
14880 cp_parser_decltype (parser
);
14881 cp_lexer_set_token_position (parser
->lexer
, token
);
14885 /* Consume the `typeof' token. */
14886 cp_lexer_consume_token (parser
->lexer
);
14887 /* Parse the operand to `typeof'. */
14888 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14889 /* If it is not already a TYPE, take its type. */
14890 if (!TYPE_P (type
))
14891 type
= finish_typeof (type
);
14894 cp_parser_set_decl_spec_type (decl_specs
, type
,
14896 /*type_definition_p=*/false);
14900 case RID_UNDERLYING_TYPE
:
14901 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14903 cp_parser_set_decl_spec_type (decl_specs
, type
,
14905 /*type_definition_p=*/false);
14910 case RID_DIRECT_BASES
:
14911 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14913 cp_parser_set_decl_spec_type (decl_specs
, type
,
14915 /*type_definition_p=*/false);
14921 /* If token is an already-parsed decltype not followed by ::,
14922 it's a simple-type-specifier. */
14923 if (token
->type
== CPP_DECLTYPE
14924 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
14926 type
= token
->u
.value
;
14928 cp_parser_set_decl_spec_type (decl_specs
, type
,
14930 /*type_definition_p=*/false);
14931 cp_lexer_consume_token (parser
->lexer
);
14935 /* If the type-specifier was for a built-in type, we're done. */
14938 /* Record the type. */
14940 && (token
->keyword
!= RID_SIGNED
14941 && token
->keyword
!= RID_UNSIGNED
14942 && token
->keyword
!= RID_SHORT
14943 && token
->keyword
!= RID_LONG
))
14944 cp_parser_set_decl_spec_type (decl_specs
,
14947 /*type_definition_p=*/false);
14949 decl_specs
->any_specifiers_p
= true;
14951 /* Consume the token. */
14952 cp_lexer_consume_token (parser
->lexer
);
14954 /* There is no valid C++ program where a non-template type is
14955 followed by a "<". That usually indicates that the user thought
14956 that the type was a template. */
14957 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
14960 return TYPE_NAME (type
);
14963 /* The type-specifier must be a user-defined type. */
14964 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
14969 /* Don't gobble tokens or issue error messages if this is an
14970 optional type-specifier. */
14971 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
14972 cp_parser_parse_tentatively (parser
);
14974 /* Look for the optional `::' operator. */
14976 = (cp_parser_global_scope_opt (parser
,
14977 /*current_scope_valid_p=*/false)
14979 /* Look for the nested-name specifier. */
14981 = (cp_parser_nested_name_specifier_opt (parser
,
14982 /*typename_keyword_p=*/false,
14983 /*check_dependency_p=*/true,
14985 /*is_declaration=*/false)
14987 token
= cp_lexer_peek_token (parser
->lexer
);
14988 /* If we have seen a nested-name-specifier, and the next token
14989 is `template', then we are using the template-id production. */
14991 && cp_parser_optional_template_keyword (parser
))
14993 /* Look for the template-id. */
14994 type
= cp_parser_template_id (parser
,
14995 /*template_keyword_p=*/true,
14996 /*check_dependency_p=*/true,
14998 /*is_declaration=*/false);
14999 /* If the template-id did not name a type, we are out of
15001 if (TREE_CODE (type
) != TYPE_DECL
)
15003 cp_parser_error (parser
, "expected template-id for type");
15007 /* Otherwise, look for a type-name. */
15009 type
= cp_parser_type_name (parser
);
15010 /* Keep track of all name-lookups performed in class scopes. */
15014 && TREE_CODE (type
) == TYPE_DECL
15015 && identifier_p (DECL_NAME (type
)))
15016 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
15017 /* If it didn't work out, we don't have a TYPE. */
15018 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
15019 && !cp_parser_parse_definitely (parser
))
15021 if (type
&& decl_specs
)
15022 cp_parser_set_decl_spec_type (decl_specs
, type
,
15024 /*type_definition_p=*/false);
15027 /* If we didn't get a type-name, issue an error message. */
15028 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15030 cp_parser_error (parser
, "expected type-name");
15031 return error_mark_node
;
15034 if (type
&& type
!= error_mark_node
)
15036 /* See if TYPE is an Objective-C type, and if so, parse and
15037 accept any protocol references following it. Do this before
15038 the cp_parser_check_for_invalid_template_id() call, because
15039 Objective-C types can be followed by '<...>' which would
15040 enclose protocol names rather than template arguments, and so
15041 everything is fine. */
15042 if (c_dialect_objc () && !parser
->scope
15043 && (objc_is_id (type
) || objc_is_class_name (type
)))
15045 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15046 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
15048 /* Clobber the "unqualified" type previously entered into
15049 DECL_SPECS with the new, improved protocol-qualified version. */
15051 decl_specs
->type
= qual_type
;
15056 /* There is no valid C++ program where a non-template type is
15057 followed by a "<". That usually indicates that the user
15058 thought that the type was a template. */
15059 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
15067 /* Parse a type-name.
15073 simple-template-id [in c++0x]
15081 Returns a TYPE_DECL for the type. */
15084 cp_parser_type_name (cp_parser
* parser
)
15088 /* We can't know yet whether it is a class-name or not. */
15089 cp_parser_parse_tentatively (parser
);
15090 /* Try a class-name. */
15091 type_decl
= cp_parser_class_name (parser
,
15092 /*typename_keyword_p=*/false,
15093 /*template_keyword_p=*/false,
15095 /*check_dependency_p=*/true,
15096 /*class_head_p=*/false,
15097 /*is_declaration=*/false);
15098 /* If it's not a class-name, keep looking. */
15099 if (!cp_parser_parse_definitely (parser
))
15101 if (cxx_dialect
< cxx11
)
15102 /* It must be a typedef-name or an enum-name. */
15103 return cp_parser_nonclass_name (parser
);
15105 cp_parser_parse_tentatively (parser
);
15106 /* It is either a simple-template-id representing an
15107 instantiation of an alias template... */
15108 type_decl
= cp_parser_template_id (parser
,
15109 /*template_keyword_p=*/false,
15110 /*check_dependency_p=*/true,
15112 /*is_declaration=*/false);
15113 /* Note that this must be an instantiation of an alias template
15114 because [temp.names]/6 says:
15116 A template-id that names an alias template specialization
15119 Whereas [temp.names]/7 says:
15121 A simple-template-id that names a class template
15122 specialization is a class-name. */
15123 if (type_decl
!= NULL_TREE
15124 && TREE_CODE (type_decl
) == TYPE_DECL
15125 && TYPE_DECL_ALIAS_P (type_decl
))
15126 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
15128 cp_parser_simulate_error (parser
);
15130 if (!cp_parser_parse_definitely (parser
))
15131 /* ... Or a typedef-name or an enum-name. */
15132 return cp_parser_nonclass_name (parser
);
15138 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15146 Returns a TYPE_DECL for the type. */
15149 cp_parser_nonclass_name (cp_parser
* parser
)
15154 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15155 identifier
= cp_parser_identifier (parser
);
15156 if (identifier
== error_mark_node
)
15157 return error_mark_node
;
15159 /* Look up the type-name. */
15160 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
15162 type_decl
= strip_using_decl (type_decl
);
15164 if (TREE_CODE (type_decl
) != TYPE_DECL
15165 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
15167 /* See if this is an Objective-C type. */
15168 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15169 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
15171 type_decl
= TYPE_NAME (type
);
15174 /* Issue an error if we did not find a type-name. */
15175 if (TREE_CODE (type_decl
) != TYPE_DECL
15176 /* In Objective-C, we have the complication that class names are
15177 normally type names and start declarations (eg, the
15178 "NSObject" in "NSObject *object;"), but can be used in an
15179 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15180 is an expression. So, a classname followed by a dot is not a
15181 valid type-name. */
15182 || (objc_is_class_name (TREE_TYPE (type_decl
))
15183 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
15185 if (!cp_parser_simulate_error (parser
))
15186 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
15187 NLE_TYPE
, token
->location
);
15188 return error_mark_node
;
15190 /* Remember that the name was used in the definition of the
15191 current class so that we can check later to see if the
15192 meaning would have been different after the class was
15193 entirely defined. */
15194 else if (type_decl
!= error_mark_node
15196 maybe_note_name_used_in_class (identifier
, type_decl
);
15201 /* Parse an elaborated-type-specifier. Note that the grammar given
15202 here incorporates the resolution to DR68.
15204 elaborated-type-specifier:
15205 class-key :: [opt] nested-name-specifier [opt] identifier
15206 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15207 enum-key :: [opt] nested-name-specifier [opt] identifier
15208 typename :: [opt] nested-name-specifier identifier
15209 typename :: [opt] nested-name-specifier template [opt]
15214 elaborated-type-specifier:
15215 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15216 class-key attributes :: [opt] nested-name-specifier [opt]
15217 template [opt] template-id
15218 enum attributes :: [opt] nested-name-specifier [opt] identifier
15220 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15221 declared `friend'. If IS_DECLARATION is TRUE, then this
15222 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15223 something is being declared.
15225 Returns the TYPE specified. */
15228 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
15230 bool is_declaration
)
15232 enum tag_types tag_type
;
15234 tree type
= NULL_TREE
;
15235 tree attributes
= NULL_TREE
;
15237 cp_token
*token
= NULL
;
15239 /* See if we're looking at the `enum' keyword. */
15240 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
15242 /* Consume the `enum' token. */
15243 cp_lexer_consume_token (parser
->lexer
);
15244 /* Remember that it's an enumeration type. */
15245 tag_type
= enum_type
;
15246 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15247 enums) is used here. */
15248 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15249 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15251 pedwarn (input_location
, 0, "elaborated-type-specifier "
15252 "for a scoped enum must not use the %<%D%> keyword",
15253 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
15254 /* Consume the `struct' or `class' and parse it anyway. */
15255 cp_lexer_consume_token (parser
->lexer
);
15257 /* Parse the attributes. */
15258 attributes
= cp_parser_attributes_opt (parser
);
15260 /* Or, it might be `typename'. */
15261 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15264 /* Consume the `typename' token. */
15265 cp_lexer_consume_token (parser
->lexer
);
15266 /* Remember that it's a `typename' type. */
15267 tag_type
= typename_type
;
15269 /* Otherwise it must be a class-key. */
15272 tag_type
= cp_parser_class_key (parser
);
15273 if (tag_type
== none_type
)
15274 return error_mark_node
;
15275 /* Parse the attributes. */
15276 attributes
= cp_parser_attributes_opt (parser
);
15279 /* Look for the `::' operator. */
15280 globalscope
= cp_parser_global_scope_opt (parser
,
15281 /*current_scope_valid_p=*/false);
15282 /* Look for the nested-name-specifier. */
15283 if (tag_type
== typename_type
&& !globalscope
)
15285 if (!cp_parser_nested_name_specifier (parser
,
15286 /*typename_keyword_p=*/true,
15287 /*check_dependency_p=*/true,
15290 return error_mark_node
;
15293 /* Even though `typename' is not present, the proposed resolution
15294 to Core Issue 180 says that in `class A<T>::B', `B' should be
15295 considered a type-name, even if `A<T>' is dependent. */
15296 cp_parser_nested_name_specifier_opt (parser
,
15297 /*typename_keyword_p=*/true,
15298 /*check_dependency_p=*/true,
15301 /* For everything but enumeration types, consider a template-id.
15302 For an enumeration type, consider only a plain identifier. */
15303 if (tag_type
!= enum_type
)
15305 bool template_p
= false;
15308 /* Allow the `template' keyword. */
15309 template_p
= cp_parser_optional_template_keyword (parser
);
15310 /* If we didn't see `template', we don't know if there's a
15311 template-id or not. */
15313 cp_parser_parse_tentatively (parser
);
15314 /* Parse the template-id. */
15315 token
= cp_lexer_peek_token (parser
->lexer
);
15316 decl
= cp_parser_template_id (parser
, template_p
,
15317 /*check_dependency_p=*/true,
15320 /* If we didn't find a template-id, look for an ordinary
15322 if (!template_p
&& !cp_parser_parse_definitely (parser
))
15324 /* We can get here when cp_parser_template_id, called by
15325 cp_parser_class_name with tag_type == none_type, succeeds
15326 and caches a BASELINK. Then, when called again here,
15327 instead of failing and returning an error_mark_node
15328 returns it (see template/typename17.C in C++11).
15329 ??? Could we diagnose this earlier? */
15330 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
15332 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
15333 type
= error_mark_node
;
15335 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15336 in effect, then we must assume that, upon instantiation, the
15337 template will correspond to a class. */
15338 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15339 && tag_type
== typename_type
)
15340 type
= make_typename_type (parser
->scope
, decl
,
15342 /*complain=*/tf_error
);
15343 /* If the `typename' keyword is in effect and DECL is not a type
15344 decl, then type is non existent. */
15345 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
15347 else if (TREE_CODE (decl
) == TYPE_DECL
)
15348 type
= check_elaborated_type_specifier (tag_type
, decl
,
15349 /*allow_template_p=*/true);
15350 else if (decl
== error_mark_node
)
15351 type
= error_mark_node
;
15356 token
= cp_lexer_peek_token (parser
->lexer
);
15357 identifier
= cp_parser_identifier (parser
);
15359 if (identifier
== error_mark_node
)
15361 parser
->scope
= NULL_TREE
;
15362 return error_mark_node
;
15365 /* For a `typename', we needn't call xref_tag. */
15366 if (tag_type
== typename_type
15367 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
15368 return cp_parser_make_typename_type (parser
, identifier
,
15371 /* Template parameter lists apply only if we are not within a
15372 function parameter list. */
15373 bool template_parm_lists_apply
15374 = parser
->num_template_parameter_lists
;
15375 if (template_parm_lists_apply
)
15376 for (cp_binding_level
*s
= current_binding_level
;
15377 s
&& s
->kind
!= sk_template_parms
;
15378 s
= s
->level_chain
)
15379 if (s
->kind
== sk_function_parms
)
15380 template_parm_lists_apply
= false;
15382 /* Look up a qualified name in the usual way. */
15386 tree ambiguous_decls
;
15388 decl
= cp_parser_lookup_name (parser
, identifier
,
15390 /*is_template=*/false,
15391 /*is_namespace=*/false,
15392 /*check_dependency=*/true,
15396 /* If the lookup was ambiguous, an error will already have been
15398 if (ambiguous_decls
)
15399 return error_mark_node
;
15401 /* If we are parsing friend declaration, DECL may be a
15402 TEMPLATE_DECL tree node here. However, we need to check
15403 whether this TEMPLATE_DECL results in valid code. Consider
15404 the following example:
15407 template <class T> class C {};
15410 template <class T> friend class N::C; // #1, valid code
15412 template <class T> class Y {
15413 friend class N::C; // #2, invalid code
15416 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15417 name lookup of `N::C'. We see that friend declaration must
15418 be template for the code to be valid. Note that
15419 processing_template_decl does not work here since it is
15420 always 1 for the above two cases. */
15422 decl
= (cp_parser_maybe_treat_template_as_class
15423 (decl
, /*tag_name_p=*/is_friend
15424 && template_parm_lists_apply
));
15426 if (TREE_CODE (decl
) != TYPE_DECL
)
15428 cp_parser_diagnose_invalid_type_name (parser
,
15431 return error_mark_node
;
15434 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15436 bool allow_template
= (template_parm_lists_apply
15437 || DECL_SELF_REFERENCE_P (decl
));
15438 type
= check_elaborated_type_specifier (tag_type
, decl
,
15441 if (type
== error_mark_node
)
15442 return error_mark_node
;
15445 /* Forward declarations of nested types, such as
15450 are invalid unless all components preceding the final '::'
15451 are complete. If all enclosing types are complete, these
15452 declarations become merely pointless.
15454 Invalid forward declarations of nested types are errors
15455 caught elsewhere in parsing. Those that are pointless arrive
15458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15459 && !is_friend
&& !processing_explicit_instantiation
)
15460 warning (0, "declaration %qD does not declare anything", decl
);
15462 type
= TREE_TYPE (decl
);
15466 /* An elaborated-type-specifier sometimes introduces a new type and
15467 sometimes names an existing type. Normally, the rule is that it
15468 introduces a new type only if there is not an existing type of
15469 the same name already in scope. For example, given:
15472 void f() { struct S s; }
15474 the `struct S' in the body of `f' is the same `struct S' as in
15475 the global scope; the existing definition is used. However, if
15476 there were no global declaration, this would introduce a new
15477 local class named `S'.
15479 An exception to this rule applies to the following code:
15481 namespace N { struct S; }
15483 Here, the elaborated-type-specifier names a new type
15484 unconditionally; even if there is already an `S' in the
15485 containing scope this declaration names a new type.
15486 This exception only applies if the elaborated-type-specifier
15487 forms the complete declaration:
15491 A declaration consisting solely of `class-key identifier ;' is
15492 either a redeclaration of the name in the current scope or a
15493 forward declaration of the identifier as a class name. It
15494 introduces the name into the current scope.
15496 We are in this situation precisely when the next token is a `;'.
15498 An exception to the exception is that a `friend' declaration does
15499 *not* name a new type; i.e., given:
15501 struct S { friend struct T; };
15503 `T' is not a new type in the scope of `S'.
15505 Also, `new struct S' or `sizeof (struct S)' never results in the
15506 definition of a new type; a new type can only be declared in a
15507 declaration context. */
15513 /* Friends have special name lookup rules. */
15514 ts
= ts_within_enclosing_non_class
;
15515 else if (is_declaration
15516 && cp_lexer_next_token_is (parser
->lexer
,
15518 /* This is a `class-key identifier ;' */
15524 (template_parm_lists_apply
15525 && (cp_parser_next_token_starts_class_definition_p (parser
)
15526 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15527 /* An unqualified name was used to reference this type, so
15528 there were no qualifying templates. */
15529 if (template_parm_lists_apply
15530 && !cp_parser_check_template_parameters (parser
,
15531 /*num_templates=*/0,
15533 /*declarator=*/NULL
))
15534 return error_mark_node
;
15535 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15539 if (type
== error_mark_node
)
15540 return error_mark_node
;
15542 /* Allow attributes on forward declarations of classes. */
15545 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15546 warning (OPT_Wattributes
,
15547 "attributes ignored on uninstantiated type");
15548 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15549 && ! processing_explicit_instantiation
)
15550 warning (OPT_Wattributes
,
15551 "attributes ignored on template instantiation");
15552 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15553 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15555 warning (OPT_Wattributes
,
15556 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15559 if (tag_type
!= enum_type
)
15561 /* Indicate whether this class was declared as a `class' or as a
15563 if (TREE_CODE (type
) == RECORD_TYPE
)
15564 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15565 cp_parser_check_class_key (tag_type
, type
);
15568 /* A "<" cannot follow an elaborated type specifier. If that
15569 happens, the user was probably trying to form a template-id. */
15570 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15576 /* Parse an enum-specifier.
15579 enum-head { enumerator-list [opt] }
15580 enum-head { enumerator-list , } [C++0x]
15583 enum-key identifier [opt] enum-base [opt]
15584 enum-key nested-name-specifier identifier enum-base [opt]
15589 enum struct [C++0x]
15592 : type-specifier-seq
15594 opaque-enum-specifier:
15595 enum-key identifier enum-base [opt] ;
15598 enum-key attributes[opt] identifier [opt] enum-base [opt]
15599 { enumerator-list [opt] }attributes[opt]
15600 enum-key attributes[opt] identifier [opt] enum-base [opt]
15601 { enumerator-list, }attributes[opt] [C++0x]
15603 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15604 if the token stream isn't an enum-specifier after all. */
15607 cp_parser_enum_specifier (cp_parser
* parser
)
15610 tree type
= NULL_TREE
;
15612 tree nested_name_specifier
= NULL_TREE
;
15614 bool scoped_enum_p
= false;
15615 bool has_underlying_type
= false;
15616 bool nested_being_defined
= false;
15617 bool new_value_list
= false;
15618 bool is_new_type
= false;
15619 bool is_anonymous
= false;
15620 tree underlying_type
= NULL_TREE
;
15621 cp_token
*type_start_token
= NULL
;
15622 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15624 parser
->colon_corrects_to_scope_p
= false;
15626 /* Parse tentatively so that we can back up if we don't find a
15628 cp_parser_parse_tentatively (parser
);
15630 /* Caller guarantees that the current token is 'enum', an identifier
15631 possibly follows, and the token after that is an opening brace.
15632 If we don't have an identifier, fabricate an anonymous name for
15633 the enumeration being defined. */
15634 cp_lexer_consume_token (parser
->lexer
);
15636 /* Parse the "class" or "struct", which indicates a scoped
15637 enumeration type in C++0x. */
15638 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15639 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15641 if (cxx_dialect
< cxx11
)
15642 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15644 /* Consume the `struct' or `class' token. */
15645 cp_lexer_consume_token (parser
->lexer
);
15647 scoped_enum_p
= true;
15650 attributes
= cp_parser_attributes_opt (parser
);
15652 /* Clear the qualification. */
15653 parser
->scope
= NULL_TREE
;
15654 parser
->qualifying_scope
= NULL_TREE
;
15655 parser
->object_scope
= NULL_TREE
;
15657 /* Figure out in what scope the declaration is being placed. */
15658 prev_scope
= current_scope ();
15660 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15662 push_deferring_access_checks (dk_no_check
);
15663 nested_name_specifier
15664 = cp_parser_nested_name_specifier_opt (parser
,
15665 /*typename_keyword_p=*/true,
15666 /*check_dependency_p=*/false,
15668 /*is_declaration=*/false);
15670 if (nested_name_specifier
)
15674 identifier
= cp_parser_identifier (parser
);
15675 name
= cp_parser_lookup_name (parser
, identifier
,
15677 /*is_template=*/false,
15678 /*is_namespace=*/false,
15679 /*check_dependency=*/true,
15680 /*ambiguous_decls=*/NULL
,
15682 if (name
&& name
!= error_mark_node
)
15684 type
= TREE_TYPE (name
);
15685 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15687 /* Are template enums allowed in ISO? */
15688 if (template_parm_scope_p ())
15689 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15690 "%qD is an enumeration template", name
);
15691 /* ignore a typename reference, for it will be solved by name
15696 else if (nested_name_specifier
== error_mark_node
)
15697 /* We already issued an error. */;
15699 error_at (type_start_token
->location
,
15700 "%qD is not an enumerator-name", identifier
);
15704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15705 identifier
= cp_parser_identifier (parser
);
15708 identifier
= make_anon_name ();
15709 is_anonymous
= true;
15711 error_at (type_start_token
->location
,
15712 "anonymous scoped enum is not allowed");
15715 pop_deferring_access_checks ();
15717 /* Check for the `:' that denotes a specified underlying type in C++0x.
15718 Note that a ':' could also indicate a bitfield width, however. */
15719 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15721 cp_decl_specifier_seq type_specifiers
;
15723 /* Consume the `:'. */
15724 cp_lexer_consume_token (parser
->lexer
);
15726 /* Parse the type-specifier-seq. */
15727 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15728 /*is_trailing_return=*/false,
15731 /* At this point this is surely not elaborated type specifier. */
15732 if (!cp_parser_parse_definitely (parser
))
15735 if (cxx_dialect
< cxx11
)
15736 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15738 has_underlying_type
= true;
15740 /* If that didn't work, stop. */
15741 if (type_specifiers
.type
!= error_mark_node
)
15743 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15744 /*initialized=*/0, NULL
);
15745 if (underlying_type
== error_mark_node
15746 || check_for_bare_parameter_packs (underlying_type
))
15747 underlying_type
= NULL_TREE
;
15751 /* Look for the `{' but don't consume it yet. */
15752 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15754 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15756 cp_parser_error (parser
, "expected %<{%>");
15757 if (has_underlying_type
)
15763 /* An opaque-enum-specifier must have a ';' here. */
15764 if ((scoped_enum_p
|| underlying_type
)
15765 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15767 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15768 if (has_underlying_type
)
15776 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15779 if (nested_name_specifier
)
15781 if (CLASS_TYPE_P (nested_name_specifier
))
15783 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15784 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15785 push_scope (nested_name_specifier
);
15787 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15789 push_nested_namespace (nested_name_specifier
);
15793 /* Issue an error message if type-definitions are forbidden here. */
15794 if (!cp_parser_check_type_definition (parser
))
15795 type
= error_mark_node
;
15797 /* Create the new type. We do this before consuming the opening
15798 brace so the enum will be recorded as being on the line of its
15799 tag (or the 'enum' keyword, if there is no tag). */
15800 type
= start_enum (identifier
, type
, underlying_type
,
15801 scoped_enum_p
, &is_new_type
);
15803 /* If the next token is not '{' it is an opaque-enum-specifier or an
15804 elaborated-type-specifier. */
15805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15807 timevar_push (TV_PARSE_ENUM
);
15808 if (nested_name_specifier
15809 && nested_name_specifier
!= error_mark_node
)
15811 /* The following catches invalid code such as:
15812 enum class S<int>::E { A, B, C }; */
15813 if (!processing_specialization
15814 && CLASS_TYPE_P (nested_name_specifier
)
15815 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15816 error_at (type_start_token
->location
, "cannot add an enumerator "
15817 "list to a template instantiation");
15819 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
15821 error_at (type_start_token
->location
,
15822 "%<%T::%E%> has not been declared",
15823 TYPE_CONTEXT (nested_name_specifier
),
15824 nested_name_specifier
);
15825 type
= error_mark_node
;
15827 /* If that scope does not contain the scope in which the
15828 class was originally declared, the program is invalid. */
15829 else if (prev_scope
&& !is_ancestor (prev_scope
,
15830 nested_name_specifier
))
15832 if (at_namespace_scope_p ())
15833 error_at (type_start_token
->location
,
15834 "declaration of %qD in namespace %qD which does not "
15836 type
, prev_scope
, nested_name_specifier
);
15838 error_at (type_start_token
->location
,
15839 "declaration of %qD in %qD which does not "
15841 type
, prev_scope
, nested_name_specifier
);
15842 type
= error_mark_node
;
15847 begin_scope (sk_scoped_enum
, type
);
15849 /* Consume the opening brace. */
15850 cp_lexer_consume_token (parser
->lexer
);
15852 if (type
== error_mark_node
)
15853 ; /* Nothing to add */
15854 else if (OPAQUE_ENUM_P (type
)
15855 || (cxx_dialect
> cxx98
&& processing_specialization
))
15857 new_value_list
= true;
15858 SET_OPAQUE_ENUM_P (type
, false);
15859 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15863 error_at (type_start_token
->location
,
15864 "multiple definition of %q#T", type
);
15865 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15866 "previous definition here");
15867 type
= error_mark_node
;
15870 if (type
== error_mark_node
)
15871 cp_parser_skip_to_end_of_block_or_statement (parser
);
15872 /* If the next token is not '}', then there are some enumerators. */
15873 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15875 if (is_anonymous
&& !scoped_enum_p
)
15876 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15877 "ISO C++ forbids empty anonymous enum");
15880 cp_parser_enumerator_list (parser
, type
);
15882 /* Consume the final '}'. */
15883 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15887 timevar_pop (TV_PARSE_ENUM
);
15891 /* If a ';' follows, then it is an opaque-enum-specifier
15892 and additional restrictions apply. */
15893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15896 error_at (type_start_token
->location
,
15897 "opaque-enum-specifier without name");
15898 else if (nested_name_specifier
)
15899 error_at (type_start_token
->location
,
15900 "opaque-enum-specifier must use a simple identifier");
15904 /* Look for trailing attributes to apply to this enumeration, and
15905 apply them if appropriate. */
15906 if (cp_parser_allow_gnu_extensions_p (parser
))
15908 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15909 trailing_attr
= chainon (trailing_attr
, attributes
);
15910 cplus_decl_attributes (&type
,
15912 (int) ATTR_FLAG_TYPE_IN_PLACE
);
15915 /* Finish up the enumeration. */
15916 if (type
!= error_mark_node
)
15918 if (new_value_list
)
15919 finish_enum_value_list (type
);
15921 finish_enum (type
);
15924 if (nested_name_specifier
)
15926 if (CLASS_TYPE_P (nested_name_specifier
))
15928 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
15929 pop_scope (nested_name_specifier
);
15931 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15933 pop_nested_namespace (nested_name_specifier
);
15937 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
15941 /* Parse an enumerator-list. The enumerators all have the indicated
15945 enumerator-definition
15946 enumerator-list , enumerator-definition */
15949 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
15953 /* Parse an enumerator-definition. */
15954 cp_parser_enumerator_definition (parser
, type
);
15956 /* If the next token is not a ',', we've reached the end of
15958 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15960 /* Otherwise, consume the `,' and keep going. */
15961 cp_lexer_consume_token (parser
->lexer
);
15962 /* If the next token is a `}', there is a trailing comma. */
15963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15965 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
15966 pedwarn (input_location
, OPT_Wpedantic
,
15967 "comma at end of enumerator list");
15973 /* Parse an enumerator-definition. The enumerator has the indicated
15976 enumerator-definition:
15978 enumerator = constant-expression
15984 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
15990 /* Save the input location because we are interested in the location
15991 of the identifier and not the location of the explicit value. */
15992 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15994 /* Look for the identifier. */
15995 identifier
= cp_parser_identifier (parser
);
15996 if (identifier
== error_mark_node
)
15999 /* If the next token is an '=', then there is an explicit value. */
16000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16002 /* Consume the `=' token. */
16003 cp_lexer_consume_token (parser
->lexer
);
16004 /* Parse the value. */
16005 value
= cp_parser_constant_expression (parser
);
16010 /* If we are processing a template, make sure the initializer of the
16011 enumerator doesn't contain any bare template parameter pack. */
16012 if (check_for_bare_parameter_packs (value
))
16013 value
= error_mark_node
;
16015 /* integral_constant_value will pull out this expression, so make sure
16016 it's folded as appropriate. */
16017 value
= fold_non_dependent_expr (value
);
16019 /* Create the enumerator. */
16020 build_enumerator (identifier
, value
, type
, loc
);
16023 /* Parse a namespace-name.
16026 original-namespace-name
16029 Returns the NAMESPACE_DECL for the namespace. */
16032 cp_parser_namespace_name (cp_parser
* parser
)
16035 tree namespace_decl
;
16037 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16039 /* Get the name of the namespace. */
16040 identifier
= cp_parser_identifier (parser
);
16041 if (identifier
== error_mark_node
)
16042 return error_mark_node
;
16044 /* Look up the identifier in the currently active scope. Look only
16045 for namespaces, due to:
16047 [basic.lookup.udir]
16049 When looking up a namespace-name in a using-directive or alias
16050 definition, only namespace names are considered.
16054 [basic.lookup.qual]
16056 During the lookup of a name preceding the :: scope resolution
16057 operator, object, function, and enumerator names are ignored.
16059 (Note that cp_parser_qualifying_entity only calls this
16060 function if the token after the name is the scope resolution
16062 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
16064 /*is_template=*/false,
16065 /*is_namespace=*/true,
16066 /*check_dependency=*/true,
16067 /*ambiguous_decls=*/NULL
,
16069 /* If it's not a namespace, issue an error. */
16070 if (namespace_decl
== error_mark_node
16071 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
16073 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16074 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
16075 cp_parser_error (parser
, "expected namespace-name");
16076 namespace_decl
= error_mark_node
;
16079 return namespace_decl
;
16082 /* Parse a namespace-definition.
16084 namespace-definition:
16085 named-namespace-definition
16086 unnamed-namespace-definition
16088 named-namespace-definition:
16089 original-namespace-definition
16090 extension-namespace-definition
16092 original-namespace-definition:
16093 namespace identifier { namespace-body }
16095 extension-namespace-definition:
16096 namespace original-namespace-name { namespace-body }
16098 unnamed-namespace-definition:
16099 namespace { namespace-body } */
16102 cp_parser_namespace_definition (cp_parser
* parser
)
16104 tree identifier
, attribs
;
16105 bool has_visibility
;
16108 cp_ensure_no_omp_declare_simd (parser
);
16109 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
16111 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
16113 cp_lexer_consume_token (parser
->lexer
);
16118 /* Look for the `namespace' keyword. */
16119 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16121 /* Get the name of the namespace. We do not attempt to distinguish
16122 between an original-namespace-definition and an
16123 extension-namespace-definition at this point. The semantic
16124 analysis routines are responsible for that. */
16125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16126 identifier
= cp_parser_identifier (parser
);
16128 identifier
= NULL_TREE
;
16130 /* Parse any specified attributes. */
16131 attribs
= cp_parser_attributes_opt (parser
);
16133 /* Look for the `{' to start the namespace. */
16134 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
16135 /* Start the namespace. */
16136 push_namespace (identifier
);
16138 /* "inline namespace" is equivalent to a stub namespace definition
16139 followed by a strong using directive. */
16142 tree name_space
= current_namespace
;
16143 /* Set up namespace association. */
16144 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
16145 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
16146 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
16147 /* Import the contents of the inline namespace. */
16149 do_using_directive (name_space
);
16150 push_namespace (identifier
);
16153 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
16155 /* Parse the body of the namespace. */
16156 cp_parser_namespace_body (parser
);
16158 if (has_visibility
)
16159 pop_visibility (1);
16161 /* Finish the namespace. */
16163 /* Look for the final `}'. */
16164 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16167 /* Parse a namespace-body.
16170 declaration-seq [opt] */
16173 cp_parser_namespace_body (cp_parser
* parser
)
16175 cp_parser_declaration_seq_opt (parser
);
16178 /* Parse a namespace-alias-definition.
16180 namespace-alias-definition:
16181 namespace identifier = qualified-namespace-specifier ; */
16184 cp_parser_namespace_alias_definition (cp_parser
* parser
)
16187 tree namespace_specifier
;
16189 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16191 /* Look for the `namespace' keyword. */
16192 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16193 /* Look for the identifier. */
16194 identifier
= cp_parser_identifier (parser
);
16195 if (identifier
== error_mark_node
)
16197 /* Look for the `=' token. */
16198 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
16199 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16201 error_at (token
->location
, "%<namespace%> definition is not allowed here");
16202 /* Skip the definition. */
16203 cp_lexer_consume_token (parser
->lexer
);
16204 if (cp_parser_skip_to_closing_brace (parser
))
16205 cp_lexer_consume_token (parser
->lexer
);
16208 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16209 /* Look for the qualified-namespace-specifier. */
16210 namespace_specifier
16211 = cp_parser_qualified_namespace_specifier (parser
);
16212 /* Look for the `;' token. */
16213 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16215 /* Register the alias in the symbol table. */
16216 do_namespace_alias (identifier
, namespace_specifier
);
16219 /* Parse a qualified-namespace-specifier.
16221 qualified-namespace-specifier:
16222 :: [opt] nested-name-specifier [opt] namespace-name
16224 Returns a NAMESPACE_DECL corresponding to the specified
16228 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
16230 /* Look for the optional `::'. */
16231 cp_parser_global_scope_opt (parser
,
16232 /*current_scope_valid_p=*/false);
16234 /* Look for the optional nested-name-specifier. */
16235 cp_parser_nested_name_specifier_opt (parser
,
16236 /*typename_keyword_p=*/false,
16237 /*check_dependency_p=*/true,
16239 /*is_declaration=*/true);
16241 return cp_parser_namespace_name (parser
);
16244 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16245 access declaration.
16248 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16249 using :: unqualified-id ;
16251 access-declaration:
16257 cp_parser_using_declaration (cp_parser
* parser
,
16258 bool access_declaration_p
)
16261 bool typename_p
= false;
16262 bool global_scope_p
;
16266 int oldcount
= errorcount
;
16267 cp_token
*diag_token
= NULL
;
16269 if (access_declaration_p
)
16271 diag_token
= cp_lexer_peek_token (parser
->lexer
);
16272 cp_parser_parse_tentatively (parser
);
16276 /* Look for the `using' keyword. */
16277 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16279 /* Peek at the next token. */
16280 token
= cp_lexer_peek_token (parser
->lexer
);
16281 /* See if it's `typename'. */
16282 if (token
->keyword
== RID_TYPENAME
)
16284 /* Remember that we've seen it. */
16286 /* Consume the `typename' token. */
16287 cp_lexer_consume_token (parser
->lexer
);
16291 /* Look for the optional global scope qualification. */
16293 = (cp_parser_global_scope_opt (parser
,
16294 /*current_scope_valid_p=*/false)
16297 /* If we saw `typename', or didn't see `::', then there must be a
16298 nested-name-specifier present. */
16299 if (typename_p
|| !global_scope_p
)
16301 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
16302 /*check_dependency_p=*/true,
16304 /*is_declaration=*/true);
16305 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
16307 cp_parser_skip_to_end_of_block_or_statement (parser
);
16311 /* Otherwise, we could be in either of the two productions. In that
16312 case, treat the nested-name-specifier as optional. */
16314 qscope
= cp_parser_nested_name_specifier_opt (parser
,
16315 /*typename_keyword_p=*/false,
16316 /*check_dependency_p=*/true,
16318 /*is_declaration=*/true);
16320 qscope
= global_namespace
;
16321 else if (UNSCOPED_ENUM_P (qscope
))
16322 qscope
= CP_TYPE_CONTEXT (qscope
);
16324 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
16325 /* Something has already gone wrong; there's no need to parse
16326 further. Since an error has occurred, the return value of
16327 cp_parser_parse_definitely will be false, as required. */
16328 return cp_parser_parse_definitely (parser
);
16330 token
= cp_lexer_peek_token (parser
->lexer
);
16331 /* Parse the unqualified-id. */
16332 identifier
= cp_parser_unqualified_id (parser
,
16333 /*template_keyword_p=*/false,
16334 /*check_dependency_p=*/true,
16335 /*declarator_p=*/true,
16336 /*optional_p=*/false);
16338 if (access_declaration_p
)
16340 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16341 cp_parser_simulate_error (parser
);
16342 if (!cp_parser_parse_definitely (parser
))
16346 /* The function we call to handle a using-declaration is different
16347 depending on what scope we are in. */
16348 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
16350 else if (!identifier_p (identifier
)
16351 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
16352 /* [namespace.udecl]
16354 A using declaration shall not name a template-id. */
16355 error_at (token
->location
,
16356 "a template-id may not appear in a using-declaration");
16359 if (at_class_scope_p ())
16361 /* Create the USING_DECL. */
16362 decl
= do_class_using_decl (parser
->scope
, identifier
);
16364 if (decl
&& typename_p
)
16365 USING_DECL_TYPENAME_P (decl
) = 1;
16367 if (check_for_bare_parameter_packs (decl
))
16369 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16373 /* Add it to the list of members in this class. */
16374 finish_member_declaration (decl
);
16378 decl
= cp_parser_lookup_name_simple (parser
,
16381 if (decl
== error_mark_node
)
16382 cp_parser_name_lookup_error (parser
, identifier
,
16385 else if (check_for_bare_parameter_packs (decl
))
16387 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16390 else if (!at_namespace_scope_p ())
16391 do_local_using_decl (decl
, qscope
, identifier
);
16393 do_toplevel_using_decl (decl
, qscope
, identifier
);
16397 /* Look for the final `;'. */
16398 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16400 if (access_declaration_p
&& errorcount
== oldcount
)
16401 warning_at (diag_token
->location
, OPT_Wdeprecated
,
16402 "access declarations are deprecated "
16403 "in favour of using-declarations; "
16404 "suggestion: add the %<using%> keyword");
16409 /* Parse an alias-declaration.
16412 using identifier attribute-specifier-seq [opt] = type-id */
16415 cp_parser_alias_declaration (cp_parser
* parser
)
16417 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
16418 location_t id_location
;
16419 cp_declarator
*declarator
;
16420 cp_decl_specifier_seq decl_specs
;
16422 const char *saved_message
= NULL
;
16424 /* Look for the `using' keyword. */
16425 cp_token
*using_token
16426 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16427 if (using_token
== NULL
)
16428 return error_mark_node
;
16430 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
16431 id
= cp_parser_identifier (parser
);
16432 if (id
== error_mark_node
)
16433 return error_mark_node
;
16435 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
16436 attributes
= cp_parser_attributes_opt (parser
);
16437 if (attributes
== error_mark_node
)
16438 return error_mark_node
;
16440 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16442 if (cp_parser_error_occurred (parser
))
16443 return error_mark_node
;
16445 cp_parser_commit_to_tentative_parse (parser
);
16447 /* Now we are going to parse the type-id of the declaration. */
16452 "A type-specifier-seq shall not define a class or enumeration
16453 unless it appears in the type-id of an alias-declaration (7.1.3) that
16454 is not the declaration of a template-declaration."
16456 In other words, if we currently are in an alias template, the
16457 type-id should not define a type.
16459 So let's set parser->type_definition_forbidden_message in that
16460 case; cp_parser_check_type_definition (called by
16461 cp_parser_class_specifier) will then emit an error if a type is
16462 defined in the type-id. */
16463 if (parser
->num_template_parameter_lists
)
16465 saved_message
= parser
->type_definition_forbidden_message
;
16466 parser
->type_definition_forbidden_message
=
16467 G_("types may not be defined in alias template declarations");
16470 type
= cp_parser_type_id (parser
);
16472 /* Restore the error message if need be. */
16473 if (parser
->num_template_parameter_lists
)
16474 parser
->type_definition_forbidden_message
= saved_message
;
16476 if (type
== error_mark_node
16477 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
16479 cp_parser_skip_to_end_of_block_or_statement (parser
);
16480 return error_mark_node
;
16483 /* A typedef-name can also be introduced by an alias-declaration. The
16484 identifier following the using keyword becomes a typedef-name. It has
16485 the same semantics as if it were introduced by the typedef
16486 specifier. In particular, it does not define a new type and it shall
16487 not appear in the type-id. */
16489 clear_decl_specs (&decl_specs
);
16490 decl_specs
.type
= type
;
16491 if (attributes
!= NULL_TREE
)
16493 decl_specs
.attributes
= attributes
;
16494 set_and_check_decl_spec_loc (&decl_specs
,
16498 set_and_check_decl_spec_loc (&decl_specs
,
16501 set_and_check_decl_spec_loc (&decl_specs
,
16505 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16506 declarator
->id_loc
= id_location
;
16508 member_p
= at_class_scope_p ();
16510 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16511 NULL_TREE
, attributes
);
16513 decl
= start_decl (declarator
, &decl_specs
, 0,
16514 attributes
, NULL_TREE
, &pushed_scope
);
16515 if (decl
== error_mark_node
)
16518 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16521 pop_scope (pushed_scope
);
16523 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16524 added into the symbol table; otherwise, return the TYPE_DECL. */
16525 if (DECL_LANG_SPECIFIC (decl
)
16526 && DECL_TEMPLATE_INFO (decl
)
16527 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16529 decl
= DECL_TI_TEMPLATE (decl
);
16531 check_member_template (decl
);
16537 /* Parse a using-directive.
16540 using namespace :: [opt] nested-name-specifier [opt]
16541 namespace-name ; */
16544 cp_parser_using_directive (cp_parser
* parser
)
16546 tree namespace_decl
;
16549 /* Look for the `using' keyword. */
16550 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16551 /* And the `namespace' keyword. */
16552 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16553 /* Look for the optional `::' operator. */
16554 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16555 /* And the optional nested-name-specifier. */
16556 cp_parser_nested_name_specifier_opt (parser
,
16557 /*typename_keyword_p=*/false,
16558 /*check_dependency_p=*/true,
16560 /*is_declaration=*/true);
16561 /* Get the namespace being used. */
16562 namespace_decl
= cp_parser_namespace_name (parser
);
16563 /* And any specified attributes. */
16564 attribs
= cp_parser_attributes_opt (parser
);
16565 /* Update the symbol table. */
16566 parse_using_directive (namespace_decl
, attribs
);
16567 /* Look for the final `;'. */
16568 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16571 /* Parse an asm-definition.
16574 asm ( string-literal ) ;
16579 asm volatile [opt] ( string-literal ) ;
16580 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16581 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16582 : asm-operand-list [opt] ) ;
16583 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16584 : asm-operand-list [opt]
16585 : asm-clobber-list [opt] ) ;
16586 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16587 : asm-clobber-list [opt]
16588 : asm-goto-list ) ; */
16591 cp_parser_asm_definition (cp_parser
* parser
)
16594 tree outputs
= NULL_TREE
;
16595 tree inputs
= NULL_TREE
;
16596 tree clobbers
= NULL_TREE
;
16597 tree labels
= NULL_TREE
;
16599 bool volatile_p
= false;
16600 bool extended_p
= false;
16601 bool invalid_inputs_p
= false;
16602 bool invalid_outputs_p
= false;
16603 bool goto_p
= false;
16604 required_token missing
= RT_NONE
;
16606 /* Look for the `asm' keyword. */
16607 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16609 if (parser
->in_function_body
16610 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
16611 error ("%<asm%> in %<constexpr%> function");
16613 /* See if the next token is `volatile'. */
16614 if (cp_parser_allow_gnu_extensions_p (parser
)
16615 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16617 /* Remember that we saw the `volatile' keyword. */
16619 /* Consume the token. */
16620 cp_lexer_consume_token (parser
->lexer
);
16622 if (cp_parser_allow_gnu_extensions_p (parser
)
16623 && parser
->in_function_body
16624 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16626 /* Remember that we saw the `goto' keyword. */
16628 /* Consume the token. */
16629 cp_lexer_consume_token (parser
->lexer
);
16631 /* Look for the opening `('. */
16632 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16634 /* Look for the string. */
16635 string
= cp_parser_string_literal (parser
, false, false);
16636 if (string
== error_mark_node
)
16638 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16639 /*consume_paren=*/true);
16643 /* If we're allowing GNU extensions, check for the extended assembly
16644 syntax. Unfortunately, the `:' tokens need not be separated by
16645 a space in C, and so, for compatibility, we tolerate that here
16646 too. Doing that means that we have to treat the `::' operator as
16648 if (cp_parser_allow_gnu_extensions_p (parser
)
16649 && parser
->in_function_body
16650 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16651 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16653 bool inputs_p
= false;
16654 bool clobbers_p
= false;
16655 bool labels_p
= false;
16657 /* The extended syntax was used. */
16660 /* Look for outputs. */
16661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16663 /* Consume the `:'. */
16664 cp_lexer_consume_token (parser
->lexer
);
16665 /* Parse the output-operands. */
16666 if (cp_lexer_next_token_is_not (parser
->lexer
,
16668 && cp_lexer_next_token_is_not (parser
->lexer
,
16670 && cp_lexer_next_token_is_not (parser
->lexer
,
16673 outputs
= cp_parser_asm_operand_list (parser
);
16675 if (outputs
== error_mark_node
)
16676 invalid_outputs_p
= true;
16678 /* If the next token is `::', there are no outputs, and the
16679 next token is the beginning of the inputs. */
16680 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16681 /* The inputs are coming next. */
16684 /* Look for inputs. */
16686 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16688 /* Consume the `:' or `::'. */
16689 cp_lexer_consume_token (parser
->lexer
);
16690 /* Parse the output-operands. */
16691 if (cp_lexer_next_token_is_not (parser
->lexer
,
16693 && cp_lexer_next_token_is_not (parser
->lexer
,
16695 && cp_lexer_next_token_is_not (parser
->lexer
,
16697 inputs
= cp_parser_asm_operand_list (parser
);
16699 if (inputs
== error_mark_node
)
16700 invalid_inputs_p
= true;
16702 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16703 /* The clobbers are coming next. */
16706 /* Look for clobbers. */
16708 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16711 /* Consume the `:' or `::'. */
16712 cp_lexer_consume_token (parser
->lexer
);
16713 /* Parse the clobbers. */
16714 if (cp_lexer_next_token_is_not (parser
->lexer
,
16716 && cp_lexer_next_token_is_not (parser
->lexer
,
16718 clobbers
= cp_parser_asm_clobber_list (parser
);
16721 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16722 /* The labels are coming next. */
16725 /* Look for labels. */
16727 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16730 /* Consume the `:' or `::'. */
16731 cp_lexer_consume_token (parser
->lexer
);
16732 /* Parse the labels. */
16733 labels
= cp_parser_asm_label_list (parser
);
16736 if (goto_p
&& !labels_p
)
16737 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16740 missing
= RT_COLON_SCOPE
;
16742 /* Look for the closing `)'. */
16743 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16744 missing
? missing
: RT_CLOSE_PAREN
))
16745 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16746 /*consume_paren=*/true);
16747 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16749 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16751 /* Create the ASM_EXPR. */
16752 if (parser
->in_function_body
)
16754 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16755 inputs
, clobbers
, labels
);
16756 /* If the extended syntax was not used, mark the ASM_EXPR. */
16759 tree temp
= asm_stmt
;
16760 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16761 temp
= TREE_OPERAND (temp
, 0);
16763 ASM_INPUT_P (temp
) = 1;
16767 symtab
->finalize_toplevel_asm (string
);
16771 /* Declarators [gram.dcl.decl] */
16773 /* Parse an init-declarator.
16776 declarator initializer [opt]
16781 declarator asm-specification [opt] attributes [opt] initializer [opt]
16783 function-definition:
16784 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16786 decl-specifier-seq [opt] declarator function-try-block
16790 function-definition:
16791 __extension__ function-definition
16795 function-definition:
16796 decl-specifier-seq [opt] declarator function-transaction-block
16798 The DECL_SPECIFIERS apply to this declarator. Returns a
16799 representation of the entity declared. If MEMBER_P is TRUE, then
16800 this declarator appears in a class scope. The new DECL created by
16801 this declarator is returned.
16803 The CHECKS are access checks that should be performed once we know
16804 what entity is being declared (and, therefore, what classes have
16807 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16808 for a function-definition here as well. If the declarator is a
16809 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16810 be TRUE upon return. By that point, the function-definition will
16811 have been completely parsed.
16813 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16816 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16817 parsed declaration if it is an uninitialized single declarator not followed
16818 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16819 if present, will not be consumed. If returned, this declarator will be
16820 created with SD_INITIALIZED but will not call cp_finish_decl. */
16823 cp_parser_init_declarator (cp_parser
* parser
,
16824 cp_decl_specifier_seq
*decl_specifiers
,
16825 vec
<deferred_access_check
, va_gc
> *checks
,
16826 bool function_definition_allowed_p
,
16828 int declares_class_or_enum
,
16829 bool* function_definition_p
,
16830 tree
* maybe_range_for_decl
)
16832 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16833 *attributes_start_token
= NULL
;
16834 cp_declarator
*declarator
;
16835 tree prefix_attributes
;
16836 tree attributes
= NULL
;
16837 tree asm_specification
;
16839 tree decl
= NULL_TREE
;
16841 int is_initialized
;
16842 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16843 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16845 enum cpp_ttype initialization_kind
;
16846 bool is_direct_init
= false;
16847 bool is_non_constant_init
;
16848 int ctor_dtor_or_conv_p
;
16849 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
16850 tree pushed_scope
= NULL_TREE
;
16851 bool range_for_decl_p
= false;
16852 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16854 /* Gather the attributes that were provided with the
16855 decl-specifiers. */
16856 prefix_attributes
= decl_specifiers
->attributes
;
16858 /* Assume that this is not the declarator for a function
16860 if (function_definition_p
)
16861 *function_definition_p
= false;
16863 /* Default arguments are only permitted for function parameters. */
16864 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16865 parser
->default_arg_ok_p
= false;
16867 /* Defer access checks while parsing the declarator; we cannot know
16868 what names are accessible until we know what is being
16870 resume_deferring_access_checks ();
16872 /* Parse the declarator. */
16873 token
= cp_lexer_peek_token (parser
->lexer
);
16875 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16876 &ctor_dtor_or_conv_p
,
16877 /*parenthesized_p=*/NULL
,
16878 member_p
, friend_p
);
16879 /* Gather up the deferred checks. */
16880 stop_deferring_access_checks ();
16882 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16884 /* If the DECLARATOR was erroneous, there's no need to go
16886 if (declarator
== cp_error_declarator
)
16887 return error_mark_node
;
16889 /* Check that the number of template-parameter-lists is OK. */
16890 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16892 return error_mark_node
;
16894 if (declares_class_or_enum
& 2)
16895 cp_parser_check_for_definition_in_return_type (declarator
,
16896 decl_specifiers
->type
,
16897 decl_specifiers
->locations
[ds_type_spec
]);
16899 /* Figure out what scope the entity declared by the DECLARATOR is
16900 located in. `grokdeclarator' sometimes changes the scope, so
16901 we compute it now. */
16902 scope
= get_scope_of_declarator (declarator
);
16904 /* Perform any lookups in the declared type which were thought to be
16905 dependent, but are not in the scope of the declarator. */
16906 decl_specifiers
->type
16907 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
16909 /* If we're allowing GNU extensions, look for an
16910 asm-specification. */
16911 if (cp_parser_allow_gnu_extensions_p (parser
))
16913 /* Look for an asm-specification. */
16914 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
16915 asm_specification
= cp_parser_asm_specification_opt (parser
);
16918 asm_specification
= NULL_TREE
;
16920 /* Look for attributes. */
16921 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
16922 attributes
= cp_parser_attributes_opt (parser
);
16924 /* Peek at the next token. */
16925 token
= cp_lexer_peek_token (parser
->lexer
);
16927 bool bogus_implicit_tmpl
= false;
16929 if (function_declarator_p (declarator
))
16931 /* Check to see if the token indicates the start of a
16932 function-definition. */
16933 if (cp_parser_token_starts_function_definition_p (token
))
16935 if (!function_definition_allowed_p
)
16937 /* If a function-definition should not appear here, issue an
16939 cp_parser_error (parser
,
16940 "a function-definition is not allowed here");
16941 return error_mark_node
;
16944 location_t func_brace_location
16945 = cp_lexer_peek_token (parser
->lexer
)->location
;
16947 /* Neither attributes nor an asm-specification are allowed
16948 on a function-definition. */
16949 if (asm_specification
)
16950 error_at (asm_spec_start_token
->location
,
16951 "an asm-specification is not allowed "
16952 "on a function-definition");
16954 error_at (attributes_start_token
->location
,
16955 "attributes are not allowed "
16956 "on a function-definition");
16957 /* This is a function-definition. */
16958 *function_definition_p
= true;
16960 /* Parse the function definition. */
16962 decl
= cp_parser_save_member_function_body (parser
,
16965 prefix_attributes
);
16968 (cp_parser_function_definition_from_specifiers_and_declarator
16969 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
16971 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
16973 /* This is where the prologue starts... */
16974 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
16975 = func_brace_location
;
16981 else if (parser
->fully_implicit_function_template_p
)
16983 /* A non-template declaration involving a function parameter list
16984 containing an implicit template parameter will be made into a
16985 template. If the resulting declaration is not going to be an
16986 actual function then finish the template scope here to prevent it.
16987 An error message will be issued once we have a decl to talk about.
16989 FIXME probably we should do type deduction rather than create an
16990 implicit template, but the standard currently doesn't allow it. */
16991 bogus_implicit_tmpl
= true;
16992 finish_fully_implicit_template (parser
, NULL_TREE
);
16997 Only in function declarations for constructors, destructors, and
16998 type conversions can the decl-specifier-seq be omitted.
17000 We explicitly postpone this check past the point where we handle
17001 function-definitions because we tolerate function-definitions
17002 that are missing their return types in some modes. */
17003 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
17005 cp_parser_error (parser
,
17006 "expected constructor, destructor, or type conversion");
17007 return error_mark_node
;
17010 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17011 if (token
->type
== CPP_EQ
17012 || token
->type
== CPP_OPEN_PAREN
17013 || token
->type
== CPP_OPEN_BRACE
)
17015 is_initialized
= SD_INITIALIZED
;
17016 initialization_kind
= token
->type
;
17017 if (maybe_range_for_decl
)
17018 *maybe_range_for_decl
= error_mark_node
;
17020 if (token
->type
== CPP_EQ
17021 && function_declarator_p (declarator
))
17023 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17024 if (t2
->keyword
== RID_DEFAULT
)
17025 is_initialized
= SD_DEFAULTED
;
17026 else if (t2
->keyword
== RID_DELETE
)
17027 is_initialized
= SD_DELETED
;
17032 /* If the init-declarator isn't initialized and isn't followed by a
17033 `,' or `;', it's not a valid init-declarator. */
17034 if (token
->type
!= CPP_COMMA
17035 && token
->type
!= CPP_SEMICOLON
)
17037 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
17038 range_for_decl_p
= true;
17041 cp_parser_error (parser
, "expected initializer");
17042 return error_mark_node
;
17045 is_initialized
= SD_UNINITIALIZED
;
17046 initialization_kind
= CPP_EOF
;
17049 /* Because start_decl has side-effects, we should only call it if we
17050 know we're going ahead. By this point, we know that we cannot
17051 possibly be looking at any other construct. */
17052 cp_parser_commit_to_tentative_parse (parser
);
17054 /* Enter the newly declared entry in the symbol table. If we're
17055 processing a declaration in a class-specifier, we wait until
17056 after processing the initializer. */
17059 if (parser
->in_unbraced_linkage_specification_p
)
17060 decl_specifiers
->storage_class
= sc_extern
;
17061 decl
= start_decl (declarator
, decl_specifiers
,
17062 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
17063 attributes
, prefix_attributes
, &pushed_scope
);
17064 cp_finalize_omp_declare_simd (parser
, decl
);
17065 /* Adjust location of decl if declarator->id_loc is more appropriate:
17066 set, and decl wasn't merged with another decl, in which case its
17067 location would be different from input_location, and more accurate. */
17069 && declarator
->id_loc
!= UNKNOWN_LOCATION
17070 && DECL_SOURCE_LOCATION (decl
) == input_location
)
17071 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
17074 /* Enter the SCOPE. That way unqualified names appearing in the
17075 initializer will be looked up in SCOPE. */
17076 pushed_scope
= push_scope (scope
);
17078 /* Perform deferred access control checks, now that we know in which
17079 SCOPE the declared entity resides. */
17080 if (!member_p
&& decl
)
17082 tree saved_current_function_decl
= NULL_TREE
;
17084 /* If the entity being declared is a function, pretend that we
17085 are in its scope. If it is a `friend', it may have access to
17086 things that would not otherwise be accessible. */
17087 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17089 saved_current_function_decl
= current_function_decl
;
17090 current_function_decl
= decl
;
17093 /* Perform access checks for template parameters. */
17094 cp_parser_perform_template_parameter_access_checks (checks
);
17096 /* Perform the access control checks for the declarator and the
17097 decl-specifiers. */
17098 perform_deferred_access_checks (tf_warning_or_error
);
17100 /* Restore the saved value. */
17101 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17102 current_function_decl
= saved_current_function_decl
;
17105 /* Parse the initializer. */
17106 initializer
= NULL_TREE
;
17107 is_direct_init
= false;
17108 is_non_constant_init
= true;
17109 if (is_initialized
)
17111 if (function_declarator_p (declarator
))
17113 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
17114 if (initialization_kind
== CPP_EQ
)
17115 initializer
= cp_parser_pure_specifier (parser
);
17118 /* If the declaration was erroneous, we don't really
17119 know what the user intended, so just silently
17120 consume the initializer. */
17121 if (decl
!= error_mark_node
)
17122 error_at (initializer_start_token
->location
,
17123 "initializer provided for function");
17124 cp_parser_skip_to_closing_parenthesis (parser
,
17125 /*recovering=*/true,
17126 /*or_comma=*/false,
17127 /*consume_paren=*/true);
17132 /* We want to record the extra mangling scope for in-class
17133 initializers of class members and initializers of static data
17134 member templates. The former involves deferring
17135 parsing of the initializer until end of class as with default
17136 arguments. So right here we only handle the latter. */
17137 if (!member_p
&& processing_template_decl
)
17138 start_lambda_scope (decl
);
17139 initializer
= cp_parser_initializer (parser
,
17141 &is_non_constant_init
);
17142 if (!member_p
&& processing_template_decl
)
17143 finish_lambda_scope ();
17144 if (initializer
== error_mark_node
)
17145 cp_parser_skip_to_end_of_statement (parser
);
17149 /* The old parser allows attributes to appear after a parenthesized
17150 initializer. Mark Mitchell proposed removing this functionality
17151 on the GCC mailing lists on 2002-08-13. This parser accepts the
17152 attributes -- but ignores them. */
17153 if (cp_parser_allow_gnu_extensions_p (parser
)
17154 && initialization_kind
== CPP_OPEN_PAREN
)
17155 if (cp_parser_attributes_opt (parser
))
17156 warning (OPT_Wattributes
,
17157 "attributes after parenthesized initializer ignored");
17159 /* And now complain about a non-function implicit template. */
17160 if (bogus_implicit_tmpl
)
17161 error_at (DECL_SOURCE_LOCATION (decl
),
17162 "non-function %qD declared as implicit template", decl
);
17164 /* For an in-class declaration, use `grokfield' to create the
17170 pop_scope (pushed_scope
);
17171 pushed_scope
= NULL_TREE
;
17173 decl
= grokfield (declarator
, decl_specifiers
,
17174 initializer
, !is_non_constant_init
,
17175 /*asmspec=*/NULL_TREE
,
17176 chainon (attributes
, prefix_attributes
));
17177 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17178 cp_parser_save_default_args (parser
, decl
);
17179 cp_finalize_omp_declare_simd (parser
, decl
);
17182 /* Finish processing the declaration. But, skip member
17184 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
17186 cp_finish_decl (decl
,
17187 initializer
, !is_non_constant_init
,
17189 /* If the initializer is in parentheses, then this is
17190 a direct-initialization, which means that an
17191 `explicit' constructor is OK. Otherwise, an
17192 `explicit' constructor cannot be used. */
17193 ((is_direct_init
|| !is_initialized
)
17194 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
17196 else if ((cxx_dialect
!= cxx98
) && friend_p
17197 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17198 /* Core issue #226 (C++0x only): A default template-argument
17199 shall not be specified in a friend class template
17201 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
17202 /*is_partial=*/false, /*is_friend_decl=*/1);
17204 if (!friend_p
&& pushed_scope
)
17205 pop_scope (pushed_scope
);
17207 if (function_declarator_p (declarator
)
17208 && parser
->fully_implicit_function_template_p
)
17211 decl
= finish_fully_implicit_template (parser
, decl
);
17213 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
17219 /* Parse a declarator.
17223 ptr-operator declarator
17225 abstract-declarator:
17226 ptr-operator abstract-declarator [opt]
17227 direct-abstract-declarator
17232 attributes [opt] direct-declarator
17233 attributes [opt] ptr-operator declarator
17235 abstract-declarator:
17236 attributes [opt] ptr-operator abstract-declarator [opt]
17237 attributes [opt] direct-abstract-declarator
17239 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17240 detect constructor, destructor or conversion operators. It is set
17241 to -1 if the declarator is a name, and +1 if it is a
17242 function. Otherwise it is set to zero. Usually you just want to
17243 test for >0, but internally the negative value is used.
17245 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17246 a decl-specifier-seq unless it declares a constructor, destructor,
17247 or conversion. It might seem that we could check this condition in
17248 semantic analysis, rather than parsing, but that makes it difficult
17249 to handle something like `f()'. We want to notice that there are
17250 no decl-specifiers, and therefore realize that this is an
17251 expression, not a declaration.)
17253 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17254 the declarator is a direct-declarator of the form "(...)".
17256 MEMBER_P is true iff this declarator is a member-declarator.
17258 FRIEND_P is true iff this declarator is a friend. */
17260 static cp_declarator
*
17261 cp_parser_declarator (cp_parser
* parser
,
17262 cp_parser_declarator_kind dcl_kind
,
17263 int* ctor_dtor_or_conv_p
,
17264 bool* parenthesized_p
,
17265 bool member_p
, bool friend_p
)
17267 cp_declarator
*declarator
;
17268 enum tree_code code
;
17269 cp_cv_quals cv_quals
;
17271 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
17273 /* Assume this is not a constructor, destructor, or type-conversion
17275 if (ctor_dtor_or_conv_p
)
17276 *ctor_dtor_or_conv_p
= 0;
17278 if (cp_parser_allow_gnu_extensions_p (parser
))
17279 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
17281 /* Check for the ptr-operator production. */
17282 cp_parser_parse_tentatively (parser
);
17283 /* Parse the ptr-operator. */
17284 code
= cp_parser_ptr_operator (parser
,
17289 /* If that worked, then we have a ptr-operator. */
17290 if (cp_parser_parse_definitely (parser
))
17292 /* If a ptr-operator was found, then this declarator was not
17294 if (parenthesized_p
)
17295 *parenthesized_p
= true;
17296 /* The dependent declarator is optional if we are parsing an
17297 abstract-declarator. */
17298 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17299 cp_parser_parse_tentatively (parser
);
17301 /* Parse the dependent declarator. */
17302 declarator
= cp_parser_declarator (parser
, dcl_kind
,
17303 /*ctor_dtor_or_conv_p=*/NULL
,
17304 /*parenthesized_p=*/NULL
,
17305 /*member_p=*/false,
17308 /* If we are parsing an abstract-declarator, we must handle the
17309 case where the dependent declarator is absent. */
17310 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
17311 && !cp_parser_parse_definitely (parser
))
17314 declarator
= cp_parser_make_indirect_declarator
17315 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
17317 /* Everything else is a direct-declarator. */
17320 if (parenthesized_p
)
17321 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
17323 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
17324 ctor_dtor_or_conv_p
,
17325 member_p
, friend_p
);
17328 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
17329 declarator
->attributes
= gnu_attributes
;
17333 /* Parse a direct-declarator or direct-abstract-declarator.
17337 direct-declarator ( parameter-declaration-clause )
17338 cv-qualifier-seq [opt]
17339 ref-qualifier [opt]
17340 exception-specification [opt]
17341 direct-declarator [ constant-expression [opt] ]
17344 direct-abstract-declarator:
17345 direct-abstract-declarator [opt]
17346 ( parameter-declaration-clause )
17347 cv-qualifier-seq [opt]
17348 ref-qualifier [opt]
17349 exception-specification [opt]
17350 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17351 ( abstract-declarator )
17353 Returns a representation of the declarator. DCL_KIND is
17354 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17355 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17356 we are parsing a direct-declarator. It is
17357 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17358 of ambiguity we prefer an abstract declarator, as per
17359 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17360 as for cp_parser_declarator. */
17362 static cp_declarator
*
17363 cp_parser_direct_declarator (cp_parser
* parser
,
17364 cp_parser_declarator_kind dcl_kind
,
17365 int* ctor_dtor_or_conv_p
,
17366 bool member_p
, bool friend_p
)
17369 cp_declarator
*declarator
= NULL
;
17370 tree scope
= NULL_TREE
;
17371 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17372 bool saved_in_declarator_p
= parser
->in_declarator_p
;
17374 tree pushed_scope
= NULL_TREE
;
17378 /* Peek at the next token. */
17379 token
= cp_lexer_peek_token (parser
->lexer
);
17380 if (token
->type
== CPP_OPEN_PAREN
)
17382 /* This is either a parameter-declaration-clause, or a
17383 parenthesized declarator. When we know we are parsing a
17384 named declarator, it must be a parenthesized declarator
17385 if FIRST is true. For instance, `(int)' is a
17386 parameter-declaration-clause, with an omitted
17387 direct-abstract-declarator. But `((*))', is a
17388 parenthesized abstract declarator. Finally, when T is a
17389 template parameter `(T)' is a
17390 parameter-declaration-clause, and not a parenthesized
17393 We first try and parse a parameter-declaration-clause,
17394 and then try a nested declarator (if FIRST is true).
17396 It is not an error for it not to be a
17397 parameter-declaration-clause, even when FIRST is
17403 The first is the declaration of a function while the
17404 second is the definition of a variable, including its
17407 Having seen only the parenthesis, we cannot know which of
17408 these two alternatives should be selected. Even more
17409 complex are examples like:
17414 The former is a function-declaration; the latter is a
17415 variable initialization.
17417 Thus again, we try a parameter-declaration-clause, and if
17418 that fails, we back out and return. */
17420 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17423 bool is_declarator
= false;
17425 /* In a member-declarator, the only valid interpretation
17426 of a parenthesis is the start of a
17427 parameter-declaration-clause. (It is invalid to
17428 initialize a static data member with a parenthesized
17429 initializer; only the "=" form of initialization is
17432 cp_parser_parse_tentatively (parser
);
17434 /* Consume the `('. */
17435 cp_lexer_consume_token (parser
->lexer
);
17438 /* If this is going to be an abstract declarator, we're
17439 in a declarator and we can't have default args. */
17440 parser
->default_arg_ok_p
= false;
17441 parser
->in_declarator_p
= true;
17444 begin_scope (sk_function_parms
, NULL_TREE
);
17446 /* Parse the parameter-declaration-clause. */
17447 params
= cp_parser_parameter_declaration_clause (parser
);
17449 /* Consume the `)'. */
17450 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17452 /* If all went well, parse the cv-qualifier-seq,
17453 ref-qualifier and the exception-specification. */
17454 if (member_p
|| cp_parser_parse_definitely (parser
))
17456 cp_cv_quals cv_quals
;
17457 cp_virt_specifiers virt_specifiers
;
17458 cp_ref_qualifier ref_qual
;
17459 tree exception_specification
;
17462 bool memfn
= (member_p
|| (pushed_scope
17463 && CLASS_TYPE_P (pushed_scope
)));
17465 is_declarator
= true;
17467 if (ctor_dtor_or_conv_p
)
17468 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17471 /* Parse the cv-qualifier-seq. */
17472 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17473 /* Parse the ref-qualifier. */
17474 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17475 /* And the exception-specification. */
17476 exception_specification
17477 = cp_parser_exception_specification_opt (parser
);
17479 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17481 /* In here, we handle cases where attribute is used after
17482 the function declaration. For example:
17483 void func (int x) __attribute__((vector(..))); */
17485 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
17487 cp_parser_parse_tentatively (parser
);
17488 tree attr
= cp_parser_gnu_attributes_opt (parser
);
17489 if (cp_lexer_next_token_is_not (parser
->lexer
,
17491 && cp_lexer_next_token_is_not (parser
->lexer
,
17493 cp_parser_abort_tentative_parse (parser
);
17494 else if (!cp_parser_parse_definitely (parser
))
17497 attrs
= chainon (attr
, attrs
);
17499 late_return
= (cp_parser_late_return_type_opt
17500 (parser
, declarator
,
17501 memfn
? cv_quals
: -1));
17504 /* Parse the virt-specifier-seq. */
17505 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17507 /* Create the function-declarator. */
17508 declarator
= make_call_declarator (declarator
,
17513 exception_specification
,
17515 declarator
->std_attributes
= attrs
;
17516 /* Any subsequent parameter lists are to do with
17517 return type, so are not those of the declared
17519 parser
->default_arg_ok_p
= false;
17522 /* Remove the function parms from scope. */
17523 pop_bindings_and_leave_scope ();
17526 /* Repeat the main loop. */
17530 /* If this is the first, we can try a parenthesized
17534 bool saved_in_type_id_in_expr_p
;
17536 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17537 parser
->in_declarator_p
= saved_in_declarator_p
;
17539 /* Consume the `('. */
17540 cp_lexer_consume_token (parser
->lexer
);
17541 /* Parse the nested declarator. */
17542 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17543 parser
->in_type_id_in_expr_p
= true;
17545 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17546 /*parenthesized_p=*/NULL
,
17547 member_p
, friend_p
);
17548 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17550 /* Expect a `)'. */
17551 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17552 declarator
= cp_error_declarator
;
17553 if (declarator
== cp_error_declarator
)
17556 goto handle_declarator
;
17558 /* Otherwise, we must be done. */
17562 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17563 && token
->type
== CPP_OPEN_SQUARE
17564 && !cp_next_tokens_can_be_attribute_p (parser
))
17566 /* Parse an array-declarator. */
17567 tree bounds
, attrs
;
17569 if (ctor_dtor_or_conv_p
)
17570 *ctor_dtor_or_conv_p
= 0;
17573 parser
->default_arg_ok_p
= false;
17574 parser
->in_declarator_p
= true;
17575 /* Consume the `['. */
17576 cp_lexer_consume_token (parser
->lexer
);
17577 /* Peek at the next token. */
17578 token
= cp_lexer_peek_token (parser
->lexer
);
17579 /* If the next token is `]', then there is no
17580 constant-expression. */
17581 if (token
->type
!= CPP_CLOSE_SQUARE
)
17583 bool non_constant_p
;
17585 = cp_parser_constant_expression (parser
,
17586 /*allow_non_constant=*/true,
17588 if (!non_constant_p
)
17590 else if (error_operand_p (bounds
))
17591 /* Already gave an error. */;
17592 else if (!parser
->in_function_body
17593 || current_binding_level
->kind
== sk_function_parms
)
17595 /* Normally, the array bound must be an integral constant
17596 expression. However, as an extension, we allow VLAs
17597 in function scopes as long as they aren't part of a
17598 parameter declaration. */
17599 cp_parser_error (parser
,
17600 "array bound is not an integer constant");
17601 bounds
= error_mark_node
;
17603 else if (processing_template_decl
17604 && !type_dependent_expression_p (bounds
))
17606 /* Remember this wasn't a constant-expression. */
17607 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17608 TREE_SIDE_EFFECTS (bounds
) = 1;
17612 bounds
= NULL_TREE
;
17613 /* Look for the closing `]'. */
17614 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17616 declarator
= cp_error_declarator
;
17620 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17621 declarator
= make_array_declarator (declarator
, bounds
);
17622 declarator
->std_attributes
= attrs
;
17624 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17627 tree qualifying_scope
;
17628 tree unqualified_name
;
17630 special_function_kind sfk
;
17632 bool pack_expansion_p
= false;
17633 cp_token
*declarator_id_start_token
;
17635 /* Parse a declarator-id */
17636 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17639 cp_parser_parse_tentatively (parser
);
17641 /* If we see an ellipsis, we should be looking at a
17643 if (token
->type
== CPP_ELLIPSIS
)
17645 /* Consume the `...' */
17646 cp_lexer_consume_token (parser
->lexer
);
17648 pack_expansion_p
= true;
17652 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17654 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17655 qualifying_scope
= parser
->scope
;
17660 if (!unqualified_name
&& pack_expansion_p
)
17662 /* Check whether an error occurred. */
17663 okay
= !cp_parser_error_occurred (parser
);
17665 /* We already consumed the ellipsis to mark a
17666 parameter pack, but we have no way to report it,
17667 so abort the tentative parse. We will be exiting
17668 immediately anyway. */
17669 cp_parser_abort_tentative_parse (parser
);
17672 okay
= cp_parser_parse_definitely (parser
);
17675 unqualified_name
= error_mark_node
;
17676 else if (unqualified_name
17677 && (qualifying_scope
17678 || (!identifier_p (unqualified_name
))))
17680 cp_parser_error (parser
, "expected unqualified-id");
17681 unqualified_name
= error_mark_node
;
17685 if (!unqualified_name
)
17687 if (unqualified_name
== error_mark_node
)
17689 declarator
= cp_error_declarator
;
17690 pack_expansion_p
= false;
17691 declarator
->parameter_pack_p
= false;
17695 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17697 if (qualifying_scope
&& at_namespace_scope_p ()
17698 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17700 /* In the declaration of a member of a template class
17701 outside of the class itself, the SCOPE will sometimes
17702 be a TYPENAME_TYPE. For example, given:
17704 template <typename T>
17705 int S<T>::R::i = 3;
17707 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17708 this context, we must resolve S<T>::R to an ordinary
17709 type, rather than a typename type.
17711 The reason we normally avoid resolving TYPENAME_TYPEs
17712 is that a specialization of `S' might render
17713 `S<T>::R' not a type. However, if `S' is
17714 specialized, then this `i' will not be used, so there
17715 is no harm in resolving the types here. */
17718 /* Resolve the TYPENAME_TYPE. */
17719 type
= resolve_typename_type (qualifying_scope
,
17720 /*only_current_p=*/false);
17721 /* If that failed, the declarator is invalid. */
17722 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17724 if (typedef_variant_p (type
))
17725 error_at (declarator_id_start_token
->location
,
17726 "cannot define member of dependent typedef "
17729 error_at (declarator_id_start_token
->location
,
17730 "%<%T::%E%> is not a type",
17731 TYPE_CONTEXT (qualifying_scope
),
17732 TYPE_IDENTIFIER (qualifying_scope
));
17734 qualifying_scope
= type
;
17739 if (unqualified_name
)
17743 if (qualifying_scope
17744 && CLASS_TYPE_P (qualifying_scope
))
17745 class_type
= qualifying_scope
;
17747 class_type
= current_class_type
;
17749 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17751 tree name_type
= TREE_TYPE (unqualified_name
);
17752 if (class_type
&& same_type_p (name_type
, class_type
))
17754 if (qualifying_scope
17755 && CLASSTYPE_USE_TEMPLATE (name_type
))
17757 error_at (declarator_id_start_token
->location
,
17758 "invalid use of constructor as a template");
17759 inform (declarator_id_start_token
->location
,
17760 "use %<%T::%D%> instead of %<%T::%D%> to "
17761 "name the constructor in a qualified name",
17763 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17764 class_type
, name_type
);
17765 declarator
= cp_error_declarator
;
17769 unqualified_name
= constructor_name (class_type
);
17773 /* We do not attempt to print the declarator
17774 here because we do not have enough
17775 information about its original syntactic
17777 cp_parser_error (parser
, "invalid declarator");
17778 declarator
= cp_error_declarator
;
17785 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17786 sfk
= sfk_destructor
;
17787 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17788 sfk
= sfk_conversion
;
17789 else if (/* There's no way to declare a constructor
17790 for an anonymous type, even if the type
17791 got a name for linkage purposes. */
17792 !TYPE_WAS_ANONYMOUS (class_type
)
17793 /* Handle correctly (c++/19200):
17807 friend void N::S();
17810 && class_type
!= qualifying_scope
)
17811 && constructor_name_p (unqualified_name
,
17814 unqualified_name
= constructor_name (class_type
);
17815 sfk
= sfk_constructor
;
17817 else if (is_overloaded_fn (unqualified_name
)
17818 && DECL_CONSTRUCTOR_P (get_first_fn
17819 (unqualified_name
)))
17820 sfk
= sfk_constructor
;
17822 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17823 *ctor_dtor_or_conv_p
= -1;
17826 declarator
= make_id_declarator (qualifying_scope
,
17829 declarator
->std_attributes
= attrs
;
17830 declarator
->id_loc
= token
->location
;
17831 declarator
->parameter_pack_p
= pack_expansion_p
;
17833 if (pack_expansion_p
)
17834 maybe_warn_variadic_templates ();
17837 handle_declarator
:;
17838 scope
= get_scope_of_declarator (declarator
);
17841 /* Any names that appear after the declarator-id for a
17842 member are looked up in the containing scope. */
17843 if (at_function_scope_p ())
17845 /* But declarations with qualified-ids can't appear in a
17847 cp_parser_error (parser
, "qualified-id in declaration");
17848 declarator
= cp_error_declarator
;
17851 pushed_scope
= push_scope (scope
);
17853 parser
->in_declarator_p
= true;
17854 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17855 || (declarator
&& declarator
->kind
== cdk_id
))
17856 /* Default args are only allowed on function
17858 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17860 parser
->default_arg_ok_p
= false;
17869 /* For an abstract declarator, we might wind up with nothing at this
17870 point. That's an error; the declarator is not optional. */
17872 cp_parser_error (parser
, "expected declarator");
17874 /* If we entered a scope, we must exit it now. */
17876 pop_scope (pushed_scope
);
17878 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17879 parser
->in_declarator_p
= saved_in_declarator_p
;
17884 /* Parse a ptr-operator.
17887 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17888 * cv-qualifier-seq [opt]
17890 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17891 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17896 & cv-qualifier-seq [opt]
17898 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17899 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17900 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17901 filled in with the TYPE containing the member. *CV_QUALS is
17902 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17903 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17904 Note that the tree codes returned by this function have nothing
17905 to do with the types of trees that will be eventually be created
17906 to represent the pointer or reference type being parsed. They are
17907 just constants with suggestive names. */
17908 static enum tree_code
17909 cp_parser_ptr_operator (cp_parser
* parser
,
17911 cp_cv_quals
*cv_quals
,
17914 enum tree_code code
= ERROR_MARK
;
17916 tree attrs
= NULL_TREE
;
17918 /* Assume that it's not a pointer-to-member. */
17920 /* And that there are no cv-qualifiers. */
17921 *cv_quals
= TYPE_UNQUALIFIED
;
17923 /* Peek at the next token. */
17924 token
= cp_lexer_peek_token (parser
->lexer
);
17926 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17927 if (token
->type
== CPP_MULT
)
17928 code
= INDIRECT_REF
;
17929 else if (token
->type
== CPP_AND
)
17931 else if ((cxx_dialect
!= cxx98
) &&
17932 token
->type
== CPP_AND_AND
) /* C++0x only */
17933 code
= NON_LVALUE_EXPR
;
17935 if (code
!= ERROR_MARK
)
17937 /* Consume the `*', `&' or `&&'. */
17938 cp_lexer_consume_token (parser
->lexer
);
17940 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17941 `&', if we are allowing GNU extensions. (The only qualifier
17942 that can legally appear after `&' is `restrict', but that is
17943 enforced during semantic analysis. */
17944 if (code
== INDIRECT_REF
17945 || cp_parser_allow_gnu_extensions_p (parser
))
17946 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17948 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17949 if (attributes
!= NULL
)
17950 *attributes
= attrs
;
17954 /* Try the pointer-to-member case. */
17955 cp_parser_parse_tentatively (parser
);
17956 /* Look for the optional `::' operator. */
17957 cp_parser_global_scope_opt (parser
,
17958 /*current_scope_valid_p=*/false);
17959 /* Look for the nested-name specifier. */
17960 token
= cp_lexer_peek_token (parser
->lexer
);
17961 cp_parser_nested_name_specifier (parser
,
17962 /*typename_keyword_p=*/false,
17963 /*check_dependency_p=*/true,
17965 /*is_declaration=*/false);
17966 /* If we found it, and the next token is a `*', then we are
17967 indeed looking at a pointer-to-member operator. */
17968 if (!cp_parser_error_occurred (parser
)
17969 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
17971 /* Indicate that the `*' operator was used. */
17972 code
= INDIRECT_REF
;
17974 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
17975 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
17976 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
17977 error_at (token
->location
, "cannot form pointer to member of "
17978 "non-class %q#T", parser
->scope
);
17981 /* The type of which the member is a member is given by the
17983 *type
= parser
->scope
;
17984 /* The next name will not be qualified. */
17985 parser
->scope
= NULL_TREE
;
17986 parser
->qualifying_scope
= NULL_TREE
;
17987 parser
->object_scope
= NULL_TREE
;
17988 /* Look for optional c++11 attributes. */
17989 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17990 if (attributes
!= NULL
)
17991 *attributes
= attrs
;
17992 /* Look for the optional cv-qualifier-seq. */
17993 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17996 /* If that didn't work we don't have a ptr-operator. */
17997 if (!cp_parser_parse_definitely (parser
))
17998 cp_parser_error (parser
, "expected ptr-operator");
18004 /* Parse an (optional) cv-qualifier-seq.
18007 cv-qualifier cv-qualifier-seq [opt]
18018 Returns a bitmask representing the cv-qualifiers. */
18021 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
18023 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
18028 cp_cv_quals cv_qualifier
;
18030 /* Peek at the next token. */
18031 token
= cp_lexer_peek_token (parser
->lexer
);
18032 /* See if it's a cv-qualifier. */
18033 switch (token
->keyword
)
18036 cv_qualifier
= TYPE_QUAL_CONST
;
18040 cv_qualifier
= TYPE_QUAL_VOLATILE
;
18044 cv_qualifier
= TYPE_QUAL_RESTRICT
;
18048 cv_qualifier
= TYPE_UNQUALIFIED
;
18055 if (cv_quals
& cv_qualifier
)
18057 error_at (token
->location
, "duplicate cv-qualifier");
18058 cp_lexer_purge_token (parser
->lexer
);
18062 cp_lexer_consume_token (parser
->lexer
);
18063 cv_quals
|= cv_qualifier
;
18070 /* Parse an (optional) ref-qualifier
18076 Returns cp_ref_qualifier representing ref-qualifier. */
18078 static cp_ref_qualifier
18079 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
18081 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
18083 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18084 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
18089 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
18090 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18092 switch (token
->type
)
18095 curr_ref_qual
= REF_QUAL_LVALUE
;
18099 curr_ref_qual
= REF_QUAL_RVALUE
;
18103 curr_ref_qual
= REF_QUAL_NONE
;
18107 if (!curr_ref_qual
)
18111 error_at (token
->location
, "multiple ref-qualifiers");
18112 cp_lexer_purge_token (parser
->lexer
);
18116 ref_qual
= curr_ref_qual
;
18117 cp_lexer_consume_token (parser
->lexer
);
18124 /* Parse an (optional) virt-specifier-seq.
18126 virt-specifier-seq:
18127 virt-specifier virt-specifier-seq [opt]
18133 Returns a bitmask representing the virt-specifiers. */
18135 static cp_virt_specifiers
18136 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
18138 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18143 cp_virt_specifiers virt_specifier
;
18145 /* Peek at the next token. */
18146 token
= cp_lexer_peek_token (parser
->lexer
);
18147 /* See if it's a virt-specifier-qualifier. */
18148 if (token
->type
!= CPP_NAME
)
18150 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
18152 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18153 virt_specifier
= VIRT_SPEC_OVERRIDE
;
18155 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
18157 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18158 virt_specifier
= VIRT_SPEC_FINAL
;
18160 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
18162 virt_specifier
= VIRT_SPEC_FINAL
;
18167 if (virt_specifiers
& virt_specifier
)
18169 error_at (token
->location
, "duplicate virt-specifier");
18170 cp_lexer_purge_token (parser
->lexer
);
18174 cp_lexer_consume_token (parser
->lexer
);
18175 virt_specifiers
|= virt_specifier
;
18178 return virt_specifiers
;
18181 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18182 is in scope even though it isn't real. */
18185 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
18189 if (current_class_ptr
)
18191 /* We don't clear this between NSDMIs. Is it already what we want? */
18192 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
18193 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
18194 && cp_type_quals (type
) == quals
)
18198 this_parm
= build_this_parm (ctype
, quals
);
18199 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18200 current_class_ptr
= NULL_TREE
;
18202 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
18203 current_class_ptr
= this_parm
;
18206 /* Return true iff our current scope is a non-static data member
18210 parsing_nsdmi (void)
18212 /* We recognize NSDMI context by the context-less 'this' pointer set up
18213 by the function above. */
18214 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
18219 /* Parse a late-specified return type, if any. This is not a separate
18220 non-terminal, but part of a function declarator, which looks like
18222 -> trailing-type-specifier-seq abstract-declarator(opt)
18224 Returns the type indicated by the type-id.
18226 In addition to this this parses any queued up omp declare simd
18227 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18229 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18233 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
18237 tree type
= NULL_TREE
;
18238 bool declare_simd_p
= (parser
->omp_declare_simd
18240 && declarator
->kind
== cdk_id
);
18242 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
18243 && declarator
&& declarator
->kind
== cdk_id
);
18245 /* Peek at the next token. */
18246 token
= cp_lexer_peek_token (parser
->lexer
);
18247 /* A late-specified return type is indicated by an initial '->'. */
18248 if (token
->type
!= CPP_DEREF
&& !(declare_simd_p
|| cilk_simd_fn_vector_p
))
18251 tree save_ccp
= current_class_ptr
;
18252 tree save_ccr
= current_class_ref
;
18255 /* DR 1207: 'this' is in scope in the trailing return type. */
18256 inject_this_parameter (current_class_type
, quals
);
18259 if (token
->type
== CPP_DEREF
)
18261 /* Consume the ->. */
18262 cp_lexer_consume_token (parser
->lexer
);
18264 type
= cp_parser_trailing_type_id (parser
);
18267 if (cilk_simd_fn_vector_p
)
18268 declarator
->std_attributes
18269 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
18270 declarator
->std_attributes
);
18271 if (declare_simd_p
)
18272 declarator
->std_attributes
18273 = cp_parser_late_parsing_omp_declare_simd (parser
,
18274 declarator
->std_attributes
);
18278 current_class_ptr
= save_ccp
;
18279 current_class_ref
= save_ccr
;
18285 /* Parse a declarator-id.
18289 :: [opt] nested-name-specifier [opt] type-name
18291 In the `id-expression' case, the value returned is as for
18292 cp_parser_id_expression if the id-expression was an unqualified-id.
18293 If the id-expression was a qualified-id, then a SCOPE_REF is
18294 returned. The first operand is the scope (either a NAMESPACE_DECL
18295 or TREE_TYPE), but the second is still just a representation of an
18299 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
18302 /* The expression must be an id-expression. Assume that qualified
18303 names are the names of types so that:
18306 int S<T>::R::i = 3;
18308 will work; we must treat `S<T>::R' as the name of a type.
18309 Similarly, assume that qualified names are templates, where
18313 int S<T>::R<T>::i = 3;
18316 id
= cp_parser_id_expression (parser
,
18317 /*template_keyword_p=*/false,
18318 /*check_dependency_p=*/false,
18319 /*template_p=*/NULL
,
18320 /*declarator_p=*/true,
18322 if (id
&& BASELINK_P (id
))
18323 id
= BASELINK_FUNCTIONS (id
);
18327 /* Parse a type-id.
18330 type-specifier-seq abstract-declarator [opt]
18332 Returns the TYPE specified. */
18335 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
18336 bool is_trailing_return
)
18338 cp_decl_specifier_seq type_specifier_seq
;
18339 cp_declarator
*abstract_declarator
;
18341 /* Parse the type-specifier-seq. */
18342 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18343 is_trailing_return
,
18344 &type_specifier_seq
);
18345 if (type_specifier_seq
.type
== error_mark_node
)
18346 return error_mark_node
;
18348 /* There might or might not be an abstract declarator. */
18349 cp_parser_parse_tentatively (parser
);
18350 /* Look for the declarator. */
18351 abstract_declarator
18352 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
18353 /*parenthesized_p=*/NULL
,
18354 /*member_p=*/false,
18355 /*friend_p=*/false);
18356 /* Check to see if there really was a declarator. */
18357 if (!cp_parser_parse_definitely (parser
))
18358 abstract_declarator
= NULL
;
18360 if (type_specifier_seq
.type
18361 /* None of the valid uses of 'auto' in C++14 involve the type-id
18362 nonterminal, but it is valid in a trailing-return-type. */
18363 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
18364 && type_uses_auto (type_specifier_seq
.type
))
18366 /* A type-id with type 'auto' is only ok if the abstract declarator
18367 is a function declarator with a late-specified return type. */
18368 if (abstract_declarator
18369 && abstract_declarator
->kind
== cdk_function
18370 && abstract_declarator
->u
.function
.late_return_type
)
18374 error ("invalid use of %<auto%>");
18375 return error_mark_node
;
18379 return groktypename (&type_specifier_seq
, abstract_declarator
,
18383 static tree
cp_parser_type_id (cp_parser
*parser
)
18385 return cp_parser_type_id_1 (parser
, false, false);
18388 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
18391 const char *saved_message
= parser
->type_definition_forbidden_message
;
18392 parser
->type_definition_forbidden_message
18393 = G_("types may not be defined in template arguments");
18394 r
= cp_parser_type_id_1 (parser
, true, false);
18395 parser
->type_definition_forbidden_message
= saved_message
;
18396 if (cxx_dialect
>= cxx14
&& type_uses_auto (r
))
18398 error ("invalid use of %<auto%> in template argument");
18399 r
= error_mark_node
;
18404 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
18406 return cp_parser_type_id_1 (parser
, false, true);
18409 /* Parse a type-specifier-seq.
18411 type-specifier-seq:
18412 type-specifier type-specifier-seq [opt]
18416 type-specifier-seq:
18417 attributes type-specifier-seq [opt]
18419 If IS_DECLARATION is true, we are at the start of a "condition" or
18420 exception-declaration, so we might be followed by a declarator-id.
18422 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18423 i.e. we've just seen "->".
18425 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18428 cp_parser_type_specifier_seq (cp_parser
* parser
,
18429 bool is_declaration
,
18430 bool is_trailing_return
,
18431 cp_decl_specifier_seq
*type_specifier_seq
)
18433 bool seen_type_specifier
= false;
18434 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
18435 cp_token
*start_token
= NULL
;
18437 /* Clear the TYPE_SPECIFIER_SEQ. */
18438 clear_decl_specs (type_specifier_seq
);
18440 /* In the context of a trailing return type, enum E { } is an
18441 elaborated-type-specifier followed by a function-body, not an
18443 if (is_trailing_return
)
18444 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
18446 /* Parse the type-specifiers and attributes. */
18449 tree type_specifier
;
18450 bool is_cv_qualifier
;
18452 /* Check for attributes first. */
18453 if (cp_next_tokens_can_be_attribute_p (parser
))
18455 type_specifier_seq
->attributes
=
18456 chainon (type_specifier_seq
->attributes
,
18457 cp_parser_attributes_opt (parser
));
18461 /* record the token of the beginning of the type specifier seq,
18462 for error reporting purposes*/
18464 start_token
= cp_lexer_peek_token (parser
->lexer
);
18466 /* Look for the type-specifier. */
18467 type_specifier
= cp_parser_type_specifier (parser
,
18469 type_specifier_seq
,
18470 /*is_declaration=*/false,
18473 if (!type_specifier
)
18475 /* If the first type-specifier could not be found, this is not a
18476 type-specifier-seq at all. */
18477 if (!seen_type_specifier
)
18479 /* Set in_declarator_p to avoid skipping to the semicolon. */
18480 int in_decl
= parser
->in_declarator_p
;
18481 parser
->in_declarator_p
= true;
18483 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
18484 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18485 cp_parser_error (parser
, "expected type-specifier");
18487 parser
->in_declarator_p
= in_decl
;
18489 type_specifier_seq
->type
= error_mark_node
;
18492 /* If subsequent type-specifiers could not be found, the
18493 type-specifier-seq is complete. */
18497 seen_type_specifier
= true;
18498 /* The standard says that a condition can be:
18500 type-specifier-seq declarator = assignment-expression
18507 we should treat the "S" as a declarator, not as a
18508 type-specifier. The standard doesn't say that explicitly for
18509 type-specifier-seq, but it does say that for
18510 decl-specifier-seq in an ordinary declaration. Perhaps it
18511 would be clearer just to allow a decl-specifier-seq here, and
18512 then add a semantic restriction that if any decl-specifiers
18513 that are not type-specifiers appear, the program is invalid. */
18514 if (is_declaration
&& !is_cv_qualifier
)
18515 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18519 /* Return whether the function currently being declared has an associated
18520 template parameter list. */
18523 function_being_declared_is_template_p (cp_parser
* parser
)
18525 if (!current_template_parms
|| processing_template_parmlist
)
18528 if (parser
->implicit_template_scope
)
18531 if (at_class_scope_p ()
18532 && TYPE_BEING_DEFINED (current_class_type
))
18533 return parser
->num_template_parameter_lists
!= 0;
18535 return ((int) parser
->num_template_parameter_lists
> template_class_depth
18536 (current_class_type
));
18539 /* Parse a parameter-declaration-clause.
18541 parameter-declaration-clause:
18542 parameter-declaration-list [opt] ... [opt]
18543 parameter-declaration-list , ...
18545 Returns a representation for the parameter declarations. A return
18546 value of NULL indicates a parameter-declaration-clause consisting
18547 only of an ellipsis. */
18550 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18559 int auto_is_implicit_function_template_parm_p
;
18561 parser
->auto_is_implicit_function_template_parm_p
18562 = auto_is_implicit_function_template_parm_p
;
18564 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18568 if (!processing_specialization
18569 && !processing_template_parmlist
18570 && !processing_explicit_instantiation
)
18571 if (!current_function_decl
18572 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
18573 parser
->auto_is_implicit_function_template_parm_p
= true;
18575 /* Peek at the next token. */
18576 token
= cp_lexer_peek_token (parser
->lexer
);
18577 /* Check for trivial parameter-declaration-clauses. */
18578 if (token
->type
== CPP_ELLIPSIS
)
18580 /* Consume the `...' token. */
18581 cp_lexer_consume_token (parser
->lexer
);
18584 else if (token
->type
== CPP_CLOSE_PAREN
)
18585 /* There are no parameters. */
18587 #ifndef NO_IMPLICIT_EXTERN_C
18588 if (in_system_header_at (input_location
)
18589 && current_class_type
== NULL
18590 && current_lang_name
== lang_name_c
)
18594 return void_list_node
;
18596 /* Check for `(void)', too, which is a special case. */
18597 else if (token
->keyword
== RID_VOID
18598 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18599 == CPP_CLOSE_PAREN
))
18601 /* Consume the `void' token. */
18602 cp_lexer_consume_token (parser
->lexer
);
18603 /* There are no parameters. */
18604 return void_list_node
;
18607 /* Parse the parameter-declaration-list. */
18608 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18609 /* If a parse error occurred while parsing the
18610 parameter-declaration-list, then the entire
18611 parameter-declaration-clause is erroneous. */
18615 /* Peek at the next token. */
18616 token
= cp_lexer_peek_token (parser
->lexer
);
18617 /* If it's a `,', the clause should terminate with an ellipsis. */
18618 if (token
->type
== CPP_COMMA
)
18620 /* Consume the `,'. */
18621 cp_lexer_consume_token (parser
->lexer
);
18622 /* Expect an ellipsis. */
18624 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18626 /* It might also be `...' if the optional trailing `,' was
18628 else if (token
->type
== CPP_ELLIPSIS
)
18630 /* Consume the `...' token. */
18631 cp_lexer_consume_token (parser
->lexer
);
18632 /* And remember that we saw it. */
18636 ellipsis_p
= false;
18638 /* Finish the parameter list. */
18640 parameters
= chainon (parameters
, void_list_node
);
18645 /* Parse a parameter-declaration-list.
18647 parameter-declaration-list:
18648 parameter-declaration
18649 parameter-declaration-list , parameter-declaration
18651 Returns a representation of the parameter-declaration-list, as for
18652 cp_parser_parameter_declaration_clause. However, the
18653 `void_list_node' is never appended to the list. Upon return,
18654 *IS_ERROR will be true iff an error occurred. */
18657 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18659 tree parameters
= NULL_TREE
;
18660 tree
*tail
= ¶meters
;
18661 bool saved_in_unbraced_linkage_specification_p
;
18664 /* Assume all will go well. */
18666 /* The special considerations that apply to a function within an
18667 unbraced linkage specifications do not apply to the parameters
18668 to the function. */
18669 saved_in_unbraced_linkage_specification_p
18670 = parser
->in_unbraced_linkage_specification_p
;
18671 parser
->in_unbraced_linkage_specification_p
= false;
18673 /* Look for more parameters. */
18676 cp_parameter_declarator
*parameter
;
18677 tree decl
= error_mark_node
;
18678 bool parenthesized_p
= false;
18679 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
18680 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18681 (current_template_parms
)) : 0);
18683 /* Parse the parameter. */
18685 = cp_parser_parameter_declaration (parser
,
18686 /*template_parm_p=*/false,
18689 /* We don't know yet if the enclosing context is deprecated, so wait
18690 and warn in grokparms if appropriate. */
18691 deprecated_state
= DEPRECATED_SUPPRESS
;
18695 /* If a function parameter pack was specified and an implicit template
18696 parameter was introduced during cp_parser_parameter_declaration,
18697 change any implicit parameters introduced into packs. */
18698 if (parser
->implicit_template_parms
18699 && parameter
->declarator
18700 && parameter
->declarator
->parameter_pack_p
)
18702 int latest_template_parm_idx
= TREE_VEC_LENGTH
18703 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18705 if (latest_template_parm_idx
!= template_parm_idx
)
18706 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18707 (parameter
->decl_specifiers
.type
,
18708 template_parm_idx
, latest_template_parm_idx
);
18711 decl
= grokdeclarator (parameter
->declarator
,
18712 ¶meter
->decl_specifiers
,
18714 parameter
->default_argument
!= NULL_TREE
,
18715 ¶meter
->decl_specifiers
.attributes
);
18718 deprecated_state
= DEPRECATED_NORMAL
;
18720 /* If a parse error occurred parsing the parameter declaration,
18721 then the entire parameter-declaration-list is erroneous. */
18722 if (decl
== error_mark_node
)
18725 parameters
= error_mark_node
;
18729 if (parameter
->decl_specifiers
.attributes
)
18730 cplus_decl_attributes (&decl
,
18731 parameter
->decl_specifiers
.attributes
,
18733 if (DECL_NAME (decl
))
18734 decl
= pushdecl (decl
);
18736 if (decl
!= error_mark_node
)
18738 retrofit_lang_decl (decl
);
18739 DECL_PARM_INDEX (decl
) = ++index
;
18740 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18743 /* Add the new parameter to the list. */
18744 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18745 tail
= &TREE_CHAIN (*tail
);
18747 /* Peek at the next token. */
18748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18749 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18750 /* These are for Objective-C++ */
18751 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18752 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18753 /* The parameter-declaration-list is complete. */
18755 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18759 /* Peek at the next token. */
18760 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18761 /* If it's an ellipsis, then the list is complete. */
18762 if (token
->type
== CPP_ELLIPSIS
)
18764 /* Otherwise, there must be more parameters. Consume the
18766 cp_lexer_consume_token (parser
->lexer
);
18767 /* When parsing something like:
18769 int i(float f, double d)
18771 we can tell after seeing the declaration for "f" that we
18772 are not looking at an initialization of a variable "i",
18773 but rather at the declaration of a function "i".
18775 Due to the fact that the parsing of template arguments
18776 (as specified to a template-id) requires backtracking we
18777 cannot use this technique when inside a template argument
18779 if (!parser
->in_template_argument_list_p
18780 && !parser
->in_type_id_in_expr_p
18781 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18782 /* However, a parameter-declaration of the form
18783 "float(f)" (which is a valid declaration of a
18784 parameter "f") can also be interpreted as an
18785 expression (the conversion of "f" to "float"). */
18786 && !parenthesized_p
)
18787 cp_parser_commit_to_tentative_parse (parser
);
18791 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18792 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18793 cp_parser_skip_to_closing_parenthesis (parser
,
18794 /*recovering=*/true,
18795 /*or_comma=*/false,
18796 /*consume_paren=*/false);
18801 parser
->in_unbraced_linkage_specification_p
18802 = saved_in_unbraced_linkage_specification_p
;
18804 /* Reset implicit_template_scope if we are about to leave the function
18805 parameter list that introduced it. Note that for out-of-line member
18806 definitions, there will be one or more class scopes before we get to
18807 the template parameter scope. */
18809 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18810 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
18812 while (maybe_its
->kind
== sk_class
)
18813 maybe_its
= maybe_its
->level_chain
;
18814 if (maybe_its
== its
)
18816 parser
->implicit_template_parms
= 0;
18817 parser
->implicit_template_scope
= 0;
18824 /* Parse a parameter declaration.
18826 parameter-declaration:
18827 decl-specifier-seq ... [opt] declarator
18828 decl-specifier-seq declarator = assignment-expression
18829 decl-specifier-seq ... [opt] abstract-declarator [opt]
18830 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18832 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18833 declares a template parameter. (In that case, a non-nested `>'
18834 token encountered during the parsing of the assignment-expression
18835 is not interpreted as a greater-than operator.)
18837 Returns a representation of the parameter, or NULL if an error
18838 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18839 true iff the declarator is of the form "(p)". */
18841 static cp_parameter_declarator
*
18842 cp_parser_parameter_declaration (cp_parser
*parser
,
18843 bool template_parm_p
,
18844 bool *parenthesized_p
)
18846 int declares_class_or_enum
;
18847 cp_decl_specifier_seq decl_specifiers
;
18848 cp_declarator
*declarator
;
18849 tree default_argument
;
18850 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18851 const char *saved_message
;
18853 /* In a template parameter, `>' is not an operator.
18857 When parsing a default template-argument for a non-type
18858 template-parameter, the first non-nested `>' is taken as the end
18859 of the template parameter-list rather than a greater-than
18862 /* Type definitions may not appear in parameter types. */
18863 saved_message
= parser
->type_definition_forbidden_message
;
18864 parser
->type_definition_forbidden_message
18865 = G_("types may not be defined in parameter types");
18867 /* Parse the declaration-specifiers. */
18868 cp_parser_decl_specifier_seq (parser
,
18869 CP_PARSER_FLAGS_NONE
,
18871 &declares_class_or_enum
);
18873 /* Complain about missing 'typename' or other invalid type names. */
18874 if (!decl_specifiers
.any_type_specifiers_p
18875 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18876 decl_specifiers
.type
= error_mark_node
;
18878 /* If an error occurred, there's no reason to attempt to parse the
18879 rest of the declaration. */
18880 if (cp_parser_error_occurred (parser
))
18882 parser
->type_definition_forbidden_message
= saved_message
;
18886 /* Peek at the next token. */
18887 token
= cp_lexer_peek_token (parser
->lexer
);
18889 /* If the next token is a `)', `,', `=', `>', or `...', then there
18890 is no declarator. However, when variadic templates are enabled,
18891 there may be a declarator following `...'. */
18892 if (token
->type
== CPP_CLOSE_PAREN
18893 || token
->type
== CPP_COMMA
18894 || token
->type
== CPP_EQ
18895 || token
->type
== CPP_GREATER
)
18898 if (parenthesized_p
)
18899 *parenthesized_p
= false;
18901 /* Otherwise, there should be a declarator. */
18904 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18905 parser
->default_arg_ok_p
= false;
18907 /* After seeing a decl-specifier-seq, if the next token is not a
18908 "(", there is no possibility that the code is a valid
18909 expression. Therefore, if parsing tentatively, we commit at
18911 if (!parser
->in_template_argument_list_p
18912 /* In an expression context, having seen:
18916 we cannot be sure whether we are looking at a
18917 function-type (taking a "char" as a parameter) or a cast
18918 of some object of type "char" to "int". */
18919 && !parser
->in_type_id_in_expr_p
18920 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18921 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
18922 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
18923 cp_parser_commit_to_tentative_parse (parser
);
18924 /* Parse the declarator. */
18925 declarator_token_start
= token
;
18926 declarator
= cp_parser_declarator (parser
,
18927 CP_PARSER_DECLARATOR_EITHER
,
18928 /*ctor_dtor_or_conv_p=*/NULL
,
18930 /*member_p=*/false,
18931 /*friend_p=*/false);
18932 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18933 /* After the declarator, allow more attributes. */
18934 decl_specifiers
.attributes
18935 = chainon (decl_specifiers
.attributes
,
18936 cp_parser_attributes_opt (parser
));
18939 /* If the next token is an ellipsis, and we have not seen a
18940 declarator name, and the type of the declarator contains parameter
18941 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18942 a parameter pack expansion expression. Otherwise, leave the
18943 ellipsis for a C-style variadic function. */
18944 token
= cp_lexer_peek_token (parser
->lexer
);
18945 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18947 tree type
= decl_specifiers
.type
;
18949 if (type
&& DECL_P (type
))
18950 type
= TREE_TYPE (type
);
18953 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
18954 && declarator_can_be_parameter_pack (declarator
)
18955 && (!declarator
|| !declarator
->parameter_pack_p
)
18956 && uses_parameter_packs (type
))
18958 /* Consume the `...'. */
18959 cp_lexer_consume_token (parser
->lexer
);
18960 maybe_warn_variadic_templates ();
18962 /* Build a pack expansion type */
18964 declarator
->parameter_pack_p
= true;
18966 decl_specifiers
.type
= make_pack_expansion (type
);
18970 /* The restriction on defining new types applies only to the type
18971 of the parameter, not to the default argument. */
18972 parser
->type_definition_forbidden_message
= saved_message
;
18974 /* If the next token is `=', then process a default argument. */
18975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18977 token
= cp_lexer_peek_token (parser
->lexer
);
18978 /* If we are defining a class, then the tokens that make up the
18979 default argument must be saved and processed later. */
18980 if (!template_parm_p
&& at_class_scope_p ()
18981 && TYPE_BEING_DEFINED (current_class_type
)
18982 && !LAMBDA_TYPE_P (current_class_type
))
18983 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
18984 /* Outside of a class definition, we can just parse the
18985 assignment-expression. */
18988 = cp_parser_default_argument (parser
, template_parm_p
);
18990 if (!parser
->default_arg_ok_p
)
18992 if (flag_permissive
)
18993 warning (0, "deprecated use of default argument for parameter of non-function");
18996 error_at (token
->location
,
18997 "default arguments are only "
18998 "permitted for function parameters");
18999 default_argument
= NULL_TREE
;
19002 else if ((declarator
&& declarator
->parameter_pack_p
)
19003 || (decl_specifiers
.type
19004 && PACK_EXPANSION_P (decl_specifiers
.type
)))
19006 /* Find the name of the parameter pack. */
19007 cp_declarator
*id_declarator
= declarator
;
19008 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
19009 id_declarator
= id_declarator
->declarator
;
19011 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
19012 error_at (declarator_token_start
->location
,
19014 ? G_("template parameter pack %qD "
19015 "cannot have a default argument")
19016 : G_("parameter pack %qD cannot have "
19017 "a default argument"),
19018 id_declarator
->u
.id
.unqualified_name
);
19020 error_at (declarator_token_start
->location
,
19022 ? G_("template parameter pack cannot have "
19023 "a default argument")
19024 : G_("parameter pack cannot have a "
19025 "default argument"));
19027 default_argument
= NULL_TREE
;
19031 default_argument
= NULL_TREE
;
19033 return make_parameter_declarator (&decl_specifiers
,
19038 /* Parse a default argument and return it.
19040 TEMPLATE_PARM_P is true if this is a default argument for a
19041 non-type template parameter. */
19043 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
19045 tree default_argument
= NULL_TREE
;
19046 bool saved_greater_than_is_operator_p
;
19047 bool saved_local_variables_forbidden_p
;
19048 bool non_constant_p
, is_direct_init
;
19050 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19052 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
19053 parser
->greater_than_is_operator_p
= !template_parm_p
;
19054 /* Local variable names (and the `this' keyword) may not
19055 appear in a default argument. */
19056 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19057 parser
->local_variables_forbidden_p
= true;
19058 /* Parse the assignment-expression. */
19059 if (template_parm_p
)
19060 push_deferring_access_checks (dk_no_deferred
);
19061 tree saved_class_ptr
= NULL_TREE
;
19062 tree saved_class_ref
= NULL_TREE
;
19063 /* The "this" pointer is not valid in a default argument. */
19066 saved_class_ptr
= current_class_ptr
;
19067 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
19068 saved_class_ref
= current_class_ref
;
19069 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
19072 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
19073 /* Restore the "this" pointer. */
19076 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
19077 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
19079 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
19080 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19081 if (template_parm_p
)
19082 pop_deferring_access_checks ();
19083 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
19084 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
19086 return default_argument
;
19089 /* Parse a function-body.
19092 compound_statement */
19095 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
19097 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
19100 /* Parse a ctor-initializer-opt followed by a function-body. Return
19101 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19102 is true we are parsing a function-try-block. */
19105 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
19106 bool in_function_try_block
)
19109 bool ctor_initializer_p
;
19110 const bool check_body_p
=
19111 DECL_CONSTRUCTOR_P (current_function_decl
)
19112 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
19115 /* Begin the function body. */
19116 body
= begin_function_body ();
19117 /* Parse the optional ctor-initializer. */
19118 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
19120 /* If we're parsing a constexpr constructor definition, we need
19121 to check that the constructor body is indeed empty. However,
19122 before we get to cp_parser_function_body lot of junk has been
19123 generated, so we can't just check that we have an empty block.
19124 Rather we take a snapshot of the outermost block, and check whether
19125 cp_parser_function_body changed its state. */
19128 list
= cur_stmt_list
;
19129 if (STATEMENT_LIST_TAIL (list
))
19130 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
19132 /* Parse the function-body. */
19133 cp_parser_function_body (parser
, in_function_try_block
);
19135 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
19136 /* Finish the function body. */
19137 finish_function_body (body
);
19139 return ctor_initializer_p
;
19142 /* Parse an initializer.
19145 = initializer-clause
19146 ( expression-list )
19148 Returns an expression representing the initializer. If no
19149 initializer is present, NULL_TREE is returned.
19151 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19152 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19153 set to TRUE if there is no initializer present. If there is an
19154 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19155 is set to true; otherwise it is set to false. */
19158 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
19159 bool* non_constant_p
)
19164 /* Peek at the next token. */
19165 token
= cp_lexer_peek_token (parser
->lexer
);
19167 /* Let our caller know whether or not this initializer was
19169 *is_direct_init
= (token
->type
!= CPP_EQ
);
19170 /* Assume that the initializer is constant. */
19171 *non_constant_p
= false;
19173 if (token
->type
== CPP_EQ
)
19175 /* Consume the `='. */
19176 cp_lexer_consume_token (parser
->lexer
);
19177 /* Parse the initializer-clause. */
19178 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
19180 else if (token
->type
== CPP_OPEN_PAREN
)
19182 vec
<tree
, va_gc
> *vec
;
19183 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19185 /*allow_expansion_p=*/true,
19188 return error_mark_node
;
19189 init
= build_tree_list_vec (vec
);
19190 release_tree_vector (vec
);
19192 else if (token
->type
== CPP_OPEN_BRACE
)
19194 cp_lexer_set_source_position (parser
->lexer
);
19195 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19196 init
= cp_parser_braced_list (parser
, non_constant_p
);
19197 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
19201 /* Anything else is an error. */
19202 cp_parser_error (parser
, "expected initializer");
19203 init
= error_mark_node
;
19209 /* Parse an initializer-clause.
19211 initializer-clause:
19212 assignment-expression
19215 Returns an expression representing the initializer.
19217 If the `assignment-expression' production is used the value
19218 returned is simply a representation for the expression.
19220 Otherwise, calls cp_parser_braced_list. */
19223 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
19227 /* Assume the expression is constant. */
19228 *non_constant_p
= false;
19230 /* If it is not a `{', then we are looking at an
19231 assignment-expression. */
19232 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
19235 = cp_parser_constant_expression (parser
,
19236 /*allow_non_constant_p=*/true,
19240 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
19242 return initializer
;
19245 /* Parse a brace-enclosed initializer list.
19248 { initializer-list , [opt] }
19251 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19252 the elements of the initializer-list (or NULL, if the last
19253 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19254 NULL_TREE. There is no way to detect whether or not the optional
19255 trailing `,' was provided. NON_CONSTANT_P is as for
19256 cp_parser_initializer. */
19259 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
19263 /* Consume the `{' token. */
19264 cp_lexer_consume_token (parser
->lexer
);
19265 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19266 initializer
= make_node (CONSTRUCTOR
);
19267 /* If it's not a `}', then there is a non-trivial initializer. */
19268 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
19270 /* Parse the initializer list. */
19271 CONSTRUCTOR_ELTS (initializer
)
19272 = cp_parser_initializer_list (parser
, non_constant_p
);
19273 /* A trailing `,' token is allowed. */
19274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19275 cp_lexer_consume_token (parser
->lexer
);
19278 *non_constant_p
= false;
19279 /* Now, there should be a trailing `}'. */
19280 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19281 TREE_TYPE (initializer
) = init_list_type_node
;
19282 return initializer
;
19285 /* Consume tokens up to, and including, the next non-nested closing `]'.
19286 Returns true iff we found a closing `]'. */
19289 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
19291 unsigned square_depth
= 0;
19295 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
19297 switch (token
->type
)
19300 case CPP_PRAGMA_EOL
:
19301 /* If we've run out of tokens, then there is no closing `]'. */
19304 case CPP_OPEN_SQUARE
:
19308 case CPP_CLOSE_SQUARE
:
19309 if (!square_depth
--)
19311 cp_lexer_consume_token (parser
->lexer
);
19320 /* Consume the token. */
19321 cp_lexer_consume_token (parser
->lexer
);
19325 /* Return true if we are looking at an array-designator, false otherwise. */
19328 cp_parser_array_designator_p (cp_parser
*parser
)
19330 /* Consume the `['. */
19331 cp_lexer_consume_token (parser
->lexer
);
19333 cp_lexer_save_tokens (parser
->lexer
);
19335 /* Skip tokens until the next token is a closing square bracket.
19336 If we find the closing `]', and the next token is a `=', then
19337 we are looking at an array designator. */
19338 bool array_designator_p
19339 = (cp_parser_skip_to_closing_square_bracket (parser
)
19340 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
19342 /* Roll back the tokens we skipped. */
19343 cp_lexer_rollback_tokens (parser
->lexer
);
19345 return array_designator_p
;
19348 /* Parse an initializer-list.
19351 initializer-clause ... [opt]
19352 initializer-list , initializer-clause ... [opt]
19357 designation initializer-clause ...[opt]
19358 initializer-list , designation initializer-clause ...[opt]
19363 [ constant-expression ] =
19365 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19366 for the initializer. If the INDEX of the elt is non-NULL, it is the
19367 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19368 as for cp_parser_initializer. */
19370 static vec
<constructor_elt
, va_gc
> *
19371 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
19373 vec
<constructor_elt
, va_gc
> *v
= NULL
;
19375 /* Assume all of the expressions are constant. */
19376 *non_constant_p
= false;
19378 /* Parse the rest of the list. */
19384 bool clause_non_constant_p
;
19386 /* If the next token is an identifier and the following one is a
19387 colon, we are looking at the GNU designated-initializer
19389 if (cp_parser_allow_gnu_extensions_p (parser
)
19390 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
19391 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
19393 /* Warn the user that they are using an extension. */
19394 pedwarn (input_location
, OPT_Wpedantic
,
19395 "ISO C++ does not allow designated initializers");
19396 /* Consume the identifier. */
19397 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19398 /* Consume the `:'. */
19399 cp_lexer_consume_token (parser
->lexer
);
19401 /* Also handle the C99 syntax, '. id ='. */
19402 else if (cp_parser_allow_gnu_extensions_p (parser
)
19403 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
19404 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
19405 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
19407 /* Warn the user that they are using an extension. */
19408 pedwarn (input_location
, OPT_Wpedantic
,
19409 "ISO C++ does not allow C99 designated initializers");
19410 /* Consume the `.'. */
19411 cp_lexer_consume_token (parser
->lexer
);
19412 /* Consume the identifier. */
19413 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19414 /* Consume the `='. */
19415 cp_lexer_consume_token (parser
->lexer
);
19417 /* Also handle C99 array designators, '[ const ] ='. */
19418 else if (cp_parser_allow_gnu_extensions_p (parser
)
19419 && !c_dialect_objc ()
19420 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19422 /* In C++11, [ could start a lambda-introducer. */
19423 bool non_const
= false;
19425 cp_parser_parse_tentatively (parser
);
19427 if (!cp_parser_array_designator_p (parser
))
19429 cp_parser_simulate_error (parser
);
19430 designator
= NULL_TREE
;
19434 designator
= cp_parser_constant_expression (parser
, true,
19436 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19437 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19440 if (!cp_parser_parse_definitely (parser
))
19441 designator
= NULL_TREE
;
19442 else if (non_const
)
19443 require_potential_rvalue_constant_expression (designator
);
19446 designator
= NULL_TREE
;
19448 /* Parse the initializer. */
19449 initializer
= cp_parser_initializer_clause (parser
,
19450 &clause_non_constant_p
);
19451 /* If any clause is non-constant, so is the entire initializer. */
19452 if (clause_non_constant_p
)
19453 *non_constant_p
= true;
19455 /* If we have an ellipsis, this is an initializer pack
19457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19459 /* Consume the `...'. */
19460 cp_lexer_consume_token (parser
->lexer
);
19462 /* Turn the initializer into an initializer expansion. */
19463 initializer
= make_pack_expansion (initializer
);
19466 /* Add it to the vector. */
19467 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
19469 /* If the next token is not a comma, we have reached the end of
19471 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19474 /* Peek at the next token. */
19475 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19476 /* If the next token is a `}', then we're still done. An
19477 initializer-clause can have a trailing `,' after the
19478 initializer-list and before the closing `}'. */
19479 if (token
->type
== CPP_CLOSE_BRACE
)
19482 /* Consume the `,' token. */
19483 cp_lexer_consume_token (parser
->lexer
);
19489 /* Classes [gram.class] */
19491 /* Parse a class-name.
19497 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19498 to indicate that names looked up in dependent types should be
19499 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19500 keyword has been used to indicate that the name that appears next
19501 is a template. TAG_TYPE indicates the explicit tag given before
19502 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19503 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19504 is the class being defined in a class-head.
19506 Returns the TYPE_DECL representing the class. */
19509 cp_parser_class_name (cp_parser
*parser
,
19510 bool typename_keyword_p
,
19511 bool template_keyword_p
,
19512 enum tag_types tag_type
,
19513 bool check_dependency_p
,
19515 bool is_declaration
)
19521 tree identifier
= NULL_TREE
;
19523 /* All class-names start with an identifier. */
19524 token
= cp_lexer_peek_token (parser
->lexer
);
19525 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
19527 cp_parser_error (parser
, "expected class-name");
19528 return error_mark_node
;
19531 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19532 to a template-id, so we save it here. */
19533 scope
= parser
->scope
;
19534 if (scope
== error_mark_node
)
19535 return error_mark_node
;
19537 /* Any name names a type if we're following the `typename' keyword
19538 in a qualified name where the enclosing scope is type-dependent. */
19539 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
19540 && dependent_type_p (scope
));
19541 /* Handle the common case (an identifier, but not a template-id)
19543 if (token
->type
== CPP_NAME
19544 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
19546 cp_token
*identifier_token
;
19549 /* Look for the identifier. */
19550 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
19551 ambiguous_p
= identifier_token
->error_reported
;
19552 identifier
= cp_parser_identifier (parser
);
19553 /* If the next token isn't an identifier, we are certainly not
19554 looking at a class-name. */
19555 if (identifier
== error_mark_node
)
19556 decl
= error_mark_node
;
19557 /* If we know this is a type-name, there's no need to look it
19559 else if (typename_p
)
19563 tree ambiguous_decls
;
19564 /* If we already know that this lookup is ambiguous, then
19565 we've already issued an error message; there's no reason
19569 cp_parser_simulate_error (parser
);
19570 return error_mark_node
;
19572 /* If the next token is a `::', then the name must be a type
19575 [basic.lookup.qual]
19577 During the lookup for a name preceding the :: scope
19578 resolution operator, object, function, and enumerator
19579 names are ignored. */
19580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19581 tag_type
= typename_type
;
19582 /* Look up the name. */
19583 decl
= cp_parser_lookup_name (parser
, identifier
,
19585 /*is_template=*/false,
19586 /*is_namespace=*/false,
19587 check_dependency_p
,
19589 identifier_token
->location
);
19590 if (ambiguous_decls
)
19592 if (cp_parser_parsing_tentatively (parser
))
19593 cp_parser_simulate_error (parser
);
19594 return error_mark_node
;
19600 /* Try a template-id. */
19601 decl
= cp_parser_template_id (parser
, template_keyword_p
,
19602 check_dependency_p
,
19605 if (decl
== error_mark_node
)
19606 return error_mark_node
;
19609 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
19611 /* If this is a typename, create a TYPENAME_TYPE. */
19612 if (typename_p
&& decl
!= error_mark_node
)
19614 decl
= make_typename_type (scope
, decl
, typename_type
,
19615 /*complain=*/tf_error
);
19616 if (decl
!= error_mark_node
)
19617 decl
= TYPE_NAME (decl
);
19620 decl
= strip_using_decl (decl
);
19622 /* Check to see that it is really the name of a class. */
19623 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19624 && identifier_p (TREE_OPERAND (decl
, 0))
19625 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19626 /* Situations like this:
19628 template <typename T> struct A {
19629 typename T::template X<int>::I i;
19632 are problematic. Is `T::template X<int>' a class-name? The
19633 standard does not seem to be definitive, but there is no other
19634 valid interpretation of the following `::'. Therefore, those
19635 names are considered class-names. */
19637 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19638 if (decl
!= error_mark_node
)
19639 decl
= TYPE_NAME (decl
);
19641 else if (TREE_CODE (decl
) != TYPE_DECL
19642 || TREE_TYPE (decl
) == error_mark_node
19643 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19644 /* In Objective-C 2.0, a classname followed by '.' starts a
19645 dot-syntax expression, and it's not a type-name. */
19646 || (c_dialect_objc ()
19647 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19648 && objc_is_class_name (decl
)))
19649 decl
= error_mark_node
;
19651 if (decl
== error_mark_node
)
19652 cp_parser_error (parser
, "expected class-name");
19653 else if (identifier
&& !parser
->scope
)
19654 maybe_note_name_used_in_class (identifier
, decl
);
19659 /* Parse a class-specifier.
19662 class-head { member-specification [opt] }
19664 Returns the TREE_TYPE representing the class. */
19667 cp_parser_class_specifier_1 (cp_parser
* parser
)
19670 tree attributes
= NULL_TREE
;
19671 bool nested_name_specifier_p
;
19672 unsigned saved_num_template_parameter_lists
;
19673 bool saved_in_function_body
;
19674 unsigned char in_statement
;
19675 bool in_switch_statement_p
;
19676 bool saved_in_unbraced_linkage_specification_p
;
19677 tree old_scope
= NULL_TREE
;
19678 tree scope
= NULL_TREE
;
19679 cp_token
*closing_brace
;
19681 push_deferring_access_checks (dk_no_deferred
);
19683 /* Parse the class-head. */
19684 type
= cp_parser_class_head (parser
,
19685 &nested_name_specifier_p
);
19686 /* If the class-head was a semantic disaster, skip the entire body
19690 cp_parser_skip_to_end_of_block_or_statement (parser
);
19691 pop_deferring_access_checks ();
19692 return error_mark_node
;
19695 /* Look for the `{'. */
19696 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19698 pop_deferring_access_checks ();
19699 return error_mark_node
;
19702 cp_ensure_no_omp_declare_simd (parser
);
19704 /* Issue an error message if type-definitions are forbidden here. */
19705 cp_parser_check_type_definition (parser
);
19706 /* Remember that we are defining one more class. */
19707 ++parser
->num_classes_being_defined
;
19708 /* Inside the class, surrounding template-parameter-lists do not
19710 saved_num_template_parameter_lists
19711 = parser
->num_template_parameter_lists
;
19712 parser
->num_template_parameter_lists
= 0;
19713 /* We are not in a function body. */
19714 saved_in_function_body
= parser
->in_function_body
;
19715 parser
->in_function_body
= false;
19716 /* Or in a loop. */
19717 in_statement
= parser
->in_statement
;
19718 parser
->in_statement
= 0;
19719 /* Or in a switch. */
19720 in_switch_statement_p
= parser
->in_switch_statement_p
;
19721 parser
->in_switch_statement_p
= false;
19722 /* We are not immediately inside an extern "lang" block. */
19723 saved_in_unbraced_linkage_specification_p
19724 = parser
->in_unbraced_linkage_specification_p
;
19725 parser
->in_unbraced_linkage_specification_p
= false;
19727 /* Start the class. */
19728 if (nested_name_specifier_p
)
19730 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19731 old_scope
= push_inner_scope (scope
);
19733 type
= begin_class_definition (type
);
19735 if (type
== error_mark_node
)
19736 /* If the type is erroneous, skip the entire body of the class. */
19737 cp_parser_skip_to_closing_brace (parser
);
19739 /* Parse the member-specification. */
19740 cp_parser_member_specification_opt (parser
);
19742 /* Look for the trailing `}'. */
19743 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19744 /* Look for trailing attributes to apply to this class. */
19745 if (cp_parser_allow_gnu_extensions_p (parser
))
19746 attributes
= cp_parser_gnu_attributes_opt (parser
);
19747 if (type
!= error_mark_node
)
19748 type
= finish_struct (type
, attributes
);
19749 if (nested_name_specifier_p
)
19750 pop_inner_scope (old_scope
, scope
);
19752 /* We've finished a type definition. Check for the common syntax
19753 error of forgetting a semicolon after the definition. We need to
19754 be careful, as we can't just check for not-a-semicolon and be done
19755 with it; the user might have typed:
19757 class X { } c = ...;
19758 class X { } *p = ...;
19760 and so forth. Instead, enumerate all the possible tokens that
19761 might follow this production; if we don't see one of them, then
19762 complain and silently insert the semicolon. */
19764 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19765 bool want_semicolon
= true;
19767 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19768 /* Don't try to parse c++11 attributes here. As per the
19769 grammar, that should be a task for
19770 cp_parser_decl_specifier_seq. */
19771 want_semicolon
= false;
19773 switch (token
->type
)
19776 case CPP_SEMICOLON
:
19779 case CPP_OPEN_PAREN
:
19780 case CPP_CLOSE_PAREN
:
19782 want_semicolon
= false;
19785 /* While it's legal for type qualifiers and storage class
19786 specifiers to follow type definitions in the grammar, only
19787 compiler testsuites contain code like that. Assume that if
19788 we see such code, then what we're really seeing is a case
19792 const <type> var = ...;
19797 static <type> func (...) ...
19799 i.e. the qualifier or specifier applies to the next
19800 declaration. To do so, however, we need to look ahead one
19801 more token to see if *that* token is a type specifier.
19803 This code could be improved to handle:
19806 static const <type> var = ...; */
19808 if (keyword_is_decl_specifier (token
->keyword
))
19810 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19812 /* Handling user-defined types here would be nice, but very
19815 = (lookahead
->type
== CPP_KEYWORD
19816 && keyword_begins_type_specifier (lookahead
->keyword
));
19823 /* If we don't have a type, then something is very wrong and we
19824 shouldn't try to do anything clever. Likewise for not seeing the
19826 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19828 cp_token_position prev
19829 = cp_lexer_previous_token_position (parser
->lexer
);
19830 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19831 location_t loc
= prev_token
->location
;
19833 if (CLASSTYPE_DECLARED_CLASS (type
))
19834 error_at (loc
, "expected %<;%> after class definition");
19835 else if (TREE_CODE (type
) == RECORD_TYPE
)
19836 error_at (loc
, "expected %<;%> after struct definition");
19837 else if (TREE_CODE (type
) == UNION_TYPE
)
19838 error_at (loc
, "expected %<;%> after union definition");
19840 gcc_unreachable ();
19842 /* Unget one token and smash it to look as though we encountered
19843 a semicolon in the input stream. */
19844 cp_lexer_set_token_position (parser
->lexer
, prev
);
19845 token
= cp_lexer_peek_token (parser
->lexer
);
19846 token
->type
= CPP_SEMICOLON
;
19847 token
->keyword
= RID_MAX
;
19851 /* If this class is not itself within the scope of another class,
19852 then we need to parse the bodies of all of the queued function
19853 definitions. Note that the queued functions defined in a class
19854 are not always processed immediately following the
19855 class-specifier for that class. Consider:
19858 struct B { void f() { sizeof (A); } };
19861 If `f' were processed before the processing of `A' were
19862 completed, there would be no way to compute the size of `A'.
19863 Note that the nesting we are interested in here is lexical --
19864 not the semantic nesting given by TYPE_CONTEXT. In particular,
19867 struct A { struct B; };
19868 struct A::B { void f() { } };
19870 there is no need to delay the parsing of `A::B::f'. */
19871 if (--parser
->num_classes_being_defined
== 0)
19874 tree class_type
= NULL_TREE
;
19875 tree pushed_scope
= NULL_TREE
;
19877 cp_default_arg_entry
*e
;
19878 tree save_ccp
, save_ccr
;
19880 /* In a first pass, parse default arguments to the functions.
19881 Then, in a second pass, parse the bodies of the functions.
19882 This two-phased approach handles cases like:
19890 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
19893 /* If there are default arguments that have not yet been processed,
19894 take care of them now. */
19895 if (class_type
!= e
->class_type
)
19898 pop_scope (pushed_scope
);
19899 class_type
= e
->class_type
;
19900 pushed_scope
= push_scope (class_type
);
19902 /* Make sure that any template parameters are in scope. */
19903 maybe_begin_member_template_processing (decl
);
19904 /* Parse the default argument expressions. */
19905 cp_parser_late_parsing_default_args (parser
, decl
);
19906 /* Remove any template parameters from the symbol table. */
19907 maybe_end_member_template_processing ();
19909 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
19910 /* Now parse any NSDMIs. */
19911 save_ccp
= current_class_ptr
;
19912 save_ccr
= current_class_ref
;
19913 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
19915 if (class_type
!= DECL_CONTEXT (decl
))
19918 pop_scope (pushed_scope
);
19919 class_type
= DECL_CONTEXT (decl
);
19920 pushed_scope
= push_scope (class_type
);
19922 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
19923 cp_parser_late_parsing_nsdmi (parser
, decl
);
19925 vec_safe_truncate (unparsed_nsdmis
, 0);
19926 current_class_ptr
= save_ccp
;
19927 current_class_ref
= save_ccr
;
19929 pop_scope (pushed_scope
);
19931 /* Now do some post-NSDMI bookkeeping. */
19932 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
19933 after_nsdmi_defaulted_late_checks (class_type
);
19934 vec_safe_truncate (unparsed_classes
, 0);
19935 after_nsdmi_defaulted_late_checks (type
);
19937 /* Now parse the body of the functions. */
19940 /* OpenMP UDRs need to be parsed before all other functions. */
19941 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19942 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
19943 cp_parser_late_parsing_for_member (parser
, decl
);
19944 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19945 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
19946 cp_parser_late_parsing_for_member (parser
, decl
);
19949 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19950 cp_parser_late_parsing_for_member (parser
, decl
);
19951 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
19954 vec_safe_push (unparsed_classes
, type
);
19956 /* Put back any saved access checks. */
19957 pop_deferring_access_checks ();
19959 /* Restore saved state. */
19960 parser
->in_switch_statement_p
= in_switch_statement_p
;
19961 parser
->in_statement
= in_statement
;
19962 parser
->in_function_body
= saved_in_function_body
;
19963 parser
->num_template_parameter_lists
19964 = saved_num_template_parameter_lists
;
19965 parser
->in_unbraced_linkage_specification_p
19966 = saved_in_unbraced_linkage_specification_p
;
19972 cp_parser_class_specifier (cp_parser
* parser
)
19975 timevar_push (TV_PARSE_STRUCT
);
19976 ret
= cp_parser_class_specifier_1 (parser
);
19977 timevar_pop (TV_PARSE_STRUCT
);
19981 /* Parse a class-head.
19984 class-key identifier [opt] base-clause [opt]
19985 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19986 class-key nested-name-specifier [opt] template-id
19989 class-virt-specifier:
19993 class-key attributes identifier [opt] base-clause [opt]
19994 class-key attributes nested-name-specifier identifier base-clause [opt]
19995 class-key attributes nested-name-specifier [opt] template-id
19998 Upon return BASES is initialized to the list of base classes (or
19999 NULL, if there are none) in the same form returned by
20000 cp_parser_base_clause.
20002 Returns the TYPE of the indicated class. Sets
20003 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20004 involving a nested-name-specifier was used, and FALSE otherwise.
20006 Returns error_mark_node if this is not a class-head.
20008 Returns NULL_TREE if the class-head is syntactically valid, but
20009 semantically invalid in a way that means we should skip the entire
20010 body of the class. */
20013 cp_parser_class_head (cp_parser
* parser
,
20014 bool* nested_name_specifier_p
)
20016 tree nested_name_specifier
;
20017 enum tag_types class_key
;
20018 tree id
= NULL_TREE
;
20019 tree type
= NULL_TREE
;
20022 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20023 bool template_id_p
= false;
20024 bool qualified_p
= false;
20025 bool invalid_nested_name_p
= false;
20026 bool invalid_explicit_specialization_p
= false;
20027 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20028 tree pushed_scope
= NULL_TREE
;
20029 unsigned num_templates
;
20030 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
20031 /* Assume no nested-name-specifier will be present. */
20032 *nested_name_specifier_p
= false;
20033 /* Assume no template parameter lists will be used in defining the
20036 parser
->colon_corrects_to_scope_p
= false;
20038 /* Look for the class-key. */
20039 class_key
= cp_parser_class_key (parser
);
20040 if (class_key
== none_type
)
20041 return error_mark_node
;
20043 /* Parse the attributes. */
20044 attributes
= cp_parser_attributes_opt (parser
);
20046 /* If the next token is `::', that is invalid -- but sometimes
20047 people do try to write:
20051 Handle this gracefully by accepting the extra qualifier, and then
20052 issuing an error about it later if this really is a
20053 class-head. If it turns out just to be an elaborated type
20054 specifier, remain silent. */
20055 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
20056 qualified_p
= true;
20058 push_deferring_access_checks (dk_no_check
);
20060 /* Determine the name of the class. Begin by looking for an
20061 optional nested-name-specifier. */
20062 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
20063 nested_name_specifier
20064 = cp_parser_nested_name_specifier_opt (parser
,
20065 /*typename_keyword_p=*/false,
20066 /*check_dependency_p=*/false,
20068 /*is_declaration=*/false);
20069 /* If there was a nested-name-specifier, then there *must* be an
20071 if (nested_name_specifier
)
20073 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20074 /* Although the grammar says `identifier', it really means
20075 `class-name' or `template-name'. You are only allowed to
20076 define a class that has already been declared with this
20079 The proposed resolution for Core Issue 180 says that wherever
20080 you see `class T::X' you should treat `X' as a type-name.
20082 It is OK to define an inaccessible class; for example:
20084 class A { class B; };
20087 We do not know if we will see a class-name, or a
20088 template-name. We look for a class-name first, in case the
20089 class-name is a template-id; if we looked for the
20090 template-name first we would stop after the template-name. */
20091 cp_parser_parse_tentatively (parser
);
20092 type
= cp_parser_class_name (parser
,
20093 /*typename_keyword_p=*/false,
20094 /*template_keyword_p=*/false,
20096 /*check_dependency_p=*/false,
20097 /*class_head_p=*/true,
20098 /*is_declaration=*/false);
20099 /* If that didn't work, ignore the nested-name-specifier. */
20100 if (!cp_parser_parse_definitely (parser
))
20102 invalid_nested_name_p
= true;
20103 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20104 id
= cp_parser_identifier (parser
);
20105 if (id
== error_mark_node
)
20108 /* If we could not find a corresponding TYPE, treat this
20109 declaration like an unqualified declaration. */
20110 if (type
== error_mark_node
)
20111 nested_name_specifier
= NULL_TREE
;
20112 /* Otherwise, count the number of templates used in TYPE and its
20113 containing scopes. */
20118 for (scope
= TREE_TYPE (type
);
20119 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
20120 scope
= get_containing_scope (scope
))
20122 && CLASS_TYPE_P (scope
)
20123 && CLASSTYPE_TEMPLATE_INFO (scope
)
20124 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
20125 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
20126 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
20130 /* Otherwise, the identifier is optional. */
20133 /* We don't know whether what comes next is a template-id,
20134 an identifier, or nothing at all. */
20135 cp_parser_parse_tentatively (parser
);
20136 /* Check for a template-id. */
20137 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20138 id
= cp_parser_template_id (parser
,
20139 /*template_keyword_p=*/false,
20140 /*check_dependency_p=*/true,
20142 /*is_declaration=*/true);
20143 /* If that didn't work, it could still be an identifier. */
20144 if (!cp_parser_parse_definitely (parser
))
20146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20148 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20149 id
= cp_parser_identifier (parser
);
20156 template_id_p
= true;
20161 pop_deferring_access_checks ();
20165 cp_parser_check_for_invalid_template_id (parser
, id
,
20167 type_start_token
->location
);
20169 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20171 /* If it's not a `:' or a `{' then we can't really be looking at a
20172 class-head, since a class-head only appears as part of a
20173 class-specifier. We have to detect this situation before calling
20174 xref_tag, since that has irreversible side-effects. */
20175 if (!cp_parser_next_token_starts_class_definition_p (parser
))
20177 cp_parser_error (parser
, "expected %<{%> or %<:%>");
20178 type
= error_mark_node
;
20182 /* At this point, we're going ahead with the class-specifier, even
20183 if some other problem occurs. */
20184 cp_parser_commit_to_tentative_parse (parser
);
20185 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
20187 cp_parser_error (parser
,
20188 "cannot specify %<override%> for a class");
20189 type
= error_mark_node
;
20192 /* Issue the error about the overly-qualified name now. */
20195 cp_parser_error (parser
,
20196 "global qualification of class name is invalid");
20197 type
= error_mark_node
;
20200 else if (invalid_nested_name_p
)
20202 cp_parser_error (parser
,
20203 "qualified name does not name a class");
20204 type
= error_mark_node
;
20207 else if (nested_name_specifier
)
20211 /* Reject typedef-names in class heads. */
20212 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
20214 error_at (type_start_token
->location
,
20215 "invalid class name in declaration of %qD",
20221 /* Figure out in what scope the declaration is being placed. */
20222 scope
= current_scope ();
20223 /* If that scope does not contain the scope in which the
20224 class was originally declared, the program is invalid. */
20225 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
20227 if (at_namespace_scope_p ())
20228 error_at (type_start_token
->location
,
20229 "declaration of %qD in namespace %qD which does not "
20231 type
, scope
, nested_name_specifier
);
20233 error_at (type_start_token
->location
,
20234 "declaration of %qD in %qD which does not enclose %qD",
20235 type
, scope
, nested_name_specifier
);
20241 A declarator-id shall not be qualified except for the
20242 definition of a ... nested class outside of its class
20243 ... [or] the definition or explicit instantiation of a
20244 class member of a namespace outside of its namespace. */
20245 if (scope
== nested_name_specifier
)
20247 permerror (nested_name_specifier_token_start
->location
,
20248 "extra qualification not allowed");
20249 nested_name_specifier
= NULL_TREE
;
20253 /* An explicit-specialization must be preceded by "template <>". If
20254 it is not, try to recover gracefully. */
20255 if (at_namespace_scope_p ()
20256 && parser
->num_template_parameter_lists
== 0
20259 error_at (type_start_token
->location
,
20260 "an explicit specialization must be preceded by %<template <>%>");
20261 invalid_explicit_specialization_p
= true;
20262 /* Take the same action that would have been taken by
20263 cp_parser_explicit_specialization. */
20264 ++parser
->num_template_parameter_lists
;
20265 begin_specialization ();
20267 /* There must be no "return" statements between this point and the
20268 end of this function; set "type "to the correct return value and
20269 use "goto done;" to return. */
20270 /* Make sure that the right number of template parameters were
20272 if (!cp_parser_check_template_parameters (parser
, num_templates
,
20273 type_start_token
->location
,
20274 /*declarator=*/NULL
))
20276 /* If something went wrong, there is no point in even trying to
20277 process the class-definition. */
20282 /* Look up the type. */
20285 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
20286 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
20287 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
20289 error_at (type_start_token
->location
,
20290 "function template %qD redeclared as a class template", id
);
20291 type
= error_mark_node
;
20295 type
= TREE_TYPE (id
);
20296 type
= maybe_process_partial_specialization (type
);
20298 if (nested_name_specifier
)
20299 pushed_scope
= push_scope (nested_name_specifier
);
20301 else if (nested_name_specifier
)
20307 template <typename T> struct S { struct T };
20308 template <typename T> struct S<T>::T { };
20310 we will get a TYPENAME_TYPE when processing the definition of
20311 `S::T'. We need to resolve it to the actual type before we
20312 try to define it. */
20313 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
20315 class_type
= resolve_typename_type (TREE_TYPE (type
),
20316 /*only_current_p=*/false);
20317 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
20318 type
= TYPE_NAME (class_type
);
20321 cp_parser_error (parser
, "could not resolve typename type");
20322 type
= error_mark_node
;
20326 if (maybe_process_partial_specialization (TREE_TYPE (type
))
20327 == error_mark_node
)
20333 class_type
= current_class_type
;
20334 /* Enter the scope indicated by the nested-name-specifier. */
20335 pushed_scope
= push_scope (nested_name_specifier
);
20336 /* Get the canonical version of this type. */
20337 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
20338 /* Call push_template_decl if it seems like we should be defining a
20339 template either from the template headers or the type we're
20340 defining, so that we diagnose both extra and missing headers. */
20341 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20342 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
20343 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
20345 type
= push_template_decl (type
);
20346 if (type
== error_mark_node
)
20353 type
= TREE_TYPE (type
);
20354 *nested_name_specifier_p
= true;
20356 else /* The name is not a nested name. */
20358 /* If the class was unnamed, create a dummy name. */
20360 id
= make_anon_name ();
20361 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
20362 parser
->num_template_parameter_lists
);
20365 /* Indicate whether this class was declared as a `class' or as a
20367 if (TREE_CODE (type
) == RECORD_TYPE
)
20368 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
20369 cp_parser_check_class_key (class_key
, type
);
20371 /* If this type was already complete, and we see another definition,
20372 that's an error. */
20373 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
20375 error_at (type_start_token
->location
, "redefinition of %q#T",
20377 error_at (type_start_token
->location
, "previous definition of %q+#T",
20382 else if (type
== error_mark_node
)
20387 /* Apply attributes now, before any use of the class as a template
20388 argument in its base list. */
20389 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
20390 fixup_attribute_variants (type
);
20393 /* We will have entered the scope containing the class; the names of
20394 base classes should be looked up in that context. For example:
20396 struct A { struct B {}; struct C; };
20397 struct A::C : B {};
20401 /* Get the list of base-classes, if there is one. */
20402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20404 /* PR59482: enter the class scope so that base-specifiers are looked
20408 bases
= cp_parser_base_clause (parser
);
20409 /* PR59482: get out of the previously pushed class scope so that the
20410 subsequent pops pop the right thing. */
20417 /* If we're really defining a class, process the base classes.
20418 If they're invalid, fail. */
20419 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20420 && !xref_basetypes (type
, bases
))
20424 /* Leave the scope given by the nested-name-specifier. We will
20425 enter the class scope itself while processing the members. */
20427 pop_scope (pushed_scope
);
20429 if (invalid_explicit_specialization_p
)
20431 end_specialization ();
20432 --parser
->num_template_parameter_lists
;
20436 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20437 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
20438 CLASSTYPE_FINAL (type
) = 1;
20440 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20444 /* Parse a class-key.
20451 Returns the kind of class-key specified, or none_type to indicate
20454 static enum tag_types
20455 cp_parser_class_key (cp_parser
* parser
)
20458 enum tag_types tag_type
;
20460 /* Look for the class-key. */
20461 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
20465 /* Check to see if the TOKEN is a class-key. */
20466 tag_type
= cp_parser_token_is_class_key (token
);
20468 cp_parser_error (parser
, "expected class-key");
20472 /* Parse a type-parameter-key.
20474 type-parameter-key:
20480 cp_parser_type_parameter_key (cp_parser
* parser
)
20482 /* Look for the type-parameter-key. */
20483 enum tag_types tag_type
= none_type
;
20484 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20485 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
20487 cp_lexer_consume_token (parser
->lexer
);
20488 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
20489 /* typename is not allowed in a template template parameter
20490 by the standard until C++1Z. */
20491 pedwarn (token
->location
, OPT_Wpedantic
,
20492 "ISO C++ forbids typename key in template template parameter;"
20493 " use -std=c++1z or -std=gnu++1z");
20496 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
20501 /* Parse an (optional) member-specification.
20503 member-specification:
20504 member-declaration member-specification [opt]
20505 access-specifier : member-specification [opt] */
20508 cp_parser_member_specification_opt (cp_parser
* parser
)
20515 /* Peek at the next token. */
20516 token
= cp_lexer_peek_token (parser
->lexer
);
20517 /* If it's a `}', or EOF then we've seen all the members. */
20518 if (token
->type
== CPP_CLOSE_BRACE
20519 || token
->type
== CPP_EOF
20520 || token
->type
== CPP_PRAGMA_EOL
)
20523 /* See if this token is a keyword. */
20524 keyword
= token
->keyword
;
20528 case RID_PROTECTED
:
20530 /* Consume the access-specifier. */
20531 cp_lexer_consume_token (parser
->lexer
);
20532 /* Remember which access-specifier is active. */
20533 current_access_specifier
= token
->u
.value
;
20534 /* Look for the `:'. */
20535 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20539 /* Accept #pragmas at class scope. */
20540 if (token
->type
== CPP_PRAGMA
)
20542 cp_parser_pragma (parser
, pragma_member
);
20546 /* Otherwise, the next construction must be a
20547 member-declaration. */
20548 cp_parser_member_declaration (parser
);
20553 /* Parse a member-declaration.
20555 member-declaration:
20556 decl-specifier-seq [opt] member-declarator-list [opt] ;
20557 function-definition ; [opt]
20558 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20560 template-declaration
20563 member-declarator-list:
20565 member-declarator-list , member-declarator
20568 declarator pure-specifier [opt]
20569 declarator constant-initializer [opt]
20570 identifier [opt] : constant-expression
20574 member-declaration:
20575 __extension__ member-declaration
20578 declarator attributes [opt] pure-specifier [opt]
20579 declarator attributes [opt] constant-initializer [opt]
20580 identifier [opt] attributes [opt] : constant-expression
20584 member-declaration:
20585 static_assert-declaration */
20588 cp_parser_member_declaration (cp_parser
* parser
)
20590 cp_decl_specifier_seq decl_specifiers
;
20591 tree prefix_attributes
;
20593 int declares_class_or_enum
;
20595 cp_token
*token
= NULL
;
20596 cp_token
*decl_spec_token_start
= NULL
;
20597 cp_token
*initializer_token_start
= NULL
;
20598 int saved_pedantic
;
20599 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20601 /* Check for the `__extension__' keyword. */
20602 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
20605 cp_parser_member_declaration (parser
);
20606 /* Restore the old value of the PEDANTIC flag. */
20607 pedantic
= saved_pedantic
;
20612 /* Check for a template-declaration. */
20613 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20615 /* An explicit specialization here is an error condition, and we
20616 expect the specialization handler to detect and report this. */
20617 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
20618 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
20619 cp_parser_explicit_specialization (parser
);
20621 cp_parser_template_declaration (parser
, /*member_p=*/true);
20626 /* Check for a using-declaration. */
20627 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
20629 if (cxx_dialect
< cxx11
)
20631 /* Parse the using-declaration. */
20632 cp_parser_using_declaration (parser
,
20633 /*access_declaration_p=*/false);
20639 bool alias_decl_expected
;
20640 cp_parser_parse_tentatively (parser
);
20641 decl
= cp_parser_alias_declaration (parser
);
20642 /* Note that if we actually see the '=' token after the
20643 identifier, cp_parser_alias_declaration commits the
20644 tentative parse. In that case, we really expects an
20645 alias-declaration. Otherwise, we expect a using
20647 alias_decl_expected
=
20648 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
20649 cp_parser_parse_definitely (parser
);
20651 if (alias_decl_expected
)
20652 finish_member_declaration (decl
);
20654 cp_parser_using_declaration (parser
,
20655 /*access_declaration_p=*/false);
20660 /* Check for @defs. */
20661 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
20664 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
20665 ivar
= ivar_chains
;
20669 ivar
= TREE_CHAIN (member
);
20670 TREE_CHAIN (member
) = NULL_TREE
;
20671 finish_member_declaration (member
);
20676 /* If the next token is `static_assert' we have a static assertion. */
20677 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
20679 cp_parser_static_assert (parser
, /*member_p=*/true);
20683 parser
->colon_corrects_to_scope_p
= false;
20685 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20688 /* Parse the decl-specifier-seq. */
20689 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20690 cp_parser_decl_specifier_seq (parser
,
20691 CP_PARSER_FLAGS_OPTIONAL
,
20693 &declares_class_or_enum
);
20694 /* Check for an invalid type-name. */
20695 if (!decl_specifiers
.any_type_specifiers_p
20696 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20698 /* If there is no declarator, then the decl-specifier-seq should
20700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20702 /* If there was no decl-specifier-seq, and the next token is a
20703 `;', then we have something like:
20709 Each member-declaration shall declare at least one member
20710 name of the class. */
20711 if (!decl_specifiers
.any_specifiers_p
)
20713 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20714 if (!in_system_header_at (token
->location
))
20715 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20721 /* See if this declaration is a friend. */
20722 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20723 /* If there were decl-specifiers, check to see if there was
20724 a class-declaration. */
20725 type
= check_tag_decl (&decl_specifiers
,
20726 /*explicit_type_instantiation_p=*/false);
20727 /* Nested classes have already been added to the class, but
20728 a `friend' needs to be explicitly registered. */
20731 /* If the `friend' keyword was present, the friend must
20732 be introduced with a class-key. */
20733 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20734 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20735 "in C++03 a class-key must be used "
20736 "when declaring a friend");
20739 template <typename T> struct A {
20740 friend struct A<T>::B;
20743 A<T>::B will be represented by a TYPENAME_TYPE, and
20744 therefore not recognized by check_tag_decl. */
20747 type
= decl_specifiers
.type
;
20748 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20749 type
= TREE_TYPE (type
);
20751 if (!type
|| !TYPE_P (type
))
20752 error_at (decl_spec_token_start
->location
,
20753 "friend declaration does not name a class or "
20756 make_friend_class (current_class_type
, type
,
20757 /*complain=*/true);
20759 /* If there is no TYPE, an error message will already have
20761 else if (!type
|| type
== error_mark_node
)
20763 /* An anonymous aggregate has to be handled specially; such
20764 a declaration really declares a data member (with a
20765 particular type), as opposed to a nested class. */
20766 else if (ANON_AGGR_TYPE_P (type
))
20769 if (decl_specifiers
.storage_class
!= sc_none
)
20770 error_at (decl_spec_token_start
->location
,
20771 "a storage class on an anonymous aggregate "
20772 "in class scope is not allowed");
20774 /* Remove constructors and such from TYPE, now that we
20775 know it is an anonymous aggregate. */
20776 fixup_anonymous_aggr (type
);
20777 /* And make the corresponding data member. */
20778 decl
= build_decl (decl_spec_token_start
->location
,
20779 FIELD_DECL
, NULL_TREE
, type
);
20780 /* Add it to the class. */
20781 finish_member_declaration (decl
);
20784 cp_parser_check_access_in_redeclaration
20786 decl_spec_token_start
->location
);
20791 bool assume_semicolon
= false;
20793 /* Clear attributes from the decl_specifiers but keep them
20794 around as prefix attributes that apply them to the entity
20796 prefix_attributes
= decl_specifiers
.attributes
;
20797 decl_specifiers
.attributes
= NULL_TREE
;
20799 /* See if these declarations will be friends. */
20800 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20802 /* Keep going until we hit the `;' at the end of the
20804 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20806 tree attributes
= NULL_TREE
;
20807 tree first_attribute
;
20809 /* Peek at the next token. */
20810 token
= cp_lexer_peek_token (parser
->lexer
);
20812 /* Check for a bitfield declaration. */
20813 if (token
->type
== CPP_COLON
20814 || (token
->type
== CPP_NAME
20815 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20821 /* Get the name of the bitfield. Note that we cannot just
20822 check TOKEN here because it may have been invalidated by
20823 the call to cp_lexer_peek_nth_token above. */
20824 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20825 identifier
= cp_parser_identifier (parser
);
20827 identifier
= NULL_TREE
;
20829 /* Consume the `:' token. */
20830 cp_lexer_consume_token (parser
->lexer
);
20831 /* Get the width of the bitfield. */
20833 = cp_parser_constant_expression (parser
);
20835 /* Look for attributes that apply to the bitfield. */
20836 attributes
= cp_parser_attributes_opt (parser
);
20837 /* Remember which attributes are prefix attributes and
20839 first_attribute
= attributes
;
20840 /* Combine the attributes. */
20841 attributes
= chainon (prefix_attributes
, attributes
);
20843 /* Create the bitfield declaration. */
20844 decl
= grokbitfield (identifier
20845 ? make_id_declarator (NULL_TREE
,
20855 cp_declarator
*declarator
;
20857 tree asm_specification
;
20858 int ctor_dtor_or_conv_p
;
20860 /* Parse the declarator. */
20862 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20863 &ctor_dtor_or_conv_p
,
20864 /*parenthesized_p=*/NULL
,
20868 /* If something went wrong parsing the declarator, make sure
20869 that we at least consume some tokens. */
20870 if (declarator
== cp_error_declarator
)
20872 /* Skip to the end of the statement. */
20873 cp_parser_skip_to_end_of_statement (parser
);
20874 /* If the next token is not a semicolon, that is
20875 probably because we just skipped over the body of
20876 a function. So, we consume a semicolon if
20877 present, but do not issue an error message if it
20879 if (cp_lexer_next_token_is (parser
->lexer
,
20881 cp_lexer_consume_token (parser
->lexer
);
20885 if (declares_class_or_enum
& 2)
20886 cp_parser_check_for_definition_in_return_type
20887 (declarator
, decl_specifiers
.type
,
20888 decl_specifiers
.locations
[ds_type_spec
]);
20890 /* Look for an asm-specification. */
20891 asm_specification
= cp_parser_asm_specification_opt (parser
);
20892 /* Look for attributes that apply to the declaration. */
20893 attributes
= cp_parser_attributes_opt (parser
);
20894 /* Remember which attributes are prefix attributes and
20896 first_attribute
= attributes
;
20897 /* Combine the attributes. */
20898 attributes
= chainon (prefix_attributes
, attributes
);
20900 /* If it's an `=', then we have a constant-initializer or a
20901 pure-specifier. It is not correct to parse the
20902 initializer before registering the member declaration
20903 since the member declaration should be in scope while
20904 its initializer is processed. However, the rest of the
20905 front end does not yet provide an interface that allows
20906 us to handle this correctly. */
20907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20911 A pure-specifier shall be used only in the declaration of
20912 a virtual function.
20914 A member-declarator can contain a constant-initializer
20915 only if it declares a static member of integral or
20918 Therefore, if the DECLARATOR is for a function, we look
20919 for a pure-specifier; otherwise, we look for a
20920 constant-initializer. When we call `grokfield', it will
20921 perform more stringent semantics checks. */
20922 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
20923 if (function_declarator_p (declarator
)
20924 || (decl_specifiers
.type
20925 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
20926 && declarator
->kind
== cdk_id
20927 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
20928 == FUNCTION_TYPE
)))
20929 initializer
= cp_parser_pure_specifier (parser
);
20930 else if (decl_specifiers
.storage_class
!= sc_static
)
20931 initializer
= cp_parser_save_nsdmi (parser
);
20932 else if (cxx_dialect
>= cxx11
)
20935 /* Don't require a constant rvalue in C++11, since we
20936 might want a reference constant. We'll enforce
20937 constancy later. */
20938 cp_lexer_consume_token (parser
->lexer
);
20939 /* Parse the initializer. */
20940 initializer
= cp_parser_initializer_clause (parser
,
20944 /* Parse the initializer. */
20945 initializer
= cp_parser_constant_initializer (parser
);
20947 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20948 && !function_declarator_p (declarator
))
20951 if (decl_specifiers
.storage_class
!= sc_static
)
20952 initializer
= cp_parser_save_nsdmi (parser
);
20954 initializer
= cp_parser_initializer (parser
, &x
, &x
);
20956 /* Otherwise, there is no initializer. */
20958 initializer
= NULL_TREE
;
20960 /* See if we are probably looking at a function
20961 definition. We are certainly not looking at a
20962 member-declarator. Calling `grokfield' has
20963 side-effects, so we must not do it unless we are sure
20964 that we are looking at a member-declarator. */
20965 if (cp_parser_token_starts_function_definition_p
20966 (cp_lexer_peek_token (parser
->lexer
)))
20968 /* The grammar does not allow a pure-specifier to be
20969 used when a member function is defined. (It is
20970 possible that this fact is an oversight in the
20971 standard, since a pure function may be defined
20972 outside of the class-specifier. */
20973 if (initializer
&& initializer_token_start
)
20974 error_at (initializer_token_start
->location
,
20975 "pure-specifier on function-definition");
20976 decl
= cp_parser_save_member_function_body (parser
,
20980 if (parser
->fully_implicit_function_template_p
)
20981 decl
= finish_fully_implicit_template (parser
, decl
);
20982 /* If the member was not a friend, declare it here. */
20984 finish_member_declaration (decl
);
20985 /* Peek at the next token. */
20986 token
= cp_lexer_peek_token (parser
->lexer
);
20987 /* If the next token is a semicolon, consume it. */
20988 if (token
->type
== CPP_SEMICOLON
)
20989 cp_lexer_consume_token (parser
->lexer
);
20993 if (declarator
->kind
== cdk_function
)
20994 declarator
->id_loc
= token
->location
;
20995 /* Create the declaration. */
20996 decl
= grokfield (declarator
, &decl_specifiers
,
20997 initializer
, /*init_const_expr_p=*/true,
20998 asm_specification
, attributes
);
20999 if (parser
->fully_implicit_function_template_p
)
21002 finish_fully_implicit_template (parser
, 0);
21004 decl
= finish_fully_implicit_template (parser
, decl
);
21008 cp_finalize_omp_declare_simd (parser
, decl
);
21010 /* Reset PREFIX_ATTRIBUTES. */
21011 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
21012 attributes
= TREE_CHAIN (attributes
);
21014 TREE_CHAIN (attributes
) = NULL_TREE
;
21016 /* If there is any qualification still in effect, clear it
21017 now; we will be starting fresh with the next declarator. */
21018 parser
->scope
= NULL_TREE
;
21019 parser
->qualifying_scope
= NULL_TREE
;
21020 parser
->object_scope
= NULL_TREE
;
21021 /* If it's a `,', then there are more declarators. */
21022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21024 cp_lexer_consume_token (parser
->lexer
);
21025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21027 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21028 error_at (token
->location
,
21029 "stray %<,%> at end of member declaration");
21032 /* If the next token isn't a `;', then we have a parse error. */
21033 else if (cp_lexer_next_token_is_not (parser
->lexer
,
21036 /* The next token might be a ways away from where the
21037 actual semicolon is missing. Find the previous token
21038 and use that for our error position. */
21039 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21040 error_at (token
->location
,
21041 "expected %<;%> at end of member declaration");
21043 /* Assume that the user meant to provide a semicolon. If
21044 we were to cp_parser_skip_to_end_of_statement, we might
21045 skip to a semicolon inside a member function definition
21046 and issue nonsensical error messages. */
21047 assume_semicolon
= true;
21052 /* Add DECL to the list of members. */
21054 finish_member_declaration (decl
);
21056 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21057 cp_parser_save_default_args (parser
, decl
);
21058 else if (TREE_CODE (decl
) == FIELD_DECL
21059 && !DECL_C_BIT_FIELD (decl
)
21060 && DECL_INITIAL (decl
))
21061 /* Add DECL to the queue of NSDMI to be parsed later. */
21062 vec_safe_push (unparsed_nsdmis
, decl
);
21065 if (assume_semicolon
)
21070 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21072 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
21075 /* Parse a pure-specifier.
21080 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21081 Otherwise, ERROR_MARK_NODE is returned. */
21084 cp_parser_pure_specifier (cp_parser
* parser
)
21088 /* Look for the `=' token. */
21089 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21090 return error_mark_node
;
21091 /* Look for the `0' token. */
21092 token
= cp_lexer_peek_token (parser
->lexer
);
21094 if (token
->type
== CPP_EOF
21095 || token
->type
== CPP_PRAGMA_EOL
)
21096 return error_mark_node
;
21098 cp_lexer_consume_token (parser
->lexer
);
21100 /* Accept = default or = delete in c++0x mode. */
21101 if (token
->keyword
== RID_DEFAULT
21102 || token
->keyword
== RID_DELETE
)
21104 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
21105 return token
->u
.value
;
21108 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21109 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
21111 cp_parser_error (parser
,
21112 "invalid pure specifier (only %<= 0%> is allowed)");
21113 cp_parser_skip_to_end_of_statement (parser
);
21114 return error_mark_node
;
21116 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21118 error_at (token
->location
, "templates may not be %<virtual%>");
21119 return error_mark_node
;
21122 return integer_zero_node
;
21125 /* Parse a constant-initializer.
21127 constant-initializer:
21128 = constant-expression
21130 Returns a representation of the constant-expression. */
21133 cp_parser_constant_initializer (cp_parser
* parser
)
21135 /* Look for the `=' token. */
21136 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21137 return error_mark_node
;
21139 /* It is invalid to write:
21141 struct S { static const int i = { 7 }; };
21144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21146 cp_parser_error (parser
,
21147 "a brace-enclosed initializer is not allowed here");
21148 /* Consume the opening brace. */
21149 cp_lexer_consume_token (parser
->lexer
);
21150 /* Skip the initializer. */
21151 cp_parser_skip_to_closing_brace (parser
);
21152 /* Look for the trailing `}'. */
21153 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21155 return error_mark_node
;
21158 return cp_parser_constant_expression (parser
);
21161 /* Derived classes [gram.class.derived] */
21163 /* Parse a base-clause.
21166 : base-specifier-list
21168 base-specifier-list:
21169 base-specifier ... [opt]
21170 base-specifier-list , base-specifier ... [opt]
21172 Returns a TREE_LIST representing the base-classes, in the order in
21173 which they were declared. The representation of each node is as
21174 described by cp_parser_base_specifier.
21176 In the case that no bases are specified, this function will return
21177 NULL_TREE, not ERROR_MARK_NODE. */
21180 cp_parser_base_clause (cp_parser
* parser
)
21182 tree bases
= NULL_TREE
;
21184 /* Look for the `:' that begins the list. */
21185 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21187 /* Scan the base-specifier-list. */
21192 bool pack_expansion_p
= false;
21194 /* Look for the base-specifier. */
21195 base
= cp_parser_base_specifier (parser
);
21196 /* Look for the (optional) ellipsis. */
21197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21199 /* Consume the `...'. */
21200 cp_lexer_consume_token (parser
->lexer
);
21202 pack_expansion_p
= true;
21205 /* Add BASE to the front of the list. */
21206 if (base
&& base
!= error_mark_node
)
21208 if (pack_expansion_p
)
21209 /* Make this a pack expansion type. */
21210 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
21212 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
21214 TREE_CHAIN (base
) = bases
;
21218 /* Peek at the next token. */
21219 token
= cp_lexer_peek_token (parser
->lexer
);
21220 /* If it's not a comma, then the list is complete. */
21221 if (token
->type
!= CPP_COMMA
)
21223 /* Consume the `,'. */
21224 cp_lexer_consume_token (parser
->lexer
);
21227 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21228 base class had a qualified name. However, the next name that
21229 appears is certainly not qualified. */
21230 parser
->scope
= NULL_TREE
;
21231 parser
->qualifying_scope
= NULL_TREE
;
21232 parser
->object_scope
= NULL_TREE
;
21234 return nreverse (bases
);
21237 /* Parse a base-specifier.
21240 :: [opt] nested-name-specifier [opt] class-name
21241 virtual access-specifier [opt] :: [opt] nested-name-specifier
21243 access-specifier virtual [opt] :: [opt] nested-name-specifier
21246 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21247 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21248 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21249 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21252 cp_parser_base_specifier (cp_parser
* parser
)
21256 bool virtual_p
= false;
21257 bool duplicate_virtual_error_issued_p
= false;
21258 bool duplicate_access_error_issued_p
= false;
21259 bool class_scope_p
, template_p
;
21260 tree access
= access_default_node
;
21263 /* Process the optional `virtual' and `access-specifier'. */
21266 /* Peek at the next token. */
21267 token
= cp_lexer_peek_token (parser
->lexer
);
21268 /* Process `virtual'. */
21269 switch (token
->keyword
)
21272 /* If `virtual' appears more than once, issue an error. */
21273 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
21275 cp_parser_error (parser
,
21276 "%<virtual%> specified more than once in base-specified");
21277 duplicate_virtual_error_issued_p
= true;
21282 /* Consume the `virtual' token. */
21283 cp_lexer_consume_token (parser
->lexer
);
21288 case RID_PROTECTED
:
21290 /* If more than one access specifier appears, issue an
21292 if (access
!= access_default_node
21293 && !duplicate_access_error_issued_p
)
21295 cp_parser_error (parser
,
21296 "more than one access specifier in base-specified");
21297 duplicate_access_error_issued_p
= true;
21300 access
= ridpointers
[(int) token
->keyword
];
21302 /* Consume the access-specifier. */
21303 cp_lexer_consume_token (parser
->lexer
);
21312 /* It is not uncommon to see programs mechanically, erroneously, use
21313 the 'typename' keyword to denote (dependent) qualified types
21314 as base classes. */
21315 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
21317 token
= cp_lexer_peek_token (parser
->lexer
);
21318 if (!processing_template_decl
)
21319 error_at (token
->location
,
21320 "keyword %<typename%> not allowed outside of templates");
21322 error_at (token
->location
,
21323 "keyword %<typename%> not allowed in this context "
21324 "(the base class is implicitly a type)");
21325 cp_lexer_consume_token (parser
->lexer
);
21328 /* Look for the optional `::' operator. */
21329 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
21330 /* Look for the nested-name-specifier. The simplest way to
21335 The keyword `typename' is not permitted in a base-specifier or
21336 mem-initializer; in these contexts a qualified name that
21337 depends on a template-parameter is implicitly assumed to be a
21340 is to pretend that we have seen the `typename' keyword at this
21342 cp_parser_nested_name_specifier_opt (parser
,
21343 /*typename_keyword_p=*/true,
21344 /*check_dependency_p=*/true,
21346 /*is_declaration=*/true);
21347 /* If the base class is given by a qualified name, assume that names
21348 we see are type names or templates, as appropriate. */
21349 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
21350 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
21353 && cp_lexer_next_token_is_decltype (parser
->lexer
))
21354 /* DR 950 allows decltype as a base-specifier. */
21355 type
= cp_parser_decltype (parser
);
21358 /* Otherwise, look for the class-name. */
21359 type
= cp_parser_class_name (parser
,
21363 /*check_dependency_p=*/true,
21364 /*class_head_p=*/false,
21365 /*is_declaration=*/true);
21366 type
= TREE_TYPE (type
);
21369 if (type
== error_mark_node
)
21370 return error_mark_node
;
21372 return finish_base_specifier (type
, access
, virtual_p
);
21375 /* Exception handling [gram.exception] */
21377 /* Parse an (optional) noexcept-specification.
21379 noexcept-specification:
21380 noexcept ( constant-expression ) [opt]
21382 If no noexcept-specification is present, returns NULL_TREE.
21383 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21384 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21385 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21386 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21387 in which case a boolean condition is returned instead. */
21390 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
21391 bool require_constexpr
,
21392 bool* consumed_expr
,
21396 const char *saved_message
;
21398 /* Peek at the next token. */
21399 token
= cp_lexer_peek_token (parser
->lexer
);
21401 /* Is it a noexcept-specification? */
21402 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
21405 cp_lexer_consume_token (parser
->lexer
);
21407 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
21409 cp_lexer_consume_token (parser
->lexer
);
21411 if (require_constexpr
)
21413 /* Types may not be defined in an exception-specification. */
21414 saved_message
= parser
->type_definition_forbidden_message
;
21415 parser
->type_definition_forbidden_message
21416 = G_("types may not be defined in an exception-specification");
21418 expr
= cp_parser_constant_expression (parser
);
21420 /* Restore the saved message. */
21421 parser
->type_definition_forbidden_message
= saved_message
;
21425 expr
= cp_parser_expression (parser
);
21426 *consumed_expr
= true;
21429 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21433 expr
= boolean_true_node
;
21434 if (!require_constexpr
)
21435 *consumed_expr
= false;
21438 /* We cannot build a noexcept-spec right away because this will check
21439 that expr is a constexpr. */
21441 return build_noexcept_spec (expr
, tf_warning_or_error
);
21449 /* Parse an (optional) exception-specification.
21451 exception-specification:
21452 throw ( type-id-list [opt] )
21454 Returns a TREE_LIST representing the exception-specification. The
21455 TREE_VALUE of each node is a type. */
21458 cp_parser_exception_specification_opt (cp_parser
* parser
)
21462 const char *saved_message
;
21464 /* Peek at the next token. */
21465 token
= cp_lexer_peek_token (parser
->lexer
);
21467 /* Is it a noexcept-specification? */
21468 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
21470 if (type_id_list
!= NULL_TREE
)
21471 return type_id_list
;
21473 /* If it's not `throw', then there's no exception-specification. */
21474 if (!cp_parser_is_keyword (token
, RID_THROW
))
21478 /* Enable this once a lot of code has transitioned to noexcept? */
21479 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
21480 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
21481 "deprecated in C++0x; use %<noexcept%> instead");
21484 /* Consume the `throw'. */
21485 cp_lexer_consume_token (parser
->lexer
);
21487 /* Look for the `('. */
21488 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21490 /* Peek at the next token. */
21491 token
= cp_lexer_peek_token (parser
->lexer
);
21492 /* If it's not a `)', then there is a type-id-list. */
21493 if (token
->type
!= CPP_CLOSE_PAREN
)
21495 /* Types may not be defined in an exception-specification. */
21496 saved_message
= parser
->type_definition_forbidden_message
;
21497 parser
->type_definition_forbidden_message
21498 = G_("types may not be defined in an exception-specification");
21499 /* Parse the type-id-list. */
21500 type_id_list
= cp_parser_type_id_list (parser
);
21501 /* Restore the saved message. */
21502 parser
->type_definition_forbidden_message
= saved_message
;
21505 type_id_list
= empty_except_spec
;
21507 /* Look for the `)'. */
21508 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21510 return type_id_list
;
21513 /* Parse an (optional) type-id-list.
21517 type-id-list , type-id ... [opt]
21519 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21520 in the order that the types were presented. */
21523 cp_parser_type_id_list (cp_parser
* parser
)
21525 tree types
= NULL_TREE
;
21532 /* Get the next type-id. */
21533 type
= cp_parser_type_id (parser
);
21534 /* Parse the optional ellipsis. */
21535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21537 /* Consume the `...'. */
21538 cp_lexer_consume_token (parser
->lexer
);
21540 /* Turn the type into a pack expansion expression. */
21541 type
= make_pack_expansion (type
);
21543 /* Add it to the list. */
21544 types
= add_exception_specifier (types
, type
, /*complain=*/1);
21545 /* Peek at the next token. */
21546 token
= cp_lexer_peek_token (parser
->lexer
);
21547 /* If it is not a `,', we are done. */
21548 if (token
->type
!= CPP_COMMA
)
21550 /* Consume the `,'. */
21551 cp_lexer_consume_token (parser
->lexer
);
21554 return nreverse (types
);
21557 /* Parse a try-block.
21560 try compound-statement handler-seq */
21563 cp_parser_try_block (cp_parser
* parser
)
21567 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
21568 if (parser
->in_function_body
21569 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
21570 error ("%<try%> in %<constexpr%> function");
21572 try_block
= begin_try_block ();
21573 cp_parser_compound_statement (parser
, NULL
, true, false);
21574 finish_try_block (try_block
);
21575 cp_parser_handler_seq (parser
);
21576 finish_handler_sequence (try_block
);
21581 /* Parse a function-try-block.
21583 function-try-block:
21584 try ctor-initializer [opt] function-body handler-seq */
21587 cp_parser_function_try_block (cp_parser
* parser
)
21589 tree compound_stmt
;
21591 bool ctor_initializer_p
;
21593 /* Look for the `try' keyword. */
21594 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
21596 /* Let the rest of the front end know where we are. */
21597 try_block
= begin_function_try_block (&compound_stmt
);
21598 /* Parse the function-body. */
21599 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21600 (parser
, /*in_function_try_block=*/true);
21601 /* We're done with the `try' part. */
21602 finish_function_try_block (try_block
);
21603 /* Parse the handlers. */
21604 cp_parser_handler_seq (parser
);
21605 /* We're done with the handlers. */
21606 finish_function_handler_sequence (try_block
, compound_stmt
);
21608 return ctor_initializer_p
;
21611 /* Parse a handler-seq.
21614 handler handler-seq [opt] */
21617 cp_parser_handler_seq (cp_parser
* parser
)
21623 /* Parse the handler. */
21624 cp_parser_handler (parser
);
21625 /* Peek at the next token. */
21626 token
= cp_lexer_peek_token (parser
->lexer
);
21627 /* If it's not `catch' then there are no more handlers. */
21628 if (!cp_parser_is_keyword (token
, RID_CATCH
))
21633 /* Parse a handler.
21636 catch ( exception-declaration ) compound-statement */
21639 cp_parser_handler (cp_parser
* parser
)
21644 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
21645 handler
= begin_handler ();
21646 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21647 declaration
= cp_parser_exception_declaration (parser
);
21648 finish_handler_parms (declaration
, handler
);
21649 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21650 cp_parser_compound_statement (parser
, NULL
, false, false);
21651 finish_handler (handler
);
21654 /* Parse an exception-declaration.
21656 exception-declaration:
21657 type-specifier-seq declarator
21658 type-specifier-seq abstract-declarator
21662 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21663 ellipsis variant is used. */
21666 cp_parser_exception_declaration (cp_parser
* parser
)
21668 cp_decl_specifier_seq type_specifiers
;
21669 cp_declarator
*declarator
;
21670 const char *saved_message
;
21672 /* If it's an ellipsis, it's easy to handle. */
21673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21675 /* Consume the `...' token. */
21676 cp_lexer_consume_token (parser
->lexer
);
21680 /* Types may not be defined in exception-declarations. */
21681 saved_message
= parser
->type_definition_forbidden_message
;
21682 parser
->type_definition_forbidden_message
21683 = G_("types may not be defined in exception-declarations");
21685 /* Parse the type-specifier-seq. */
21686 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
21687 /*is_trailing_return=*/false,
21689 /* If it's a `)', then there is no declarator. */
21690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21693 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21694 /*ctor_dtor_or_conv_p=*/NULL
,
21695 /*parenthesized_p=*/NULL
,
21696 /*member_p=*/false,
21697 /*friend_p=*/false);
21699 /* Restore the saved message. */
21700 parser
->type_definition_forbidden_message
= saved_message
;
21702 if (!type_specifiers
.any_specifiers_p
)
21703 return error_mark_node
;
21705 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21708 /* Parse a throw-expression.
21711 throw assignment-expression [opt]
21713 Returns a THROW_EXPR representing the throw-expression. */
21716 cp_parser_throw_expression (cp_parser
* parser
)
21721 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21722 token
= cp_lexer_peek_token (parser
->lexer
);
21723 /* Figure out whether or not there is an assignment-expression
21724 following the "throw" keyword. */
21725 if (token
->type
== CPP_COMMA
21726 || token
->type
== CPP_SEMICOLON
21727 || token
->type
== CPP_CLOSE_PAREN
21728 || token
->type
== CPP_CLOSE_SQUARE
21729 || token
->type
== CPP_CLOSE_BRACE
21730 || token
->type
== CPP_COLON
)
21731 expression
= NULL_TREE
;
21733 expression
= cp_parser_assignment_expression (parser
);
21735 return build_throw (expression
);
21738 /* GNU Extensions */
21740 /* Parse an (optional) asm-specification.
21743 asm ( string-literal )
21745 If the asm-specification is present, returns a STRING_CST
21746 corresponding to the string-literal. Otherwise, returns
21750 cp_parser_asm_specification_opt (cp_parser
* parser
)
21753 tree asm_specification
;
21755 /* Peek at the next token. */
21756 token
= cp_lexer_peek_token (parser
->lexer
);
21757 /* If the next token isn't the `asm' keyword, then there's no
21758 asm-specification. */
21759 if (!cp_parser_is_keyword (token
, RID_ASM
))
21762 /* Consume the `asm' token. */
21763 cp_lexer_consume_token (parser
->lexer
);
21764 /* Look for the `('. */
21765 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21767 /* Look for the string-literal. */
21768 asm_specification
= cp_parser_string_literal (parser
, false, false);
21770 /* Look for the `)'. */
21771 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21773 return asm_specification
;
21776 /* Parse an asm-operand-list.
21780 asm-operand-list , asm-operand
21783 string-literal ( expression )
21784 [ string-literal ] string-literal ( expression )
21786 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21787 each node is the expression. The TREE_PURPOSE is itself a
21788 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21789 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21790 is a STRING_CST for the string literal before the parenthesis. Returns
21791 ERROR_MARK_NODE if any of the operands are invalid. */
21794 cp_parser_asm_operand_list (cp_parser
* parser
)
21796 tree asm_operands
= NULL_TREE
;
21797 bool invalid_operands
= false;
21801 tree string_literal
;
21805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21807 /* Consume the `[' token. */
21808 cp_lexer_consume_token (parser
->lexer
);
21809 /* Read the operand name. */
21810 name
= cp_parser_identifier (parser
);
21811 if (name
!= error_mark_node
)
21812 name
= build_string (IDENTIFIER_LENGTH (name
),
21813 IDENTIFIER_POINTER (name
));
21814 /* Look for the closing `]'. */
21815 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21819 /* Look for the string-literal. */
21820 string_literal
= cp_parser_string_literal (parser
, false, false);
21822 /* Look for the `('. */
21823 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21824 /* Parse the expression. */
21825 expression
= cp_parser_expression (parser
);
21826 /* Look for the `)'. */
21827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21829 if (name
== error_mark_node
21830 || string_literal
== error_mark_node
21831 || expression
== error_mark_node
)
21832 invalid_operands
= true;
21834 /* Add this operand to the list. */
21835 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21838 /* If the next token is not a `,', there are no more
21840 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21842 /* Consume the `,'. */
21843 cp_lexer_consume_token (parser
->lexer
);
21846 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21849 /* Parse an asm-clobber-list.
21853 asm-clobber-list , string-literal
21855 Returns a TREE_LIST, indicating the clobbers in the order that they
21856 appeared. The TREE_VALUE of each node is a STRING_CST. */
21859 cp_parser_asm_clobber_list (cp_parser
* parser
)
21861 tree clobbers
= NULL_TREE
;
21865 tree string_literal
;
21867 /* Look for the string literal. */
21868 string_literal
= cp_parser_string_literal (parser
, false, false);
21869 /* Add it to the list. */
21870 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21871 /* If the next token is not a `,', then the list is
21873 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21875 /* Consume the `,' token. */
21876 cp_lexer_consume_token (parser
->lexer
);
21882 /* Parse an asm-label-list.
21886 asm-label-list , identifier
21888 Returns a TREE_LIST, indicating the labels in the order that they
21889 appeared. The TREE_VALUE of each node is a label. */
21892 cp_parser_asm_label_list (cp_parser
* parser
)
21894 tree labels
= NULL_TREE
;
21898 tree identifier
, label
, name
;
21900 /* Look for the identifier. */
21901 identifier
= cp_parser_identifier (parser
);
21902 if (!error_operand_p (identifier
))
21904 label
= lookup_label (identifier
);
21905 if (TREE_CODE (label
) == LABEL_DECL
)
21907 TREE_USED (label
) = 1;
21908 check_goto (label
);
21909 name
= build_string (IDENTIFIER_LENGTH (identifier
),
21910 IDENTIFIER_POINTER (identifier
));
21911 labels
= tree_cons (name
, label
, labels
);
21914 /* If the next token is not a `,', then the list is
21916 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21918 /* Consume the `,' token. */
21919 cp_lexer_consume_token (parser
->lexer
);
21922 return nreverse (labels
);
21925 /* Return TRUE iff the next tokens in the stream are possibly the
21926 beginning of a GNU extension attribute. */
21929 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
21931 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
21934 /* Return TRUE iff the next tokens in the stream are possibly the
21935 beginning of a standard C++-11 attribute specifier. */
21938 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
21940 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
21943 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21944 beginning of a standard C++-11 attribute specifier. */
21947 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
21949 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21951 return (cxx_dialect
>= cxx11
21952 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
21953 || (token
->type
== CPP_OPEN_SQUARE
21954 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
21955 && token
->type
== CPP_OPEN_SQUARE
)));
21958 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21959 beginning of a GNU extension attribute. */
21962 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
21964 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21966 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
21969 /* Return true iff the next tokens can be the beginning of either a
21970 GNU attribute list, or a standard C++11 attribute sequence. */
21973 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
21975 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
21976 || cp_next_tokens_can_be_std_attribute_p (parser
));
21979 /* Return true iff the next Nth tokens can be the beginning of either
21980 a GNU attribute list, or a standard C++11 attribute sequence. */
21983 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
21985 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
21986 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
21989 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21990 of GNU attributes, or return NULL. */
21993 cp_parser_attributes_opt (cp_parser
*parser
)
21995 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
21996 return cp_parser_gnu_attributes_opt (parser
);
21997 return cp_parser_std_attribute_spec_seq (parser
);
22000 #define CILK_SIMD_FN_CLAUSE_MASK \
22001 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22002 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22003 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22004 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22005 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22007 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22008 vector [(<clauses>)] */
22011 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
22013 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
22014 cp_token
*token
= v_token
;
22017 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
22018 parser
->cilk_simd_fn_info
->error_seen
= false;
22019 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
22020 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
22022 int paren_scope
= 0;
22023 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22025 cp_lexer_consume_token (parser
->lexer
);
22026 v_token
= cp_lexer_peek_token (parser
->lexer
);
22029 while (paren_scope
> 0)
22031 token
= cp_lexer_peek_token (parser
->lexer
);
22032 if (token
->type
== CPP_OPEN_PAREN
)
22034 else if (token
->type
== CPP_CLOSE_PAREN
)
22036 /* Do not push the last ')' */
22037 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
22038 cp_lexer_consume_token (parser
->lexer
);
22041 token
->type
= CPP_PRAGMA_EOL
;
22042 parser
->lexer
->next_token
= token
;
22043 cp_lexer_consume_token (parser
->lexer
);
22045 struct cp_token_cache
*cp
22046 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
22047 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
22050 /* Parse an (optional) series of attributes.
22053 attributes attribute
22056 __attribute__ (( attribute-list [opt] ))
22058 The return value is as for cp_parser_gnu_attribute_list. */
22061 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
22063 tree attributes
= NULL_TREE
;
22068 tree attribute_list
;
22071 /* Peek at the next token. */
22072 token
= cp_lexer_peek_token (parser
->lexer
);
22073 /* If it's not `__attribute__', then we're done. */
22074 if (token
->keyword
!= RID_ATTRIBUTE
)
22077 /* Consume the `__attribute__' keyword. */
22078 cp_lexer_consume_token (parser
->lexer
);
22079 /* Look for the two `(' tokens. */
22080 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22081 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22083 /* Peek at the next token. */
22084 token
= cp_lexer_peek_token (parser
->lexer
);
22085 if (token
->type
!= CPP_CLOSE_PAREN
)
22086 /* Parse the attribute-list. */
22087 attribute_list
= cp_parser_gnu_attribute_list (parser
);
22089 /* If the next token is a `)', then there is no attribute
22091 attribute_list
= NULL
;
22093 /* Look for the two `)' tokens. */
22094 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22096 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22099 cp_parser_skip_to_end_of_statement (parser
);
22101 /* Add these new attributes to the list. */
22102 attributes
= chainon (attributes
, attribute_list
);
22108 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22109 "__vector" or "__vector__." */
22112 is_cilkplus_vector_p (tree name
)
22114 if (flag_cilkplus
&& is_attribute_p ("vector", name
))
22119 /* Parse a GNU attribute-list.
22123 attribute-list , attribute
22127 identifier ( identifier )
22128 identifier ( identifier , expression-list )
22129 identifier ( expression-list )
22131 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22132 to an attribute. The TREE_PURPOSE of each node is the identifier
22133 indicating which attribute is in use. The TREE_VALUE represents
22134 the arguments, if any. */
22137 cp_parser_gnu_attribute_list (cp_parser
* parser
)
22139 tree attribute_list
= NULL_TREE
;
22140 bool save_translate_strings_p
= parser
->translate_strings_p
;
22142 parser
->translate_strings_p
= false;
22149 /* Look for the identifier. We also allow keywords here; for
22150 example `__attribute__ ((const))' is legal. */
22151 token
= cp_lexer_peek_token (parser
->lexer
);
22152 if (token
->type
== CPP_NAME
22153 || token
->type
== CPP_KEYWORD
)
22155 tree arguments
= NULL_TREE
;
22157 /* Consume the token, but save it since we need it for the
22158 SIMD enabled function parsing. */
22159 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
22161 /* Save away the identifier that indicates which attribute
22163 identifier
= (token
->type
== CPP_KEYWORD
)
22164 /* For keywords, use the canonical spelling, not the
22165 parsed identifier. */
22166 ? ridpointers
[(int) token
->keyword
]
22167 : id_token
->u
.value
;
22169 attribute
= build_tree_list (identifier
, NULL_TREE
);
22171 /* Peek at the next token. */
22172 token
= cp_lexer_peek_token (parser
->lexer
);
22173 /* If it's an `(', then parse the attribute arguments. */
22174 if (token
->type
== CPP_OPEN_PAREN
)
22176 vec
<tree
, va_gc
> *vec
;
22177 int attr_flag
= (attribute_takes_identifier_p (identifier
)
22178 ? id_attr
: normal_attr
);
22179 if (is_cilkplus_vector_p (identifier
))
22181 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22185 vec
= cp_parser_parenthesized_expression_list
22186 (parser
, attr_flag
, /*cast_p=*/false,
22187 /*allow_expansion_p=*/false,
22188 /*non_constant_p=*/NULL
);
22190 arguments
= error_mark_node
;
22193 arguments
= build_tree_list_vec (vec
);
22194 release_tree_vector (vec
);
22196 /* Save the arguments away. */
22197 TREE_VALUE (attribute
) = arguments
;
22199 else if (is_cilkplus_vector_p (identifier
))
22201 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22205 if (arguments
!= error_mark_node
)
22207 /* Add this attribute to the list. */
22208 TREE_CHAIN (attribute
) = attribute_list
;
22209 attribute_list
= attribute
;
22212 token
= cp_lexer_peek_token (parser
->lexer
);
22214 /* Now, look for more attributes. If the next token isn't a
22215 `,', we're done. */
22216 if (token
->type
!= CPP_COMMA
)
22219 /* Consume the comma and keep going. */
22220 cp_lexer_consume_token (parser
->lexer
);
22222 parser
->translate_strings_p
= save_translate_strings_p
;
22224 /* We built up the list in reverse order. */
22225 return nreverse (attribute_list
);
22228 /* Parse a standard C++11 attribute.
22230 The returned representation is a TREE_LIST which TREE_PURPOSE is
22231 the scoped name of the attribute, and the TREE_VALUE is its
22234 Note that the scoped name of the attribute is itself a TREE_LIST
22235 which TREE_PURPOSE is the namespace of the attribute, and
22236 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22237 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22238 and which TREE_PURPOSE is directly the attribute name.
22240 Clients of the attribute code should use get_attribute_namespace
22241 and get_attribute_name to get the actual namespace and name of
22242 attributes, regardless of their being GNU or C++11 attributes.
22245 attribute-token attribute-argument-clause [opt]
22249 attribute-scoped-token
22251 attribute-scoped-token:
22252 attribute-namespace :: identifier
22254 attribute-namespace:
22257 attribute-argument-clause:
22258 ( balanced-token-seq )
22260 balanced-token-seq:
22261 balanced-token [opt]
22262 balanced-token-seq balanced-token
22265 ( balanced-token-seq )
22266 [ balanced-token-seq ]
22267 { balanced-token-seq }. */
22270 cp_parser_std_attribute (cp_parser
*parser
)
22272 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
22275 /* First, parse name of the the attribute, a.k.a
22276 attribute-token. */
22278 token
= cp_lexer_peek_token (parser
->lexer
);
22279 if (token
->type
== CPP_NAME
)
22280 attr_id
= token
->u
.value
;
22281 else if (token
->type
== CPP_KEYWORD
)
22282 attr_id
= ridpointers
[(int) token
->keyword
];
22283 else if (token
->flags
& NAMED_OP
)
22284 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
22286 if (attr_id
== NULL_TREE
)
22289 cp_lexer_consume_token (parser
->lexer
);
22291 token
= cp_lexer_peek_token (parser
->lexer
);
22292 if (token
->type
== CPP_SCOPE
)
22294 /* We are seeing a scoped attribute token. */
22296 cp_lexer_consume_token (parser
->lexer
);
22299 token
= cp_lexer_consume_token (parser
->lexer
);
22300 if (token
->type
== CPP_NAME
)
22301 attr_id
= token
->u
.value
;
22302 else if (token
->type
== CPP_KEYWORD
)
22303 attr_id
= ridpointers
[(int) token
->keyword
];
22306 error_at (token
->location
,
22307 "expected an identifier for the attribute name");
22308 return error_mark_node
;
22310 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
22312 token
= cp_lexer_peek_token (parser
->lexer
);
22316 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
22318 /* C++11 noreturn attribute is equivalent to GNU's. */
22319 if (is_attribute_p ("noreturn", attr_id
))
22320 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22321 /* C++14 deprecated attribute is equivalent to GNU's. */
22322 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
22324 if (cxx_dialect
== cxx11
)
22325 pedwarn (token
->location
, OPT_Wpedantic
,
22326 "%<deprecated%> is a C++14 feature;"
22327 " use %<gnu::deprecated%>");
22328 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22332 /* Now parse the optional argument clause of the attribute. */
22334 if (token
->type
!= CPP_OPEN_PAREN
)
22338 vec
<tree
, va_gc
> *vec
;
22339 int attr_flag
= normal_attr
;
22341 if (attr_ns
== get_identifier ("gnu")
22342 && attribute_takes_identifier_p (attr_id
))
22343 /* A GNU attribute that takes an identifier in parameter. */
22344 attr_flag
= id_attr
;
22346 vec
= cp_parser_parenthesized_expression_list
22347 (parser
, attr_flag
, /*cast_p=*/false,
22348 /*allow_expansion_p=*/true,
22349 /*non_constant_p=*/NULL
);
22351 arguments
= error_mark_node
;
22354 arguments
= build_tree_list_vec (vec
);
22355 release_tree_vector (vec
);
22358 if (arguments
== error_mark_node
)
22359 attribute
= error_mark_node
;
22361 TREE_VALUE (attribute
) = arguments
;
22367 /* Parse a list of standard C++-11 attributes.
22371 attribute-list , attribute[opt]
22373 attribute-list , attribute ...
22377 cp_parser_std_attribute_list (cp_parser
*parser
)
22379 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
22380 cp_token
*token
= NULL
;
22384 attribute
= cp_parser_std_attribute (parser
);
22385 if (attribute
== error_mark_node
)
22387 if (attribute
!= NULL_TREE
)
22389 TREE_CHAIN (attribute
) = attributes
;
22390 attributes
= attribute
;
22392 token
= cp_lexer_peek_token (parser
->lexer
);
22393 if (token
->type
!= CPP_COMMA
)
22395 cp_lexer_consume_token (parser
->lexer
);
22397 attributes
= nreverse (attributes
);
22401 /* Parse a standard C++-11 attribute specifier.
22403 attribute-specifier:
22404 [ [ attribute-list ] ]
22405 alignment-specifier
22407 alignment-specifier:
22408 alignas ( type-id ... [opt] )
22409 alignas ( alignment-expression ... [opt] ). */
22412 cp_parser_std_attribute_spec (cp_parser
*parser
)
22414 tree attributes
= NULL_TREE
;
22415 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22417 if (token
->type
== CPP_OPEN_SQUARE
22418 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
22420 cp_lexer_consume_token (parser
->lexer
);
22421 cp_lexer_consume_token (parser
->lexer
);
22423 attributes
= cp_parser_std_attribute_list (parser
);
22425 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
22426 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
22427 cp_parser_skip_to_end_of_statement (parser
);
22429 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22430 when we are sure that we have actually parsed them. */
22431 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22437 /* Look for an alignment-specifier. */
22439 token
= cp_lexer_peek_token (parser
->lexer
);
22441 if (token
->type
!= CPP_KEYWORD
22442 || token
->keyword
!= RID_ALIGNAS
)
22445 cp_lexer_consume_token (parser
->lexer
);
22446 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22448 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
22450 cp_parser_error (parser
, "expected %<(%>");
22451 return error_mark_node
;
22454 cp_parser_parse_tentatively (parser
);
22455 alignas_expr
= cp_parser_type_id (parser
);
22457 if (!cp_parser_parse_definitely (parser
))
22459 gcc_assert (alignas_expr
== error_mark_node
22460 || alignas_expr
== NULL_TREE
);
22463 cp_parser_assignment_expression (parser
);
22464 if (alignas_expr
== error_mark_node
)
22465 cp_parser_skip_to_end_of_statement (parser
);
22466 if (alignas_expr
== NULL_TREE
22467 || alignas_expr
== error_mark_node
)
22468 return alignas_expr
;
22471 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
22473 cp_parser_error (parser
, "expected %<)%>");
22474 return error_mark_node
;
22477 alignas_expr
= cxx_alignas_expr (alignas_expr
);
22479 /* Build the C++-11 representation of an 'aligned'
22482 build_tree_list (build_tree_list (get_identifier ("gnu"),
22483 get_identifier ("aligned")),
22484 build_tree_list (NULL_TREE
, alignas_expr
));
22490 /* Parse a standard C++-11 attribute-specifier-seq.
22492 attribute-specifier-seq:
22493 attribute-specifier-seq [opt] attribute-specifier
22497 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
22499 tree attr_specs
= NULL
;
22503 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
22504 if (attr_spec
== NULL_TREE
)
22506 if (attr_spec
== error_mark_node
)
22507 return error_mark_node
;
22509 TREE_CHAIN (attr_spec
) = attr_specs
;
22510 attr_specs
= attr_spec
;
22513 attr_specs
= nreverse (attr_specs
);
22517 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22518 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22519 current value of the PEDANTIC flag, regardless of whether or not
22520 the `__extension__' keyword is present. The caller is responsible
22521 for restoring the value of the PEDANTIC flag. */
22524 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
22526 /* Save the old value of the PEDANTIC flag. */
22527 *saved_pedantic
= pedantic
;
22529 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
22531 /* Consume the `__extension__' token. */
22532 cp_lexer_consume_token (parser
->lexer
);
22533 /* We're not being pedantic while the `__extension__' keyword is
22543 /* Parse a label declaration.
22546 __label__ label-declarator-seq ;
22548 label-declarator-seq:
22549 identifier , label-declarator-seq
22553 cp_parser_label_declaration (cp_parser
* parser
)
22555 /* Look for the `__label__' keyword. */
22556 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
22562 /* Look for an identifier. */
22563 identifier
= cp_parser_identifier (parser
);
22564 /* If we failed, stop. */
22565 if (identifier
== error_mark_node
)
22567 /* Declare it as a label. */
22568 finish_label_decl (identifier
);
22569 /* If the next token is a `;', stop. */
22570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22572 /* Look for the `,' separating the label declarations. */
22573 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
22576 /* Look for the final `;'. */
22577 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22580 /* Support Functions */
22582 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22583 NAME should have one of the representations used for an
22584 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22585 is returned. If PARSER->SCOPE is a dependent type, then a
22586 SCOPE_REF is returned.
22588 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22589 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22590 was formed. Abstractly, such entities should not be passed to this
22591 function, because they do not need to be looked up, but it is
22592 simpler to check for this special case here, rather than at the
22595 In cases not explicitly covered above, this function returns a
22596 DECL, OVERLOAD, or baselink representing the result of the lookup.
22597 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22600 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22601 (e.g., "struct") that was used. In that case bindings that do not
22602 refer to types are ignored.
22604 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22607 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22610 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22613 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22614 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22615 NULL_TREE otherwise. */
22618 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
22619 enum tag_types tag_type
,
22622 bool check_dependency
,
22623 tree
*ambiguous_decls
,
22624 location_t name_location
)
22627 tree object_type
= parser
->context
->object_type
;
22629 /* Assume that the lookup will be unambiguous. */
22630 if (ambiguous_decls
)
22631 *ambiguous_decls
= NULL_TREE
;
22633 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22634 no longer valid. Note that if we are parsing tentatively, and
22635 the parse fails, OBJECT_TYPE will be automatically restored. */
22636 parser
->context
->object_type
= NULL_TREE
;
22638 if (name
== error_mark_node
)
22639 return error_mark_node
;
22641 /* A template-id has already been resolved; there is no lookup to
22643 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
22645 if (BASELINK_P (name
))
22647 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
22648 == TEMPLATE_ID_EXPR
);
22652 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22653 it should already have been checked to make sure that the name
22654 used matches the type being destroyed. */
22655 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
22659 /* Figure out to which type this destructor applies. */
22661 type
= parser
->scope
;
22662 else if (object_type
)
22663 type
= object_type
;
22665 type
= current_class_type
;
22666 /* If that's not a class type, there is no destructor. */
22667 if (!type
|| !CLASS_TYPE_P (type
))
22668 return error_mark_node
;
22669 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
22670 lazily_declare_fn (sfk_destructor
, type
);
22671 if (!CLASSTYPE_DESTRUCTORS (type
))
22672 return error_mark_node
;
22673 /* If it was a class type, return the destructor. */
22674 return CLASSTYPE_DESTRUCTORS (type
);
22677 /* By this point, the NAME should be an ordinary identifier. If
22678 the id-expression was a qualified name, the qualifying scope is
22679 stored in PARSER->SCOPE at this point. */
22680 gcc_assert (identifier_p (name
));
22682 /* Perform the lookup. */
22687 if (parser
->scope
== error_mark_node
)
22688 return error_mark_node
;
22690 /* If the SCOPE is dependent, the lookup must be deferred until
22691 the template is instantiated -- unless we are explicitly
22692 looking up names in uninstantiated templates. Even then, we
22693 cannot look up the name if the scope is not a class type; it
22694 might, for example, be a template type parameter. */
22695 dependent_p
= (TYPE_P (parser
->scope
)
22696 && dependent_scope_p (parser
->scope
));
22697 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
22699 /* Defer lookup. */
22700 decl
= error_mark_node
;
22703 tree pushed_scope
= NULL_TREE
;
22705 /* If PARSER->SCOPE is a dependent type, then it must be a
22706 class type, and we must not be checking dependencies;
22707 otherwise, we would have processed this lookup above. So
22708 that PARSER->SCOPE is not considered a dependent base by
22709 lookup_member, we must enter the scope here. */
22711 pushed_scope
= push_scope (parser
->scope
);
22713 /* If the PARSER->SCOPE is a template specialization, it
22714 may be instantiated during name lookup. In that case,
22715 errors may be issued. Even if we rollback the current
22716 tentative parse, those errors are valid. */
22717 decl
= lookup_qualified_name (parser
->scope
, name
,
22718 tag_type
!= none_type
,
22719 /*complain=*/true);
22721 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22722 lookup result and the nested-name-specifier nominates a class C:
22723 * if the name specified after the nested-name-specifier, when
22724 looked up in C, is the injected-class-name of C (Clause 9), or
22725 * if the name specified after the nested-name-specifier is the
22726 same as the identifier or the simple-template-id's template-
22727 name in the last component of the nested-name-specifier,
22728 the name is instead considered to name the constructor of
22729 class C. [ Note: for example, the constructor is not an
22730 acceptable lookup result in an elaborated-type-specifier so
22731 the constructor would not be used in place of the
22732 injected-class-name. --end note ] Such a constructor name
22733 shall be used only in the declarator-id of a declaration that
22734 names a constructor or in a using-declaration. */
22735 if (tag_type
== none_type
22736 && DECL_SELF_REFERENCE_P (decl
)
22737 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
22738 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
22739 tag_type
!= none_type
,
22740 /*complain=*/true);
22742 /* If we have a single function from a using decl, pull it out. */
22743 if (TREE_CODE (decl
) == OVERLOAD
22744 && !really_overloaded_fn (decl
))
22745 decl
= OVL_FUNCTION (decl
);
22748 pop_scope (pushed_scope
);
22751 /* If the scope is a dependent type and either we deferred lookup or
22752 we did lookup but didn't find the name, rememeber the name. */
22753 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
22754 && dependent_type_p (parser
->scope
))
22760 /* The resolution to Core Issue 180 says that `struct
22761 A::B' should be considered a type-name, even if `A'
22763 type
= make_typename_type (parser
->scope
, name
, tag_type
,
22764 /*complain=*/tf_error
);
22765 if (type
!= error_mark_node
)
22766 decl
= TYPE_NAME (type
);
22768 else if (is_template
22769 && (cp_parser_next_token_ends_template_argument_p (parser
)
22770 || cp_lexer_next_token_is (parser
->lexer
,
22772 decl
= make_unbound_class_template (parser
->scope
,
22774 /*complain=*/tf_error
);
22776 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22777 parser
->scope
, name
,
22780 parser
->qualifying_scope
= parser
->scope
;
22781 parser
->object_scope
= NULL_TREE
;
22783 else if (object_type
)
22785 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22786 OBJECT_TYPE is not a class. */
22787 if (CLASS_TYPE_P (object_type
))
22788 /* If the OBJECT_TYPE is a template specialization, it may
22789 be instantiated during name lookup. In that case, errors
22790 may be issued. Even if we rollback the current tentative
22791 parse, those errors are valid. */
22792 decl
= lookup_member (object_type
,
22795 tag_type
!= none_type
,
22796 tf_warning_or_error
);
22801 /* Look it up in the enclosing context. */
22802 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22804 /*block_p=*/true, is_namespace
, 0);
22805 parser
->object_scope
= object_type
;
22806 parser
->qualifying_scope
= NULL_TREE
;
22810 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22812 /*block_p=*/true, is_namespace
, 0);
22813 parser
->qualifying_scope
= NULL_TREE
;
22814 parser
->object_scope
= NULL_TREE
;
22817 /* If the lookup failed, let our caller know. */
22818 if (!decl
|| decl
== error_mark_node
)
22819 return error_mark_node
;
22821 /* Pull out the template from an injected-class-name (or multiple). */
22823 decl
= maybe_get_template_decl_from_type_decl (decl
);
22825 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22826 if (TREE_CODE (decl
) == TREE_LIST
)
22828 if (ambiguous_decls
)
22829 *ambiguous_decls
= decl
;
22830 /* The error message we have to print is too complicated for
22831 cp_parser_error, so we incorporate its actions directly. */
22832 if (!cp_parser_simulate_error (parser
))
22834 error_at (name_location
, "reference to %qD is ambiguous",
22836 print_candidates (decl
);
22838 return error_mark_node
;
22841 gcc_assert (DECL_P (decl
)
22842 || TREE_CODE (decl
) == OVERLOAD
22843 || TREE_CODE (decl
) == SCOPE_REF
22844 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
22845 || BASELINK_P (decl
));
22847 /* If we have resolved the name of a member declaration, check to
22848 see if the declaration is accessible. When the name resolves to
22849 set of overloaded functions, accessibility is checked when
22850 overload resolution is done.
22852 During an explicit instantiation, access is not checked at all,
22853 as per [temp.explicit]. */
22855 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
22857 maybe_record_typedef_use (decl
);
22862 /* Like cp_parser_lookup_name, but for use in the typical case where
22863 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22864 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22867 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
22869 return cp_parser_lookup_name (parser
, name
,
22871 /*is_template=*/false,
22872 /*is_namespace=*/false,
22873 /*check_dependency=*/true,
22874 /*ambiguous_decls=*/NULL
,
22878 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22879 the current context, return the TYPE_DECL. If TAG_NAME_P is
22880 true, the DECL indicates the class being defined in a class-head,
22881 or declared in an elaborated-type-specifier.
22883 Otherwise, return DECL. */
22886 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22888 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22889 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22892 template <typename T> struct B;
22895 template <typename T> struct A::B {};
22897 Similarly, in an elaborated-type-specifier:
22899 namespace N { struct X{}; }
22902 template <typename T> friend struct N::X;
22905 However, if the DECL refers to a class type, and we are in
22906 the scope of the class, then the name lookup automatically
22907 finds the TYPE_DECL created by build_self_reference rather
22908 than a TEMPLATE_DECL. For example, in:
22910 template <class T> struct S {
22914 there is no need to handle such case. */
22916 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
22917 return DECL_TEMPLATE_RESULT (decl
);
22922 /* If too many, or too few, template-parameter lists apply to the
22923 declarator, issue an error message. Returns TRUE if all went well,
22924 and FALSE otherwise. */
22927 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
22928 cp_declarator
*declarator
,
22929 location_t declarator_location
)
22931 switch (declarator
->kind
)
22935 unsigned num_templates
= 0;
22936 tree scope
= declarator
->u
.id
.qualifying_scope
;
22939 num_templates
= num_template_headers_for_class (scope
);
22940 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
22941 == TEMPLATE_ID_EXPR
)
22942 /* If the DECLARATOR has the form `X<y>' then it uses one
22943 additional level of template parameters. */
22946 return cp_parser_check_template_parameters
22947 (parser
, num_templates
, declarator_location
, declarator
);
22953 case cdk_reference
:
22955 return (cp_parser_check_declarator_template_parameters
22956 (parser
, declarator
->declarator
, declarator_location
));
22962 gcc_unreachable ();
22967 /* NUM_TEMPLATES were used in the current declaration. If that is
22968 invalid, return FALSE and issue an error messages. Otherwise,
22969 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22970 declarator and we can print more accurate diagnostics. */
22973 cp_parser_check_template_parameters (cp_parser
* parser
,
22974 unsigned num_templates
,
22975 location_t location
,
22976 cp_declarator
*declarator
)
22978 /* If there are the same number of template classes and parameter
22979 lists, that's OK. */
22980 if (parser
->num_template_parameter_lists
== num_templates
)
22982 /* If there are more, but only one more, then we are referring to a
22983 member template. That's OK too. */
22984 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
22986 /* If there are more template classes than parameter lists, we have
22989 template <class T> void S<T>::R<T>::f (); */
22990 if (parser
->num_template_parameter_lists
< num_templates
)
22992 if (declarator
&& !current_function_decl
)
22993 error_at (location
, "specializing member %<%T::%E%> "
22994 "requires %<template<>%> syntax",
22995 declarator
->u
.id
.qualifying_scope
,
22996 declarator
->u
.id
.unqualified_name
);
22997 else if (declarator
)
22998 error_at (location
, "invalid declaration of %<%T::%E%>",
22999 declarator
->u
.id
.qualifying_scope
,
23000 declarator
->u
.id
.unqualified_name
);
23002 error_at (location
, "too few template-parameter-lists");
23005 /* Otherwise, there are too many template parameter lists. We have
23008 template <class T> template <class U> void S::f(); */
23009 error_at (location
, "too many template-parameter-lists");
23013 /* Parse an optional `::' token indicating that the following name is
23014 from the global namespace. If so, PARSER->SCOPE is set to the
23015 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23016 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23017 Returns the new value of PARSER->SCOPE, if the `::' token is
23018 present, and NULL_TREE otherwise. */
23021 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
23025 /* Peek at the next token. */
23026 token
= cp_lexer_peek_token (parser
->lexer
);
23027 /* If we're looking at a `::' token then we're starting from the
23028 global namespace, not our current location. */
23029 if (token
->type
== CPP_SCOPE
)
23031 /* Consume the `::' token. */
23032 cp_lexer_consume_token (parser
->lexer
);
23033 /* Set the SCOPE so that we know where to start the lookup. */
23034 parser
->scope
= global_namespace
;
23035 parser
->qualifying_scope
= global_namespace
;
23036 parser
->object_scope
= NULL_TREE
;
23038 return parser
->scope
;
23040 else if (!current_scope_valid_p
)
23042 parser
->scope
= NULL_TREE
;
23043 parser
->qualifying_scope
= NULL_TREE
;
23044 parser
->object_scope
= NULL_TREE
;
23050 /* Returns TRUE if the upcoming token sequence is the start of a
23051 constructor declarator. If FRIEND_P is true, the declarator is
23052 preceded by the `friend' specifier. */
23055 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
23057 bool constructor_p
;
23058 bool outside_class_specifier_p
;
23059 tree nested_name_specifier
;
23060 cp_token
*next_token
;
23062 /* The common case is that this is not a constructor declarator, so
23063 try to avoid doing lots of work if at all possible. It's not
23064 valid declare a constructor at function scope. */
23065 if (parser
->in_function_body
)
23067 /* And only certain tokens can begin a constructor declarator. */
23068 next_token
= cp_lexer_peek_token (parser
->lexer
);
23069 if (next_token
->type
!= CPP_NAME
23070 && next_token
->type
!= CPP_SCOPE
23071 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
23072 && next_token
->type
!= CPP_TEMPLATE_ID
)
23075 /* Parse tentatively; we are going to roll back all of the tokens
23077 cp_parser_parse_tentatively (parser
);
23078 /* Assume that we are looking at a constructor declarator. */
23079 constructor_p
= true;
23081 /* Look for the optional `::' operator. */
23082 cp_parser_global_scope_opt (parser
,
23083 /*current_scope_valid_p=*/false);
23084 /* Look for the nested-name-specifier. */
23085 nested_name_specifier
23086 = (cp_parser_nested_name_specifier_opt (parser
,
23087 /*typename_keyword_p=*/false,
23088 /*check_dependency_p=*/false,
23090 /*is_declaration=*/false));
23092 outside_class_specifier_p
= (!at_class_scope_p ()
23093 || !TYPE_BEING_DEFINED (current_class_type
)
23096 /* Outside of a class-specifier, there must be a
23097 nested-name-specifier. */
23098 if (!nested_name_specifier
&& outside_class_specifier_p
)
23099 constructor_p
= false;
23100 else if (nested_name_specifier
== error_mark_node
)
23101 constructor_p
= false;
23103 /* If we have a class scope, this is easy; DR 147 says that S::S always
23104 names the constructor, and no other qualified name could. */
23105 if (constructor_p
&& nested_name_specifier
23106 && CLASS_TYPE_P (nested_name_specifier
))
23108 tree id
= cp_parser_unqualified_id (parser
,
23109 /*template_keyword_p=*/false,
23110 /*check_dependency_p=*/false,
23111 /*declarator_p=*/true,
23112 /*optional_p=*/false);
23113 if (is_overloaded_fn (id
))
23114 id
= DECL_NAME (get_first_fn (id
));
23115 if (!constructor_name_p (id
, nested_name_specifier
))
23116 constructor_p
= false;
23118 /* If we still think that this might be a constructor-declarator,
23119 look for a class-name. */
23120 else if (constructor_p
)
23124 template <typename T> struct S {
23128 we must recognize that the nested `S' names a class. */
23130 type_decl
= cp_parser_class_name (parser
,
23131 /*typename_keyword_p=*/false,
23132 /*template_keyword_p=*/false,
23134 /*check_dependency_p=*/false,
23135 /*class_head_p=*/false,
23136 /*is_declaration=*/false);
23137 /* If there was no class-name, then this is not a constructor.
23138 Otherwise, if we are in a class-specifier and we aren't
23139 handling a friend declaration, check that its type matches
23140 current_class_type (c++/38313). Note: error_mark_node
23141 is left alone for error recovery purposes. */
23142 constructor_p
= (!cp_parser_error_occurred (parser
)
23143 && (outside_class_specifier_p
23144 || type_decl
== error_mark_node
23145 || same_type_p (current_class_type
,
23146 TREE_TYPE (type_decl
))));
23148 /* If we're still considering a constructor, we have to see a `(',
23149 to begin the parameter-declaration-clause, followed by either a
23150 `)', an `...', or a decl-specifier. We need to check for a
23151 type-specifier to avoid being fooled into thinking that:
23155 is a constructor. (It is actually a function named `f' that
23156 takes one parameter (of type `int') and returns a value of type
23159 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23160 constructor_p
= false;
23163 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
23164 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
23165 /* A parameter declaration begins with a decl-specifier,
23166 which is either the "attribute" keyword, a storage class
23167 specifier, or (usually) a type-specifier. */
23168 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
23171 tree pushed_scope
= NULL_TREE
;
23172 unsigned saved_num_template_parameter_lists
;
23174 /* Names appearing in the type-specifier should be looked up
23175 in the scope of the class. */
23176 if (current_class_type
)
23180 type
= TREE_TYPE (type_decl
);
23181 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23183 type
= resolve_typename_type (type
,
23184 /*only_current_p=*/false);
23185 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23187 cp_parser_abort_tentative_parse (parser
);
23191 pushed_scope
= push_scope (type
);
23194 /* Inside the constructor parameter list, surrounding
23195 template-parameter-lists do not apply. */
23196 saved_num_template_parameter_lists
23197 = parser
->num_template_parameter_lists
;
23198 parser
->num_template_parameter_lists
= 0;
23200 /* Look for the type-specifier. */
23201 cp_parser_type_specifier (parser
,
23202 CP_PARSER_FLAGS_NONE
,
23203 /*decl_specs=*/NULL
,
23204 /*is_declarator=*/true,
23205 /*declares_class_or_enum=*/NULL
,
23206 /*is_cv_qualifier=*/NULL
);
23208 parser
->num_template_parameter_lists
23209 = saved_num_template_parameter_lists
;
23211 /* Leave the scope of the class. */
23213 pop_scope (pushed_scope
);
23215 constructor_p
= !cp_parser_error_occurred (parser
);
23219 /* We did not really want to consume any tokens. */
23220 cp_parser_abort_tentative_parse (parser
);
23222 return constructor_p
;
23225 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23226 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23227 they must be performed once we are in the scope of the function.
23229 Returns the function defined. */
23232 cp_parser_function_definition_from_specifiers_and_declarator
23233 (cp_parser
* parser
,
23234 cp_decl_specifier_seq
*decl_specifiers
,
23236 const cp_declarator
*declarator
)
23241 /* Begin the function-definition. */
23242 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
23244 /* The things we're about to see are not directly qualified by any
23245 template headers we've seen thus far. */
23246 reset_specialization ();
23248 /* If there were names looked up in the decl-specifier-seq that we
23249 did not check, check them now. We must wait until we are in the
23250 scope of the function to perform the checks, since the function
23251 might be a friend. */
23252 perform_deferred_access_checks (tf_warning_or_error
);
23256 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
23257 parser
->omp_declare_simd
= NULL
;
23262 /* Skip the entire function. */
23263 cp_parser_skip_to_end_of_block_or_statement (parser
);
23264 fn
= error_mark_node
;
23266 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
23268 /* Seen already, skip it. An error message has already been output. */
23269 cp_parser_skip_to_end_of_block_or_statement (parser
);
23270 fn
= current_function_decl
;
23271 current_function_decl
= NULL_TREE
;
23272 /* If this is a function from a class, pop the nested class. */
23273 if (current_class_name
)
23274 pop_nested_class ();
23279 if (DECL_DECLARED_INLINE_P (current_function_decl
))
23280 tv
= TV_PARSE_INLINE
;
23282 tv
= TV_PARSE_FUNC
;
23284 fn
= cp_parser_function_definition_after_declarator (parser
,
23285 /*inline_p=*/false);
23292 /* Parse the part of a function-definition that follows the
23293 declarator. INLINE_P is TRUE iff this function is an inline
23294 function defined within a class-specifier.
23296 Returns the function defined. */
23299 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
23303 bool ctor_initializer_p
= false;
23304 bool saved_in_unbraced_linkage_specification_p
;
23305 bool saved_in_function_body
;
23306 unsigned saved_num_template_parameter_lists
;
23308 bool fully_implicit_function_template_p
23309 = parser
->fully_implicit_function_template_p
;
23310 parser
->fully_implicit_function_template_p
= false;
23311 tree implicit_template_parms
23312 = parser
->implicit_template_parms
;
23313 parser
->implicit_template_parms
= 0;
23314 cp_binding_level
* implicit_template_scope
23315 = parser
->implicit_template_scope
;
23316 parser
->implicit_template_scope
= 0;
23318 saved_in_function_body
= parser
->in_function_body
;
23319 parser
->in_function_body
= true;
23320 /* If the next token is `return', then the code may be trying to
23321 make use of the "named return value" extension that G++ used to
23323 token
= cp_lexer_peek_token (parser
->lexer
);
23324 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
23326 /* Consume the `return' keyword. */
23327 cp_lexer_consume_token (parser
->lexer
);
23328 /* Look for the identifier that indicates what value is to be
23330 cp_parser_identifier (parser
);
23331 /* Issue an error message. */
23332 error_at (token
->location
,
23333 "named return values are no longer supported");
23334 /* Skip tokens until we reach the start of the function body. */
23337 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23338 if (token
->type
== CPP_OPEN_BRACE
23339 || token
->type
== CPP_EOF
23340 || token
->type
== CPP_PRAGMA_EOL
)
23342 cp_lexer_consume_token (parser
->lexer
);
23345 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23346 anything declared inside `f'. */
23347 saved_in_unbraced_linkage_specification_p
23348 = parser
->in_unbraced_linkage_specification_p
;
23349 parser
->in_unbraced_linkage_specification_p
= false;
23350 /* Inside the function, surrounding template-parameter-lists do not
23352 saved_num_template_parameter_lists
23353 = parser
->num_template_parameter_lists
;
23354 parser
->num_template_parameter_lists
= 0;
23356 start_lambda_scope (current_function_decl
);
23358 /* If the next token is `try', `__transaction_atomic', or
23359 `__transaction_relaxed`, then we are looking at either function-try-block
23360 or function-transaction-block. Note that all of these include the
23362 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
23363 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23364 RID_TRANSACTION_ATOMIC
);
23365 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23366 RID_TRANSACTION_RELAXED
))
23367 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23368 RID_TRANSACTION_RELAXED
);
23369 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23370 ctor_initializer_p
= cp_parser_function_try_block (parser
);
23372 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
23373 (parser
, /*in_function_try_block=*/false);
23375 finish_lambda_scope ();
23377 /* Finish the function. */
23378 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
23379 (inline_p
? 2 : 0));
23380 /* Generate code for it, if necessary. */
23381 expand_or_defer_fn (fn
);
23382 /* Restore the saved values. */
23383 parser
->in_unbraced_linkage_specification_p
23384 = saved_in_unbraced_linkage_specification_p
;
23385 parser
->num_template_parameter_lists
23386 = saved_num_template_parameter_lists
;
23387 parser
->in_function_body
= saved_in_function_body
;
23389 parser
->fully_implicit_function_template_p
23390 = fully_implicit_function_template_p
;
23391 parser
->implicit_template_parms
23392 = implicit_template_parms
;
23393 parser
->implicit_template_scope
23394 = implicit_template_scope
;
23396 if (parser
->fully_implicit_function_template_p
)
23397 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
23402 /* Parse a template-declaration, assuming that the `export' (and
23403 `extern') keywords, if present, has already been scanned. MEMBER_P
23404 is as for cp_parser_template_declaration. */
23407 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
23409 tree decl
= NULL_TREE
;
23410 vec
<deferred_access_check
, va_gc
> *checks
;
23411 tree parameter_list
;
23412 bool friend_p
= false;
23413 bool need_lang_pop
;
23416 /* Look for the `template' keyword. */
23417 token
= cp_lexer_peek_token (parser
->lexer
);
23418 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
23422 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
23424 if (at_class_scope_p () && current_function_decl
)
23426 /* 14.5.2.2 [temp.mem]
23428 A local class shall not have member templates. */
23429 error_at (token
->location
,
23430 "invalid declaration of member template in local class");
23431 cp_parser_skip_to_end_of_block_or_statement (parser
);
23436 A template ... shall not have C linkage. */
23437 if (current_lang_name
== lang_name_c
)
23439 error_at (token
->location
, "template with C linkage");
23440 /* Give it C++ linkage to avoid confusing other parts of the
23442 push_lang_context (lang_name_cplusplus
);
23443 need_lang_pop
= true;
23446 need_lang_pop
= false;
23448 /* We cannot perform access checks on the template parameter
23449 declarations until we know what is being declared, just as we
23450 cannot check the decl-specifier list. */
23451 push_deferring_access_checks (dk_deferred
);
23453 /* If the next token is `>', then we have an invalid
23454 specialization. Rather than complain about an invalid template
23455 parameter, issue an error message here. */
23456 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
23458 cp_parser_error (parser
, "invalid explicit specialization");
23459 begin_specialization ();
23460 parameter_list
= NULL_TREE
;
23464 /* Parse the template parameters. */
23465 parameter_list
= cp_parser_template_parameter_list (parser
);
23468 /* Get the deferred access checks from the parameter list. These
23469 will be checked once we know what is being declared, as for a
23470 member template the checks must be performed in the scope of the
23471 class containing the member. */
23472 checks
= get_deferred_access_checks ();
23474 /* Look for the `>'. */
23475 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23476 /* We just processed one more parameter list. */
23477 ++parser
->num_template_parameter_lists
;
23478 /* If the next token is `template', there are more template
23480 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23482 cp_parser_template_declaration_after_export (parser
, member_p
);
23483 else if (cxx_dialect
>= cxx11
23484 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23485 decl
= cp_parser_alias_declaration (parser
);
23488 /* There are no access checks when parsing a template, as we do not
23489 know if a specialization will be a friend. */
23490 push_deferring_access_checks (dk_no_check
);
23491 token
= cp_lexer_peek_token (parser
->lexer
);
23492 decl
= cp_parser_single_declaration (parser
,
23495 /*explicit_specialization_p=*/false,
23497 pop_deferring_access_checks ();
23499 /* If this is a member template declaration, let the front
23501 if (member_p
&& !friend_p
&& decl
)
23503 if (TREE_CODE (decl
) == TYPE_DECL
)
23504 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
23506 decl
= finish_member_template_decl (decl
);
23508 else if (friend_p
&& decl
23509 && DECL_DECLARES_TYPE_P (decl
))
23510 make_friend_class (current_class_type
, TREE_TYPE (decl
),
23511 /*complain=*/true);
23513 /* We are done with the current parameter list. */
23514 --parser
->num_template_parameter_lists
;
23516 pop_deferring_access_checks ();
23519 finish_template_decl (parameter_list
);
23521 /* Check the template arguments for a literal operator template. */
23523 && DECL_DECLARES_FUNCTION_P (decl
)
23524 && UDLIT_OPER_P (DECL_NAME (decl
)))
23527 if (parameter_list
== NULL_TREE
)
23531 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
23532 if (num_parms
== 1)
23534 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
23535 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23536 if (TREE_TYPE (parm
) != char_type_node
23537 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23540 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
23542 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
23543 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
23544 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
23545 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23546 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
23547 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23555 if (cxx_dialect
>= cxx14
)
23556 error ("literal operator template %qD has invalid parameter list."
23557 " Expected non-type template argument pack <char...>"
23558 " or <typename CharT, CharT...>",
23561 error ("literal operator template %qD has invalid parameter list."
23562 " Expected non-type template argument pack <char...>",
23566 /* Register member declarations. */
23567 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
23568 finish_member_declaration (decl
);
23569 /* For the erroneous case of a template with C linkage, we pushed an
23570 implicit C++ linkage scope; exit that scope now. */
23572 pop_lang_context ();
23573 /* If DECL is a function template, we must return to parse it later.
23574 (Even though there is no definition, there might be default
23575 arguments that need handling.) */
23576 if (member_p
&& decl
23577 && DECL_DECLARES_FUNCTION_P (decl
))
23578 vec_safe_push (unparsed_funs_with_definitions
, decl
);
23581 /* Perform the deferred access checks from a template-parameter-list.
23582 CHECKS is a TREE_LIST of access checks, as returned by
23583 get_deferred_access_checks. */
23586 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
23588 ++processing_template_parmlist
;
23589 perform_access_checks (checks
, tf_warning_or_error
);
23590 --processing_template_parmlist
;
23593 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23594 `function-definition' sequence that follows a template header.
23595 If MEMBER_P is true, this declaration appears in a class scope.
23597 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23598 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23601 cp_parser_single_declaration (cp_parser
* parser
,
23602 vec
<deferred_access_check
, va_gc
> *checks
,
23604 bool explicit_specialization_p
,
23607 int declares_class_or_enum
;
23608 tree decl
= NULL_TREE
;
23609 cp_decl_specifier_seq decl_specifiers
;
23610 bool function_definition_p
= false;
23611 cp_token
*decl_spec_token_start
;
23613 /* This function is only used when processing a template
23615 gcc_assert (innermost_scope_kind () == sk_template_parms
23616 || innermost_scope_kind () == sk_template_spec
);
23618 /* Defer access checks until we know what is being declared. */
23619 push_deferring_access_checks (dk_deferred
);
23621 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23623 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23624 cp_parser_decl_specifier_seq (parser
,
23625 CP_PARSER_FLAGS_OPTIONAL
,
23627 &declares_class_or_enum
);
23629 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
23631 /* There are no template typedefs. */
23632 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
23634 error_at (decl_spec_token_start
->location
,
23635 "template declaration of %<typedef%>");
23636 decl
= error_mark_node
;
23639 /* Gather up the access checks that occurred the
23640 decl-specifier-seq. */
23641 stop_deferring_access_checks ();
23643 /* Check for the declaration of a template class. */
23644 if (declares_class_or_enum
)
23646 if (cp_parser_declares_only_class_p (parser
))
23648 decl
= shadow_tag (&decl_specifiers
);
23653 friend template <typename T> struct A<T>::B;
23656 A<T>::B will be represented by a TYPENAME_TYPE, and
23657 therefore not recognized by shadow_tag. */
23658 if (friend_p
&& *friend_p
23660 && decl_specifiers
.type
23661 && TYPE_P (decl_specifiers
.type
))
23662 decl
= decl_specifiers
.type
;
23664 if (decl
&& decl
!= error_mark_node
)
23665 decl
= TYPE_NAME (decl
);
23667 decl
= error_mark_node
;
23669 /* Perform access checks for template parameters. */
23670 cp_parser_perform_template_parameter_access_checks (checks
);
23674 /* Complain about missing 'typename' or other invalid type names. */
23675 if (!decl_specifiers
.any_type_specifiers_p
23676 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23678 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23679 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23680 the rest of this declaration. */
23681 decl
= error_mark_node
;
23685 /* If it's not a template class, try for a template function. If
23686 the next token is a `;', then this declaration does not declare
23687 anything. But, if there were errors in the decl-specifiers, then
23688 the error might well have come from an attempted class-specifier.
23689 In that case, there's no need to warn about a missing declarator. */
23691 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
23692 || decl_specifiers
.type
!= error_mark_node
))
23694 decl
= cp_parser_init_declarator (parser
,
23697 /*function_definition_allowed_p=*/true,
23699 declares_class_or_enum
,
23700 &function_definition_p
,
23703 /* 7.1.1-1 [dcl.stc]
23705 A storage-class-specifier shall not be specified in an explicit
23706 specialization... */
23708 && explicit_specialization_p
23709 && decl_specifiers
.storage_class
!= sc_none
)
23711 error_at (decl_spec_token_start
->location
,
23712 "explicit template specialization cannot have a storage class");
23713 decl
= error_mark_node
;
23716 if (decl
&& VAR_P (decl
))
23717 check_template_variable (decl
);
23720 /* Look for a trailing `;' after the declaration. */
23721 if (!function_definition_p
23722 && (decl
== error_mark_node
23723 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
23724 cp_parser_skip_to_end_of_block_or_statement (parser
);
23727 pop_deferring_access_checks ();
23729 /* Clear any current qualification; whatever comes next is the start
23730 of something new. */
23731 parser
->scope
= NULL_TREE
;
23732 parser
->qualifying_scope
= NULL_TREE
;
23733 parser
->object_scope
= NULL_TREE
;
23738 /* Parse a cast-expression that is not the operand of a unary "&". */
23741 cp_parser_simple_cast_expression (cp_parser
*parser
)
23743 return cp_parser_cast_expression (parser
, /*address_p=*/false,
23744 /*cast_p=*/false, /*decltype*/false, NULL
);
23747 /* Parse a functional cast to TYPE. Returns an expression
23748 representing the cast. */
23751 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
23753 vec
<tree
, va_gc
> *vec
;
23754 tree expression_list
;
23759 type
= error_mark_node
;
23761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23763 cp_lexer_set_source_position (parser
->lexer
);
23764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23765 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
23766 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
23767 if (TREE_CODE (type
) == TYPE_DECL
)
23768 type
= TREE_TYPE (type
);
23769 return finish_compound_literal (type
, expression_list
,
23770 tf_warning_or_error
);
23774 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23776 /*allow_expansion_p=*/true,
23777 /*non_constant_p=*/NULL
);
23779 expression_list
= error_mark_node
;
23782 expression_list
= build_tree_list_vec (vec
);
23783 release_tree_vector (vec
);
23786 cast
= build_functional_cast (type
, expression_list
,
23787 tf_warning_or_error
);
23788 /* [expr.const]/1: In an integral constant expression "only type
23789 conversions to integral or enumeration type can be used". */
23790 if (TREE_CODE (type
) == TYPE_DECL
)
23791 type
= TREE_TYPE (type
);
23792 if (cast
!= error_mark_node
23793 && !cast_valid_in_integral_constant_expression_p (type
)
23794 && cp_parser_non_integral_constant_expression (parser
,
23796 return error_mark_node
;
23800 /* Save the tokens that make up the body of a member function defined
23801 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23802 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23803 specifiers applied to the declaration. Returns the FUNCTION_DECL
23804 for the member function. */
23807 cp_parser_save_member_function_body (cp_parser
* parser
,
23808 cp_decl_specifier_seq
*decl_specifiers
,
23809 cp_declarator
*declarator
,
23816 /* Create the FUNCTION_DECL. */
23817 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
23818 cp_finalize_omp_declare_simd (parser
, fn
);
23819 /* If something went badly wrong, bail out now. */
23820 if (fn
== error_mark_node
)
23822 /* If there's a function-body, skip it. */
23823 if (cp_parser_token_starts_function_definition_p
23824 (cp_lexer_peek_token (parser
->lexer
)))
23825 cp_parser_skip_to_end_of_block_or_statement (parser
);
23826 return error_mark_node
;
23829 /* Remember it, if there default args to post process. */
23830 cp_parser_save_default_args (parser
, fn
);
23832 /* Save away the tokens that make up the body of the
23834 first
= parser
->lexer
->next_token
;
23835 /* Handle function try blocks. */
23836 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23837 cp_lexer_consume_token (parser
->lexer
);
23838 /* We can have braced-init-list mem-initializers before the fn body. */
23839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23841 cp_lexer_consume_token (parser
->lexer
);
23842 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23844 /* cache_group will stop after an un-nested { } pair, too. */
23845 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
23848 /* variadic mem-inits have ... after the ')'. */
23849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23850 cp_lexer_consume_token (parser
->lexer
);
23853 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23854 /* Handle function try blocks. */
23855 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
23856 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23857 last
= parser
->lexer
->next_token
;
23859 /* Save away the inline definition; we will process it when the
23860 class is complete. */
23861 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
23862 DECL_PENDING_INLINE_P (fn
) = 1;
23864 /* We need to know that this was defined in the class, so that
23865 friend templates are handled correctly. */
23866 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
23868 /* Add FN to the queue of functions to be parsed later. */
23869 vec_safe_push (unparsed_funs_with_definitions
, fn
);
23874 /* Save the tokens that make up the in-class initializer for a non-static
23875 data member. Returns a DEFAULT_ARG. */
23878 cp_parser_save_nsdmi (cp_parser
* parser
)
23880 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
23883 /* Parse a template-argument-list, as well as the trailing ">" (but
23884 not the opening "<"). See cp_parser_template_argument_list for the
23888 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
23892 tree saved_qualifying_scope
;
23893 tree saved_object_scope
;
23894 bool saved_greater_than_is_operator_p
;
23895 int saved_unevaluated_operand
;
23896 int saved_inhibit_evaluation_warnings
;
23900 When parsing a template-id, the first non-nested `>' is taken as
23901 the end of the template-argument-list rather than a greater-than
23903 saved_greater_than_is_operator_p
23904 = parser
->greater_than_is_operator_p
;
23905 parser
->greater_than_is_operator_p
= false;
23906 /* Parsing the argument list may modify SCOPE, so we save it
23908 saved_scope
= parser
->scope
;
23909 saved_qualifying_scope
= parser
->qualifying_scope
;
23910 saved_object_scope
= parser
->object_scope
;
23911 /* We need to evaluate the template arguments, even though this
23912 template-id may be nested within a "sizeof". */
23913 saved_unevaluated_operand
= cp_unevaluated_operand
;
23914 cp_unevaluated_operand
= 0;
23915 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23916 c_inhibit_evaluation_warnings
= 0;
23917 /* Parse the template-argument-list itself. */
23918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
23919 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23920 arguments
= NULL_TREE
;
23922 arguments
= cp_parser_template_argument_list (parser
);
23923 /* Look for the `>' that ends the template-argument-list. If we find
23924 a '>>' instead, it's probably just a typo. */
23925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23927 if (cxx_dialect
!= cxx98
)
23929 /* In C++0x, a `>>' in a template argument list or cast
23930 expression is considered to be two separate `>'
23931 tokens. So, change the current token to a `>', but don't
23932 consume it: it will be consumed later when the outer
23933 template argument list (or cast expression) is parsed.
23934 Note that this replacement of `>' for `>>' is necessary
23935 even if we are parsing tentatively: in the tentative
23936 case, after calling
23937 cp_parser_enclosed_template_argument_list we will always
23938 throw away all of the template arguments and the first
23939 closing `>', either because the template argument list
23940 was erroneous or because we are replacing those tokens
23941 with a CPP_TEMPLATE_ID token. The second `>' (which will
23942 not have been thrown away) is needed either to close an
23943 outer template argument list or to complete a new-style
23945 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23946 token
->type
= CPP_GREATER
;
23948 else if (!saved_greater_than_is_operator_p
)
23950 /* If we're in a nested template argument list, the '>>' has
23951 to be a typo for '> >'. We emit the error message, but we
23952 continue parsing and we push a '>' as next token, so that
23953 the argument list will be parsed correctly. Note that the
23954 global source location is still on the token before the
23955 '>>', so we need to say explicitly where we want it. */
23956 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23957 error_at (token
->location
, "%<>>%> should be %<> >%> "
23958 "within a nested template argument list");
23960 token
->type
= CPP_GREATER
;
23964 /* If this is not a nested template argument list, the '>>'
23965 is a typo for '>'. Emit an error message and continue.
23966 Same deal about the token location, but here we can get it
23967 right by consuming the '>>' before issuing the diagnostic. */
23968 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
23969 error_at (token
->location
,
23970 "spurious %<>>%>, use %<>%> to terminate "
23971 "a template argument list");
23975 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23976 /* The `>' token might be a greater-than operator again now. */
23977 parser
->greater_than_is_operator_p
23978 = saved_greater_than_is_operator_p
;
23979 /* Restore the SAVED_SCOPE. */
23980 parser
->scope
= saved_scope
;
23981 parser
->qualifying_scope
= saved_qualifying_scope
;
23982 parser
->object_scope
= saved_object_scope
;
23983 cp_unevaluated_operand
= saved_unevaluated_operand
;
23984 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23989 /* MEMBER_FUNCTION is a member function, or a friend. If default
23990 arguments, or the body of the function have not yet been parsed,
23994 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
23996 timevar_push (TV_PARSE_INMETH
);
23997 /* If this member is a template, get the underlying
23999 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
24000 member_function
= DECL_TEMPLATE_RESULT (member_function
);
24002 /* There should not be any class definitions in progress at this
24003 point; the bodies of members are only parsed outside of all class
24005 gcc_assert (parser
->num_classes_being_defined
== 0);
24006 /* While we're parsing the member functions we might encounter more
24007 classes. We want to handle them right away, but we don't want
24008 them getting mixed up with functions that are currently in the
24010 push_unparsed_function_queues (parser
);
24012 /* Make sure that any template parameters are in scope. */
24013 maybe_begin_member_template_processing (member_function
);
24015 /* If the body of the function has not yet been parsed, parse it
24017 if (DECL_PENDING_INLINE_P (member_function
))
24019 tree function_scope
;
24020 cp_token_cache
*tokens
;
24022 /* The function is no longer pending; we are processing it. */
24023 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
24024 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
24025 DECL_PENDING_INLINE_P (member_function
) = 0;
24027 /* If this is a local class, enter the scope of the containing
24029 function_scope
= current_function_decl
;
24030 if (function_scope
)
24031 push_function_context ();
24033 /* Push the body of the function onto the lexer stack. */
24034 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24036 /* Let the front end know that we going to be defining this
24038 start_preparsed_function (member_function
, NULL_TREE
,
24039 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
24041 /* Don't do access checking if it is a templated function. */
24042 if (processing_template_decl
)
24043 push_deferring_access_checks (dk_no_check
);
24045 /* #pragma omp declare reduction needs special parsing. */
24046 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
24048 parser
->lexer
->in_pragma
= true;
24049 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
24050 finish_function (/*inline*/2);
24051 cp_check_omp_declare_reduction (member_function
);
24054 /* Now, parse the body of the function. */
24055 cp_parser_function_definition_after_declarator (parser
,
24056 /*inline_p=*/true);
24058 if (processing_template_decl
)
24059 pop_deferring_access_checks ();
24061 /* Leave the scope of the containing function. */
24062 if (function_scope
)
24063 pop_function_context ();
24064 cp_parser_pop_lexer (parser
);
24067 /* Remove any template parameters from the symbol table. */
24068 maybe_end_member_template_processing ();
24070 /* Restore the queue. */
24071 pop_unparsed_function_queues (parser
);
24072 timevar_pop (TV_PARSE_INMETH
);
24075 /* If DECL contains any default args, remember it on the unparsed
24076 functions queue. */
24079 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
24083 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
24085 probe
= TREE_CHAIN (probe
))
24086 if (TREE_PURPOSE (probe
))
24088 cp_default_arg_entry entry
= {current_class_type
, decl
};
24089 vec_safe_push (unparsed_funs_with_default_args
, entry
);
24094 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24095 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24096 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24097 from the parameter-type-list. */
24100 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
24101 tree default_arg
, tree parmtype
)
24103 cp_token_cache
*tokens
;
24107 if (default_arg
== error_mark_node
)
24108 return error_mark_node
;
24110 /* Push the saved tokens for the default argument onto the parser's
24112 tokens
= DEFARG_TOKENS (default_arg
);
24113 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24115 start_lambda_scope (decl
);
24117 /* Parse the default argument. */
24118 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
24119 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
24120 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
24122 finish_lambda_scope ();
24124 if (parsed_arg
== error_mark_node
)
24125 cp_parser_skip_to_end_of_statement (parser
);
24127 if (!processing_template_decl
)
24129 /* In a non-template class, check conversions now. In a template,
24130 we'll wait and instantiate these as needed. */
24131 if (TREE_CODE (decl
) == PARM_DECL
)
24132 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
24133 tf_warning_or_error
);
24135 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
24138 /* If the token stream has not been completely used up, then
24139 there was extra junk after the end of the default
24141 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
24143 if (TREE_CODE (decl
) == PARM_DECL
)
24144 cp_parser_error (parser
, "expected %<,%>");
24146 cp_parser_error (parser
, "expected %<;%>");
24149 /* Revert to the main lexer. */
24150 cp_parser_pop_lexer (parser
);
24155 /* FIELD is a non-static data member with an initializer which we saved for
24156 later; parse it now. */
24159 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
24163 maybe_begin_member_template_processing (field
);
24165 push_unparsed_function_queues (parser
);
24166 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
24167 DECL_INITIAL (field
),
24169 pop_unparsed_function_queues (parser
);
24171 maybe_end_member_template_processing ();
24173 DECL_INITIAL (field
) = def
;
24176 /* FN is a FUNCTION_DECL which may contains a parameter with an
24177 unparsed DEFAULT_ARG. Parse the default args now. This function
24178 assumes that the current scope is the scope in which the default
24179 argument should be processed. */
24182 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
24184 bool saved_local_variables_forbidden_p
;
24185 tree parm
, parmdecl
;
24187 /* While we're parsing the default args, we might (due to the
24188 statement expression extension) encounter more classes. We want
24189 to handle them right away, but we don't want them getting mixed
24190 up with default args that are currently in the queue. */
24191 push_unparsed_function_queues (parser
);
24193 /* Local variable names (and the `this' keyword) may not appear
24194 in a default argument. */
24195 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
24196 parser
->local_variables_forbidden_p
= true;
24198 push_defarg_context (fn
);
24200 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
24201 parmdecl
= DECL_ARGUMENTS (fn
);
24202 parm
&& parm
!= void_list_node
;
24203 parm
= TREE_CHAIN (parm
),
24204 parmdecl
= DECL_CHAIN (parmdecl
))
24206 tree default_arg
= TREE_PURPOSE (parm
);
24208 vec
<tree
, va_gc
> *insts
;
24215 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
24216 /* This can happen for a friend declaration for a function
24217 already declared with default arguments. */
24221 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
24223 TREE_VALUE (parm
));
24224 if (parsed_arg
== error_mark_node
)
24229 TREE_PURPOSE (parm
) = parsed_arg
;
24231 /* Update any instantiations we've already created. */
24232 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
24233 vec_safe_iterate (insts
, ix
, ©
); ix
++)
24234 TREE_PURPOSE (copy
) = parsed_arg
;
24237 pop_defarg_context ();
24239 /* Make sure no default arg is missing. */
24240 check_default_args (fn
);
24242 /* Restore the state of local_variables_forbidden_p. */
24243 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
24245 /* Restore the queue. */
24246 pop_unparsed_function_queues (parser
);
24249 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24251 sizeof ... ( identifier )
24253 where the 'sizeof' token has already been consumed. */
24256 cp_parser_sizeof_pack (cp_parser
*parser
)
24258 /* Consume the `...'. */
24259 cp_lexer_consume_token (parser
->lexer
);
24260 maybe_warn_variadic_templates ();
24262 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
24264 cp_lexer_consume_token (parser
->lexer
);
24266 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
24267 "%<sizeof...%> argument must be surrounded by parentheses");
24269 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24270 tree name
= cp_parser_identifier (parser
);
24271 if (name
== error_mark_node
)
24272 return error_mark_node
;
24273 /* The name is not qualified. */
24274 parser
->scope
= NULL_TREE
;
24275 parser
->qualifying_scope
= NULL_TREE
;
24276 parser
->object_scope
= NULL_TREE
;
24277 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
24278 if (expr
== error_mark_node
)
24279 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
24281 if (TREE_CODE (expr
) == TYPE_DECL
)
24282 expr
= TREE_TYPE (expr
);
24283 else if (TREE_CODE (expr
) == CONST_DECL
)
24284 expr
= DECL_INITIAL (expr
);
24285 expr
= make_pack_expansion (expr
);
24288 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24293 /* Parse the operand of `sizeof' (or a similar operator). Returns
24294 either a TYPE or an expression, depending on the form of the
24295 input. The KEYWORD indicates which kind of expression we have
24299 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
24301 tree expr
= NULL_TREE
;
24302 const char *saved_message
;
24304 bool saved_integral_constant_expression_p
;
24305 bool saved_non_integral_constant_expression_p
;
24307 /* If it's a `...', then we are computing the length of a parameter
24309 if (keyword
== RID_SIZEOF
24310 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24311 return cp_parser_sizeof_pack (parser
);
24313 /* Types cannot be defined in a `sizeof' expression. Save away the
24315 saved_message
= parser
->type_definition_forbidden_message
;
24316 /* And create the new one. */
24317 tmp
= concat ("types may not be defined in %<",
24318 IDENTIFIER_POINTER (ridpointers
[keyword
]),
24319 "%> expressions", NULL
);
24320 parser
->type_definition_forbidden_message
= tmp
;
24322 /* The restrictions on constant-expressions do not apply inside
24323 sizeof expressions. */
24324 saved_integral_constant_expression_p
24325 = parser
->integral_constant_expression_p
;
24326 saved_non_integral_constant_expression_p
24327 = parser
->non_integral_constant_expression_p
;
24328 parser
->integral_constant_expression_p
= false;
24330 /* Do not actually evaluate the expression. */
24331 ++cp_unevaluated_operand
;
24332 ++c_inhibit_evaluation_warnings
;
24333 /* If it's a `(', then we might be looking at the type-id
24335 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24337 tree type
= NULL_TREE
;
24339 /* We can't be sure yet whether we're looking at a type-id or an
24341 cp_parser_parse_tentatively (parser
);
24342 /* Note: as a GNU Extension, compound literals are considered
24343 postfix-expressions as they are in C99, so they are valid
24344 arguments to sizeof. See comment in cp_parser_cast_expression
24346 if (cp_parser_compound_literal_p (parser
))
24347 cp_parser_simulate_error (parser
);
24350 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
24351 parser
->in_type_id_in_expr_p
= true;
24352 /* Look for the type-id. */
24353 type
= cp_parser_type_id (parser
);
24354 /* Look for the closing `)'. */
24355 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24356 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
24359 /* If all went well, then we're done. */
24360 if (cp_parser_parse_definitely (parser
))
24362 cp_decl_specifier_seq decl_specs
;
24364 /* Build a trivial decl-specifier-seq. */
24365 clear_decl_specs (&decl_specs
);
24366 decl_specs
.type
= type
;
24368 /* Call grokdeclarator to figure out what type this is. */
24369 expr
= grokdeclarator (NULL
,
24373 /*attrlist=*/NULL
);
24377 /* If the type-id production did not work out, then we must be
24378 looking at the unary-expression production. */
24380 expr
= cp_parser_unary_expression (parser
);
24382 /* Go back to evaluating expressions. */
24383 --cp_unevaluated_operand
;
24384 --c_inhibit_evaluation_warnings
;
24386 /* Free the message we created. */
24388 /* And restore the old one. */
24389 parser
->type_definition_forbidden_message
= saved_message
;
24390 parser
->integral_constant_expression_p
24391 = saved_integral_constant_expression_p
;
24392 parser
->non_integral_constant_expression_p
24393 = saved_non_integral_constant_expression_p
;
24398 /* If the current declaration has no declarator, return true. */
24401 cp_parser_declares_only_class_p (cp_parser
*parser
)
24403 /* If the next token is a `;' or a `,' then there is no
24405 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24406 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
24409 /* Update the DECL_SPECS to reflect the storage class indicated by
24413 cp_parser_set_storage_class (cp_parser
*parser
,
24414 cp_decl_specifier_seq
*decl_specs
,
24418 cp_storage_class storage_class
;
24420 if (parser
->in_unbraced_linkage_specification_p
)
24422 error_at (token
->location
, "invalid use of %qD in linkage specification",
24423 ridpointers
[keyword
]);
24426 else if (decl_specs
->storage_class
!= sc_none
)
24428 decl_specs
->conflicting_specifiers_p
= true;
24432 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
24433 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
24434 && decl_specs
->gnu_thread_keyword_p
)
24436 pedwarn (decl_specs
->locations
[ds_thread
], 0,
24437 "%<__thread%> before %qD", ridpointers
[keyword
]);
24443 storage_class
= sc_auto
;
24446 storage_class
= sc_register
;
24449 storage_class
= sc_static
;
24452 storage_class
= sc_extern
;
24455 storage_class
= sc_mutable
;
24458 gcc_unreachable ();
24460 decl_specs
->storage_class
= storage_class
;
24461 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
24463 /* A storage class specifier cannot be applied alongside a typedef
24464 specifier. If there is a typedef specifier present then set
24465 conflicting_specifiers_p which will trigger an error later
24466 on in grokdeclarator. */
24467 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
24468 decl_specs
->conflicting_specifiers_p
= true;
24471 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24472 is true, the type is a class or enum definition. */
24475 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
24478 bool type_definition_p
)
24480 decl_specs
->any_specifiers_p
= true;
24482 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24483 (with, for example, in "typedef int wchar_t;") we remember that
24484 this is what happened. In system headers, we ignore these
24485 declarations so that G++ can work with system headers that are not
24487 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
24488 && !type_definition_p
24489 && (type_spec
== boolean_type_node
24490 || type_spec
== char16_type_node
24491 || type_spec
== char32_type_node
24492 || type_spec
== wchar_type_node
)
24493 && (decl_specs
->type
24494 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
24495 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
24496 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
24497 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
24499 decl_specs
->redefined_builtin_type
= type_spec
;
24500 set_and_check_decl_spec_loc (decl_specs
,
24501 ds_redefined_builtin_type_spec
,
24503 if (!decl_specs
->type
)
24505 decl_specs
->type
= type_spec
;
24506 decl_specs
->type_definition_p
= false;
24507 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
24510 else if (decl_specs
->type
)
24511 decl_specs
->multiple_types_p
= true;
24514 decl_specs
->type
= type_spec
;
24515 decl_specs
->type_definition_p
= type_definition_p
;
24516 decl_specs
->redefined_builtin_type
= NULL_TREE
;
24517 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
24521 /* True iff TOKEN is the GNU keyword __thread. */
24524 token_is__thread (cp_token
*token
)
24526 gcc_assert (token
->keyword
== RID_THREAD
);
24527 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
24530 /* Set the location for a declarator specifier and check if it is
24533 DECL_SPECS is the sequence of declarator specifiers onto which to
24536 DS is the single declarator specifier to set which location is to
24537 be set onto the existing sequence of declarators.
24539 LOCATION is the location for the declarator specifier to
24543 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
24544 cp_decl_spec ds
, cp_token
*token
)
24546 gcc_assert (ds
< ds_last
);
24548 if (decl_specs
== NULL
)
24551 source_location location
= token
->location
;
24553 if (decl_specs
->locations
[ds
] == 0)
24555 decl_specs
->locations
[ds
] = location
;
24556 if (ds
== ds_thread
)
24557 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
24563 if (decl_specs
->locations
[ds_long_long
] != 0)
24564 error_at (location
,
24565 "%<long long long%> is too long for GCC");
24568 decl_specs
->locations
[ds_long_long
] = location
;
24569 pedwarn_cxx98 (location
,
24571 "ISO C++ 1998 does not support %<long long%>");
24574 else if (ds
== ds_thread
)
24576 bool gnu
= token_is__thread (token
);
24577 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
24578 error_at (location
,
24579 "both %<__thread%> and %<thread_local%> specified");
24581 error_at (location
, "duplicate %qD", token
->u
.value
);
24585 static const char *const decl_spec_names
[] = {
24602 error_at (location
,
24603 "duplicate %qs", decl_spec_names
[ds
]);
24608 /* Return true iff the declarator specifier DS is present in the
24609 sequence of declarator specifiers DECL_SPECS. */
24612 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
24615 gcc_assert (ds
< ds_last
);
24617 if (decl_specs
== NULL
)
24620 return decl_specs
->locations
[ds
] != 0;
24623 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24624 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24627 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
24629 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
24632 /* Issue an error message indicating that TOKEN_DESC was expected.
24633 If KEYWORD is true, it indicated this function is called by
24634 cp_parser_require_keword and the required token can only be
24635 a indicated keyword. */
24638 cp_parser_required_error (cp_parser
*parser
,
24639 required_token token_desc
,
24642 switch (token_desc
)
24645 cp_parser_error (parser
, "expected %<new%>");
24648 cp_parser_error (parser
, "expected %<delete%>");
24651 cp_parser_error (parser
, "expected %<return%>");
24654 cp_parser_error (parser
, "expected %<while%>");
24657 cp_parser_error (parser
, "expected %<extern%>");
24659 case RT_STATIC_ASSERT
:
24660 cp_parser_error (parser
, "expected %<static_assert%>");
24663 cp_parser_error (parser
, "expected %<decltype%>");
24666 cp_parser_error (parser
, "expected %<operator%>");
24669 cp_parser_error (parser
, "expected %<class%>");
24672 cp_parser_error (parser
, "expected %<template%>");
24675 cp_parser_error (parser
, "expected %<namespace%>");
24678 cp_parser_error (parser
, "expected %<using%>");
24681 cp_parser_error (parser
, "expected %<asm%>");
24684 cp_parser_error (parser
, "expected %<try%>");
24687 cp_parser_error (parser
, "expected %<catch%>");
24690 cp_parser_error (parser
, "expected %<throw%>");
24693 cp_parser_error (parser
, "expected %<__label__%>");
24696 cp_parser_error (parser
, "expected %<@try%>");
24698 case RT_AT_SYNCHRONIZED
:
24699 cp_parser_error (parser
, "expected %<@synchronized%>");
24702 cp_parser_error (parser
, "expected %<@throw%>");
24704 case RT_TRANSACTION_ATOMIC
:
24705 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
24707 case RT_TRANSACTION_RELAXED
:
24708 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
24715 switch (token_desc
)
24718 cp_parser_error (parser
, "expected %<;%>");
24720 case RT_OPEN_PAREN
:
24721 cp_parser_error (parser
, "expected %<(%>");
24723 case RT_CLOSE_BRACE
:
24724 cp_parser_error (parser
, "expected %<}%>");
24726 case RT_OPEN_BRACE
:
24727 cp_parser_error (parser
, "expected %<{%>");
24729 case RT_CLOSE_SQUARE
:
24730 cp_parser_error (parser
, "expected %<]%>");
24732 case RT_OPEN_SQUARE
:
24733 cp_parser_error (parser
, "expected %<[%>");
24736 cp_parser_error (parser
, "expected %<,%>");
24739 cp_parser_error (parser
, "expected %<::%>");
24742 cp_parser_error (parser
, "expected %<<%>");
24745 cp_parser_error (parser
, "expected %<>%>");
24748 cp_parser_error (parser
, "expected %<=%>");
24751 cp_parser_error (parser
, "expected %<...%>");
24754 cp_parser_error (parser
, "expected %<*%>");
24757 cp_parser_error (parser
, "expected %<~%>");
24760 cp_parser_error (parser
, "expected %<:%>");
24762 case RT_COLON_SCOPE
:
24763 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24765 case RT_CLOSE_PAREN
:
24766 cp_parser_error (parser
, "expected %<)%>");
24768 case RT_COMMA_CLOSE_PAREN
:
24769 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24771 case RT_PRAGMA_EOL
:
24772 cp_parser_error (parser
, "expected end of line");
24775 cp_parser_error (parser
, "expected identifier");
24778 cp_parser_error (parser
, "expected selection-statement");
24780 case RT_INTERATION
:
24781 cp_parser_error (parser
, "expected iteration-statement");
24784 cp_parser_error (parser
, "expected jump-statement");
24787 cp_parser_error (parser
, "expected class-key");
24789 case RT_CLASS_TYPENAME_TEMPLATE
:
24790 cp_parser_error (parser
,
24791 "expected %<class%>, %<typename%>, or %<template%>");
24794 gcc_unreachable ();
24798 gcc_unreachable ();
24803 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24804 issue an error message indicating that TOKEN_DESC was expected.
24806 Returns the token consumed, if the token had the appropriate type.
24807 Otherwise, returns NULL. */
24810 cp_parser_require (cp_parser
* parser
,
24811 enum cpp_ttype type
,
24812 required_token token_desc
)
24814 if (cp_lexer_next_token_is (parser
->lexer
, type
))
24815 return cp_lexer_consume_token (parser
->lexer
);
24818 /* Output the MESSAGE -- unless we're parsing tentatively. */
24819 if (!cp_parser_simulate_error (parser
))
24820 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
24825 /* An error message is produced if the next token is not '>'.
24826 All further tokens are skipped until the desired token is
24827 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24830 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
24832 /* Current level of '< ... >'. */
24833 unsigned level
= 0;
24834 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24835 unsigned nesting_depth
= 0;
24837 /* Are we ready, yet? If not, issue error message. */
24838 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
24841 /* Skip tokens until the desired token is found. */
24844 /* Peek at the next token. */
24845 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24848 if (!nesting_depth
)
24853 if (cxx_dialect
== cxx98
)
24854 /* C++0x views the `>>' operator as two `>' tokens, but
24857 else if (!nesting_depth
&& level
-- == 0)
24859 /* We've hit a `>>' where the first `>' closes the
24860 template argument list, and the second `>' is
24861 spurious. Just consume the `>>' and stop; we've
24862 already produced at least one error. */
24863 cp_lexer_consume_token (parser
->lexer
);
24866 /* Fall through for C++0x, so we handle the second `>' in
24870 if (!nesting_depth
&& level
-- == 0)
24872 /* We've reached the token we want, consume it and stop. */
24873 cp_lexer_consume_token (parser
->lexer
);
24878 case CPP_OPEN_PAREN
:
24879 case CPP_OPEN_SQUARE
:
24883 case CPP_CLOSE_PAREN
:
24884 case CPP_CLOSE_SQUARE
:
24885 if (nesting_depth
-- == 0)
24890 case CPP_PRAGMA_EOL
:
24891 case CPP_SEMICOLON
:
24892 case CPP_OPEN_BRACE
:
24893 case CPP_CLOSE_BRACE
:
24894 /* The '>' was probably forgotten, don't look further. */
24901 /* Consume this token. */
24902 cp_lexer_consume_token (parser
->lexer
);
24906 /* If the next token is the indicated keyword, consume it. Otherwise,
24907 issue an error message indicating that TOKEN_DESC was expected.
24909 Returns the token consumed, if the token had the appropriate type.
24910 Otherwise, returns NULL. */
24913 cp_parser_require_keyword (cp_parser
* parser
,
24915 required_token token_desc
)
24917 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
24919 if (token
&& token
->keyword
!= keyword
)
24921 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
24928 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24929 function-definition. */
24932 cp_parser_token_starts_function_definition_p (cp_token
* token
)
24934 return (/* An ordinary function-body begins with an `{'. */
24935 token
->type
== CPP_OPEN_BRACE
24936 /* A ctor-initializer begins with a `:'. */
24937 || token
->type
== CPP_COLON
24938 /* A function-try-block begins with `try'. */
24939 || token
->keyword
== RID_TRY
24940 /* A function-transaction-block begins with `__transaction_atomic'
24941 or `__transaction_relaxed'. */
24942 || token
->keyword
== RID_TRANSACTION_ATOMIC
24943 || token
->keyword
== RID_TRANSACTION_RELAXED
24944 /* The named return value extension begins with `return'. */
24945 || token
->keyword
== RID_RETURN
);
24948 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24952 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
24956 token
= cp_lexer_peek_token (parser
->lexer
);
24957 return (token
->type
== CPP_OPEN_BRACE
24958 || (token
->type
== CPP_COLON
24959 && !parser
->colon_doesnt_start_class_def_p
));
24962 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24963 C++0x) ending a template-argument. */
24966 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
24970 token
= cp_lexer_peek_token (parser
->lexer
);
24971 return (token
->type
== CPP_COMMA
24972 || token
->type
== CPP_GREATER
24973 || token
->type
== CPP_ELLIPSIS
24974 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
24977 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24978 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24981 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
24986 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24987 if (token
->type
== CPP_LESS
)
24989 /* Check for the sequence `<::' in the original code. It would be lexed as
24990 `[:', where `[' is a digraph, and there is no whitespace before
24992 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
24995 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
24996 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
25002 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25003 or none_type otherwise. */
25005 static enum tag_types
25006 cp_parser_token_is_class_key (cp_token
* token
)
25008 switch (token
->keyword
)
25013 return record_type
;
25022 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25023 or none_type otherwise or if the token is null. */
25025 static enum tag_types
25026 cp_parser_token_is_type_parameter_key (cp_token
* token
)
25031 switch (token
->keyword
)
25036 return typename_type
;
25043 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25046 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
25048 if (type
== error_mark_node
)
25050 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
25052 if (permerror (input_location
, "%qs tag used in naming %q#T",
25053 class_key
== union_type
? "union"
25054 : class_key
== record_type
? "struct" : "class",
25056 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
25057 "%q#T was previously declared here", type
);
25061 /* Issue an error message if DECL is redeclared with different
25062 access than its original declaration [class.access.spec/3].
25063 This applies to nested classes and nested class templates.
25067 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
25069 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
25072 if ((TREE_PRIVATE (decl
)
25073 != (current_access_specifier
== access_private_node
))
25074 || (TREE_PROTECTED (decl
)
25075 != (current_access_specifier
== access_protected_node
)))
25076 error_at (location
, "%qD redeclared with different access", decl
);
25079 /* Look for the `template' keyword, as a syntactic disambiguator.
25080 Return TRUE iff it is present, in which case it will be
25084 cp_parser_optional_template_keyword (cp_parser
*parser
)
25086 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25088 /* In C++98 the `template' keyword can only be used within templates;
25089 outside templates the parser can always figure out what is a
25090 template and what is not. In C++11, per the resolution of DR 468,
25091 `template' is allowed in cases where it is not strictly necessary. */
25092 if (!processing_template_decl
25093 && pedantic
&& cxx_dialect
== cxx98
)
25095 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25096 pedwarn (token
->location
, OPT_Wpedantic
,
25097 "in C++98 %<template%> (as a disambiguator) is only "
25098 "allowed within templates");
25099 /* If this part of the token stream is rescanned, the same
25100 error message would be generated. So, we purge the token
25101 from the stream. */
25102 cp_lexer_purge_token (parser
->lexer
);
25107 /* Consume the `template' keyword. */
25108 cp_lexer_consume_token (parser
->lexer
);
25115 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25116 set PARSER->SCOPE, and perform other related actions. */
25119 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
25122 struct tree_check
*check_value
;
25123 deferred_access_check
*chk
;
25124 vec
<deferred_access_check
, va_gc
> *checks
;
25126 /* Get the stored value. */
25127 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
25128 /* Perform any access checks that were deferred. */
25129 checks
= check_value
->checks
;
25132 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
25133 perform_or_defer_access_check (chk
->binfo
,
25135 chk
->diag_decl
, tf_warning_or_error
);
25137 /* Set the scope from the stored value. */
25138 parser
->scope
= check_value
->value
;
25139 parser
->qualifying_scope
= check_value
->qualifying_scope
;
25140 parser
->object_scope
= NULL_TREE
;
25143 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25144 encounter the end of a block before what we were looking for. */
25147 cp_parser_cache_group (cp_parser
*parser
,
25148 enum cpp_ttype end
,
25153 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25155 /* Abort a parenthesized expression if we encounter a semicolon. */
25156 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
25157 && token
->type
== CPP_SEMICOLON
)
25159 /* If we've reached the end of the file, stop. */
25160 if (token
->type
== CPP_EOF
25161 || (end
!= CPP_PRAGMA_EOL
25162 && token
->type
== CPP_PRAGMA_EOL
))
25164 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
25165 /* We've hit the end of an enclosing block, so there's been some
25166 kind of syntax error. */
25169 /* Consume the token. */
25170 cp_lexer_consume_token (parser
->lexer
);
25171 /* See if it starts a new group. */
25172 if (token
->type
== CPP_OPEN_BRACE
)
25174 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
25175 /* In theory this should probably check end == '}', but
25176 cp_parser_save_member_function_body needs it to exit
25177 after either '}' or ')' when called with ')'. */
25181 else if (token
->type
== CPP_OPEN_PAREN
)
25183 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
25184 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
25187 else if (token
->type
== CPP_PRAGMA
)
25188 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
25189 else if (token
->type
== end
)
25194 /* Like above, for caching a default argument or NSDMI. Both of these are
25195 terminated by a non-nested comma, but it can be unclear whether or not a
25196 comma is nested in a template argument list unless we do more parsing.
25197 In order to handle this ambiguity, when we encounter a ',' after a '<'
25198 we try to parse what follows as a parameter-declaration-list (in the
25199 case of a default argument) or a member-declarator (in the case of an
25200 NSDMI). If that succeeds, then we stop caching. */
25203 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
25205 unsigned depth
= 0;
25206 int maybe_template_id
= 0;
25207 cp_token
*first_token
;
25209 tree default_argument
;
25211 /* Add tokens until we have processed the entire default
25212 argument. We add the range [first_token, token). */
25213 first_token
= cp_lexer_peek_token (parser
->lexer
);
25214 if (first_token
->type
== CPP_OPEN_BRACE
)
25216 /* For list-initialization, this is straightforward. */
25217 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25218 token
= cp_lexer_peek_token (parser
->lexer
);
25224 /* Peek at the next token. */
25225 token
= cp_lexer_peek_token (parser
->lexer
);
25226 /* What we do depends on what token we have. */
25227 switch (token
->type
)
25229 /* In valid code, a default argument must be
25230 immediately followed by a `,' `)', or `...'. */
25232 if (depth
== 0 && maybe_template_id
)
25234 /* If we've seen a '<', we might be in a
25235 template-argument-list. Until Core issue 325 is
25236 resolved, we don't know how this situation ought
25237 to be handled, so try to DTRT. We check whether
25238 what comes after the comma is a valid parameter
25239 declaration list. If it is, then the comma ends
25240 the default argument; otherwise the default
25241 argument continues. */
25242 bool error
= false;
25244 /* Set ITALP so cp_parser_parameter_declaration_list
25245 doesn't decide to commit to this parse. */
25246 bool saved_italp
= parser
->in_template_argument_list_p
;
25247 parser
->in_template_argument_list_p
= true;
25249 cp_parser_parse_tentatively (parser
);
25250 cp_lexer_consume_token (parser
->lexer
);
25254 int ctor_dtor_or_conv_p
;
25255 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25256 &ctor_dtor_or_conv_p
,
25257 /*parenthesized_p=*/NULL
,
25259 /*friend_p=*/false);
25263 begin_scope (sk_function_parms
, NULL_TREE
);
25264 cp_parser_parameter_declaration_list (parser
, &error
);
25265 pop_bindings_and_leave_scope ();
25267 if (!cp_parser_error_occurred (parser
) && !error
)
25269 cp_parser_abort_tentative_parse (parser
);
25271 parser
->in_template_argument_list_p
= saved_italp
;
25274 case CPP_CLOSE_PAREN
:
25276 /* If we run into a non-nested `;', `}', or `]',
25277 then the code is invalid -- but the default
25278 argument is certainly over. */
25279 case CPP_SEMICOLON
:
25280 case CPP_CLOSE_BRACE
:
25281 case CPP_CLOSE_SQUARE
:
25283 /* Handle correctly int n = sizeof ... ( p ); */
25284 && token
->type
!= CPP_ELLIPSIS
)
25286 /* Update DEPTH, if necessary. */
25287 else if (token
->type
== CPP_CLOSE_PAREN
25288 || token
->type
== CPP_CLOSE_BRACE
25289 || token
->type
== CPP_CLOSE_SQUARE
)
25293 case CPP_OPEN_PAREN
:
25294 case CPP_OPEN_SQUARE
:
25295 case CPP_OPEN_BRACE
:
25301 /* This might be the comparison operator, or it might
25302 start a template argument list. */
25303 ++maybe_template_id
;
25307 if (cxx_dialect
== cxx98
)
25309 /* Fall through for C++0x, which treats the `>>'
25310 operator like two `>' tokens in certain
25316 /* This might be an operator, or it might close a
25317 template argument list. But if a previous '<'
25318 started a template argument list, this will have
25319 closed it, so we can't be in one anymore. */
25320 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
25321 if (maybe_template_id
< 0)
25322 maybe_template_id
= 0;
25326 /* If we run out of tokens, issue an error message. */
25328 case CPP_PRAGMA_EOL
:
25329 error_at (token
->location
, "file ends in default argument");
25335 /* In these cases, we should look for template-ids.
25336 For example, if the default argument is
25337 `X<int, double>()', we need to do name lookup to
25338 figure out whether or not `X' is a template; if
25339 so, the `,' does not end the default argument.
25341 That is not yet done. */
25348 /* If we've reached the end, stop. */
25352 /* Add the token to the token block. */
25353 token
= cp_lexer_consume_token (parser
->lexer
);
25356 /* Create a DEFAULT_ARG to represent the unparsed default
25358 default_argument
= make_node (DEFAULT_ARG
);
25359 DEFARG_TOKENS (default_argument
)
25360 = cp_token_cache_new (first_token
, token
);
25361 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
25363 return default_argument
;
25366 /* Begin parsing tentatively. We always save tokens while parsing
25367 tentatively so that if the tentative parsing fails we can restore the
25371 cp_parser_parse_tentatively (cp_parser
* parser
)
25373 /* Enter a new parsing context. */
25374 parser
->context
= cp_parser_context_new (parser
->context
);
25375 /* Begin saving tokens. */
25376 cp_lexer_save_tokens (parser
->lexer
);
25377 /* In order to avoid repetitive access control error messages,
25378 access checks are queued up until we are no longer parsing
25380 push_deferring_access_checks (dk_deferred
);
25383 /* Commit to the currently active tentative parse. */
25386 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
25388 cp_parser_context
*context
;
25391 /* Mark all of the levels as committed. */
25392 lexer
= parser
->lexer
;
25393 for (context
= parser
->context
; context
->next
; context
= context
->next
)
25395 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25397 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25398 while (!cp_lexer_saving_tokens (lexer
))
25399 lexer
= lexer
->next
;
25400 cp_lexer_commit_tokens (lexer
);
25404 /* Commit to the topmost currently active tentative parse.
25406 Note that this function shouldn't be called when there are
25407 irreversible side-effects while in a tentative state. For
25408 example, we shouldn't create a permanent entry in the symbol
25409 table, or issue an error message that might not apply if the
25410 tentative parse is aborted. */
25413 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
25415 cp_parser_context
*context
= parser
->context
;
25416 cp_lexer
*lexer
= parser
->lexer
;
25420 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25422 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25424 while (!cp_lexer_saving_tokens (lexer
))
25425 lexer
= lexer
->next
;
25426 cp_lexer_commit_tokens (lexer
);
25430 /* Abort the currently active tentative parse. All consumed tokens
25431 will be rolled back, and no diagnostics will be issued. */
25434 cp_parser_abort_tentative_parse (cp_parser
* parser
)
25436 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
25437 || errorcount
> 0);
25438 cp_parser_simulate_error (parser
);
25439 /* Now, pretend that we want to see if the construct was
25440 successfully parsed. */
25441 cp_parser_parse_definitely (parser
);
25444 /* Stop parsing tentatively. If a parse error has occurred, restore the
25445 token stream. Otherwise, commit to the tokens we have consumed.
25446 Returns true if no error occurred; false otherwise. */
25449 cp_parser_parse_definitely (cp_parser
* parser
)
25451 bool error_occurred
;
25452 cp_parser_context
*context
;
25454 /* Remember whether or not an error occurred, since we are about to
25455 destroy that information. */
25456 error_occurred
= cp_parser_error_occurred (parser
);
25457 /* Remove the topmost context from the stack. */
25458 context
= parser
->context
;
25459 parser
->context
= context
->next
;
25460 /* If no parse errors occurred, commit to the tentative parse. */
25461 if (!error_occurred
)
25463 /* Commit to the tokens read tentatively, unless that was
25465 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
25466 cp_lexer_commit_tokens (parser
->lexer
);
25468 pop_to_parent_deferring_access_checks ();
25470 /* Otherwise, if errors occurred, roll back our state so that things
25471 are just as they were before we began the tentative parse. */
25474 cp_lexer_rollback_tokens (parser
->lexer
);
25475 pop_deferring_access_checks ();
25477 /* Add the context to the front of the free list. */
25478 context
->next
= cp_parser_context_free_list
;
25479 cp_parser_context_free_list
= context
;
25481 return !error_occurred
;
25484 /* Returns true if we are parsing tentatively and are not committed to
25485 this tentative parse. */
25488 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
25490 return (cp_parser_parsing_tentatively (parser
)
25491 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
25494 /* Returns nonzero iff an error has occurred during the most recent
25495 tentative parse. */
25498 cp_parser_error_occurred (cp_parser
* parser
)
25500 return (cp_parser_parsing_tentatively (parser
)
25501 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
25504 /* Returns nonzero if GNU extensions are allowed. */
25507 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
25509 return parser
->allow_gnu_extensions_p
;
25512 /* Objective-C++ Productions */
25515 /* Parse an Objective-C expression, which feeds into a primary-expression
25519 objc-message-expression
25520 objc-string-literal
25521 objc-encode-expression
25522 objc-protocol-expression
25523 objc-selector-expression
25525 Returns a tree representation of the expression. */
25528 cp_parser_objc_expression (cp_parser
* parser
)
25530 /* Try to figure out what kind of declaration is present. */
25531 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25535 case CPP_OPEN_SQUARE
:
25536 return cp_parser_objc_message_expression (parser
);
25538 case CPP_OBJC_STRING
:
25539 kwd
= cp_lexer_consume_token (parser
->lexer
);
25540 return objc_build_string_object (kwd
->u
.value
);
25543 switch (kwd
->keyword
)
25545 case RID_AT_ENCODE
:
25546 return cp_parser_objc_encode_expression (parser
);
25548 case RID_AT_PROTOCOL
:
25549 return cp_parser_objc_protocol_expression (parser
);
25551 case RID_AT_SELECTOR
:
25552 return cp_parser_objc_selector_expression (parser
);
25558 error_at (kwd
->location
,
25559 "misplaced %<@%D%> Objective-C++ construct",
25561 cp_parser_skip_to_end_of_block_or_statement (parser
);
25564 return error_mark_node
;
25567 /* Parse an Objective-C message expression.
25569 objc-message-expression:
25570 [ objc-message-receiver objc-message-args ]
25572 Returns a representation of an Objective-C message. */
25575 cp_parser_objc_message_expression (cp_parser
* parser
)
25577 tree receiver
, messageargs
;
25579 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
25580 receiver
= cp_parser_objc_message_receiver (parser
);
25581 messageargs
= cp_parser_objc_message_args (parser
);
25582 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25584 return objc_build_message_expr (receiver
, messageargs
);
25587 /* Parse an objc-message-receiver.
25589 objc-message-receiver:
25591 simple-type-specifier
25593 Returns a representation of the type or expression. */
25596 cp_parser_objc_message_receiver (cp_parser
* parser
)
25600 /* An Objective-C message receiver may be either (1) a type
25601 or (2) an expression. */
25602 cp_parser_parse_tentatively (parser
);
25603 rcv
= cp_parser_expression (parser
);
25605 if (cp_parser_parse_definitely (parser
))
25608 rcv
= cp_parser_simple_type_specifier (parser
,
25609 /*decl_specs=*/NULL
,
25610 CP_PARSER_FLAGS_NONE
);
25612 return objc_get_class_reference (rcv
);
25615 /* Parse the arguments and selectors comprising an Objective-C message.
25620 objc-selector-args , objc-comma-args
25622 objc-selector-args:
25623 objc-selector [opt] : assignment-expression
25624 objc-selector-args objc-selector [opt] : assignment-expression
25627 assignment-expression
25628 objc-comma-args , assignment-expression
25630 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25631 selector arguments and TREE_VALUE containing a list of comma
25635 cp_parser_objc_message_args (cp_parser
* parser
)
25637 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
25638 bool maybe_unary_selector_p
= true;
25639 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25641 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25643 tree selector
= NULL_TREE
, arg
;
25645 if (token
->type
!= CPP_COLON
)
25646 selector
= cp_parser_objc_selector (parser
);
25648 /* Detect if we have a unary selector. */
25649 if (maybe_unary_selector_p
25650 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25651 return build_tree_list (selector
, NULL_TREE
);
25653 maybe_unary_selector_p
= false;
25654 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25655 arg
= cp_parser_assignment_expression (parser
);
25658 = chainon (sel_args
,
25659 build_tree_list (selector
, arg
));
25661 token
= cp_lexer_peek_token (parser
->lexer
);
25664 /* Handle non-selector arguments, if any. */
25665 while (token
->type
== CPP_COMMA
)
25669 cp_lexer_consume_token (parser
->lexer
);
25670 arg
= cp_parser_assignment_expression (parser
);
25673 = chainon (addl_args
,
25674 build_tree_list (NULL_TREE
, arg
));
25676 token
= cp_lexer_peek_token (parser
->lexer
);
25679 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
25681 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
25682 return build_tree_list (error_mark_node
, error_mark_node
);
25685 return build_tree_list (sel_args
, addl_args
);
25688 /* Parse an Objective-C encode expression.
25690 objc-encode-expression:
25691 @encode objc-typename
25693 Returns an encoded representation of the type argument. */
25696 cp_parser_objc_encode_expression (cp_parser
* parser
)
25701 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
25702 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25703 token
= cp_lexer_peek_token (parser
->lexer
);
25704 type
= complete_type (cp_parser_type_id (parser
));
25705 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25709 error_at (token
->location
,
25710 "%<@encode%> must specify a type as an argument");
25711 return error_mark_node
;
25714 /* This happens if we find @encode(T) (where T is a template
25715 typename or something dependent on a template typename) when
25716 parsing a template. In that case, we can't compile it
25717 immediately, but we rather create an AT_ENCODE_EXPR which will
25718 need to be instantiated when the template is used.
25720 if (dependent_type_p (type
))
25722 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
25723 TREE_READONLY (value
) = 1;
25727 return objc_build_encode_expr (type
);
25730 /* Parse an Objective-C @defs expression. */
25733 cp_parser_objc_defs_expression (cp_parser
*parser
)
25737 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
25738 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25739 name
= cp_parser_identifier (parser
);
25740 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25742 return objc_get_class_ivars (name
);
25745 /* Parse an Objective-C protocol expression.
25747 objc-protocol-expression:
25748 @protocol ( identifier )
25750 Returns a representation of the protocol expression. */
25753 cp_parser_objc_protocol_expression (cp_parser
* parser
)
25757 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25758 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25759 proto
= cp_parser_identifier (parser
);
25760 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25762 return objc_build_protocol_expr (proto
);
25765 /* Parse an Objective-C selector expression.
25767 objc-selector-expression:
25768 @selector ( objc-method-signature )
25770 objc-method-signature:
25776 objc-selector-seq objc-selector :
25778 Returns a representation of the method selector. */
25781 cp_parser_objc_selector_expression (cp_parser
* parser
)
25783 tree sel_seq
= NULL_TREE
;
25784 bool maybe_unary_selector_p
= true;
25786 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25788 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
25789 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25790 token
= cp_lexer_peek_token (parser
->lexer
);
25792 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
25793 || token
->type
== CPP_SCOPE
)
25795 tree selector
= NULL_TREE
;
25797 if (token
->type
!= CPP_COLON
25798 || token
->type
== CPP_SCOPE
)
25799 selector
= cp_parser_objc_selector (parser
);
25801 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
25802 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
25804 /* Detect if we have a unary selector. */
25805 if (maybe_unary_selector_p
)
25807 sel_seq
= selector
;
25808 goto finish_selector
;
25812 cp_parser_error (parser
, "expected %<:%>");
25815 maybe_unary_selector_p
= false;
25816 token
= cp_lexer_consume_token (parser
->lexer
);
25818 if (token
->type
== CPP_SCOPE
)
25821 = chainon (sel_seq
,
25822 build_tree_list (selector
, NULL_TREE
));
25824 = chainon (sel_seq
,
25825 build_tree_list (NULL_TREE
, NULL_TREE
));
25829 = chainon (sel_seq
,
25830 build_tree_list (selector
, NULL_TREE
));
25832 token
= cp_lexer_peek_token (parser
->lexer
);
25836 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25838 return objc_build_selector_expr (loc
, sel_seq
);
25841 /* Parse a list of identifiers.
25843 objc-identifier-list:
25845 objc-identifier-list , identifier
25847 Returns a TREE_LIST of identifier nodes. */
25850 cp_parser_objc_identifier_list (cp_parser
* parser
)
25856 identifier
= cp_parser_identifier (parser
);
25857 if (identifier
== error_mark_node
)
25858 return error_mark_node
;
25860 list
= build_tree_list (NULL_TREE
, identifier
);
25861 sep
= cp_lexer_peek_token (parser
->lexer
);
25863 while (sep
->type
== CPP_COMMA
)
25865 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25866 identifier
= cp_parser_identifier (parser
);
25867 if (identifier
== error_mark_node
)
25870 list
= chainon (list
, build_tree_list (NULL_TREE
,
25872 sep
= cp_lexer_peek_token (parser
->lexer
);
25878 /* Parse an Objective-C alias declaration.
25880 objc-alias-declaration:
25881 @compatibility_alias identifier identifier ;
25883 This function registers the alias mapping with the Objective-C front end.
25884 It returns nothing. */
25887 cp_parser_objc_alias_declaration (cp_parser
* parser
)
25891 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
25892 alias
= cp_parser_identifier (parser
);
25893 orig
= cp_parser_identifier (parser
);
25894 objc_declare_alias (alias
, orig
);
25895 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25898 /* Parse an Objective-C class forward-declaration.
25900 objc-class-declaration:
25901 @class objc-identifier-list ;
25903 The function registers the forward declarations with the Objective-C
25904 front end. It returns nothing. */
25907 cp_parser_objc_class_declaration (cp_parser
* parser
)
25909 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
25914 id
= cp_parser_identifier (parser
);
25915 if (id
== error_mark_node
)
25918 objc_declare_class (id
);
25920 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25921 cp_lexer_consume_token (parser
->lexer
);
25925 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25928 /* Parse a list of Objective-C protocol references.
25930 objc-protocol-refs-opt:
25931 objc-protocol-refs [opt]
25933 objc-protocol-refs:
25934 < objc-identifier-list >
25936 Returns a TREE_LIST of identifiers, if any. */
25939 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
25941 tree protorefs
= NULL_TREE
;
25943 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
25945 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
25946 protorefs
= cp_parser_objc_identifier_list (parser
);
25947 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
25953 /* Parse a Objective-C visibility specification. */
25956 cp_parser_objc_visibility_spec (cp_parser
* parser
)
25958 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
25960 switch (vis
->keyword
)
25962 case RID_AT_PRIVATE
:
25963 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
25965 case RID_AT_PROTECTED
:
25966 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
25968 case RID_AT_PUBLIC
:
25969 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
25971 case RID_AT_PACKAGE
:
25972 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
25978 /* Eat '@private'/'@protected'/'@public'. */
25979 cp_lexer_consume_token (parser
->lexer
);
25982 /* Parse an Objective-C method type. Return 'true' if it is a class
25983 (+) method, and 'false' if it is an instance (-) method. */
25986 cp_parser_objc_method_type (cp_parser
* parser
)
25988 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
25994 /* Parse an Objective-C protocol qualifier. */
25997 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
25999 tree quals
= NULL_TREE
, node
;
26000 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26002 node
= token
->u
.value
;
26004 while (node
&& identifier_p (node
)
26005 && (node
== ridpointers
[(int) RID_IN
]
26006 || node
== ridpointers
[(int) RID_OUT
]
26007 || node
== ridpointers
[(int) RID_INOUT
]
26008 || node
== ridpointers
[(int) RID_BYCOPY
]
26009 || node
== ridpointers
[(int) RID_BYREF
]
26010 || node
== ridpointers
[(int) RID_ONEWAY
]))
26012 quals
= tree_cons (NULL_TREE
, node
, quals
);
26013 cp_lexer_consume_token (parser
->lexer
);
26014 token
= cp_lexer_peek_token (parser
->lexer
);
26015 node
= token
->u
.value
;
26021 /* Parse an Objective-C typename. */
26024 cp_parser_objc_typename (cp_parser
* parser
)
26026 tree type_name
= NULL_TREE
;
26028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26030 tree proto_quals
, cp_type
= NULL_TREE
;
26032 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26033 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
26035 /* An ObjC type name may consist of just protocol qualifiers, in which
26036 case the type shall default to 'id'. */
26037 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
26039 cp_type
= cp_parser_type_id (parser
);
26041 /* If the type could not be parsed, an error has already
26042 been produced. For error recovery, behave as if it had
26043 not been specified, which will use the default type
26045 if (cp_type
== error_mark_node
)
26047 cp_type
= NULL_TREE
;
26048 /* We need to skip to the closing parenthesis as
26049 cp_parser_type_id() does not seem to do it for
26051 cp_parser_skip_to_closing_parenthesis (parser
,
26052 /*recovering=*/true,
26053 /*or_comma=*/false,
26054 /*consume_paren=*/false);
26058 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26059 type_name
= build_tree_list (proto_quals
, cp_type
);
26065 /* Check to see if TYPE refers to an Objective-C selector name. */
26068 cp_parser_objc_selector_p (enum cpp_ttype type
)
26070 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
26071 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
26072 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
26073 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
26074 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
26077 /* Parse an Objective-C selector. */
26080 cp_parser_objc_selector (cp_parser
* parser
)
26082 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
26084 if (!cp_parser_objc_selector_p (token
->type
))
26086 error_at (token
->location
, "invalid Objective-C++ selector name");
26087 return error_mark_node
;
26090 /* C++ operator names are allowed to appear in ObjC selectors. */
26091 switch (token
->type
)
26093 case CPP_AND_AND
: return get_identifier ("and");
26094 case CPP_AND_EQ
: return get_identifier ("and_eq");
26095 case CPP_AND
: return get_identifier ("bitand");
26096 case CPP_OR
: return get_identifier ("bitor");
26097 case CPP_COMPL
: return get_identifier ("compl");
26098 case CPP_NOT
: return get_identifier ("not");
26099 case CPP_NOT_EQ
: return get_identifier ("not_eq");
26100 case CPP_OR_OR
: return get_identifier ("or");
26101 case CPP_OR_EQ
: return get_identifier ("or_eq");
26102 case CPP_XOR
: return get_identifier ("xor");
26103 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
26104 default: return token
->u
.value
;
26108 /* Parse an Objective-C params list. */
26111 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
26113 tree params
= NULL_TREE
;
26114 bool maybe_unary_selector_p
= true;
26115 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26117 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
26119 tree selector
= NULL_TREE
, type_name
, identifier
;
26120 tree parm_attr
= NULL_TREE
;
26122 if (token
->keyword
== RID_ATTRIBUTE
)
26125 if (token
->type
!= CPP_COLON
)
26126 selector
= cp_parser_objc_selector (parser
);
26128 /* Detect if we have a unary selector. */
26129 if (maybe_unary_selector_p
26130 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26132 params
= selector
; /* Might be followed by attributes. */
26136 maybe_unary_selector_p
= false;
26137 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26139 /* Something went quite wrong. There should be a colon
26140 here, but there is not. Stop parsing parameters. */
26143 type_name
= cp_parser_objc_typename (parser
);
26144 /* New ObjC allows attributes on parameters too. */
26145 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
26146 parm_attr
= cp_parser_attributes_opt (parser
);
26147 identifier
= cp_parser_identifier (parser
);
26151 objc_build_keyword_decl (selector
,
26156 token
= cp_lexer_peek_token (parser
->lexer
);
26159 if (params
== NULL_TREE
)
26161 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26162 return error_mark_node
;
26165 /* We allow tail attributes for the method. */
26166 if (token
->keyword
== RID_ATTRIBUTE
)
26168 *attributes
= cp_parser_attributes_opt (parser
);
26169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26170 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26172 cp_parser_error (parser
,
26173 "method attributes must be specified at the end");
26174 return error_mark_node
;
26177 if (params
== NULL_TREE
)
26179 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26180 return error_mark_node
;
26185 /* Parse the non-keyword Objective-C params. */
26188 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
26191 tree params
= make_node (TREE_LIST
);
26192 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26193 *ellipsisp
= false; /* Initially, assume no ellipsis. */
26195 while (token
->type
== CPP_COMMA
)
26197 cp_parameter_declarator
*parmdecl
;
26200 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26201 token
= cp_lexer_peek_token (parser
->lexer
);
26203 if (token
->type
== CPP_ELLIPSIS
)
26205 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
26207 token
= cp_lexer_peek_token (parser
->lexer
);
26211 /* TODO: parse attributes for tail parameters. */
26212 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
26213 parm
= grokdeclarator (parmdecl
->declarator
,
26214 &parmdecl
->decl_specifiers
,
26215 PARM
, /*initialized=*/0,
26216 /*attrlist=*/NULL
);
26218 chainon (params
, build_tree_list (NULL_TREE
, parm
));
26219 token
= cp_lexer_peek_token (parser
->lexer
);
26222 /* We allow tail attributes for the method. */
26223 if (token
->keyword
== RID_ATTRIBUTE
)
26225 if (*attributes
== NULL_TREE
)
26227 *attributes
= cp_parser_attributes_opt (parser
);
26228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26229 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26233 /* We have an error, but parse the attributes, so that we can
26235 *attributes
= cp_parser_attributes_opt (parser
);
26237 cp_parser_error (parser
,
26238 "method attributes must be specified at the end");
26239 return error_mark_node
;
26245 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26248 cp_parser_objc_interstitial_code (cp_parser
* parser
)
26250 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26252 /* If the next token is `extern' and the following token is a string
26253 literal, then we have a linkage specification. */
26254 if (token
->keyword
== RID_EXTERN
26255 && cp_parser_is_pure_string_literal
26256 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
26257 cp_parser_linkage_specification (parser
);
26258 /* Handle #pragma, if any. */
26259 else if (token
->type
== CPP_PRAGMA
)
26260 cp_parser_pragma (parser
, pragma_objc_icode
);
26261 /* Allow stray semicolons. */
26262 else if (token
->type
== CPP_SEMICOLON
)
26263 cp_lexer_consume_token (parser
->lexer
);
26264 /* Mark methods as optional or required, when building protocols. */
26265 else if (token
->keyword
== RID_AT_OPTIONAL
)
26267 cp_lexer_consume_token (parser
->lexer
);
26268 objc_set_method_opt (true);
26270 else if (token
->keyword
== RID_AT_REQUIRED
)
26272 cp_lexer_consume_token (parser
->lexer
);
26273 objc_set_method_opt (false);
26275 else if (token
->keyword
== RID_NAMESPACE
)
26276 cp_parser_namespace_definition (parser
);
26277 /* Other stray characters must generate errors. */
26278 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
26280 cp_lexer_consume_token (parser
->lexer
);
26281 error ("stray %qs between Objective-C++ methods",
26282 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
26284 /* Finally, try to parse a block-declaration, or a function-definition. */
26286 cp_parser_block_declaration (parser
, /*statement_p=*/false);
26289 /* Parse a method signature. */
26292 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
26294 tree rettype
, kwdparms
, optparms
;
26295 bool ellipsis
= false;
26296 bool is_class_method
;
26298 is_class_method
= cp_parser_objc_method_type (parser
);
26299 rettype
= cp_parser_objc_typename (parser
);
26300 *attributes
= NULL_TREE
;
26301 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
26302 if (kwdparms
== error_mark_node
)
26303 return error_mark_node
;
26304 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
26305 if (optparms
== error_mark_node
)
26306 return error_mark_node
;
26308 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
26312 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
26315 cp_lexer_save_tokens (parser
->lexer
);
26316 tattr
= cp_parser_attributes_opt (parser
);
26317 gcc_assert (tattr
) ;
26319 /* If the attributes are followed by a method introducer, this is not allowed.
26320 Dump the attributes and flag the situation. */
26321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
26322 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
26325 /* Otherwise, the attributes introduce some interstitial code, possibly so
26326 rewind to allow that check. */
26327 cp_lexer_rollback_tokens (parser
->lexer
);
26331 /* Parse an Objective-C method prototype list. */
26334 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
26336 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26338 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26340 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26342 tree attributes
, sig
;
26343 bool is_class_method
;
26344 if (token
->type
== CPP_PLUS
)
26345 is_class_method
= true;
26347 is_class_method
= false;
26348 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
26349 if (sig
== error_mark_node
)
26351 cp_parser_skip_to_end_of_block_or_statement (parser
);
26352 token
= cp_lexer_peek_token (parser
->lexer
);
26355 objc_add_method_declaration (is_class_method
, sig
, attributes
);
26356 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26358 else if (token
->keyword
== RID_AT_PROPERTY
)
26359 cp_parser_objc_at_property_declaration (parser
);
26360 else if (token
->keyword
== RID_ATTRIBUTE
26361 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26362 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26364 "prefix attributes are ignored for methods");
26366 /* Allow for interspersed non-ObjC++ code. */
26367 cp_parser_objc_interstitial_code (parser
);
26369 token
= cp_lexer_peek_token (parser
->lexer
);
26372 if (token
->type
!= CPP_EOF
)
26373 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26375 cp_parser_error (parser
, "expected %<@end%>");
26377 objc_finish_interface ();
26380 /* Parse an Objective-C method definition list. */
26383 cp_parser_objc_method_definition_list (cp_parser
* parser
)
26385 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26387 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26391 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26394 tree sig
, attribute
;
26395 bool is_class_method
;
26396 if (token
->type
== CPP_PLUS
)
26397 is_class_method
= true;
26399 is_class_method
= false;
26400 push_deferring_access_checks (dk_deferred
);
26401 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
26402 if (sig
== error_mark_node
)
26404 cp_parser_skip_to_end_of_block_or_statement (parser
);
26405 token
= cp_lexer_peek_token (parser
->lexer
);
26408 objc_start_method_definition (is_class_method
, sig
, attribute
,
26411 /* For historical reasons, we accept an optional semicolon. */
26412 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26413 cp_lexer_consume_token (parser
->lexer
);
26415 ptk
= cp_lexer_peek_token (parser
->lexer
);
26416 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
26417 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
26419 perform_deferred_access_checks (tf_warning_or_error
);
26420 stop_deferring_access_checks ();
26421 meth
= cp_parser_function_definition_after_declarator (parser
,
26423 pop_deferring_access_checks ();
26424 objc_finish_method_definition (meth
);
26427 /* The following case will be removed once @synthesize is
26428 completely implemented. */
26429 else if (token
->keyword
== RID_AT_PROPERTY
)
26430 cp_parser_objc_at_property_declaration (parser
);
26431 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
26432 cp_parser_objc_at_synthesize_declaration (parser
);
26433 else if (token
->keyword
== RID_AT_DYNAMIC
)
26434 cp_parser_objc_at_dynamic_declaration (parser
);
26435 else if (token
->keyword
== RID_ATTRIBUTE
26436 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26437 warning_at (token
->location
, OPT_Wattributes
,
26438 "prefix attributes are ignored for methods");
26440 /* Allow for interspersed non-ObjC++ code. */
26441 cp_parser_objc_interstitial_code (parser
);
26443 token
= cp_lexer_peek_token (parser
->lexer
);
26446 if (token
->type
!= CPP_EOF
)
26447 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26449 cp_parser_error (parser
, "expected %<@end%>");
26451 objc_finish_implementation ();
26454 /* Parse Objective-C ivars. */
26457 cp_parser_objc_class_ivars (cp_parser
* parser
)
26459 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26461 if (token
->type
!= CPP_OPEN_BRACE
)
26462 return; /* No ivars specified. */
26464 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
26465 token
= cp_lexer_peek_token (parser
->lexer
);
26467 while (token
->type
!= CPP_CLOSE_BRACE
26468 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26470 cp_decl_specifier_seq declspecs
;
26471 int decl_class_or_enum_p
;
26472 tree prefix_attributes
;
26474 cp_parser_objc_visibility_spec (parser
);
26476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26479 cp_parser_decl_specifier_seq (parser
,
26480 CP_PARSER_FLAGS_OPTIONAL
,
26482 &decl_class_or_enum_p
);
26484 /* auto, register, static, extern, mutable. */
26485 if (declspecs
.storage_class
!= sc_none
)
26487 cp_parser_error (parser
, "invalid type for instance variable");
26488 declspecs
.storage_class
= sc_none
;
26491 /* thread_local. */
26492 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26494 cp_parser_error (parser
, "invalid type for instance variable");
26495 declspecs
.locations
[ds_thread
] = 0;
26499 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26501 cp_parser_error (parser
, "invalid type for instance variable");
26502 declspecs
.locations
[ds_typedef
] = 0;
26505 prefix_attributes
= declspecs
.attributes
;
26506 declspecs
.attributes
= NULL_TREE
;
26508 /* Keep going until we hit the `;' at the end of the
26510 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26512 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
26513 cp_declarator
*declarator
= NULL
;
26514 int ctor_dtor_or_conv_p
;
26516 /* Check for a (possibly unnamed) bitfield declaration. */
26517 token
= cp_lexer_peek_token (parser
->lexer
);
26518 if (token
->type
== CPP_COLON
)
26521 if (token
->type
== CPP_NAME
26522 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
26525 /* Get the name of the bitfield. */
26526 declarator
= make_id_declarator (NULL_TREE
,
26527 cp_parser_identifier (parser
),
26531 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26532 /* Get the width of the bitfield. */
26534 = cp_parser_constant_expression (parser
);
26538 /* Parse the declarator. */
26540 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26541 &ctor_dtor_or_conv_p
,
26542 /*parenthesized_p=*/NULL
,
26543 /*member_p=*/false,
26544 /*friend_p=*/false);
26547 /* Look for attributes that apply to the ivar. */
26548 attributes
= cp_parser_attributes_opt (parser
);
26549 /* Remember which attributes are prefix attributes and
26551 first_attribute
= attributes
;
26552 /* Combine the attributes. */
26553 attributes
= chainon (prefix_attributes
, attributes
);
26556 /* Create the bitfield declaration. */
26557 decl
= grokbitfield (declarator
, &declspecs
,
26561 decl
= grokfield (declarator
, &declspecs
,
26562 NULL_TREE
, /*init_const_expr_p=*/false,
26563 NULL_TREE
, attributes
);
26565 /* Add the instance variable. */
26566 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
26567 objc_add_instance_variable (decl
);
26569 /* Reset PREFIX_ATTRIBUTES. */
26570 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26571 attributes
= TREE_CHAIN (attributes
);
26573 TREE_CHAIN (attributes
) = NULL_TREE
;
26575 token
= cp_lexer_peek_token (parser
->lexer
);
26577 if (token
->type
== CPP_COMMA
)
26579 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26585 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26586 token
= cp_lexer_peek_token (parser
->lexer
);
26589 if (token
->keyword
== RID_AT_END
)
26590 cp_parser_error (parser
, "expected %<}%>");
26592 /* Do not consume the RID_AT_END, so it will be read again as terminating
26593 the @interface of @implementation. */
26594 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26595 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
26597 /* For historical reasons, we accept an optional semicolon. */
26598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26599 cp_lexer_consume_token (parser
->lexer
);
26602 /* Parse an Objective-C protocol declaration. */
26605 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
26607 tree proto
, protorefs
;
26610 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
26611 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
26613 tok
= cp_lexer_peek_token (parser
->lexer
);
26614 error_at (tok
->location
, "identifier expected after %<@protocol%>");
26615 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26619 /* See if we have a forward declaration or a definition. */
26620 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26622 /* Try a forward declaration first. */
26623 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
26629 id
= cp_parser_identifier (parser
);
26630 if (id
== error_mark_node
)
26633 objc_declare_protocol (id
, attributes
);
26635 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26636 cp_lexer_consume_token (parser
->lexer
);
26640 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26643 /* Ok, we got a full-fledged definition (or at least should). */
26646 proto
= cp_parser_identifier (parser
);
26647 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
26648 objc_start_protocol (proto
, protorefs
, attributes
);
26649 cp_parser_objc_method_prototype_list (parser
);
26653 /* Parse an Objective-C superclass or category. */
26656 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
26659 tree
*categ
, bool *is_class_extension
)
26661 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
26663 *super
= *categ
= NULL_TREE
;
26664 *is_class_extension
= false;
26665 if (next
->type
== CPP_COLON
)
26667 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26668 *super
= cp_parser_identifier (parser
);
26670 else if (next
->type
== CPP_OPEN_PAREN
)
26672 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26674 /* If there is no category name, and this is an @interface, we
26675 have a class extension. */
26676 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26678 *categ
= NULL_TREE
;
26679 *is_class_extension
= true;
26682 *categ
= cp_parser_identifier (parser
);
26684 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26688 /* Parse an Objective-C class interface. */
26691 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
26693 tree name
, super
, categ
, protos
;
26694 bool is_class_extension
;
26696 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
26697 name
= cp_parser_identifier (parser
);
26698 if (name
== error_mark_node
)
26700 /* It's hard to recover because even if valid @interface stuff
26701 is to follow, we can't compile it (or validate it) if we
26702 don't even know which class it refers to. Let's assume this
26703 was a stray '@interface' token in the stream and skip it.
26707 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
26708 &is_class_extension
);
26709 protos
= cp_parser_objc_protocol_refs_opt (parser
);
26711 /* We have either a class or a category on our hands. */
26712 if (categ
|| is_class_extension
)
26713 objc_start_category_interface (name
, categ
, protos
, attributes
);
26716 objc_start_class_interface (name
, super
, protos
, attributes
);
26717 /* Handle instance variable declarations, if any. */
26718 cp_parser_objc_class_ivars (parser
);
26719 objc_continue_interface ();
26722 cp_parser_objc_method_prototype_list (parser
);
26725 /* Parse an Objective-C class implementation. */
26728 cp_parser_objc_class_implementation (cp_parser
* parser
)
26730 tree name
, super
, categ
;
26731 bool is_class_extension
;
26733 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
26734 name
= cp_parser_identifier (parser
);
26735 if (name
== error_mark_node
)
26737 /* It's hard to recover because even if valid @implementation
26738 stuff is to follow, we can't compile it (or validate it) if
26739 we don't even know which class it refers to. Let's assume
26740 this was a stray '@implementation' token in the stream and
26745 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
26746 &is_class_extension
);
26748 /* We have either a class or a category on our hands. */
26750 objc_start_category_implementation (name
, categ
);
26753 objc_start_class_implementation (name
, super
);
26754 /* Handle instance variable declarations, if any. */
26755 cp_parser_objc_class_ivars (parser
);
26756 objc_continue_implementation ();
26759 cp_parser_objc_method_definition_list (parser
);
26762 /* Consume the @end token and finish off the implementation. */
26765 cp_parser_objc_end_implementation (cp_parser
* parser
)
26767 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26768 objc_finish_implementation ();
26771 /* Parse an Objective-C declaration. */
26774 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
26776 /* Try to figure out what kind of declaration is present. */
26777 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26780 switch (kwd
->keyword
)
26785 error_at (kwd
->location
, "attributes may not be specified before"
26786 " the %<@%D%> Objective-C++ keyword",
26790 case RID_AT_IMPLEMENTATION
:
26791 warning_at (kwd
->location
, OPT_Wattributes
,
26792 "prefix attributes are ignored before %<@%D%>",
26799 switch (kwd
->keyword
)
26802 cp_parser_objc_alias_declaration (parser
);
26805 cp_parser_objc_class_declaration (parser
);
26807 case RID_AT_PROTOCOL
:
26808 cp_parser_objc_protocol_declaration (parser
, attributes
);
26810 case RID_AT_INTERFACE
:
26811 cp_parser_objc_class_interface (parser
, attributes
);
26813 case RID_AT_IMPLEMENTATION
:
26814 cp_parser_objc_class_implementation (parser
);
26817 cp_parser_objc_end_implementation (parser
);
26820 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26822 cp_parser_skip_to_end_of_block_or_statement (parser
);
26826 /* Parse an Objective-C try-catch-finally statement.
26828 objc-try-catch-finally-stmt:
26829 @try compound-statement objc-catch-clause-seq [opt]
26830 objc-finally-clause [opt]
26832 objc-catch-clause-seq:
26833 objc-catch-clause objc-catch-clause-seq [opt]
26836 @catch ( objc-exception-declaration ) compound-statement
26838 objc-finally-clause:
26839 @finally compound-statement
26841 objc-exception-declaration:
26842 parameter-declaration
26845 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26849 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26850 for C. Keep them in sync. */
26853 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
26855 location_t location
;
26858 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
26859 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26860 objc_maybe_warn_exceptions (location
);
26861 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26862 node, lest it get absorbed into the surrounding block. */
26863 stmt
= push_stmt_list ();
26864 cp_parser_compound_statement (parser
, NULL
, false, false);
26865 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
26867 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
26869 cp_parameter_declarator
*parm
;
26870 tree parameter_declaration
= error_mark_node
;
26871 bool seen_open_paren
= false;
26873 cp_lexer_consume_token (parser
->lexer
);
26874 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26875 seen_open_paren
= true;
26876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26878 /* We have "@catch (...)" (where the '...' are literally
26879 what is in the code). Skip the '...'.
26880 parameter_declaration is set to NULL_TREE, and
26881 objc_being_catch_clauses() knows that that means
26883 cp_lexer_consume_token (parser
->lexer
);
26884 parameter_declaration
= NULL_TREE
;
26888 /* We have "@catch (NSException *exception)" or something
26889 like that. Parse the parameter declaration. */
26890 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
26892 parameter_declaration
= error_mark_node
;
26894 parameter_declaration
= grokdeclarator (parm
->declarator
,
26895 &parm
->decl_specifiers
,
26896 PARM
, /*initialized=*/0,
26897 /*attrlist=*/NULL
);
26899 if (seen_open_paren
)
26900 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26903 /* If there was no open parenthesis, we are recovering from
26904 an error, and we are trying to figure out what mistake
26905 the user has made. */
26907 /* If there is an immediate closing parenthesis, the user
26908 probably forgot the opening one (ie, they typed "@catch
26909 NSException *e)". Parse the closing parenthesis and keep
26911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26912 cp_lexer_consume_token (parser
->lexer
);
26914 /* If these is no immediate closing parenthesis, the user
26915 probably doesn't know that parenthesis are required at
26916 all (ie, they typed "@catch NSException *e"). So, just
26917 forget about the closing parenthesis and keep going. */
26919 objc_begin_catch_clause (parameter_declaration
);
26920 cp_parser_compound_statement (parser
, NULL
, false, false);
26921 objc_finish_catch_clause ();
26923 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
26925 cp_lexer_consume_token (parser
->lexer
);
26926 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26927 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26928 node, lest it get absorbed into the surrounding block. */
26929 stmt
= push_stmt_list ();
26930 cp_parser_compound_statement (parser
, NULL
, false, false);
26931 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
26934 return objc_finish_try_stmt ();
26937 /* Parse an Objective-C synchronized statement.
26939 objc-synchronized-stmt:
26940 @synchronized ( expression ) compound-statement
26942 Returns NULL_TREE. */
26945 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
26947 location_t location
;
26950 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
26952 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26953 objc_maybe_warn_exceptions (location
);
26954 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
26955 lock
= cp_parser_expression (parser
);
26956 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26958 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26959 node, lest it get absorbed into the surrounding block. */
26960 stmt
= push_stmt_list ();
26961 cp_parser_compound_statement (parser
, NULL
, false, false);
26963 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
26966 /* Parse an Objective-C throw statement.
26969 @throw assignment-expression [opt] ;
26971 Returns a constructed '@throw' statement. */
26974 cp_parser_objc_throw_statement (cp_parser
*parser
)
26976 tree expr
= NULL_TREE
;
26977 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26979 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
26981 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26982 expr
= cp_parser_expression (parser
);
26984 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26986 return objc_build_throw_stmt (loc
, expr
);
26989 /* Parse an Objective-C statement. */
26992 cp_parser_objc_statement (cp_parser
* parser
)
26994 /* Try to figure out what kind of declaration is present. */
26995 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26997 switch (kwd
->keyword
)
27000 return cp_parser_objc_try_catch_finally_statement (parser
);
27001 case RID_AT_SYNCHRONIZED
:
27002 return cp_parser_objc_synchronized_statement (parser
);
27004 return cp_parser_objc_throw_statement (parser
);
27006 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
27008 cp_parser_skip_to_end_of_block_or_statement (parser
);
27011 return error_mark_node
;
27014 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27015 look ahead to see if an objc keyword follows the attributes. This
27016 is to detect the use of prefix attributes on ObjC @interface and
27020 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
27022 cp_lexer_save_tokens (parser
->lexer
);
27023 *attrib
= cp_parser_attributes_opt (parser
);
27024 gcc_assert (*attrib
);
27025 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
27027 cp_lexer_commit_tokens (parser
->lexer
);
27030 cp_lexer_rollback_tokens (parser
->lexer
);
27034 /* This routine is a minimal replacement for
27035 c_parser_struct_declaration () used when parsing the list of
27036 types/names or ObjC++ properties. For example, when parsing the
27039 @property (readonly) int a, b, c;
27041 this function is responsible for parsing "int a, int b, int c" and
27042 returning the declarations as CHAIN of DECLs.
27044 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27045 similar parsing. */
27047 cp_parser_objc_struct_declaration (cp_parser
*parser
)
27049 tree decls
= NULL_TREE
;
27050 cp_decl_specifier_seq declspecs
;
27051 int decl_class_or_enum_p
;
27052 tree prefix_attributes
;
27054 cp_parser_decl_specifier_seq (parser
,
27055 CP_PARSER_FLAGS_NONE
,
27057 &decl_class_or_enum_p
);
27059 if (declspecs
.type
== error_mark_node
)
27060 return error_mark_node
;
27062 /* auto, register, static, extern, mutable. */
27063 if (declspecs
.storage_class
!= sc_none
)
27065 cp_parser_error (parser
, "invalid type for property");
27066 declspecs
.storage_class
= sc_none
;
27069 /* thread_local. */
27070 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
27072 cp_parser_error (parser
, "invalid type for property");
27073 declspecs
.locations
[ds_thread
] = 0;
27077 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
27079 cp_parser_error (parser
, "invalid type for property");
27080 declspecs
.locations
[ds_typedef
] = 0;
27083 prefix_attributes
= declspecs
.attributes
;
27084 declspecs
.attributes
= NULL_TREE
;
27086 /* Keep going until we hit the `;' at the end of the declaration. */
27087 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27089 tree attributes
, first_attribute
, decl
;
27090 cp_declarator
*declarator
;
27093 /* Parse the declarator. */
27094 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27095 NULL
, NULL
, false, false);
27097 /* Look for attributes that apply to the ivar. */
27098 attributes
= cp_parser_attributes_opt (parser
);
27099 /* Remember which attributes are prefix attributes and
27101 first_attribute
= attributes
;
27102 /* Combine the attributes. */
27103 attributes
= chainon (prefix_attributes
, attributes
);
27105 decl
= grokfield (declarator
, &declspecs
,
27106 NULL_TREE
, /*init_const_expr_p=*/false,
27107 NULL_TREE
, attributes
);
27109 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
27110 return error_mark_node
;
27112 /* Reset PREFIX_ATTRIBUTES. */
27113 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
27114 attributes
= TREE_CHAIN (attributes
);
27116 TREE_CHAIN (attributes
) = NULL_TREE
;
27118 DECL_CHAIN (decl
) = decls
;
27121 token
= cp_lexer_peek_token (parser
->lexer
);
27122 if (token
->type
== CPP_COMMA
)
27124 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27133 /* Parse an Objective-C @property declaration. The syntax is:
27135 objc-property-declaration:
27136 '@property' objc-property-attributes[opt] struct-declaration ;
27138 objc-property-attributes:
27139 '(' objc-property-attribute-list ')'
27141 objc-property-attribute-list:
27142 objc-property-attribute
27143 objc-property-attribute-list, objc-property-attribute
27145 objc-property-attribute
27146 'getter' = identifier
27147 'setter' = identifier
27156 @property NSString *name;
27157 @property (readonly) id object;
27158 @property (retain, nonatomic, getter=getTheName) id name;
27159 @property int a, b, c;
27161 PS: This function is identical to
27162 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27164 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
27166 /* The following variables hold the attributes of the properties as
27167 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27168 seen. When we see an attribute, we set them to 'true' (if they
27169 are boolean properties) or to the identifier (if they have an
27170 argument, ie, for getter and setter). Note that here we only
27171 parse the list of attributes, check the syntax and accumulate the
27172 attributes that we find. objc_add_property_declaration() will
27173 then process the information. */
27174 bool property_assign
= false;
27175 bool property_copy
= false;
27176 tree property_getter_ident
= NULL_TREE
;
27177 bool property_nonatomic
= false;
27178 bool property_readonly
= false;
27179 bool property_readwrite
= false;
27180 bool property_retain
= false;
27181 tree property_setter_ident
= NULL_TREE
;
27183 /* 'properties' is the list of properties that we read. Usually a
27184 single one, but maybe more (eg, in "@property int a, b, c;" there
27189 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27191 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
27193 /* Parse the optional attribute list... */
27194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27197 cp_lexer_consume_token (parser
->lexer
);
27201 bool syntax_error
= false;
27202 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27205 if (token
->type
!= CPP_NAME
)
27207 cp_parser_error (parser
, "expected identifier");
27210 keyword
= C_RID_CODE (token
->u
.value
);
27211 cp_lexer_consume_token (parser
->lexer
);
27214 case RID_ASSIGN
: property_assign
= true; break;
27215 case RID_COPY
: property_copy
= true; break;
27216 case RID_NONATOMIC
: property_nonatomic
= true; break;
27217 case RID_READONLY
: property_readonly
= true; break;
27218 case RID_READWRITE
: property_readwrite
= true; break;
27219 case RID_RETAIN
: property_retain
= true; break;
27223 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
27225 if (keyword
== RID_GETTER
)
27226 cp_parser_error (parser
,
27227 "missing %<=%> (after %<getter%> attribute)");
27229 cp_parser_error (parser
,
27230 "missing %<=%> (after %<setter%> attribute)");
27231 syntax_error
= true;
27234 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
27235 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
27237 cp_parser_error (parser
, "expected identifier");
27238 syntax_error
= true;
27241 if (keyword
== RID_SETTER
)
27243 if (property_setter_ident
!= NULL_TREE
)
27245 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
27246 cp_lexer_consume_token (parser
->lexer
);
27249 property_setter_ident
= cp_parser_objc_selector (parser
);
27250 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27251 cp_parser_error (parser
, "setter name must terminate with %<:%>");
27253 cp_lexer_consume_token (parser
->lexer
);
27257 if (property_getter_ident
!= NULL_TREE
)
27259 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
27260 cp_lexer_consume_token (parser
->lexer
);
27263 property_getter_ident
= cp_parser_objc_selector (parser
);
27267 cp_parser_error (parser
, "unknown property attribute");
27268 syntax_error
= true;
27275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27276 cp_lexer_consume_token (parser
->lexer
);
27281 /* FIXME: "@property (setter, assign);" will generate a spurious
27282 "error: expected ‘)’ before ‘,’ token". This is because
27283 cp_parser_require, unlike the C counterpart, will produce an
27284 error even if we are in error recovery. */
27285 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27287 cp_parser_skip_to_closing_parenthesis (parser
,
27288 /*recovering=*/true,
27289 /*or_comma=*/false,
27290 /*consume_paren=*/true);
27294 /* ... and the property declaration(s). */
27295 properties
= cp_parser_objc_struct_declaration (parser
);
27297 if (properties
== error_mark_node
)
27299 cp_parser_skip_to_end_of_statement (parser
);
27300 /* If the next token is now a `;', consume it. */
27301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27302 cp_lexer_consume_token (parser
->lexer
);
27306 if (properties
== NULL_TREE
)
27307 cp_parser_error (parser
, "expected identifier");
27310 /* Comma-separated properties are chained together in
27311 reverse order; add them one by one. */
27312 properties
= nreverse (properties
);
27314 for (; properties
; properties
= TREE_CHAIN (properties
))
27315 objc_add_property_declaration (loc
, copy_node (properties
),
27316 property_readonly
, property_readwrite
,
27317 property_assign
, property_retain
,
27318 property_copy
, property_nonatomic
,
27319 property_getter_ident
, property_setter_ident
);
27322 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27325 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27327 objc-synthesize-declaration:
27328 @synthesize objc-synthesize-identifier-list ;
27330 objc-synthesize-identifier-list:
27331 objc-synthesize-identifier
27332 objc-synthesize-identifier-list, objc-synthesize-identifier
27334 objc-synthesize-identifier
27336 identifier = identifier
27339 @synthesize MyProperty;
27340 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27342 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27343 for C. Keep them in sync.
27346 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
27348 tree list
= NULL_TREE
;
27350 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27352 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
27355 tree property
, ivar
;
27356 property
= cp_parser_identifier (parser
);
27357 if (property
== error_mark_node
)
27359 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
27364 cp_lexer_consume_token (parser
->lexer
);
27365 ivar
= cp_parser_identifier (parser
);
27366 if (ivar
== error_mark_node
)
27368 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27374 list
= chainon (list
, build_tree_list (ivar
, property
));
27375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27376 cp_lexer_consume_token (parser
->lexer
);
27380 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27381 objc_add_synthesize_declaration (loc
, list
);
27384 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27386 objc-dynamic-declaration:
27387 @dynamic identifier-list ;
27390 @dynamic MyProperty;
27391 @dynamic MyProperty, AnotherProperty;
27393 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27394 for C. Keep them in sync.
27397 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
27399 tree list
= NULL_TREE
;
27401 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27403 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
27407 property
= cp_parser_identifier (parser
);
27408 if (property
== error_mark_node
)
27410 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27413 list
= chainon (list
, build_tree_list (NULL
, property
));
27414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27415 cp_lexer_consume_token (parser
->lexer
);
27419 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27420 objc_add_dynamic_declaration (loc
, list
);
27424 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27426 /* Returns name of the next clause.
27427 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27428 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27429 returned and the token is consumed. */
27431 static pragma_omp_clause
27432 cp_parser_omp_clause_name (cp_parser
*parser
)
27434 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
27436 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
27437 result
= PRAGMA_OMP_CLAUSE_IF
;
27438 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
27439 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
27440 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
27441 result
= PRAGMA_OMP_CLAUSE_DELETE
;
27442 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
27443 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
27444 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27445 result
= PRAGMA_OMP_CLAUSE_FOR
;
27446 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27448 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27449 const char *p
= IDENTIFIER_POINTER (id
);
27454 if (!strcmp ("aligned", p
))
27455 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
27456 else if (!strcmp ("async", p
))
27457 result
= PRAGMA_OMP_CLAUSE_ASYNC
;
27460 if (!strcmp ("collapse", p
))
27461 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
27462 else if (!strcmp ("copy", p
))
27463 result
= PRAGMA_OMP_CLAUSE_COPY
;
27464 else if (!strcmp ("copyin", p
))
27465 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
27466 else if (!strcmp ("copyout", p
))
27467 result
= PRAGMA_OMP_CLAUSE_COPYOUT
;
27468 else if (!strcmp ("copyprivate", p
))
27469 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
27470 else if (!strcmp ("create", p
))
27471 result
= PRAGMA_OMP_CLAUSE_CREATE
;
27474 if (!strcmp ("depend", p
))
27475 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
27476 else if (!strcmp ("device", p
))
27477 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
27478 else if (!strcmp ("deviceptr", p
))
27479 result
= PRAGMA_OMP_CLAUSE_DEVICEPTR
;
27480 else if (!strcmp ("dist_schedule", p
))
27481 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
27484 if (!strcmp ("final", p
))
27485 result
= PRAGMA_OMP_CLAUSE_FINAL
;
27486 else if (!strcmp ("firstprivate", p
))
27487 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
27488 else if (!strcmp ("from", p
))
27489 result
= PRAGMA_OMP_CLAUSE_FROM
;
27492 if (!strcmp ("host", p
))
27493 result
= PRAGMA_OMP_CLAUSE_HOST
;
27496 if (!strcmp ("inbranch", p
))
27497 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
27500 if (!strcmp ("lastprivate", p
))
27501 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
27502 else if (!strcmp ("linear", p
))
27503 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
27506 if (!strcmp ("map", p
))
27507 result
= PRAGMA_OMP_CLAUSE_MAP
;
27508 else if (!strcmp ("mergeable", p
))
27509 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
27510 else if (flag_cilkplus
&& !strcmp ("mask", p
))
27511 result
= PRAGMA_CILK_CLAUSE_MASK
;
27514 if (!strcmp ("notinbranch", p
))
27515 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
27516 else if (!strcmp ("nowait", p
))
27517 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
27518 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
27519 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
27520 else if (!strcmp ("num_gangs", p
))
27521 result
= PRAGMA_OMP_CLAUSE_NUM_GANGS
;
27522 else if (!strcmp ("num_teams", p
))
27523 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
27524 else if (!strcmp ("num_threads", p
))
27525 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
27526 else if (!strcmp ("num_workers", p
))
27527 result
= PRAGMA_OMP_CLAUSE_NUM_WORKERS
;
27530 if (!strcmp ("ordered", p
))
27531 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
27534 if (!strcmp ("parallel", p
))
27535 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
27536 else if (!strcmp ("present", p
))
27537 result
= PRAGMA_OMP_CLAUSE_PRESENT
;
27538 else if (!strcmp ("present_or_copy", p
))
27539 result
= PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY
;
27540 else if (!strcmp ("present_or_copyin", p
))
27541 result
= PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN
;
27542 else if (!strcmp ("present_or_copyout", p
))
27543 result
= PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT
;
27544 else if (!strcmp ("present_or_create", p
))
27545 result
= PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE
;
27546 else if (!strcmp ("private", p
))
27547 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
27548 else if (!strcmp ("proc_bind", p
))
27549 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
27552 if (!strcmp ("reduction", p
))
27553 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
27556 if (!strcmp ("safelen", p
))
27557 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
27558 else if (!strcmp ("schedule", p
))
27559 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
27560 else if (!strcmp ("sections", p
))
27561 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
27562 else if (!strcmp ("self", p
))
27563 result
= PRAGMA_OMP_CLAUSE_SELF
;
27564 else if (!strcmp ("shared", p
))
27565 result
= PRAGMA_OMP_CLAUSE_SHARED
;
27566 else if (!strcmp ("simdlen", p
))
27567 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
27570 if (!strcmp ("taskgroup", p
))
27571 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
27572 else if (!strcmp ("thread_limit", p
))
27573 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
27574 else if (!strcmp ("to", p
))
27575 result
= PRAGMA_OMP_CLAUSE_TO
;
27578 if (!strcmp ("uniform", p
))
27579 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
27580 else if (!strcmp ("untied", p
))
27581 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
27584 if (!strcmp ("vector_length", p
))
27585 result
= PRAGMA_OMP_CLAUSE_VECTOR_LENGTH
;
27586 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
27587 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
27590 if (!strcmp ("WAIT", p
))
27591 result
= PRAGMA_OMP_CLAUSE_WAIT
;
27596 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
27597 cp_lexer_consume_token (parser
->lexer
);
27602 /* Validate that a clause of the given type does not already exist. */
27605 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
27606 const char *name
, location_t location
)
27610 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27611 if (OMP_CLAUSE_CODE (c
) == code
)
27613 error_at (location
, "too many %qs clauses", name
);
27621 variable-list , identifier
27623 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27624 colon). An opening parenthesis will have been consumed by the caller.
27626 If KIND is nonzero, create the appropriate node and install the decl
27627 in OMP_CLAUSE_DECL and add the node to the head of the list.
27629 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27630 return the list created.
27632 COLON can be NULL if only closing parenthesis should end the list,
27633 or pointer to bool which will receive false if the list is terminated
27634 by closing parenthesis or true if the list is terminated by colon. */
27637 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
27638 tree list
, bool *colon
)
27641 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27644 parser
->colon_corrects_to_scope_p
= false;
27651 token
= cp_lexer_peek_token (parser
->lexer
);
27652 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
27653 /*check_dependency_p=*/true,
27654 /*template_p=*/NULL
,
27655 /*declarator_p=*/false,
27656 /*optional_p=*/false);
27657 if (name
== error_mark_node
)
27660 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27661 if (decl
== error_mark_node
)
27662 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
27664 else if (kind
!= 0)
27668 case OMP_NO_CLAUSE_CACHE
:
27669 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
27671 error_at (token
->location
, "expected %<[%>");
27672 decl
= error_mark_node
;
27675 /* FALL THROUGH. */
27676 case OMP_CLAUSE_MAP
:
27677 case OMP_CLAUSE_FROM
:
27678 case OMP_CLAUSE_TO
:
27679 case OMP_CLAUSE_DEPEND
:
27680 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27682 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
27684 parser
->colon_corrects_to_scope_p
= false;
27685 cp_lexer_consume_token (parser
->lexer
);
27686 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27687 low_bound
= cp_parser_expression (parser
);
27689 parser
->colon_corrects_to_scope_p
27690 = saved_colon_corrects_to_scope_p
;
27691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
27692 length
= integer_one_node
;
27695 /* Look for `:'. */
27696 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27698 if (!cp_lexer_next_token_is (parser
->lexer
,
27700 length
= cp_parser_expression (parser
);
27702 /* Look for the closing `]'. */
27703 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
27707 if (kind
== OMP_NO_CLAUSE_CACHE
)
27709 mark_exp_read (low_bound
);
27710 mark_exp_read (length
);
27712 if (TREE_CODE (low_bound
) != INTEGER_CST
27713 && !TREE_READONLY (low_bound
))
27715 error_at (token
->location
,
27716 "%qD is not a constant", low_bound
);
27717 decl
= error_mark_node
;
27720 if (TREE_CODE (length
) != INTEGER_CST
27721 && !TREE_READONLY (length
))
27723 error_at (token
->location
,
27724 "%qD is not a constant", length
);
27725 decl
= error_mark_node
;
27729 decl
= tree_cons (low_bound
, length
, decl
);
27736 tree u
= build_omp_clause (token
->location
, kind
);
27737 OMP_CLAUSE_DECL (u
) = decl
;
27738 OMP_CLAUSE_CHAIN (u
) = list
;
27742 list
= tree_cons (decl
, NULL_TREE
, list
);
27745 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27747 cp_lexer_consume_token (parser
->lexer
);
27751 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27753 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27756 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27760 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27764 /* Try to resync to an unnested comma. Copied from
27765 cp_parser_parenthesized_expression_list. */
27768 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27769 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
27770 /*recovering=*/true,
27772 /*consume_paren=*/true);
27780 /* Similarly, but expect leading and trailing parenthesis. This is a very
27781 common case for omp clauses. */
27784 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
27786 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27787 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
27792 copy ( variable-list )
27793 copyin ( variable-list )
27794 copyout ( variable-list )
27795 create ( variable-list )
27796 delete ( variable-list )
27797 present ( variable-list )
27798 present_or_copy ( variable-list )
27799 pcopy ( variable-list )
27800 present_or_copyin ( variable-list )
27801 pcopyin ( variable-list )
27802 present_or_copyout ( variable-list )
27803 pcopyout ( variable-list )
27804 present_or_create ( variable-list )
27805 pcreate ( variable-list ) */
27808 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
27811 enum omp_clause_map_kind kind
;
27815 gcc_unreachable ();
27816 case PRAGMA_OMP_CLAUSE_COPY
:
27817 kind
= OMP_CLAUSE_MAP_FORCE_TOFROM
;
27819 case PRAGMA_OMP_CLAUSE_COPYIN
:
27820 kind
= OMP_CLAUSE_MAP_FORCE_TO
;
27822 case PRAGMA_OMP_CLAUSE_COPYOUT
:
27823 kind
= OMP_CLAUSE_MAP_FORCE_FROM
;
27825 case PRAGMA_OMP_CLAUSE_CREATE
:
27826 kind
= OMP_CLAUSE_MAP_FORCE_ALLOC
;
27828 case PRAGMA_OMP_CLAUSE_DELETE
:
27829 kind
= OMP_CLAUSE_MAP_FORCE_DEALLOC
;
27831 case PRAGMA_OMP_CLAUSE_DEVICE
:
27832 kind
= OMP_CLAUSE_MAP_FORCE_TO
;
27834 case PRAGMA_OMP_CLAUSE_HOST
:
27835 kind
= OMP_CLAUSE_MAP_FORCE_FROM
;
27837 case PRAGMA_OMP_CLAUSE_PRESENT
:
27838 kind
= OMP_CLAUSE_MAP_FORCE_PRESENT
;
27840 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY
:
27841 kind
= OMP_CLAUSE_MAP_TOFROM
;
27843 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN
:
27844 kind
= OMP_CLAUSE_MAP_TO
;
27846 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT
:
27847 kind
= OMP_CLAUSE_MAP_FROM
;
27849 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE
:
27850 kind
= OMP_CLAUSE_MAP_ALLOC
;
27852 case PRAGMA_OMP_CLAUSE_SELF
:
27853 kind
= OMP_CLAUSE_MAP_FORCE_FROM
;
27857 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
27859 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27860 OMP_CLAUSE_MAP_KIND (c
) = kind
;
27866 deviceptr ( variable-list ) */
27869 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
27871 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27874 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27875 cp_parser_oacc_data_clause), as for PRAGMA_OMP_CLAUSE_DEVICEPTR,
27876 variable-list must only allow for pointer variables. */
27877 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
27878 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
27880 tree v
= TREE_PURPOSE (t
);
27882 /* FIXME diagnostics: Ideally we should keep individual
27883 locations for all the variables in the var list to make the
27884 following errors more precise. Perhaps
27885 c_parser_omp_var_list_parens should construct a list of
27886 locations to go along with the var list. */
27888 if (TREE_CODE (v
) != VAR_DECL
)
27889 error_at (loc
, "%qD is not a variable", v
);
27890 else if (TREE_TYPE (v
) == error_mark_node
)
27892 else if (!POINTER_TYPE_P (TREE_TYPE (v
)))
27893 error_at (loc
, "%qD is not a pointer variable", v
);
27895 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
27896 OMP_CLAUSE_MAP_KIND (u
) = OMP_CLAUSE_MAP_FORCE_DEVICEPTR
;
27897 OMP_CLAUSE_DECL (u
) = v
;
27898 OMP_CLAUSE_CHAIN (u
) = list
;
27906 vector_length ( expression ) */
27909 cp_parser_oacc_clause_vector_length (cp_parser
*parser
, tree list
)
27912 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27913 bool error
= false;
27916 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27919 t
= cp_parser_condition (parser
);
27920 if (t
== error_mark_node
27921 || !INTEGRAL_TYPE_P (TREE_TYPE (t
))
27922 || !tree_fits_shwi_p (t
)
27923 || (n
= tree_to_shwi (t
)) <= 0
27926 error_at (location
, "expected positive integer expression");
27930 if (error
|| !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27932 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27933 /*or_comma=*/false,
27934 /*consume_paren=*/true);
27938 check_no_duplicate_clause (list
, OMP_CLAUSE_VECTOR_LENGTH
,
27939 "vector_length", location
);
27941 c
= build_omp_clause (location
, OMP_CLAUSE_VECTOR_LENGTH
);
27942 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c
) = t
;
27943 OMP_CLAUSE_CHAIN (c
) = list
;
27950 Parse wait clause or directive parameters. */
27953 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
27955 vec
<tree
, va_gc
> *args
;
27958 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27960 /*allow_expansion_p=*/true,
27961 /*non_constant_p=*/NULL
);
27963 if (args
== NULL
|| args
->length() == 0)
27965 cp_parser_error (parser
, "expected integer expression before ')'");
27967 release_tree_vector (args
);
27971 args_tree
= build_tree_list_vec (args
);
27973 release_tree_vector (args
);
27975 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
27977 tree targ
= TREE_VALUE (t
);
27979 if (targ
!= error_mark_node
)
27981 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
27983 error("%<wait%> expression must be integral");
27984 targ
= error_mark_node
;
27988 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
27990 mark_rvalue_use (targ
);
27992 OMP_CLAUSE_DECL (c
) = targ
;
27993 OMP_CLAUSE_CHAIN (c
) = list
;
28003 wait ( int-expr-list ) */
28006 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
28008 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28010 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
28013 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
28019 collapse ( constant-expression ) */
28022 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
28028 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28029 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28032 num
= cp_parser_constant_expression (parser
);
28034 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28035 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28036 /*or_comma=*/false,
28037 /*consume_paren=*/true);
28039 if (num
== error_mark_node
)
28041 num
= fold_non_dependent_expr (num
);
28042 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
28043 || !tree_fits_shwi_p (num
)
28044 || (n
= tree_to_shwi (num
)) <= 0
28047 error_at (loc
, "collapse argument needs positive constant integer expression");
28051 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
28052 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
28053 OMP_CLAUSE_CHAIN (c
) = list
;
28054 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
28060 default ( shared | none ) */
28063 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
28065 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
28068 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28072 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28073 const char *p
= IDENTIFIER_POINTER (id
);
28078 if (strcmp ("none", p
) != 0)
28080 kind
= OMP_CLAUSE_DEFAULT_NONE
;
28084 if (strcmp ("shared", p
) != 0)
28086 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
28093 cp_lexer_consume_token (parser
->lexer
);
28098 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
28101 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28102 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28103 /*or_comma=*/false,
28104 /*consume_paren=*/true);
28106 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
28109 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
28110 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
28111 OMP_CLAUSE_CHAIN (c
) = list
;
28112 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
28118 final ( expression ) */
28121 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
28125 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28128 t
= cp_parser_condition (parser
);
28130 if (t
== error_mark_node
28131 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28132 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28133 /*or_comma=*/false,
28134 /*consume_paren=*/true);
28136 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
28138 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
28139 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
28140 OMP_CLAUSE_CHAIN (c
) = list
;
28146 if ( expression ) */
28149 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
28153 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28156 t
= cp_parser_condition (parser
);
28158 if (t
== error_mark_node
28159 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28160 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28161 /*or_comma=*/false,
28162 /*consume_paren=*/true);
28164 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
28166 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
28167 OMP_CLAUSE_IF_EXPR (c
) = t
;
28168 OMP_CLAUSE_CHAIN (c
) = list
;
28177 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
28178 tree list
, location_t location
)
28182 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
28185 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
28186 OMP_CLAUSE_CHAIN (c
) = list
;
28194 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
28195 tree list
, location_t location
)
28199 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
28201 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
28202 OMP_CLAUSE_CHAIN (c
) = list
;
28207 num_gangs ( expression ) */
28210 cp_parser_omp_clause_num_gangs (cp_parser
*parser
, tree list
)
28213 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28216 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28219 t
= cp_parser_condition (parser
);
28221 if (t
== error_mark_node
28222 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28223 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28224 /*or_comma=*/false,
28225 /*consume_paren=*/true);
28227 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
28228 || !tree_fits_shwi_p (t
)
28229 || (n
= tree_to_shwi (t
)) <= 0
28232 error_at (location
, "expected positive integer expression");
28236 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_GANGS
, "num_gangs", location
);
28238 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_GANGS
);
28239 OMP_CLAUSE_NUM_GANGS_EXPR (c
) = t
;
28240 OMP_CLAUSE_CHAIN (c
) = list
;
28247 num_threads ( expression ) */
28250 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
28251 location_t location
)
28255 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28258 t
= cp_parser_expression (parser
);
28260 if (t
== error_mark_node
28261 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28262 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28263 /*or_comma=*/false,
28264 /*consume_paren=*/true);
28266 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
28267 "num_threads", location
);
28269 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
28270 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
28271 OMP_CLAUSE_CHAIN (c
) = list
;
28277 num_workers ( expression ) */
28280 cp_parser_omp_clause_num_workers (cp_parser
*parser
, tree list
)
28283 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28286 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28289 t
= cp_parser_condition (parser
);
28291 if (t
== error_mark_node
28292 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28293 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28294 /*or_comma=*/false,
28295 /*consume_paren=*/true);
28297 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
28298 || !tree_fits_shwi_p (t
)
28299 || (n
= tree_to_shwi (t
)) <= 0
28302 error_at (location
, "expected positive integer expression");
28306 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_WORKERS
, "num_gangs",
28309 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_WORKERS
);
28310 OMP_CLAUSE_NUM_WORKERS_EXPR (c
) = t
;
28311 OMP_CLAUSE_CHAIN (c
) = list
;
28321 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
28322 tree list
, location_t location
)
28326 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
28327 "ordered", location
);
28329 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
28330 OMP_CLAUSE_CHAIN (c
) = list
;
28335 reduction ( reduction-operator : variable-list )
28337 reduction-operator:
28338 One of: + * - & ^ | && ||
28342 reduction-operator:
28343 One of: + * - & ^ | && || min max
28347 reduction-operator:
28348 One of: + * - & ^ | && ||
28352 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
28354 enum tree_code code
= ERROR_MARK
;
28355 tree nlist
, c
, id
= NULL_TREE
;
28357 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28360 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28362 case CPP_PLUS
: code
= PLUS_EXPR
; break;
28363 case CPP_MULT
: code
= MULT_EXPR
; break;
28364 case CPP_MINUS
: code
= MINUS_EXPR
; break;
28365 case CPP_AND
: code
= BIT_AND_EXPR
; break;
28366 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
28367 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
28368 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
28369 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
28373 if (code
!= ERROR_MARK
)
28374 cp_lexer_consume_token (parser
->lexer
);
28377 bool saved_colon_corrects_to_scope_p
;
28378 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
28379 parser
->colon_corrects_to_scope_p
= false;
28380 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
28381 /*check_dependency_p=*/true,
28382 /*template_p=*/NULL
,
28383 /*declarator_p=*/false,
28384 /*optional_p=*/false);
28385 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
28386 if (identifier_p (id
))
28388 const char *p
= IDENTIFIER_POINTER (id
);
28390 if (strcmp (p
, "min") == 0)
28392 else if (strcmp (p
, "max") == 0)
28394 else if (id
== ansi_opname (PLUS_EXPR
))
28396 else if (id
== ansi_opname (MULT_EXPR
))
28398 else if (id
== ansi_opname (MINUS_EXPR
))
28400 else if (id
== ansi_opname (BIT_AND_EXPR
))
28401 code
= BIT_AND_EXPR
;
28402 else if (id
== ansi_opname (BIT_IOR_EXPR
))
28403 code
= BIT_IOR_EXPR
;
28404 else if (id
== ansi_opname (BIT_XOR_EXPR
))
28405 code
= BIT_XOR_EXPR
;
28406 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
28407 code
= TRUTH_ANDIF_EXPR
;
28408 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
28409 code
= TRUTH_ORIF_EXPR
;
28410 id
= omp_reduction_id (code
, id
, NULL_TREE
);
28411 tree scope
= parser
->scope
;
28413 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
28414 parser
->scope
= NULL_TREE
;
28415 parser
->qualifying_scope
= NULL_TREE
;
28416 parser
->object_scope
= NULL_TREE
;
28420 error ("invalid reduction-identifier");
28422 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28423 /*or_comma=*/false,
28424 /*consume_paren=*/true);
28429 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28432 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
28434 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28436 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
28437 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
28444 schedule ( schedule-kind )
28445 schedule ( schedule-kind , expression )
28448 static | dynamic | guided | runtime | auto */
28451 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
28455 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28458 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
28460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28462 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28463 const char *p
= IDENTIFIER_POINTER (id
);
28468 if (strcmp ("dynamic", p
) != 0)
28470 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
28474 if (strcmp ("guided", p
) != 0)
28476 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
28480 if (strcmp ("runtime", p
) != 0)
28482 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
28489 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28490 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
28491 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
28492 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
28495 cp_lexer_consume_token (parser
->lexer
);
28497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28500 cp_lexer_consume_token (parser
->lexer
);
28502 token
= cp_lexer_peek_token (parser
->lexer
);
28503 t
= cp_parser_assignment_expression (parser
);
28505 if (t
== error_mark_node
)
28507 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
28508 error_at (token
->location
, "schedule %<runtime%> does not take "
28509 "a %<chunk_size%> parameter");
28510 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
28511 error_at (token
->location
, "schedule %<auto%> does not take "
28512 "a %<chunk_size%> parameter");
28514 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
28516 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28519 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28522 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
28523 OMP_CLAUSE_CHAIN (c
) = list
;
28527 cp_parser_error (parser
, "invalid schedule kind");
28529 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28530 /*or_comma=*/false,
28531 /*consume_paren=*/true);
28539 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
28540 tree list
, location_t location
)
28544 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
28546 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
28547 OMP_CLAUSE_CHAIN (c
) = list
;
28556 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
28557 tree list
, location_t location
)
28559 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
28560 tree c
= build_omp_clause (location
, code
);
28561 OMP_CLAUSE_CHAIN (c
) = list
;
28572 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
28573 enum omp_clause_code code
,
28574 tree list
, location_t location
)
28576 tree c
= build_omp_clause (location
, code
);
28577 OMP_CLAUSE_CHAIN (c
) = list
;
28582 num_teams ( expression ) */
28585 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
28586 location_t location
)
28590 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28593 t
= cp_parser_expression (parser
);
28595 if (t
== error_mark_node
28596 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28597 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28598 /*or_comma=*/false,
28599 /*consume_paren=*/true);
28601 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
28602 "num_teams", location
);
28604 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
28605 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
28606 OMP_CLAUSE_CHAIN (c
) = list
;
28612 thread_limit ( expression ) */
28615 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
28616 location_t location
)
28620 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28623 t
= cp_parser_expression (parser
);
28625 if (t
== error_mark_node
28626 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28627 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28628 /*or_comma=*/false,
28629 /*consume_paren=*/true);
28631 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
28632 "thread_limit", location
);
28634 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
28635 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
28636 OMP_CLAUSE_CHAIN (c
) = list
;
28642 aligned ( variable-list )
28643 aligned ( variable-list : constant-expression ) */
28646 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
28648 tree nlist
, c
, alignment
= NULL_TREE
;
28651 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28654 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
28659 alignment
= cp_parser_constant_expression (parser
);
28661 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28662 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28663 /*or_comma=*/false,
28664 /*consume_paren=*/true);
28666 if (alignment
== error_mark_node
)
28667 alignment
= NULL_TREE
;
28670 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28671 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
28677 linear ( variable-list )
28678 linear ( variable-list : expression ) */
28681 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
28682 bool is_cilk_simd_fn
)
28684 tree nlist
, c
, step
= integer_one_node
;
28687 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28690 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
28695 step
= cp_parser_expression (parser
);
28697 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
28699 sorry ("using parameters for %<linear%> step is not supported yet");
28700 step
= integer_one_node
;
28702 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28703 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28704 /*or_comma=*/false,
28705 /*consume_paren=*/true);
28707 if (step
== error_mark_node
)
28711 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28712 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
28718 safelen ( constant-expression ) */
28721 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
28722 location_t location
)
28726 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28729 t
= cp_parser_constant_expression (parser
);
28731 if (t
== error_mark_node
28732 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28733 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28734 /*or_comma=*/false,
28735 /*consume_paren=*/true);
28737 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
28739 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
28740 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
28741 OMP_CLAUSE_CHAIN (c
) = list
;
28747 simdlen ( constant-expression ) */
28750 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
28751 location_t location
)
28755 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28758 t
= cp_parser_constant_expression (parser
);
28760 if (t
== error_mark_node
28761 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28762 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28763 /*or_comma=*/false,
28764 /*consume_paren=*/true);
28766 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
28768 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
28769 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
28770 OMP_CLAUSE_CHAIN (c
) = list
;
28776 depend ( depend-kind : variable-list )
28779 in | out | inout */
28782 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
28785 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
28787 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28792 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28793 const char *p
= IDENTIFIER_POINTER (id
);
28795 if (strcmp ("in", p
) == 0)
28796 kind
= OMP_CLAUSE_DEPEND_IN
;
28797 else if (strcmp ("inout", p
) == 0)
28798 kind
= OMP_CLAUSE_DEPEND_INOUT
;
28799 else if (strcmp ("out", p
) == 0)
28800 kind
= OMP_CLAUSE_DEPEND_OUT
;
28807 cp_lexer_consume_token (parser
->lexer
);
28808 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28811 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
28814 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28815 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
28820 cp_parser_error (parser
, "invalid depend kind");
28822 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28823 /*or_comma=*/false,
28824 /*consume_paren=*/true);
28829 map ( map-kind : variable-list )
28830 map ( variable-list )
28833 alloc | to | from | tofrom */
28836 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
28839 enum omp_clause_map_kind kind
= OMP_CLAUSE_MAP_TOFROM
;
28841 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28844 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
28845 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
28847 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28848 const char *p
= IDENTIFIER_POINTER (id
);
28850 if (strcmp ("alloc", p
) == 0)
28851 kind
= OMP_CLAUSE_MAP_ALLOC
;
28852 else if (strcmp ("to", p
) == 0)
28853 kind
= OMP_CLAUSE_MAP_TO
;
28854 else if (strcmp ("from", p
) == 0)
28855 kind
= OMP_CLAUSE_MAP_FROM
;
28856 else if (strcmp ("tofrom", p
) == 0)
28857 kind
= OMP_CLAUSE_MAP_TOFROM
;
28860 cp_parser_error (parser
, "invalid map kind");
28861 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28862 /*or_comma=*/false,
28863 /*consume_paren=*/true);
28866 cp_lexer_consume_token (parser
->lexer
);
28867 cp_lexer_consume_token (parser
->lexer
);
28870 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
28873 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28874 OMP_CLAUSE_MAP_KIND (c
) = kind
;
28880 device ( expression ) */
28883 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
28884 location_t location
)
28888 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28891 t
= cp_parser_expression (parser
);
28893 if (t
== error_mark_node
28894 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28895 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28896 /*or_comma=*/false,
28897 /*consume_paren=*/true);
28899 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
28900 "device", location
);
28902 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
28903 OMP_CLAUSE_DEVICE_ID (c
) = t
;
28904 OMP_CLAUSE_CHAIN (c
) = list
;
28910 dist_schedule ( static )
28911 dist_schedule ( static , expression ) */
28914 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
28915 location_t location
)
28919 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28922 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
28924 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28926 cp_lexer_consume_token (parser
->lexer
);
28928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28930 cp_lexer_consume_token (parser
->lexer
);
28932 t
= cp_parser_assignment_expression (parser
);
28934 if (t
== error_mark_node
)
28936 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
28938 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28941 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28944 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
28946 OMP_CLAUSE_CHAIN (c
) = list
;
28950 cp_parser_error (parser
, "invalid dist_schedule kind");
28952 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28953 /*or_comma=*/false,
28954 /*consume_paren=*/true);
28959 proc_bind ( proc-bind-kind )
28962 master | close | spread */
28965 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
28966 location_t location
)
28969 enum omp_clause_proc_bind_kind kind
;
28971 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28976 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28977 const char *p
= IDENTIFIER_POINTER (id
);
28979 if (strcmp ("master", p
) == 0)
28980 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
28981 else if (strcmp ("close", p
) == 0)
28982 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
28983 else if (strcmp ("spread", p
) == 0)
28984 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
28991 cp_lexer_consume_token (parser
->lexer
);
28992 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28995 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
28996 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
28998 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
28999 OMP_CLAUSE_CHAIN (c
) = list
;
29003 cp_parser_error (parser
, "invalid depend kind");
29005 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29006 /*or_comma=*/false,
29007 /*consume_paren=*/true);
29012 async [( int-expr )] */
29015 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
29018 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29020 /* TODO XXX: FIX -1 (acc_async_noval). */
29021 t
= build_int_cst (integer_type_node
, -1);
29023 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
29025 cp_lexer_consume_token (parser
->lexer
);
29027 t
= cp_parser_expression (parser
);
29028 if (t
== error_mark_node
29029 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29030 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29031 /*or_comma=*/false,
29032 /*consume_paren=*/true);
29035 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
29037 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
29038 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
29039 OMP_CLAUSE_CHAIN (c
) = list
;
29045 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29046 is a bitmask in MASK. Return the list of clauses found. */
29049 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
29050 const char *where
, cp_token
*pragma_tok
,
29051 bool finish_p
= true)
29053 tree clauses
= NULL
;
29056 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29059 pragma_omp_clause c_kind
;
29060 const char *c_name
;
29061 tree prev
= clauses
;
29063 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29064 cp_lexer_consume_token (parser
->lexer
);
29066 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
29067 c_kind
= cp_parser_omp_clause_name (parser
);
29071 case PRAGMA_OMP_CLAUSE_ASYNC
:
29072 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
29075 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
29076 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
29077 c_name
= "collapse";
29079 case PRAGMA_OMP_CLAUSE_COPY
:
29080 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29083 case PRAGMA_OMP_CLAUSE_COPYIN
:
29084 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29087 case PRAGMA_OMP_CLAUSE_COPYOUT
:
29088 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29089 c_name
= "copyout";
29091 case PRAGMA_OMP_CLAUSE_CREATE
:
29092 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29095 case PRAGMA_OMP_CLAUSE_DELETE
:
29096 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29099 case PRAGMA_OMP_CLAUSE_DEVICE
:
29100 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29103 case PRAGMA_OMP_CLAUSE_DEVICEPTR
:
29104 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
29105 c_name
= "deviceptr";
29107 case PRAGMA_OMP_CLAUSE_HOST
:
29108 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29111 case PRAGMA_OMP_CLAUSE_IF
:
29112 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
);
29115 case PRAGMA_OMP_CLAUSE_NUM_GANGS
:
29116 clauses
= cp_parser_omp_clause_num_gangs (parser
, clauses
);
29117 c_name
= "num_gangs";
29119 case PRAGMA_OMP_CLAUSE_NUM_WORKERS
:
29120 clauses
= cp_parser_omp_clause_num_workers (parser
, clauses
);
29121 c_name
= "num_workers";
29123 case PRAGMA_OMP_CLAUSE_PRESENT
:
29124 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29125 c_name
= "present";
29127 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY
:
29128 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29129 c_name
= "present_or_copy";
29131 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN
:
29132 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29133 c_name
= "present_or_copyin";
29135 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT
:
29136 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29137 c_name
= "present_or_copyout";
29139 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE
:
29140 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29141 c_name
= "present_or_create";
29143 case PRAGMA_OMP_CLAUSE_REDUCTION
:
29144 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
29145 c_name
= "reduction";
29147 case PRAGMA_OMP_CLAUSE_SELF
:
29148 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29151 case PRAGMA_OMP_CLAUSE_VECTOR_LENGTH
:
29153 cp_parser_oacc_clause_vector_length (parser
, clauses
);
29154 c_name
= "vector_length";
29156 case PRAGMA_OMP_CLAUSE_WAIT
:
29157 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
29161 cp_parser_error (parser
, "expected clause");
29167 if (((mask
>> c_kind
) & 1) == 0)
29169 /* Remove the invalid clause(s) from the list to avoid
29170 confusing the rest of the compiler. */
29172 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
29177 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29180 return finish_omp_clauses (clauses
);
29185 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29186 is a bitmask in MASK. Return the list of clauses found; the result
29187 of clause default goes in *pdefault. */
29190 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
29191 const char *where
, cp_token
*pragma_tok
,
29192 bool finish_p
= true)
29194 tree clauses
= NULL
;
29196 cp_token
*token
= NULL
;
29197 bool cilk_simd_fn
= false;
29199 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29201 pragma_omp_clause c_kind
;
29202 const char *c_name
;
29203 tree prev
= clauses
;
29205 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29206 cp_lexer_consume_token (parser
->lexer
);
29208 token
= cp_lexer_peek_token (parser
->lexer
);
29209 c_kind
= cp_parser_omp_clause_name (parser
);
29213 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
29214 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
29216 c_name
= "collapse";
29218 case PRAGMA_OMP_CLAUSE_COPYIN
:
29219 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
29222 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
29223 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
29225 c_name
= "copyprivate";
29227 case PRAGMA_OMP_CLAUSE_DEFAULT
:
29228 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
29230 c_name
= "default";
29232 case PRAGMA_OMP_CLAUSE_FINAL
:
29233 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
29236 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
29237 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
29239 c_name
= "firstprivate";
29241 case PRAGMA_OMP_CLAUSE_IF
:
29242 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
29245 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
29246 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
29248 c_name
= "lastprivate";
29250 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
29251 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
29253 c_name
= "mergeable";
29255 case PRAGMA_OMP_CLAUSE_NOWAIT
:
29256 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
29259 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
29260 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
29262 c_name
= "num_threads";
29264 case PRAGMA_OMP_CLAUSE_ORDERED
:
29265 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
29267 c_name
= "ordered";
29269 case PRAGMA_OMP_CLAUSE_PRIVATE
:
29270 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
29272 c_name
= "private";
29274 case PRAGMA_OMP_CLAUSE_REDUCTION
:
29275 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
29276 c_name
= "reduction";
29278 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
29279 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
29281 c_name
= "schedule";
29283 case PRAGMA_OMP_CLAUSE_SHARED
:
29284 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
29288 case PRAGMA_OMP_CLAUSE_UNTIED
:
29289 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
29293 case PRAGMA_OMP_CLAUSE_INBRANCH
:
29294 case PRAGMA_CILK_CLAUSE_MASK
:
29295 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
29296 clauses
, token
->location
);
29297 c_name
= "inbranch";
29299 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
29300 case PRAGMA_CILK_CLAUSE_NOMASK
:
29301 clauses
= cp_parser_omp_clause_branch (parser
,
29302 OMP_CLAUSE_NOTINBRANCH
,
29303 clauses
, token
->location
);
29304 c_name
= "notinbranch";
29306 case PRAGMA_OMP_CLAUSE_PARALLEL
:
29307 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
29308 clauses
, token
->location
);
29309 c_name
= "parallel";
29313 error_at (token
->location
, "%qs must be the first clause of %qs",
29318 case PRAGMA_OMP_CLAUSE_FOR
:
29319 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
29320 clauses
, token
->location
);
29323 goto clause_not_first
;
29325 case PRAGMA_OMP_CLAUSE_SECTIONS
:
29326 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
29327 clauses
, token
->location
);
29328 c_name
= "sections";
29330 goto clause_not_first
;
29332 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
29333 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
29334 clauses
, token
->location
);
29335 c_name
= "taskgroup";
29337 goto clause_not_first
;
29339 case PRAGMA_OMP_CLAUSE_TO
:
29340 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
29344 case PRAGMA_OMP_CLAUSE_FROM
:
29345 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
29349 case PRAGMA_OMP_CLAUSE_UNIFORM
:
29350 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
29352 c_name
= "uniform";
29354 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
29355 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
29357 c_name
= "num_teams";
29359 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
29360 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
29362 c_name
= "thread_limit";
29364 case PRAGMA_OMP_CLAUSE_ALIGNED
:
29365 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
29366 c_name
= "aligned";
29368 case PRAGMA_OMP_CLAUSE_LINEAR
:
29369 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
29370 cilk_simd_fn
= true;
29371 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, cilk_simd_fn
);
29374 case PRAGMA_OMP_CLAUSE_DEPEND
:
29375 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
29378 case PRAGMA_OMP_CLAUSE_MAP
:
29379 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
29382 case PRAGMA_OMP_CLAUSE_DEVICE
:
29383 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
29387 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
29388 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
29390 c_name
= "dist_schedule";
29392 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
29393 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
29395 c_name
= "proc_bind";
29397 case PRAGMA_OMP_CLAUSE_SAFELEN
:
29398 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
29400 c_name
= "safelen";
29402 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
29403 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
29405 c_name
= "simdlen";
29407 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
29408 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
29409 c_name
= "simdlen";
29412 cp_parser_error (parser
, "expected clause");
29418 if (((mask
>> c_kind
) & 1) == 0)
29420 /* Remove the invalid clause(s) from the list to avoid
29421 confusing the rest of the compiler. */
29423 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
29427 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29428 no reason to skip to the end. */
29429 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
29430 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29432 return finish_omp_clauses (clauses
);
29440 In practice, we're also interested in adding the statement to an
29441 outer node. So it is convenient if we work around the fact that
29442 cp_parser_statement calls add_stmt. */
29445 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
29447 unsigned save
= parser
->in_statement
;
29449 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29450 This preserves the "not within loop or switch" style error messages
29451 for nonsense cases like
29457 if (parser
->in_statement
)
29458 parser
->in_statement
= IN_OMP_BLOCK
;
29464 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
29466 parser
->in_statement
= save
;
29470 cp_parser_omp_structured_block (cp_parser
*parser
)
29472 tree stmt
= begin_omp_structured_block ();
29473 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
29475 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29477 cp_parser_end_omp_structured_block (parser
, save
);
29478 return finish_omp_structured_block (stmt
);
29482 # pragma omp atomic new-line
29486 x binop= expr | x++ | ++x | x-- | --x
29488 +, *, -, /, &, ^, |, <<, >>
29490 where x is an lvalue expression with scalar type.
29493 # pragma omp atomic new-line
29496 # pragma omp atomic read new-line
29499 # pragma omp atomic write new-line
29502 # pragma omp atomic update new-line
29505 # pragma omp atomic capture new-line
29508 # pragma omp atomic capture new-line
29516 expression-stmt | x = x binop expr
29518 v = expression-stmt
29520 { v = x; update-stmt; } | { update-stmt; v = x; }
29524 expression-stmt | x = x binop expr | x = expr binop x
29528 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29530 where x and v are lvalue expressions with scalar type. */
29533 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
29535 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
29536 tree rhs1
= NULL_TREE
, orig_lhs
;
29537 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
29538 bool structured_block
= false;
29539 bool seq_cst
= false;
29541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29543 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29544 const char *p
= IDENTIFIER_POINTER (id
);
29546 if (!strcmp (p
, "seq_cst"))
29549 cp_lexer_consume_token (parser
->lexer
);
29550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29551 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29552 cp_lexer_consume_token (parser
->lexer
);
29555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29557 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29558 const char *p
= IDENTIFIER_POINTER (id
);
29560 if (!strcmp (p
, "read"))
29561 code
= OMP_ATOMIC_READ
;
29562 else if (!strcmp (p
, "write"))
29564 else if (!strcmp (p
, "update"))
29566 else if (!strcmp (p
, "capture"))
29567 code
= OMP_ATOMIC_CAPTURE_NEW
;
29571 cp_lexer_consume_token (parser
->lexer
);
29575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29576 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29577 cp_lexer_consume_token (parser
->lexer
);
29579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29581 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29582 const char *p
= IDENTIFIER_POINTER (id
);
29584 if (!strcmp (p
, "seq_cst"))
29587 cp_lexer_consume_token (parser
->lexer
);
29591 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29595 case OMP_ATOMIC_READ
:
29596 case NOP_EXPR
: /* atomic write */
29597 v
= cp_parser_unary_expression (parser
);
29598 if (v
== error_mark_node
)
29600 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29602 if (code
== NOP_EXPR
)
29603 lhs
= cp_parser_expression (parser
);
29605 lhs
= cp_parser_unary_expression (parser
);
29606 if (lhs
== error_mark_node
)
29608 if (code
== NOP_EXPR
)
29610 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29618 case OMP_ATOMIC_CAPTURE_NEW
:
29619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29621 cp_lexer_consume_token (parser
->lexer
);
29622 structured_block
= true;
29626 v
= cp_parser_unary_expression (parser
);
29627 if (v
== error_mark_node
)
29629 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29637 lhs
= cp_parser_unary_expression (parser
);
29639 switch (TREE_CODE (lhs
))
29644 case POSTINCREMENT_EXPR
:
29645 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29646 code
= OMP_ATOMIC_CAPTURE_OLD
;
29648 case PREINCREMENT_EXPR
:
29649 lhs
= TREE_OPERAND (lhs
, 0);
29650 opcode
= PLUS_EXPR
;
29651 rhs
= integer_one_node
;
29654 case POSTDECREMENT_EXPR
:
29655 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29656 code
= OMP_ATOMIC_CAPTURE_OLD
;
29658 case PREDECREMENT_EXPR
:
29659 lhs
= TREE_OPERAND (lhs
, 0);
29660 opcode
= MINUS_EXPR
;
29661 rhs
= integer_one_node
;
29664 case COMPOUND_EXPR
:
29665 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
29666 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
29667 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
29668 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
29669 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29670 (TREE_OPERAND (lhs
, 1), 0), 0)))
29672 /* Undo effects of boolean_increment for post {in,de}crement. */
29673 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
29676 if (TREE_CODE (lhs
) == MODIFY_EXPR
29677 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
29679 /* Undo effects of boolean_increment. */
29680 if (integer_onep (TREE_OPERAND (lhs
, 1)))
29682 /* This is pre or post increment. */
29683 rhs
= TREE_OPERAND (lhs
, 1);
29684 lhs
= TREE_OPERAND (lhs
, 0);
29686 if (code
== OMP_ATOMIC_CAPTURE_NEW
29687 && !structured_block
29688 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
29689 code
= OMP_ATOMIC_CAPTURE_OLD
;
29695 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29698 opcode
= MULT_EXPR
;
29701 opcode
= TRUNC_DIV_EXPR
;
29704 opcode
= PLUS_EXPR
;
29707 opcode
= MINUS_EXPR
;
29709 case CPP_LSHIFT_EQ
:
29710 opcode
= LSHIFT_EXPR
;
29712 case CPP_RSHIFT_EQ
:
29713 opcode
= RSHIFT_EXPR
;
29716 opcode
= BIT_AND_EXPR
;
29719 opcode
= BIT_IOR_EXPR
;
29722 opcode
= BIT_XOR_EXPR
;
29725 enum cp_parser_prec oprec
;
29727 cp_lexer_consume_token (parser
->lexer
);
29728 cp_parser_parse_tentatively (parser
);
29729 rhs1
= cp_parser_simple_cast_expression (parser
);
29730 if (rhs1
== error_mark_node
)
29732 cp_parser_abort_tentative_parse (parser
);
29733 cp_parser_simple_cast_expression (parser
);
29736 token
= cp_lexer_peek_token (parser
->lexer
);
29737 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
29739 cp_parser_abort_tentative_parse (parser
);
29740 cp_parser_parse_tentatively (parser
);
29741 rhs
= cp_parser_binary_expression (parser
, false, true,
29742 PREC_NOT_OPERATOR
, NULL
);
29743 if (rhs
== error_mark_node
)
29745 cp_parser_abort_tentative_parse (parser
);
29746 cp_parser_binary_expression (parser
, false, true,
29747 PREC_NOT_OPERATOR
, NULL
);
29750 switch (TREE_CODE (rhs
))
29753 case TRUNC_DIV_EXPR
:
29761 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
29763 if (cp_parser_parse_definitely (parser
))
29765 opcode
= TREE_CODE (rhs
);
29766 rhs1
= TREE_OPERAND (rhs
, 0);
29767 rhs
= TREE_OPERAND (rhs
, 1);
29777 cp_parser_abort_tentative_parse (parser
);
29778 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
29780 rhs
= cp_parser_expression (parser
);
29781 if (rhs
== error_mark_node
)
29787 cp_parser_error (parser
,
29788 "invalid form of %<#pragma omp atomic%>");
29791 if (!cp_parser_parse_definitely (parser
))
29793 switch (token
->type
)
29795 case CPP_SEMICOLON
:
29796 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29798 code
= OMP_ATOMIC_CAPTURE_OLD
;
29803 cp_lexer_consume_token (parser
->lexer
);
29806 else if (structured_block
)
29813 cp_parser_error (parser
,
29814 "invalid form of %<#pragma omp atomic%>");
29817 opcode
= MULT_EXPR
;
29820 opcode
= TRUNC_DIV_EXPR
;
29823 opcode
= PLUS_EXPR
;
29826 opcode
= MINUS_EXPR
;
29829 opcode
= LSHIFT_EXPR
;
29832 opcode
= RSHIFT_EXPR
;
29835 opcode
= BIT_AND_EXPR
;
29838 opcode
= BIT_IOR_EXPR
;
29841 opcode
= BIT_XOR_EXPR
;
29844 cp_parser_error (parser
,
29845 "invalid operator for %<#pragma omp atomic%>");
29848 oprec
= TOKEN_PRECEDENCE (token
);
29849 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
29850 if (commutative_tree_code (opcode
))
29851 oprec
= (enum cp_parser_prec
) (oprec
- 1);
29852 cp_lexer_consume_token (parser
->lexer
);
29853 rhs
= cp_parser_binary_expression (parser
, false, false,
29855 if (rhs
== error_mark_node
)
29860 cp_parser_error (parser
,
29861 "invalid operator for %<#pragma omp atomic%>");
29864 cp_lexer_consume_token (parser
->lexer
);
29866 rhs
= cp_parser_expression (parser
);
29867 if (rhs
== error_mark_node
)
29872 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29874 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
29876 v
= cp_parser_unary_expression (parser
);
29877 if (v
== error_mark_node
)
29879 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29881 lhs1
= cp_parser_unary_expression (parser
);
29882 if (lhs1
== error_mark_node
)
29885 if (structured_block
)
29887 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29888 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29891 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
29892 if (!structured_block
)
29893 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29897 cp_parser_skip_to_end_of_block_or_statement (parser
);
29898 if (structured_block
)
29900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29901 cp_lexer_consume_token (parser
->lexer
);
29902 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
29904 cp_parser_skip_to_end_of_block_or_statement (parser
);
29905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29906 cp_lexer_consume_token (parser
->lexer
);
29913 # pragma omp barrier new-line */
29916 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
29918 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29919 finish_omp_barrier ();
29923 # pragma omp critical [(name)] new-line
29924 structured-block */
29927 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
29929 tree stmt
, name
= NULL
;
29931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29933 cp_lexer_consume_token (parser
->lexer
);
29935 name
= cp_parser_identifier (parser
);
29937 if (name
== error_mark_node
29938 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29939 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29940 /*or_comma=*/false,
29941 /*consume_paren=*/true);
29942 if (name
== error_mark_node
)
29945 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29947 stmt
= cp_parser_omp_structured_block (parser
);
29948 return c_finish_omp_critical (input_location
, stmt
, name
);
29952 # pragma omp flush flush-vars[opt] new-line
29955 ( variable-list ) */
29958 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
29960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29961 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29962 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29964 finish_omp_flush ();
29967 /* Helper function, to parse omp for increment expression. */
29970 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
29972 tree cond
= cp_parser_binary_expression (parser
, false, true,
29973 PREC_NOT_OPERATOR
, NULL
);
29974 if (cond
== error_mark_node
29975 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29977 cp_parser_skip_to_end_of_statement (parser
);
29978 return error_mark_node
;
29981 switch (TREE_CODE (cond
))
29989 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
29991 /* Fall through: OpenMP disallows NE_EXPR. */
29993 return error_mark_node
;
29996 /* If decl is an iterator, preserve LHS and RHS of the relational
29997 expr until finish_omp_for. */
29999 && (type_dependent_expression_p (decl
)
30000 || CLASS_TYPE_P (TREE_TYPE (decl
))))
30003 return build_x_binary_op (input_location
, TREE_CODE (cond
),
30004 TREE_OPERAND (cond
, 0), ERROR_MARK
,
30005 TREE_OPERAND (cond
, 1), ERROR_MARK
,
30006 /*overload=*/NULL
, tf_warning_or_error
);
30009 /* Helper function, to parse omp for increment expression. */
30012 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
30014 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30020 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
30022 op
= (token
->type
== CPP_PLUS_PLUS
30023 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
30024 cp_lexer_consume_token (parser
->lexer
);
30025 lhs
= cp_parser_simple_cast_expression (parser
);
30027 return error_mark_node
;
30028 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
30031 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
30033 return error_mark_node
;
30035 token
= cp_lexer_peek_token (parser
->lexer
);
30036 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
30038 op
= (token
->type
== CPP_PLUS_PLUS
30039 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
30040 cp_lexer_consume_token (parser
->lexer
);
30041 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
30044 op
= cp_parser_assignment_operator_opt (parser
);
30045 if (op
== ERROR_MARK
)
30046 return error_mark_node
;
30048 if (op
!= NOP_EXPR
)
30050 rhs
= cp_parser_assignment_expression (parser
);
30051 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
30052 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
30055 lhs
= cp_parser_binary_expression (parser
, false, false,
30056 PREC_ADDITIVE_EXPRESSION
, NULL
);
30057 token
= cp_lexer_peek_token (parser
->lexer
);
30058 decl_first
= lhs
== decl
;
30061 if (token
->type
!= CPP_PLUS
30062 && token
->type
!= CPP_MINUS
)
30063 return error_mark_node
;
30067 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
30068 cp_lexer_consume_token (parser
->lexer
);
30069 rhs
= cp_parser_binary_expression (parser
, false, false,
30070 PREC_ADDITIVE_EXPRESSION
, NULL
);
30071 token
= cp_lexer_peek_token (parser
->lexer
);
30072 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
30074 if (lhs
== NULL_TREE
)
30076 if (op
== PLUS_EXPR
)
30079 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
30080 tf_warning_or_error
);
30083 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
30084 ERROR_MARK
, NULL
, tf_warning_or_error
);
30087 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
30091 if (rhs
!= decl
|| op
== MINUS_EXPR
)
30092 return error_mark_node
;
30093 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
30096 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
30098 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
30101 /* Parse the initialization statement of either an OpenMP for loop or
30102 a Cilk Plus for loop.
30104 Return true if the resulting construct should have an
30105 OMP_CLAUSE_PRIVATE added to it. */
30108 cp_parser_omp_for_loop_init (cp_parser
*parser
,
30109 enum tree_code code
,
30110 tree
&this_pre_body
,
30111 vec
<tree
, va_gc
> *for_block
,
30116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30119 bool add_private_clause
= false;
30121 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30125 integer-type var = lb
30126 random-access-iterator-type var = lb
30127 pointer-type var = lb
30129 cp_decl_specifier_seq type_specifiers
;
30131 /* First, try to parse as an initialized declaration. See
30132 cp_parser_condition, from whence the bulk of this is copied. */
30134 cp_parser_parse_tentatively (parser
);
30135 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
30136 /*is_trailing_return=*/false,
30138 if (cp_parser_parse_definitely (parser
))
30140 /* If parsing a type specifier seq succeeded, then this
30141 MUST be a initialized declaration. */
30142 tree asm_specification
, attributes
;
30143 cp_declarator
*declarator
;
30145 declarator
= cp_parser_declarator (parser
,
30146 CP_PARSER_DECLARATOR_NAMED
,
30147 /*ctor_dtor_or_conv_p=*/NULL
,
30148 /*parenthesized_p=*/NULL
,
30149 /*member_p=*/false,
30150 /*friend_p=*/false);
30151 attributes
= cp_parser_attributes_opt (parser
);
30152 asm_specification
= cp_parser_asm_specification_opt (parser
);
30154 if (declarator
== cp_error_declarator
)
30155 cp_parser_skip_to_end_of_statement (parser
);
30159 tree pushed_scope
, auto_node
;
30161 decl
= start_decl (declarator
, &type_specifiers
,
30162 SD_INITIALIZED
, attributes
,
30163 /*prefix_attributes=*/NULL_TREE
,
30166 auto_node
= type_uses_auto (TREE_TYPE (decl
));
30167 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30169 if (cp_lexer_next_token_is (parser
->lexer
,
30172 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
30173 error ("parenthesized initialization is not allowed in "
30174 "OpenMP %<for%> loop");
30176 error ("parenthesized initialization is "
30177 "not allowed in for-loop");
30180 /* Trigger an error. */
30181 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
30183 init
= error_mark_node
;
30184 cp_parser_skip_to_end_of_statement (parser
);
30186 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
30187 || type_dependent_expression_p (decl
)
30190 bool is_direct_init
, is_non_constant_init
;
30192 init
= cp_parser_initializer (parser
,
30194 &is_non_constant_init
);
30199 = do_auto_deduction (TREE_TYPE (decl
), init
,
30202 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
30203 && !type_dependent_expression_p (decl
))
30207 cp_finish_decl (decl
, init
, !is_non_constant_init
,
30209 LOOKUP_ONLYCONVERTING
);
30210 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
30212 vec_safe_push (for_block
, this_pre_body
);
30216 init
= pop_stmt_list (this_pre_body
);
30217 this_pre_body
= NULL_TREE
;
30222 cp_lexer_consume_token (parser
->lexer
);
30223 init
= cp_parser_assignment_expression (parser
);
30226 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
30227 init
= error_mark_node
;
30229 cp_finish_decl (decl
, NULL_TREE
,
30230 /*init_const_expr_p=*/false,
30232 LOOKUP_ONLYCONVERTING
);
30236 pop_scope (pushed_scope
);
30242 /* If parsing a type specifier sequence failed, then
30243 this MUST be a simple expression. */
30244 if (code
== CILK_FOR
)
30245 error ("%<_Cilk_for%> allows expression instead of declaration only "
30246 "in C, not in C++");
30247 cp_parser_parse_tentatively (parser
);
30248 decl
= cp_parser_primary_expression (parser
, false, false,
30250 if (!cp_parser_error_occurred (parser
)
30253 && CLASS_TYPE_P (TREE_TYPE (decl
)))
30257 cp_parser_parse_definitely (parser
);
30258 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
30259 rhs
= cp_parser_assignment_expression (parser
);
30260 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
30263 tf_warning_or_error
));
30264 add_private_clause
= true;
30269 cp_parser_abort_tentative_parse (parser
);
30270 init
= cp_parser_expression (parser
);
30273 if (TREE_CODE (init
) == MODIFY_EXPR
30274 || TREE_CODE (init
) == MODOP_EXPR
)
30275 real_decl
= TREE_OPERAND (init
, 0);
30279 return add_private_clause
;
30282 /* Parse the restricted form of the for statement allowed by OpenMP. */
30285 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
30288 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
30289 tree real_decl
, initv
, condv
, incrv
, declv
;
30290 tree this_pre_body
, cl
;
30291 location_t loc_first
;
30292 bool collapse_err
= false;
30293 int i
, collapse
= 1, nbraces
= 0;
30294 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
30296 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
30297 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
30298 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
30300 gcc_assert (collapse
>= 1);
30302 declv
= make_tree_vec (collapse
);
30303 initv
= make_tree_vec (collapse
);
30304 condv
= make_tree_vec (collapse
);
30305 incrv
= make_tree_vec (collapse
);
30307 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
30309 for (i
= 0; i
< collapse
; i
++)
30311 int bracecount
= 0;
30312 bool add_private_clause
= false;
30315 if (code
!= CILK_FOR
30316 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30318 cp_parser_error (parser
, "for statement expected");
30321 if (code
== CILK_FOR
30322 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
30324 cp_parser_error (parser
, "_Cilk_for statement expected");
30327 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
30329 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30332 init
= decl
= real_decl
= NULL
;
30333 this_pre_body
= push_stmt_list ();
30336 |= cp_parser_omp_for_loop_init (parser
, code
,
30337 this_pre_body
, for_block
,
30338 init
, decl
, real_decl
);
30340 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30343 this_pre_body
= pop_stmt_list (this_pre_body
);
30347 pre_body
= push_stmt_list ();
30349 add_stmt (this_pre_body
);
30350 pre_body
= pop_stmt_list (pre_body
);
30353 pre_body
= this_pre_body
;
30358 if (cclauses
!= NULL
30359 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
30360 && real_decl
!= NULL_TREE
)
30363 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
30364 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
30365 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30367 error_at (loc
, "iteration variable %qD"
30368 " should not be firstprivate", real_decl
);
30369 *c
= OMP_CLAUSE_CHAIN (*c
);
30371 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
30372 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30374 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30375 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30376 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
30377 OMP_CLAUSE_DECL (l
) = real_decl
;
30378 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
30379 if (code
== OMP_SIMD
)
30381 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30382 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
30386 OMP_CLAUSE_CHAIN (l
) = clauses
;
30389 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
30390 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
30391 add_private_clause
= false;
30395 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
30396 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30397 add_private_clause
= false;
30398 c
= &OMP_CLAUSE_CHAIN (*c
);
30402 if (add_private_clause
)
30405 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30407 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
30408 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
30409 && OMP_CLAUSE_DECL (c
) == decl
)
30411 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
30412 && OMP_CLAUSE_DECL (c
) == decl
)
30413 error_at (loc
, "iteration variable %qD "
30414 "should not be firstprivate",
30416 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
30417 && OMP_CLAUSE_DECL (c
) == decl
)
30418 error_at (loc
, "iteration variable %qD should not be reduction",
30423 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
30424 OMP_CLAUSE_DECL (c
) = decl
;
30425 c
= finish_omp_clauses (c
);
30428 OMP_CLAUSE_CHAIN (c
) = clauses
;
30435 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30436 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
30437 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30440 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30442 /* If decl is an iterator, preserve the operator on decl
30443 until finish_omp_for. */
30445 && ((processing_template_decl
30446 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
30447 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
30448 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
30450 incr
= cp_parser_expression (parser
);
30451 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
30452 SET_EXPR_LOCATION (incr
, input_location
);
30455 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30456 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30457 /*or_comma=*/false,
30458 /*consume_paren=*/true);
30460 TREE_VEC_ELT (declv
, i
) = decl
;
30461 TREE_VEC_ELT (initv
, i
) = init
;
30462 TREE_VEC_ELT (condv
, i
) = cond
;
30463 TREE_VEC_ELT (incrv
, i
) = incr
;
30465 if (i
== collapse
- 1)
30468 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30469 in between the collapsed for loops to be still considered perfectly
30470 nested. Hopefully the final version clarifies this.
30471 For now handle (multiple) {'s and empty statements. */
30472 cp_parser_parse_tentatively (parser
);
30475 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30477 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30479 cp_lexer_consume_token (parser
->lexer
);
30482 else if (bracecount
30483 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30484 cp_lexer_consume_token (parser
->lexer
);
30487 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30488 error_at (loc
, "not enough collapsed for loops");
30489 collapse_err
= true;
30490 cp_parser_abort_tentative_parse (parser
);
30499 cp_parser_parse_definitely (parser
);
30500 nbraces
+= bracecount
;
30504 /* Note that we saved the original contents of this flag when we entered
30505 the structured block, and so we don't need to re-save it here. */
30506 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
30507 parser
->in_statement
= IN_CILK_SIMD_FOR
;
30509 parser
->in_statement
= IN_OMP_FOR
;
30511 /* Note that the grammar doesn't call for a structured block here,
30512 though the loop as a whole is a structured block. */
30513 body
= push_stmt_list ();
30514 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30515 body
= pop_stmt_list (body
);
30517 if (declv
== NULL_TREE
)
30520 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
30521 pre_body
, clauses
);
30525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30527 cp_lexer_consume_token (parser
->lexer
);
30530 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30531 cp_lexer_consume_token (parser
->lexer
);
30536 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30537 "collapsed loops not perfectly nested");
30539 collapse_err
= true;
30540 cp_parser_statement_seq_opt (parser
, NULL
);
30541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
30546 while (!for_block
->is_empty ())
30547 add_stmt (pop_stmt_list (for_block
->pop ()));
30548 release_tree_vector (for_block
);
30553 /* Helper function for OpenMP parsing, split clauses and call
30554 finish_omp_clauses on each of the set of clauses afterwards. */
30557 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
30558 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
30561 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
30562 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
30564 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
30568 #pragma omp simd simd-clause[optseq] new-line
30571 #define OMP_SIMD_CLAUSE_MASK \
30572 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30581 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30582 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30584 tree clauses
, sb
, ret
;
30586 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30588 strcat (p_name
, " simd");
30589 mask
|= OMP_SIMD_CLAUSE_MASK
;
30590 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
30592 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30596 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
30597 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
30600 sb
= begin_omp_structured_block ();
30601 save
= cp_parser_begin_omp_structured_block (parser
);
30603 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
30605 cp_parser_end_omp_structured_block (parser
, save
);
30606 add_stmt (finish_omp_structured_block (sb
));
30612 #pragma omp for for-clause[optseq] new-line
30616 #pragma omp for simd for-simd-clause[optseq] new-line
30619 #define OMP_FOR_CLAUSE_MASK \
30620 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30630 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
30631 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30633 tree clauses
, sb
, ret
;
30635 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30637 strcat (p_name
, " for");
30638 mask
|= OMP_FOR_CLAUSE_MASK
;
30640 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30644 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30645 const char *p
= IDENTIFIER_POINTER (id
);
30647 if (strcmp (p
, "simd") == 0)
30649 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30650 if (cclauses
== NULL
)
30651 cclauses
= cclauses_buf
;
30653 cp_lexer_consume_token (parser
->lexer
);
30654 if (!flag_openmp
) /* flag_openmp_simd */
30655 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30657 sb
= begin_omp_structured_block ();
30658 save
= cp_parser_begin_omp_structured_block (parser
);
30659 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30661 cp_parser_end_omp_structured_block (parser
, save
);
30662 tree body
= finish_omp_structured_block (sb
);
30665 ret
= make_node (OMP_FOR
);
30666 TREE_TYPE (ret
) = void_type_node
;
30667 OMP_FOR_BODY (ret
) = body
;
30668 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30669 SET_EXPR_LOCATION (ret
, loc
);
30674 if (!flag_openmp
) /* flag_openmp_simd */
30676 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30680 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30684 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
30685 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30688 sb
= begin_omp_structured_block ();
30689 save
= cp_parser_begin_omp_structured_block (parser
);
30691 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
30693 cp_parser_end_omp_structured_block (parser
, save
);
30694 add_stmt (finish_omp_structured_block (sb
));
30700 # pragma omp master new-line
30701 structured-block */
30704 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
30706 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30707 return c_finish_omp_master (input_location
,
30708 cp_parser_omp_structured_block (parser
));
30712 # pragma omp ordered new-line
30713 structured-block */
30716 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
30718 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30719 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30720 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
30726 { section-sequence }
30729 section-directive[opt] structured-block
30730 section-sequence section-directive structured-block */
30733 cp_parser_omp_sections_scope (cp_parser
*parser
)
30735 tree stmt
, substmt
;
30736 bool error_suppress
= false;
30739 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
30742 stmt
= push_stmt_list ();
30744 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
30746 substmt
= cp_parser_omp_structured_block (parser
);
30747 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30748 add_stmt (substmt
);
30753 tok
= cp_lexer_peek_token (parser
->lexer
);
30754 if (tok
->type
== CPP_CLOSE_BRACE
)
30756 if (tok
->type
== CPP_EOF
)
30759 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
30761 cp_lexer_consume_token (parser
->lexer
);
30762 cp_parser_require_pragma_eol (parser
, tok
);
30763 error_suppress
= false;
30765 else if (!error_suppress
)
30767 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
30768 error_suppress
= true;
30771 substmt
= cp_parser_omp_structured_block (parser
);
30772 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30773 add_stmt (substmt
);
30775 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
30777 substmt
= pop_stmt_list (stmt
);
30779 stmt
= make_node (OMP_SECTIONS
);
30780 TREE_TYPE (stmt
) = void_type_node
;
30781 OMP_SECTIONS_BODY (stmt
) = substmt
;
30788 # pragma omp sections sections-clause[optseq] newline
30791 #define OMP_SECTIONS_CLAUSE_MASK \
30792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30799 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
30800 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30803 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30805 strcat (p_name
, " sections");
30806 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
30808 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30810 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30814 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
30815 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
30818 ret
= cp_parser_omp_sections_scope (parser
);
30820 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
30826 # pragma omp parallel parallel-clause[optseq] new-line
30828 # pragma omp parallel for parallel-for-clause[optseq] new-line
30830 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30834 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30835 structured-block */
30837 #define OMP_PARALLEL_CLAUSE_MASK \
30838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30849 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
30850 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30852 tree stmt
, clauses
, block
;
30854 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30856 strcat (p_name
, " parallel");
30857 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
30859 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30861 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30862 if (cclauses
== NULL
)
30863 cclauses
= cclauses_buf
;
30865 cp_lexer_consume_token (parser
->lexer
);
30866 if (!flag_openmp
) /* flag_openmp_simd */
30867 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30868 block
= begin_omp_parallel ();
30869 save
= cp_parser_begin_omp_structured_block (parser
);
30870 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30871 cp_parser_end_omp_structured_block (parser
, save
);
30872 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30874 if (ret
== NULL_TREE
)
30876 OMP_PARALLEL_COMBINED (stmt
) = 1;
30881 error_at (loc
, "expected %<for%> after %qs", p_name
);
30882 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30885 else if (!flag_openmp
) /* flag_openmp_simd */
30887 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30890 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30892 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30893 const char *p
= IDENTIFIER_POINTER (id
);
30894 if (strcmp (p
, "sections") == 0)
30896 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30897 cclauses
= cclauses_buf
;
30899 cp_lexer_consume_token (parser
->lexer
);
30900 block
= begin_omp_parallel ();
30901 save
= cp_parser_begin_omp_structured_block (parser
);
30902 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30903 cp_parser_end_omp_structured_block (parser
, save
);
30904 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30906 OMP_PARALLEL_COMBINED (stmt
) = 1;
30911 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
30913 block
= begin_omp_parallel ();
30914 save
= cp_parser_begin_omp_structured_block (parser
);
30915 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30916 cp_parser_end_omp_structured_block (parser
, save
);
30917 stmt
= finish_omp_parallel (clauses
, block
);
30922 # pragma omp single single-clause[optseq] new-line
30923 structured-block */
30925 #define OMP_SINGLE_CLAUSE_MASK \
30926 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30932 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
30934 tree stmt
= make_node (OMP_SINGLE
);
30935 TREE_TYPE (stmt
) = void_type_node
;
30937 OMP_SINGLE_CLAUSES (stmt
)
30938 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
30939 "#pragma omp single", pragma_tok
);
30940 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30942 return add_stmt (stmt
);
30946 # pragma omp task task-clause[optseq] new-line
30947 structured-block */
30949 #define OMP_TASK_CLAUSE_MASK \
30950 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30961 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
30963 tree clauses
, block
;
30966 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
30967 "#pragma omp task", pragma_tok
);
30968 block
= begin_omp_task ();
30969 save
= cp_parser_begin_omp_structured_block (parser
);
30970 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30971 cp_parser_end_omp_structured_block (parser
, save
);
30972 return finish_omp_task (clauses
, block
);
30976 # pragma omp taskwait new-line */
30979 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
30981 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30982 finish_omp_taskwait ();
30986 # pragma omp taskyield new-line */
30989 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
30991 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30992 finish_omp_taskyield ();
30996 # pragma omp taskgroup new-line
30997 structured-block */
31000 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
31002 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31003 return c_finish_omp_taskgroup (input_location
,
31004 cp_parser_omp_structured_block (parser
));
31009 # pragma omp threadprivate (variable-list) */
31012 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
31016 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31017 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31019 finish_omp_threadprivate (vars
);
31023 # pragma omp cancel cancel-clause[optseq] new-line */
31025 #define OMP_CANCEL_CLAUSE_MASK \
31026 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31033 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
31035 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
31036 "#pragma omp cancel", pragma_tok
);
31037 finish_omp_cancel (clauses
);
31041 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31043 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31050 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
31053 bool point_seen
= false;
31055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31057 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31058 const char *p
= IDENTIFIER_POINTER (id
);
31060 if (strcmp (p
, "point") == 0)
31062 cp_lexer_consume_token (parser
->lexer
);
31068 cp_parser_error (parser
, "expected %<point%>");
31069 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31073 clauses
= cp_parser_omp_all_clauses (parser
,
31074 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
31075 "#pragma omp cancellation point",
31077 finish_omp_cancellation_point (clauses
);
31081 #pragma omp distribute distribute-clause[optseq] new-line
31084 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31085 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31091 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
31092 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31094 tree clauses
, sb
, ret
;
31096 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31098 strcat (p_name
, " distribute");
31099 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
31101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31103 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31104 const char *p
= IDENTIFIER_POINTER (id
);
31106 bool parallel
= false;
31108 if (strcmp (p
, "simd") == 0)
31111 parallel
= strcmp (p
, "parallel") == 0;
31112 if (parallel
|| simd
)
31114 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31115 if (cclauses
== NULL
)
31116 cclauses
= cclauses_buf
;
31117 cp_lexer_consume_token (parser
->lexer
);
31118 if (!flag_openmp
) /* flag_openmp_simd */
31121 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
31124 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
31127 sb
= begin_omp_structured_block ();
31128 save
= cp_parser_begin_omp_structured_block (parser
);
31130 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
31133 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
31135 cp_parser_end_omp_structured_block (parser
, save
);
31136 tree body
= finish_omp_structured_block (sb
);
31139 ret
= make_node (OMP_DISTRIBUTE
);
31140 TREE_TYPE (ret
) = void_type_node
;
31141 OMP_FOR_BODY (ret
) = body
;
31142 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
31143 SET_EXPR_LOCATION (ret
, loc
);
31148 if (!flag_openmp
) /* flag_openmp_simd */
31150 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31154 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31158 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
31159 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
31162 sb
= begin_omp_structured_block ();
31163 save
= cp_parser_begin_omp_structured_block (parser
);
31165 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
31167 cp_parser_end_omp_structured_block (parser
, save
);
31168 add_stmt (finish_omp_structured_block (sb
));
31174 # pragma omp teams teams-clause[optseq] new-line
31175 structured-block */
31177 #define OMP_TEAMS_CLAUSE_MASK \
31178 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31187 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
31188 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31190 tree clauses
, sb
, ret
;
31192 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31194 strcat (p_name
, " teams");
31195 mask
|= OMP_TEAMS_CLAUSE_MASK
;
31197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31199 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31200 const char *p
= IDENTIFIER_POINTER (id
);
31201 if (strcmp (p
, "distribute") == 0)
31203 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31204 if (cclauses
== NULL
)
31205 cclauses
= cclauses_buf
;
31207 cp_lexer_consume_token (parser
->lexer
);
31208 if (!flag_openmp
) /* flag_openmp_simd */
31209 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
31211 sb
= begin_omp_structured_block ();
31212 save
= cp_parser_begin_omp_structured_block (parser
);
31213 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
31215 cp_parser_end_omp_structured_block (parser
, save
);
31216 tree body
= finish_omp_structured_block (sb
);
31219 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
31220 ret
= make_node (OMP_TEAMS
);
31221 TREE_TYPE (ret
) = void_type_node
;
31222 OMP_TEAMS_CLAUSES (ret
) = clauses
;
31223 OMP_TEAMS_BODY (ret
) = body
;
31224 return add_stmt (ret
);
31227 if (!flag_openmp
) /* flag_openmp_simd */
31229 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31233 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31237 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
31238 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
31241 tree stmt
= make_node (OMP_TEAMS
);
31242 TREE_TYPE (stmt
) = void_type_node
;
31243 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
31244 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31246 return add_stmt (stmt
);
31250 # pragma omp target data target-data-clause[optseq] new-line
31251 structured-block */
31253 #define OMP_TARGET_DATA_CLAUSE_MASK \
31254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31259 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
31261 tree stmt
= make_node (OMP_TARGET_DATA
);
31262 TREE_TYPE (stmt
) = void_type_node
;
31264 OMP_TARGET_DATA_CLAUSES (stmt
)
31265 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
31266 "#pragma omp target data", pragma_tok
);
31267 keep_next_level (true);
31268 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31270 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31271 return add_stmt (stmt
);
31275 # pragma omp target update target-update-clause[optseq] new-line */
31277 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31284 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
31285 enum pragma_context context
)
31287 if (context
== pragma_stmt
)
31289 error_at (pragma_tok
->location
,
31290 "%<#pragma omp target update%> may only be "
31291 "used in compound statements");
31292 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31297 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
31298 "#pragma omp target update", pragma_tok
);
31299 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
31300 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
31302 error_at (pragma_tok
->location
,
31303 "%<#pragma omp target update must contain at least one "
31304 "%<from%> or %<to%> clauses");
31308 tree stmt
= make_node (OMP_TARGET_UPDATE
);
31309 TREE_TYPE (stmt
) = void_type_node
;
31310 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
31311 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31317 # pragma omp target target-clause[optseq] new-line
31318 structured-block */
31320 #define OMP_TARGET_CLAUSE_MASK \
31321 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31326 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
31327 enum pragma_context context
)
31329 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
31331 cp_parser_error (parser
, "expected declaration specifiers");
31332 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31338 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31339 const char *p
= IDENTIFIER_POINTER (id
);
31341 if (strcmp (p
, "teams") == 0)
31343 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
31344 char p_name
[sizeof ("#pragma omp target teams distribute "
31345 "parallel for simd")];
31347 cp_lexer_consume_token (parser
->lexer
);
31348 strcpy (p_name
, "#pragma omp target");
31349 if (!flag_openmp
) /* flag_openmp_simd */
31351 tree stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
31352 OMP_TARGET_CLAUSE_MASK
,
31354 return stmt
!= NULL_TREE
;
31356 keep_next_level (true);
31357 tree sb
= begin_omp_structured_block ();
31358 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
31359 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
31360 OMP_TARGET_CLAUSE_MASK
, cclauses
);
31361 cp_parser_end_omp_structured_block (parser
, save
);
31362 tree body
= finish_omp_structured_block (sb
);
31363 if (ret
== NULL_TREE
)
31365 tree stmt
= make_node (OMP_TARGET
);
31366 TREE_TYPE (stmt
) = void_type_node
;
31367 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
31368 OMP_TARGET_BODY (stmt
) = body
;
31372 else if (!flag_openmp
) /* flag_openmp_simd */
31374 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31377 else if (strcmp (p
, "data") == 0)
31379 cp_lexer_consume_token (parser
->lexer
);
31380 cp_parser_omp_target_data (parser
, pragma_tok
);
31383 else if (strcmp (p
, "update") == 0)
31385 cp_lexer_consume_token (parser
->lexer
);
31386 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
31390 tree stmt
= make_node (OMP_TARGET
);
31391 TREE_TYPE (stmt
) = void_type_node
;
31393 OMP_TARGET_CLAUSES (stmt
)
31394 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
31395 "#pragma omp target", pragma_tok
);
31396 keep_next_level (true);
31397 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31399 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31405 # pragma acc cache (variable-list) new-line
31409 cp_parser_oacc_cache (cp_parser
*parser
,
31410 cp_token
*pragma_tok
__attribute__((unused
)))
31412 cp_parser_omp_var_list (parser
, OMP_NO_CLAUSE_CACHE
, NULL_TREE
);
31413 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
31419 # pragma acc data oacc-data-clause[optseq] new-line
31420 structured-block */
31422 #define OACC_DATA_CLAUSE_MASK \
31423 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE))
31436 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
)
31438 tree stmt
, clauses
, block
;
31441 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
31442 "#pragma acc data", pragma_tok
);
31444 block
= begin_omp_parallel ();
31445 save
= cp_parser_begin_omp_structured_block (parser
);
31446 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31447 cp_parser_end_omp_structured_block (parser
, save
);
31448 stmt
= finish_oacc_data (clauses
, block
);
31453 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31454 structured-block */
31456 #define OACC_KERNELS_CLAUSE_MASK \
31457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
31469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31472 cp_parser_oacc_kernels (cp_parser
*parser
, cp_token
*pragma_tok
)
31474 tree stmt
, clauses
, block
;
31477 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_KERNELS_CLAUSE_MASK
,
31478 "#pragma acc kernels", pragma_tok
);
31480 block
= begin_omp_parallel ();
31481 save
= cp_parser_begin_omp_structured_block (parser
);
31482 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31483 cp_parser_end_omp_structured_block (parser
, save
);
31484 stmt
= finish_oacc_kernels (clauses
, block
);
31489 # pragma acc loop oacc-loop-clause[optseq] new-line
31490 structured-block */
31492 #define OACC_LOOP_CLAUSE_MASK \
31493 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
31494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION))
31497 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
)
31499 tree stmt
, clauses
, block
;
31502 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_LOOP_CLAUSE_MASK
,
31503 "#pragma acc loop", pragma_tok
);
31505 block
= begin_omp_structured_block ();
31506 save
= cp_parser_begin_omp_structured_block (parser
);
31507 stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
);
31508 cp_parser_end_omp_structured_block (parser
, save
);
31509 add_stmt (finish_omp_structured_block (block
));
31514 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31515 structured-block */
31517 #define OACC_PARALLEL_CLAUSE_MASK \
31518 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_GANGS) \
31526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_WORKERS) \
31527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
31532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_VECTOR_LENGTH) \
31534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31537 cp_parser_oacc_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
31539 tree stmt
, clauses
, block
;
31542 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_PARALLEL_CLAUSE_MASK
,
31543 "#pragma acc parallel", pragma_tok
);
31545 block
= begin_omp_parallel ();
31546 save
= cp_parser_begin_omp_structured_block (parser
);
31547 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31548 cp_parser_end_omp_structured_block (parser
, save
);
31549 stmt
= finish_oacc_parallel (clauses
, block
);
31554 # pragma acc update oacc-update-clause[optseq] new-line
31557 #define OACC_UPDATE_CLAUSE_MASK \
31558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HOST) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SELF) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31566 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
31568 tree stmt
, clauses
;
31570 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
31571 "#pragma acc update", pragma_tok
);
31573 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
31575 error_at (pragma_tok
->location
,
31576 "%<#pragma acc update%> must contain at least one "
31577 "%<device%> or %<host/self%> clause");
31581 stmt
= make_node (OACC_UPDATE
);
31582 TREE_TYPE (stmt
) = void_type_node
;
31583 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
31584 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31590 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31592 LOC is the location of the #pragma token.
31595 #define OACC_WAIT_CLAUSE_MASK \
31596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC))
31599 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
31601 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
31602 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31604 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31605 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
31607 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
31608 "#pragma acc wait", pragma_tok
);
31610 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
31616 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31618 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31627 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
31628 enum pragma_context context
)
31630 bool first_p
= parser
->omp_declare_simd
== NULL
;
31631 cp_omp_declare_simd_data data
;
31634 data
.error_seen
= false;
31635 data
.fndecl_seen
= false;
31636 data
.tokens
= vNULL
;
31637 parser
->omp_declare_simd
= &data
;
31639 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31640 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
31641 cp_lexer_consume_token (parser
->lexer
);
31642 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31643 parser
->omp_declare_simd
->error_seen
= true;
31644 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31645 struct cp_token_cache
*cp
31646 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
31647 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
31650 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
31651 cp_parser_pragma (parser
, context
);
31654 case pragma_external
:
31655 cp_parser_declaration (parser
);
31657 case pragma_member
:
31658 cp_parser_member_declaration (parser
);
31660 case pragma_objc_icode
:
31661 cp_parser_block_declaration (parser
, /*statement_p=*/false);
31664 cp_parser_declaration_statement (parser
);
31667 if (parser
->omp_declare_simd
31668 && !parser
->omp_declare_simd
->error_seen
31669 && !parser
->omp_declare_simd
->fndecl_seen
)
31670 error_at (pragma_tok
->location
,
31671 "%<#pragma omp declare simd%> not immediately followed by "
31672 "function declaration or definition");
31673 data
.tokens
.release ();
31674 parser
->omp_declare_simd
= NULL
;
31678 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31679 This function is modelled similar to the late parsing of omp declare
31683 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
31685 struct cp_token_cache
*ce
;
31686 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
31689 if (parser
->omp_declare_simd
!= NULL
)
31691 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31692 " marked as a Cilk Plus SIMD-enabled function");
31693 XDELETE (parser
->cilk_simd_fn_info
);
31694 parser
->cilk_simd_fn_info
= NULL
;
31697 if (!info
->error_seen
&& info
->fndecl_seen
)
31699 error ("vector attribute not immediately followed by a single function"
31700 " declaration or definition");
31701 info
->error_seen
= true;
31703 if (info
->error_seen
)
31706 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
31710 cp_parser_push_lexer_for_tokens (parser
, ce
);
31711 parser
->lexer
->in_pragma
= true;
31712 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
31713 "SIMD-enabled functions attribute",
31715 cp_parser_pop_lexer (parser
);
31717 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
31719 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
31720 TREE_CHAIN (c
) = attrs
;
31723 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
31724 TREE_CHAIN (c
) = attrs
;
31725 if (processing_template_decl
)
31726 ATTR_IS_DEPENDENT (c
) = 1;
31729 info
->fndecl_seen
= true;
31730 XDELETE (parser
->cilk_simd_fn_info
);
31731 parser
->cilk_simd_fn_info
= NULL
;
31735 /* Finalize #pragma omp declare simd clauses after direct declarator has
31736 been parsed, and put that into "omp declare simd" attribute. */
31739 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
31741 struct cp_token_cache
*ce
;
31742 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
31745 if (!data
->error_seen
&& data
->fndecl_seen
)
31747 error ("%<#pragma omp declare simd%> not immediately followed by "
31748 "a single function declaration or definition");
31749 data
->error_seen
= true;
31752 if (data
->error_seen
)
31755 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
31759 cp_parser_push_lexer_for_tokens (parser
, ce
);
31760 parser
->lexer
->in_pragma
= true;
31761 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
31762 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
31763 cp_lexer_consume_token (parser
->lexer
);
31764 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
31765 "#pragma omp declare simd", pragma_tok
);
31766 cp_parser_pop_lexer (parser
);
31768 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
31769 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
31770 TREE_CHAIN (c
) = attrs
;
31771 if (processing_template_decl
)
31772 ATTR_IS_DEPENDENT (c
) = 1;
31776 data
->fndecl_seen
= true;
31782 # pragma omp declare target new-line
31783 declarations and definitions
31784 # pragma omp end declare target new-line */
31787 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31789 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31790 scope_chain
->omp_declare_target_attribute
++;
31794 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31796 const char *p
= "";
31797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31799 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31800 p
= IDENTIFIER_POINTER (id
);
31802 if (strcmp (p
, "declare") == 0)
31804 cp_lexer_consume_token (parser
->lexer
);
31806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31808 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31809 p
= IDENTIFIER_POINTER (id
);
31811 if (strcmp (p
, "target") == 0)
31812 cp_lexer_consume_token (parser
->lexer
);
31815 cp_parser_error (parser
, "expected %<target%>");
31816 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31822 cp_parser_error (parser
, "expected %<declare%>");
31823 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31826 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31827 if (!scope_chain
->omp_declare_target_attribute
)
31828 error_at (pragma_tok
->location
,
31829 "%<#pragma omp end declare target%> without corresponding "
31830 "%<#pragma omp declare target%>");
31832 scope_chain
->omp_declare_target_attribute
--;
31835 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31836 expression and optional initializer clause of
31837 #pragma omp declare reduction. We store the expression(s) as
31838 either 3, 6 or 7 special statements inside of the artificial function's
31839 body. The first two statements are DECL_EXPRs for the artificial
31840 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31841 expression that uses those variables.
31842 If there was any INITIALIZER clause, this is followed by further statements,
31843 the fourth and fifth statements are DECL_EXPRs for the artificial
31844 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31845 constructor variant (first token after open paren is not omp_priv),
31846 then the sixth statement is a statement with the function call expression
31847 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31848 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31849 to initialize the OMP_PRIV artificial variable and there is seventh
31850 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31853 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
31855 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
31856 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
31857 type
= TREE_TYPE (type
);
31858 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
31859 DECL_ARTIFICIAL (omp_out
) = 1;
31860 pushdecl (omp_out
);
31861 add_decl_expr (omp_out
);
31862 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
31863 DECL_ARTIFICIAL (omp_in
) = 1;
31865 add_decl_expr (omp_in
);
31867 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
31869 keep_next_level (true);
31870 tree block
= begin_omp_structured_block ();
31871 combiner
= cp_parser_expression (parser
);
31872 finish_expr_stmt (combiner
);
31873 block
= finish_omp_structured_block (block
);
31876 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31879 const char *p
= "";
31880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31882 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31883 p
= IDENTIFIER_POINTER (id
);
31886 if (strcmp (p
, "initializer") == 0)
31888 cp_lexer_consume_token (parser
->lexer
);
31889 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31895 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31896 p
= IDENTIFIER_POINTER (id
);
31899 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
31900 DECL_ARTIFICIAL (omp_priv
) = 1;
31901 pushdecl (omp_priv
);
31902 add_decl_expr (omp_priv
);
31903 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
31904 DECL_ARTIFICIAL (omp_orig
) = 1;
31905 pushdecl (omp_orig
);
31906 add_decl_expr (omp_orig
);
31908 keep_next_level (true);
31909 block
= begin_omp_structured_block ();
31912 if (strcmp (p
, "omp_priv") == 0)
31914 bool is_direct_init
, is_non_constant_init
;
31916 cp_lexer_consume_token (parser
->lexer
);
31917 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
31919 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
31920 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31922 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
31923 == CPP_CLOSE_PAREN
))
31925 finish_omp_structured_block (block
);
31926 error ("invalid initializer clause");
31929 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
31930 &is_non_constant_init
);
31931 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
31932 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
31936 cp_parser_parse_tentatively (parser
);
31937 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31938 /*check_dependency_p=*/true,
31939 /*template_p=*/NULL
,
31940 /*declarator_p=*/false,
31941 /*optional_p=*/false);
31942 vec
<tree
, va_gc
> *args
;
31943 if (fn_name
== error_mark_node
31944 || cp_parser_error_occurred (parser
)
31945 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
31946 || ((args
= cp_parser_parenthesized_expression_list
31947 (parser
, non_attr
, /*cast_p=*/false,
31948 /*allow_expansion_p=*/true,
31949 /*non_constant_p=*/NULL
)),
31950 cp_parser_error_occurred (parser
)))
31952 finish_omp_structured_block (block
);
31953 cp_parser_abort_tentative_parse (parser
);
31954 cp_parser_error (parser
, "expected id-expression (arguments)");
31959 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
31960 if (arg
== omp_priv
31961 || (TREE_CODE (arg
) == ADDR_EXPR
31962 && TREE_OPERAND (arg
, 0) == omp_priv
))
31964 cp_parser_abort_tentative_parse (parser
);
31965 if (arg
== NULL_TREE
)
31966 error ("one of the initializer call arguments should be %<omp_priv%>"
31967 " or %<&omp_priv%>");
31968 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
31970 finish_expr_stmt (initializer
);
31973 block
= finish_omp_structured_block (block
);
31974 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
31975 finish_expr_stmt (block
);
31978 add_decl_expr (omp_orig
);
31980 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31984 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
31985 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
31991 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31992 initializer-clause[opt] new-line
31994 initializer-clause:
31995 initializer (omp_priv initializer)
31996 initializer (function-name (argument-list)) */
31999 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
32000 enum pragma_context
)
32002 auto_vec
<tree
> types
;
32003 enum tree_code reduc_code
= ERROR_MARK
;
32004 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
32006 cp_token
*first_token
;
32007 cp_token_cache
*cp
;
32011 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32012 p
= obstack_alloc (&declarator_obstack
, 0);
32014 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32017 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32020 reduc_code
= PLUS_EXPR
;
32023 reduc_code
= MULT_EXPR
;
32026 reduc_code
= MINUS_EXPR
;
32029 reduc_code
= BIT_AND_EXPR
;
32032 reduc_code
= BIT_XOR_EXPR
;
32035 reduc_code
= BIT_IOR_EXPR
;
32038 reduc_code
= TRUTH_ANDIF_EXPR
;
32041 reduc_code
= TRUTH_ORIF_EXPR
;
32044 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
32047 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32048 "%<|%>, %<&&%>, %<||%> or identifier");
32052 if (reduc_code
!= ERROR_MARK
)
32053 cp_lexer_consume_token (parser
->lexer
);
32055 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
32056 if (reduc_id
== error_mark_node
)
32059 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32062 /* Types may not be defined in declare reduction type list. */
32063 const char *saved_message
;
32064 saved_message
= parser
->type_definition_forbidden_message
;
32065 parser
->type_definition_forbidden_message
32066 = G_("types may not be defined in declare reduction type list");
32067 bool saved_colon_corrects_to_scope_p
;
32068 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32069 parser
->colon_corrects_to_scope_p
= false;
32070 bool saved_colon_doesnt_start_class_def_p
;
32071 saved_colon_doesnt_start_class_def_p
32072 = parser
->colon_doesnt_start_class_def_p
;
32073 parser
->colon_doesnt_start_class_def_p
= true;
32077 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32078 type
= cp_parser_type_id (parser
);
32079 if (type
== error_mark_node
)
32081 else if (ARITHMETIC_TYPE_P (type
)
32082 && (orig_reduc_id
== NULL_TREE
32083 || (TREE_CODE (type
) != COMPLEX_TYPE
32084 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
32086 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
32088 error_at (loc
, "predeclared arithmetic type %qT in "
32089 "%<#pragma omp declare reduction%>", type
);
32090 else if (TREE_CODE (type
) == FUNCTION_TYPE
32091 || TREE_CODE (type
) == METHOD_TYPE
32092 || TREE_CODE (type
) == ARRAY_TYPE
)
32093 error_at (loc
, "function or array type %qT in "
32094 "%<#pragma omp declare reduction%>", type
);
32095 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
32096 error_at (loc
, "reference type %qT in "
32097 "%<#pragma omp declare reduction%>", type
);
32098 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
32099 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
32100 "%<#pragma omp declare reduction%>", type
);
32102 types
.safe_push (type
);
32104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32105 cp_lexer_consume_token (parser
->lexer
);
32110 /* Restore the saved message. */
32111 parser
->type_definition_forbidden_message
= saved_message
;
32112 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32113 parser
->colon_doesnt_start_class_def_p
32114 = saved_colon_doesnt_start_class_def_p
;
32116 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
32117 || types
.is_empty ())
32120 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32124 first_token
= cp_lexer_peek_token (parser
->lexer
);
32127 FOR_EACH_VEC_ELT (types
, i
, type
)
32130 = build_function_type_list (void_type_node
,
32131 cp_build_reference_type (type
, false),
32133 tree this_reduc_id
= reduc_id
;
32134 if (!dependent_type_p (type
))
32135 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
32136 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
32137 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
32138 DECL_ARTIFICIAL (fndecl
) = 1;
32139 DECL_EXTERNAL (fndecl
) = 1;
32140 DECL_DECLARED_INLINE_P (fndecl
) = 1;
32141 DECL_IGNORED_P (fndecl
) = 1;
32142 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
32143 DECL_ATTRIBUTES (fndecl
)
32144 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
32145 DECL_ATTRIBUTES (fndecl
));
32146 if (processing_template_decl
)
32147 fndecl
= push_template_decl (fndecl
);
32148 bool block_scope
= false;
32149 tree block
= NULL_TREE
;
32150 if (current_function_decl
)
32152 block_scope
= true;
32153 DECL_CONTEXT (fndecl
) = global_namespace
;
32154 if (!processing_template_decl
)
32157 else if (current_class_type
)
32161 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
32162 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
32163 cp_lexer_consume_token (parser
->lexer
);
32164 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32166 cp
= cp_token_cache_new (first_token
,
32167 cp_lexer_peek_nth_token (parser
->lexer
,
32170 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
32171 finish_member_declaration (fndecl
);
32172 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
32173 DECL_PENDING_INLINE_P (fndecl
) = 1;
32174 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
32179 DECL_CONTEXT (fndecl
) = current_namespace
;
32183 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
32185 block
= begin_omp_structured_block ();
32188 cp_parser_push_lexer_for_tokens (parser
, cp
);
32189 parser
->lexer
->in_pragma
= true;
32191 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
32194 finish_function (0);
32196 DECL_CONTEXT (fndecl
) = current_function_decl
;
32198 cp_parser_pop_lexer (parser
);
32202 cp_parser_pop_lexer (parser
);
32204 finish_function (0);
32207 DECL_CONTEXT (fndecl
) = current_function_decl
;
32208 block
= finish_omp_structured_block (block
);
32209 if (TREE_CODE (block
) == BIND_EXPR
)
32210 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
32211 else if (TREE_CODE (block
) == STATEMENT_LIST
)
32212 DECL_SAVED_TREE (fndecl
) = block
;
32213 if (processing_template_decl
)
32214 add_decl_expr (fndecl
);
32216 cp_check_omp_declare_reduction (fndecl
);
32217 if (cp
== NULL
&& types
.length () > 1)
32218 cp
= cp_token_cache_new (first_token
,
32219 cp_lexer_peek_nth_token (parser
->lexer
, 2));
32220 if (errs
!= errorcount
)
32224 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32227 /* Free any declarators allocated. */
32228 obstack_free (&declarator_obstack
, p
);
32232 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32233 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32234 initializer-clause[opt] new-line
32235 #pragma omp declare target new-line */
32238 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
32239 enum pragma_context context
)
32241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32243 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32244 const char *p
= IDENTIFIER_POINTER (id
);
32246 if (strcmp (p
, "simd") == 0)
32248 cp_lexer_consume_token (parser
->lexer
);
32249 cp_parser_omp_declare_simd (parser
, pragma_tok
,
32253 cp_ensure_no_omp_declare_simd (parser
);
32254 if (strcmp (p
, "reduction") == 0)
32256 cp_lexer_consume_token (parser
->lexer
);
32257 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
32261 if (!flag_openmp
) /* flag_openmp_simd */
32263 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32266 if (strcmp (p
, "target") == 0)
32268 cp_lexer_consume_token (parser
->lexer
);
32269 cp_parser_omp_declare_target (parser
, pragma_tok
);
32273 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
32275 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32278 /* Main entry point to OpenMP statement pragmas. */
32281 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
32284 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
32285 omp_clause_mask
mask (0);
32287 switch (pragma_tok
->pragma_kind
)
32289 case PRAGMA_OACC_CACHE
:
32290 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
32292 case PRAGMA_OACC_DATA
:
32293 stmt
= cp_parser_oacc_data (parser
, pragma_tok
);
32295 case PRAGMA_OACC_KERNELS
:
32296 stmt
= cp_parser_oacc_kernels (parser
, pragma_tok
);
32298 case PRAGMA_OACC_LOOP
:
32299 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
);
32301 case PRAGMA_OACC_PARALLEL
:
32302 stmt
= cp_parser_oacc_parallel (parser
, pragma_tok
);
32304 case PRAGMA_OACC_UPDATE
:
32305 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
32307 case PRAGMA_OACC_WAIT
:
32308 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
32310 case PRAGMA_OMP_ATOMIC
:
32311 cp_parser_omp_atomic (parser
, pragma_tok
);
32313 case PRAGMA_OMP_CRITICAL
:
32314 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
32316 case PRAGMA_OMP_DISTRIBUTE
:
32317 strcpy (p_name
, "#pragma omp");
32318 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
32320 case PRAGMA_OMP_FOR
:
32321 strcpy (p_name
, "#pragma omp");
32322 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
32324 case PRAGMA_OMP_MASTER
:
32325 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
32327 case PRAGMA_OMP_ORDERED
:
32328 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
32330 case PRAGMA_OMP_PARALLEL
:
32331 strcpy (p_name
, "#pragma omp");
32332 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
32334 case PRAGMA_OMP_SECTIONS
:
32335 strcpy (p_name
, "#pragma omp");
32336 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
32338 case PRAGMA_OMP_SIMD
:
32339 strcpy (p_name
, "#pragma omp");
32340 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
32342 case PRAGMA_OMP_SINGLE
:
32343 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
32345 case PRAGMA_OMP_TASK
:
32346 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
32348 case PRAGMA_OMP_TASKGROUP
:
32349 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
32351 case PRAGMA_OMP_TEAMS
:
32352 strcpy (p_name
, "#pragma omp");
32353 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
32356 gcc_unreachable ();
32360 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
32363 /* Transactional Memory parsing routines. */
32365 /* Parse a transaction attribute.
32371 ??? Simplify this when C++0x bracket attributes are
32372 implemented properly. */
32375 cp_parser_txn_attribute_opt (cp_parser
*parser
)
32378 tree attr_name
, attr
= NULL
;
32380 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
32381 return cp_parser_attributes_opt (parser
);
32383 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
32385 cp_lexer_consume_token (parser
->lexer
);
32386 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
32389 token
= cp_lexer_peek_token (parser
->lexer
);
32390 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
32392 token
= cp_lexer_consume_token (parser
->lexer
);
32394 attr_name
= (token
->type
== CPP_KEYWORD
32395 /* For keywords, use the canonical spelling,
32396 not the parsed identifier. */
32397 ? ridpointers
[(int) token
->keyword
]
32399 attr
= build_tree_list (attr_name
, NULL_TREE
);
32402 cp_parser_error (parser
, "expected identifier");
32404 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32406 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32410 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32412 transaction-statement:
32413 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32415 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32419 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
32421 unsigned char old_in
= parser
->in_transaction
;
32422 unsigned char this_in
= 1, new_in
;
32424 tree stmt
, attrs
, noex
;
32426 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32427 || keyword
== RID_TRANSACTION_RELAXED
);
32428 token
= cp_parser_require_keyword (parser
, keyword
,
32429 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32430 : RT_TRANSACTION_RELAXED
));
32431 gcc_assert (token
!= NULL
);
32433 if (keyword
== RID_TRANSACTION_RELAXED
)
32434 this_in
|= TM_STMT_ATTR_RELAXED
;
32437 attrs
= cp_parser_txn_attribute_opt (parser
);
32439 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
32442 /* Parse a noexcept specification. */
32443 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
32445 /* Keep track if we're in the lexical scope of an outer transaction. */
32446 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
32448 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
32450 parser
->in_transaction
= new_in
;
32451 cp_parser_compound_statement (parser
, NULL
, false, false);
32452 parser
->in_transaction
= old_in
;
32454 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
32459 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32461 transaction-expression:
32462 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32463 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32467 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
32469 unsigned char old_in
= parser
->in_transaction
;
32470 unsigned char this_in
= 1;
32475 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32476 || keyword
== RID_TRANSACTION_RELAXED
);
32479 error (keyword
== RID_TRANSACTION_RELAXED
32480 ? G_("%<__transaction_relaxed%> without transactional memory "
32482 : G_("%<__transaction_atomic%> without transactional memory "
32483 "support enabled"));
32485 token
= cp_parser_require_keyword (parser
, keyword
,
32486 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32487 : RT_TRANSACTION_RELAXED
));
32488 gcc_assert (token
!= NULL
);
32490 if (keyword
== RID_TRANSACTION_RELAXED
)
32491 this_in
|= TM_STMT_ATTR_RELAXED
;
32493 /* Set this early. This might mean that we allow transaction_cancel in
32494 an expression that we find out later actually has to be a constexpr.
32495 However, we expect that cxx_constant_value will be able to deal with
32496 this; also, if the noexcept has no constexpr, then what we parse next
32497 really is a transaction's body. */
32498 parser
->in_transaction
= this_in
;
32500 /* Parse a noexcept specification. */
32501 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
32504 if (!noex
|| !noex_expr
32505 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32507 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
32509 expr
= cp_parser_expression (parser
);
32510 expr
= finish_parenthesized_expr (expr
);
32512 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
32516 /* The only expression that is available got parsed for the noexcept
32517 already. noexcept is true then. */
32519 noex
= boolean_true_node
;
32522 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
32523 parser
->in_transaction
= old_in
;
32525 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
32526 return error_mark_node
;
32528 return (flag_tm
? expr
: error_mark_node
);
32531 /* Parse a function-transaction-block.
32533 function-transaction-block:
32534 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32536 __transaction_atomic txn-attribute[opt] function-try-block
32537 __transaction_relaxed ctor-initializer[opt] function-body
32538 __transaction_relaxed function-try-block
32542 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
32544 unsigned char old_in
= parser
->in_transaction
;
32545 unsigned char new_in
= 1;
32546 tree compound_stmt
, stmt
, attrs
;
32547 bool ctor_initializer_p
;
32550 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32551 || keyword
== RID_TRANSACTION_RELAXED
);
32552 token
= cp_parser_require_keyword (parser
, keyword
,
32553 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32554 : RT_TRANSACTION_RELAXED
));
32555 gcc_assert (token
!= NULL
);
32557 if (keyword
== RID_TRANSACTION_RELAXED
)
32558 new_in
|= TM_STMT_ATTR_RELAXED
;
32561 attrs
= cp_parser_txn_attribute_opt (parser
);
32563 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
32566 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
32568 parser
->in_transaction
= new_in
;
32570 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
32571 ctor_initializer_p
= cp_parser_function_try_block (parser
);
32573 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
32574 (parser
, /*in_function_try_block=*/false);
32576 parser
->in_transaction
= old_in
;
32578 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
32580 return ctor_initializer_p
;
32583 /* Parse a __transaction_cancel statement.
32586 __transaction_cancel txn-attribute[opt] ;
32587 __transaction_cancel txn-attribute[opt] throw-expression ;
32589 ??? Cancel and throw is not yet implemented. */
32592 cp_parser_transaction_cancel (cp_parser
*parser
)
32595 bool is_outer
= false;
32598 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
32599 RT_TRANSACTION_CANCEL
);
32600 gcc_assert (token
!= NULL
);
32602 attrs
= cp_parser_txn_attribute_opt (parser
);
32604 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
32606 /* ??? Parse cancel-and-throw here. */
32608 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
32612 error_at (token
->location
, "%<__transaction_cancel%> without "
32613 "transactional memory support enabled");
32614 return error_mark_node
;
32616 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
32618 error_at (token
->location
, "%<__transaction_cancel%> within a "
32619 "%<__transaction_relaxed%>");
32620 return error_mark_node
;
32624 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
32625 && !is_tm_may_cancel_outer (current_function_decl
))
32627 error_at (token
->location
, "outer %<__transaction_cancel%> not "
32628 "within outer %<__transaction_atomic%>");
32629 error_at (token
->location
,
32630 " or a %<transaction_may_cancel_outer%> function");
32631 return error_mark_node
;
32634 else if (parser
->in_transaction
== 0)
32636 error_at (token
->location
, "%<__transaction_cancel%> not within "
32637 "%<__transaction_atomic%>");
32638 return error_mark_node
;
32641 stmt
= build_tm_abort_call (token
->location
, is_outer
);
32649 static GTY (()) cp_parser
*the_parser
;
32652 /* Special handling for the first token or line in the file. The first
32653 thing in the file might be #pragma GCC pch_preprocess, which loads a
32654 PCH file, which is a GC collection point. So we need to handle this
32655 first pragma without benefit of an existing lexer structure.
32657 Always returns one token to the caller in *FIRST_TOKEN. This is
32658 either the true first token of the file, or the first token after
32659 the initial pragma. */
32662 cp_parser_initial_pragma (cp_token
*first_token
)
32666 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32667 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
32670 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32671 if (first_token
->type
== CPP_STRING
)
32673 name
= first_token
->u
.value
;
32675 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32676 if (first_token
->type
!= CPP_PRAGMA_EOL
)
32677 error_at (first_token
->location
,
32678 "junk at end of %<#pragma GCC pch_preprocess%>");
32681 error_at (first_token
->location
, "expected string literal");
32683 /* Skip to the end of the pragma. */
32684 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
32685 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32687 /* Now actually load the PCH file. */
32689 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
32691 /* Read one more token to return to our caller. We have to do this
32692 after reading the PCH file in, since its pointers have to be
32694 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32697 /* Parses the grainsize pragma for the _Cilk_for statement.
32699 #pragma cilk grainsize = <VALUE>. */
32702 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
)
32704 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32706 tree exp
= cp_parser_binary_expression (parser
, false, false,
32707 PREC_NOT_OPERATOR
, NULL
);
32708 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32709 if (!exp
|| exp
== error_mark_node
)
32711 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
32715 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32716 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
32717 cp_parser_cilk_for (parser
, exp
);
32719 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
32720 "%<#pragma cilk grainsize%> is not followed by "
32724 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32727 /* Normal parsing of a pragma token. Here we can (and must) use the
32731 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
32733 cp_token
*pragma_tok
;
32736 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
32737 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
32738 parser
->lexer
->in_pragma
= true;
32740 id
= pragma_tok
->pragma_kind
;
32741 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
32742 cp_ensure_no_omp_declare_simd (parser
);
32745 case PRAGMA_GCC_PCH_PREPROCESS
:
32746 error_at (pragma_tok
->location
,
32747 "%<#pragma GCC pch_preprocess%> must be first");
32750 case PRAGMA_OMP_BARRIER
:
32753 case pragma_compound
:
32754 cp_parser_omp_barrier (parser
, pragma_tok
);
32757 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
32758 "used in compound statements");
32765 case PRAGMA_OMP_FLUSH
:
32768 case pragma_compound
:
32769 cp_parser_omp_flush (parser
, pragma_tok
);
32772 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
32773 "used in compound statements");
32780 case PRAGMA_OMP_TASKWAIT
:
32783 case pragma_compound
:
32784 cp_parser_omp_taskwait (parser
, pragma_tok
);
32787 error_at (pragma_tok
->location
,
32788 "%<#pragma omp taskwait%> may only be "
32789 "used in compound statements");
32796 case PRAGMA_OMP_TASKYIELD
:
32799 case pragma_compound
:
32800 cp_parser_omp_taskyield (parser
, pragma_tok
);
32803 error_at (pragma_tok
->location
,
32804 "%<#pragma omp taskyield%> may only be "
32805 "used in compound statements");
32812 case PRAGMA_OMP_CANCEL
:
32815 case pragma_compound
:
32816 cp_parser_omp_cancel (parser
, pragma_tok
);
32819 error_at (pragma_tok
->location
,
32820 "%<#pragma omp cancel%> may only be "
32821 "used in compound statements");
32828 case PRAGMA_OMP_CANCELLATION_POINT
:
32831 case pragma_compound
:
32832 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
32835 error_at (pragma_tok
->location
,
32836 "%<#pragma omp cancellation point%> may only be "
32837 "used in compound statements");
32844 case PRAGMA_OMP_THREADPRIVATE
:
32845 cp_parser_omp_threadprivate (parser
, pragma_tok
);
32848 case PRAGMA_OMP_DECLARE_REDUCTION
:
32849 cp_parser_omp_declare (parser
, pragma_tok
, context
);
32852 case PRAGMA_OACC_CACHE
:
32853 case PRAGMA_OACC_DATA
:
32854 case PRAGMA_OACC_KERNELS
:
32855 case PRAGMA_OACC_PARALLEL
:
32856 case PRAGMA_OACC_LOOP
:
32857 case PRAGMA_OACC_UPDATE
:
32858 case PRAGMA_OACC_WAIT
:
32859 case PRAGMA_OMP_ATOMIC
:
32860 case PRAGMA_OMP_CRITICAL
:
32861 case PRAGMA_OMP_DISTRIBUTE
:
32862 case PRAGMA_OMP_FOR
:
32863 case PRAGMA_OMP_MASTER
:
32864 case PRAGMA_OMP_ORDERED
:
32865 case PRAGMA_OMP_PARALLEL
:
32866 case PRAGMA_OMP_SECTIONS
:
32867 case PRAGMA_OMP_SIMD
:
32868 case PRAGMA_OMP_SINGLE
:
32869 case PRAGMA_OMP_TASK
:
32870 case PRAGMA_OMP_TASKGROUP
:
32871 case PRAGMA_OMP_TEAMS
:
32872 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
32874 cp_parser_omp_construct (parser
, pragma_tok
);
32877 case PRAGMA_OMP_TARGET
:
32878 return cp_parser_omp_target (parser
, pragma_tok
, context
);
32880 case PRAGMA_OMP_END_DECLARE_TARGET
:
32881 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
32884 case PRAGMA_OMP_SECTION
:
32885 error_at (pragma_tok
->location
,
32886 "%<#pragma omp section%> may only be used in "
32887 "%<#pragma omp sections%> construct");
32892 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32894 tok
= cp_lexer_peek_token (the_parser
->lexer
);
32895 if (tok
->type
!= CPP_KEYWORD
32896 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
32897 && tok
->keyword
!= RID_DO
))
32899 cp_parser_error (parser
, "for, while or do statement expected");
32902 cp_parser_iteration_statement (parser
, true);
32906 case PRAGMA_CILK_SIMD
:
32907 if (context
== pragma_external
)
32909 error_at (pragma_tok
->location
,
32910 "%<#pragma simd%> must be inside a function");
32913 cp_parser_cilk_simd (parser
, pragma_tok
);
32916 case PRAGMA_CILK_GRAINSIZE
:
32917 if (context
== pragma_external
)
32919 error_at (pragma_tok
->location
,
32920 "%<#pragma cilk grainsize%> must be inside a function");
32924 /* Ignore the pragma if Cilk Plus is not enabled. */
32927 cp_parser_cilk_grainsize (parser
, pragma_tok
);
32932 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
32933 "%<#pragma cilk grainsize%>");
32938 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
32939 c_invoke_pragma_handler (id
);
32943 cp_parser_error (parser
, "expected declaration specifiers");
32947 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32951 /* The interface the pragma parsers have to the lexer. */
32954 pragma_lex (tree
*value
)
32957 enum cpp_ttype ret
;
32959 tok
= cp_lexer_peek_token (the_parser
->lexer
);
32962 *value
= tok
->u
.value
;
32964 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
32966 else if (ret
== CPP_STRING
)
32967 *value
= cp_parser_string_literal (the_parser
, false, false);
32970 cp_lexer_consume_token (the_parser
->lexer
);
32971 if (ret
== CPP_KEYWORD
)
32979 /* External interface. */
32981 /* Parse one entire translation unit. */
32984 c_parse_file (void)
32986 static bool already_called
= false;
32988 if (already_called
)
32989 fatal_error ("inter-module optimizations not implemented for C++");
32990 already_called
= true;
32992 the_parser
= cp_parser_new ();
32993 push_deferring_access_checks (flag_access_control
32994 ? dk_no_deferred
: dk_no_check
);
32995 cp_parser_translation_unit (the_parser
);
32999 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33000 vectorlength clause:
33002 vectorlength ( constant-expression ) */
33005 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
33008 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33010 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33011 safelen clause. Thus, vectorlength is represented as OMP 4.0
33012 safelen. For SIMD-enabled function it is represented by OMP 4.0
33015 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
33018 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
33021 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33022 return error_mark_node
;
33024 expr
= cp_parser_constant_expression (parser
);
33025 expr
= maybe_constant_value (expr
);
33027 /* If expr == error_mark_node, then don't emit any errors nor
33028 create a clause. if any of the above functions returns
33029 error mark node then they would have emitted an error message. */
33030 if (expr
== error_mark_node
)
33032 else if (!TREE_TYPE (expr
)
33033 || !TREE_CONSTANT (expr
)
33034 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
33035 error_at (loc
, "vectorlength must be an integer constant");
33036 else if (TREE_CONSTANT (expr
)
33037 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
33038 error_at (loc
, "vectorlength must be a power of 2");
33044 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
33045 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
33046 OMP_CLAUSE_CHAIN (c
) = clauses
;
33051 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
33052 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
33053 OMP_CLAUSE_CHAIN (c
) = clauses
;
33058 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33059 return error_mark_node
;
33063 /* Handles the Cilk Plus #pragma simd linear clause.
33065 linear ( simd-linear-variable-list )
33067 simd-linear-variable-list:
33068 simd-linear-variable
33069 simd-linear-variable-list , simd-linear-variable
33071 simd-linear-variable:
33073 id-expression : simd-linear-step
33076 conditional-expression */
33079 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
33081 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33083 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33085 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33087 cp_parser_error (parser
, "expected identifier");
33088 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33089 return error_mark_node
;
33092 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33093 parser
->colon_corrects_to_scope_p
= false;
33096 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33097 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33099 cp_parser_error (parser
, "expected variable-name");
33100 clauses
= error_mark_node
;
33104 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
33106 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
33108 if (decl
== error_mark_node
)
33110 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
33112 clauses
= error_mark_node
;
33116 tree e
= NULL_TREE
;
33117 tree step_size
= integer_one_node
;
33119 /* If present, parse the linear step. Otherwise, assume the default
33121 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
33123 cp_lexer_consume_token (parser
->lexer
);
33125 e
= cp_parser_assignment_expression (parser
);
33126 e
= maybe_constant_value (e
);
33128 if (e
== error_mark_node
)
33130 /* If an error has occurred, then the whole pragma is
33131 considered ill-formed. Thus, no reason to keep
33133 clauses
= error_mark_node
;
33136 else if (type_dependent_expression_p (e
)
33137 || value_dependent_expression_p (e
)
33139 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
33140 && (TREE_CONSTANT (e
)
33144 cp_parser_error (parser
,
33145 "step size must be an integer constant "
33146 "expression or an integer variable");
33149 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33150 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
33151 OMP_CLAUSE_DECL (l
) = decl
;
33152 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
33153 OMP_CLAUSE_CHAIN (l
) = clauses
;
33156 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33157 cp_lexer_consume_token (parser
->lexer
);
33158 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33162 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33163 "expected %<,%> or %<)%> after %qE", decl
);
33164 clauses
= error_mark_node
;
33168 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33169 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33173 /* Returns the name of the next clause. If the clause is not
33174 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33175 token is not consumed. Otherwise, the appropriate enum from the
33176 pragma_simd_clause is returned and the token is consumed. */
33178 static pragma_omp_clause
33179 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
33181 pragma_omp_clause clause_type
;
33182 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33184 if (token
->keyword
== RID_PRIVATE
)
33185 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
33186 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
33187 return PRAGMA_CILK_CLAUSE_NONE
;
33188 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
33189 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
33190 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
33191 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
33192 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
33193 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
33194 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
33195 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
33196 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
33197 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
33199 return PRAGMA_CILK_CLAUSE_NONE
;
33201 cp_lexer_consume_token (parser
->lexer
);
33202 return clause_type
;
33205 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33208 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
33210 tree clauses
= NULL_TREE
;
33212 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
33213 && clauses
!= error_mark_node
)
33215 pragma_omp_clause c_kind
;
33216 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
33217 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
33218 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
33219 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
33220 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
33221 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
33222 /* Use the OpenMP 4.0 equivalent function. */
33223 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
33224 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
33225 /* Use the OpenMP 4.0 equivalent function. */
33226 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33228 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
33229 /* Use the OMP 4.0 equivalent function. */
33230 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33232 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
33233 /* Use the OMP 4.0 equivalent function. */
33234 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33237 clauses
= error_mark_node
;
33238 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
33243 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
33245 if (clauses
== error_mark_node
)
33246 return error_mark_node
;
33248 return c_finish_cilk_clauses (clauses
);
33251 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33254 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
33256 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
33258 if (clauses
== error_mark_node
)
33261 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
33263 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33264 "for statement expected");
33268 tree sb
= begin_omp_structured_block ();
33269 int save
= cp_parser_begin_omp_structured_block (parser
);
33270 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
33272 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
33273 cp_parser_end_omp_structured_block (parser
, save
);
33274 add_stmt (finish_omp_structured_block (sb
));
33277 /* Main entry-point for parsing Cilk Plus _Cilk_for
33278 loops. The return value is error_mark_node
33279 when errors happen and CILK_FOR tree on success. */
33282 cp_parser_cilk_for (cp_parser
*parser
, tree grain
)
33284 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
33285 gcc_unreachable ();
33287 tree sb
= begin_omp_structured_block ();
33288 int save
= cp_parser_begin_omp_structured_block (parser
);
33290 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
33291 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
33292 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
33293 clauses
= finish_omp_clauses (clauses
);
33295 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
);
33297 cpp_validate_cilk_plus_loop (ret
);
33299 ret
= error_mark_node
;
33301 cp_parser_end_omp_structured_block (parser
, save
);
33302 add_stmt (finish_omp_structured_block (sb
));
33306 /* Create an identifier for a generic parameter type (a synthesized
33307 template parameter implied by `auto' or a concept identifier). */
33309 static GTY(()) int generic_parm_count
;
33311 make_generic_type_name ()
33314 sprintf (buf
, "auto:%d", ++generic_parm_count
);
33315 return get_identifier (buf
);
33318 /* Predicate that behaves as is_auto_or_concept but matches the parent
33319 node of the generic type rather than the generic type itself. This
33320 allows for type transformation in add_implicit_template_parms. */
33323 tree_type_is_auto_or_concept (const_tree t
)
33325 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
33328 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33329 (creating a new template parameter list if necessary). Returns the newly
33330 created template type parm. */
33333 synthesize_implicit_template_parm (cp_parser
*parser
)
33335 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
33337 /* We are either continuing a function template that already contains implicit
33338 template parameters, creating a new fully-implicit function template, or
33339 extending an existing explicit function template with implicit template
33342 cp_binding_level
*const entry_scope
= current_binding_level
;
33344 bool become_template
= false;
33345 cp_binding_level
*parent_scope
= 0;
33347 if (parser
->implicit_template_scope
)
33349 gcc_assert (parser
->implicit_template_parms
);
33351 current_binding_level
= parser
->implicit_template_scope
;
33355 /* Roll back to the existing template parameter scope (in the case of
33356 extending an explicit function template) or introduce a new template
33357 parameter scope ahead of the function parameter scope (or class scope
33358 in the case of out-of-line member definitions). The function scope is
33359 added back after template parameter synthesis below. */
33361 cp_binding_level
*scope
= entry_scope
;
33363 while (scope
->kind
== sk_function_parms
)
33365 parent_scope
= scope
;
33366 scope
= scope
->level_chain
;
33368 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
33370 /* If not defining a class, then any class scope is a scope level in
33371 an out-of-line member definition. In this case simply wind back
33372 beyond the first such scope to inject the template parameter list.
33373 Otherwise wind back to the class being defined. The latter can
33374 occur in class member friend declarations such as:
33380 friend void A::foo (auto);
33383 The template parameter list synthesized for the friend declaration
33384 must be injected in the scope of 'B'. This can also occur in
33385 erroneous cases such as:
33391 void B::foo (auto) {}
33394 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33395 but, nevertheless, the template parameter list synthesized for the
33396 declarator should be injected into the scope of 'A' as if the
33397 ill-formed template was specified explicitly. */
33399 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
33401 parent_scope
= scope
;
33402 scope
= scope
->level_chain
;
33406 current_binding_level
= scope
;
33408 if (scope
->kind
!= sk_template_parms
33409 || !function_being_declared_is_template_p (parser
))
33411 /* Introduce a new template parameter list for implicit template
33414 become_template
= true;
33416 parser
->implicit_template_scope
33417 = begin_scope (sk_template_parms
, NULL
);
33419 ++processing_template_decl
;
33421 parser
->fully_implicit_function_template_p
= true;
33422 ++parser
->num_template_parameter_lists
;
33426 /* Synthesize implicit template parameters at the end of the explicit
33427 template parameter list. */
33429 gcc_assert (current_template_parms
);
33431 parser
->implicit_template_scope
= scope
;
33433 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
33434 parser
->implicit_template_parms
33435 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
33439 /* Synthesize a new template parameter and track the current template
33440 parameter chain with implicit_template_parms. */
33442 tree synth_id
= make_generic_type_name ();
33443 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
33446 = process_template_parm (parser
->implicit_template_parms
,
33448 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
33449 /*non_type=*/false,
33450 /*param_pack=*/false);
33453 if (parser
->implicit_template_parms
)
33454 parser
->implicit_template_parms
33455 = TREE_CHAIN (parser
->implicit_template_parms
);
33457 parser
->implicit_template_parms
= new_parm
;
33459 tree new_type
= TREE_TYPE (getdecls ());
33461 /* If creating a fully implicit function template, start the new implicit
33462 template parameter list with this synthesized type, otherwise grow the
33463 current template parameter list. */
33465 if (become_template
)
33467 parent_scope
->level_chain
= current_binding_level
;
33469 tree new_parms
= make_tree_vec (1);
33470 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
33471 current_template_parms
= tree_cons (size_int (processing_template_decl
),
33472 new_parms
, current_template_parms
);
33476 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
33477 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
33478 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
33479 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
33482 current_binding_level
= entry_scope
;
33487 /* Finish the declaration of a fully implicit function template. Such a
33488 template has no explicit template parameter list so has not been through the
33489 normal template head and tail processing. synthesize_implicit_template_parm
33490 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33491 provided if the declaration is a class member such that its template
33492 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33493 form is returned. Otherwise NULL_TREE is returned. */
33496 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
33498 gcc_assert (parser
->fully_implicit_function_template_p
);
33500 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
33501 && DECL_VIRTUAL_P (member_decl_opt
))
33503 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
33504 "implicit templates may not be %<virtual%>");
33505 DECL_VIRTUAL_P (member_decl_opt
) = false;
33508 if (member_decl_opt
)
33509 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
33510 end_template_decl ();
33512 parser
->fully_implicit_function_template_p
= false;
33513 --parser
->num_template_parameter_lists
;
33515 return member_decl_opt
;
33518 #include "gt-cp-parser.h"