1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2013 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
28 #include "coretypes.h"
32 #include "c-family/c-common.h"
33 #include "c-family/c-objc.h"
34 #include "tree-inline.h"
39 #include "diagnostic.h"
41 #include "tree-iterator.h"
47 #include "hash-table.h"
50 static bool verify_constant (tree
, bool, bool *, bool *);
51 #define VERIFY_CONSTANT(X) \
53 if (verify_constant ((X), allow_non_constant, non_constant_p, overflow_p)) \
57 /* There routines provide a modular interface to perform many parsing
58 operations. They may therefore be used during actual parsing, or
59 during template instantiation, which may be regarded as a
60 degenerate form of parsing. */
62 static tree
maybe_convert_cond (tree
);
63 static tree
finalize_nrv_r (tree
*, int *, void *);
64 static tree
capture_decltype (tree
);
67 /* Deferred Access Checking Overview
68 ---------------------------------
70 Most C++ expressions and declarations require access checking
71 to be performed during parsing. However, in several cases,
72 this has to be treated differently.
74 For member declarations, access checking has to be deferred
75 until more information about the declaration is known. For
87 When we are parsing the function return type `A::X', we don't
88 really know if this is allowed until we parse the function name.
90 Furthermore, some contexts require that access checking is
91 never performed at all. These include class heads, and template
94 Typical use of access checking functions is described here:
96 1. When we enter a context that requires certain access checking
97 mode, the function `push_deferring_access_checks' is called with
98 DEFERRING argument specifying the desired mode. Access checking
99 may be performed immediately (dk_no_deferred), deferred
100 (dk_deferred), or not performed (dk_no_check).
102 2. When a declaration such as a type, or a variable, is encountered,
103 the function `perform_or_defer_access_check' is called. It
104 maintains a vector of all deferred checks.
106 3. The global `current_class_type' or `current_function_decl' is then
107 setup by the parser. `enforce_access' relies on these information
110 4. Upon exiting the context mentioned in step 1,
111 `perform_deferred_access_checks' is called to check all declaration
112 stored in the vector. `pop_deferring_access_checks' is then
113 called to restore the previous access checking mode.
115 In case of parsing error, we simply call `pop_deferring_access_checks'
116 without `perform_deferred_access_checks'. */
118 typedef struct GTY(()) deferred_access
{
119 /* A vector representing name-lookups for which we have deferred
120 checking access controls. We cannot check the accessibility of
121 names used in a decl-specifier-seq until we know what is being
122 declared because code like:
129 A::B* A::f() { return 0; }
131 is valid, even though `A::B' is not generally accessible. */
132 vec
<deferred_access_check
, va_gc
> * GTY(()) deferred_access_checks
;
134 /* The current mode of access checks. */
135 enum deferring_kind deferring_access_checks_kind
;
139 /* Data for deferred access checking. */
140 static GTY(()) vec
<deferred_access
, va_gc
> *deferred_access_stack
;
141 static GTY(()) unsigned deferred_access_no_check
;
143 /* Save the current deferred access states and start deferred
144 access checking iff DEFER_P is true. */
147 push_deferring_access_checks (deferring_kind deferring
)
149 /* For context like template instantiation, access checking
150 disabling applies to all nested context. */
151 if (deferred_access_no_check
|| deferring
== dk_no_check
)
152 deferred_access_no_check
++;
155 deferred_access e
= {NULL
, deferring
};
156 vec_safe_push (deferred_access_stack
, e
);
160 /* Save the current deferred access states and start deferred access
161 checking, continuing the set of deferred checks in CHECKS. */
164 reopen_deferring_access_checks (vec
<deferred_access_check
, va_gc
> * checks
)
166 push_deferring_access_checks (dk_deferred
);
167 if (!deferred_access_no_check
)
168 deferred_access_stack
->last().deferred_access_checks
= checks
;
171 /* Resume deferring access checks again after we stopped doing
175 resume_deferring_access_checks (void)
177 if (!deferred_access_no_check
)
178 deferred_access_stack
->last().deferring_access_checks_kind
= dk_deferred
;
181 /* Stop deferring access checks. */
184 stop_deferring_access_checks (void)
186 if (!deferred_access_no_check
)
187 deferred_access_stack
->last().deferring_access_checks_kind
= dk_no_deferred
;
190 /* Discard the current deferred access checks and restore the
194 pop_deferring_access_checks (void)
196 if (deferred_access_no_check
)
197 deferred_access_no_check
--;
199 deferred_access_stack
->pop ();
202 /* Returns a TREE_LIST representing the deferred checks.
203 The TREE_PURPOSE of each node is the type through which the
204 access occurred; the TREE_VALUE is the declaration named.
207 vec
<deferred_access_check
, va_gc
> *
208 get_deferred_access_checks (void)
210 if (deferred_access_no_check
)
213 return (deferred_access_stack
->last().deferred_access_checks
);
216 /* Take current deferred checks and combine with the
217 previous states if we also defer checks previously.
218 Otherwise perform checks now. */
221 pop_to_parent_deferring_access_checks (void)
223 if (deferred_access_no_check
)
224 deferred_access_no_check
--;
227 vec
<deferred_access_check
, va_gc
> *checks
;
228 deferred_access
*ptr
;
230 checks
= (deferred_access_stack
->last ().deferred_access_checks
);
232 deferred_access_stack
->pop ();
233 ptr
= &deferred_access_stack
->last ();
234 if (ptr
->deferring_access_checks_kind
== dk_no_deferred
)
237 perform_access_checks (checks
, tf_warning_or_error
);
241 /* Merge with parent. */
243 deferred_access_check
*chk
, *probe
;
245 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
247 FOR_EACH_VEC_SAFE_ELT (ptr
->deferred_access_checks
, j
, probe
)
249 if (probe
->binfo
== chk
->binfo
&&
250 probe
->decl
== chk
->decl
&&
251 probe
->diag_decl
== chk
->diag_decl
)
254 /* Insert into parent's checks. */
255 vec_safe_push (ptr
->deferred_access_checks
, *chk
);
262 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
263 is the BINFO indicating the qualifying scope used to access the
264 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
265 or we aren't in SFINAE context or all the checks succeed return TRUE,
269 perform_access_checks (vec
<deferred_access_check
, va_gc
> *checks
,
270 tsubst_flags_t complain
)
273 deferred_access_check
*chk
;
274 location_t loc
= input_location
;
280 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
282 input_location
= chk
->loc
;
283 ok
&= enforce_access (chk
->binfo
, chk
->decl
, chk
->diag_decl
, complain
);
286 input_location
= loc
;
287 return (complain
& tf_error
) ? true : ok
;
290 /* Perform the deferred access checks.
292 After performing the checks, we still have to keep the list
293 `deferred_access_stack->deferred_access_checks' since we may want
294 to check access for them again later in a different context.
301 A::X A::a, x; // No error for `A::a', error for `x'
303 We have to perform deferred access of `A::X', first with `A::a',
304 next with `x'. Return value like perform_access_checks above. */
307 perform_deferred_access_checks (tsubst_flags_t complain
)
309 return perform_access_checks (get_deferred_access_checks (), complain
);
312 /* Defer checking the accessibility of DECL, when looked up in
313 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
314 Return value like perform_access_checks above. */
317 perform_or_defer_access_check (tree binfo
, tree decl
, tree diag_decl
,
318 tsubst_flags_t complain
)
321 deferred_access
*ptr
;
322 deferred_access_check
*chk
;
325 /* Exit if we are in a context that no access checking is performed.
327 if (deferred_access_no_check
)
330 gcc_assert (TREE_CODE (binfo
) == TREE_BINFO
);
332 ptr
= &deferred_access_stack
->last ();
334 /* If we are not supposed to defer access checks, just check now. */
335 if (ptr
->deferring_access_checks_kind
== dk_no_deferred
)
337 bool ok
= enforce_access (binfo
, decl
, diag_decl
, complain
);
338 return (complain
& tf_error
) ? true : ok
;
341 /* See if we are already going to perform this check. */
342 FOR_EACH_VEC_SAFE_ELT (ptr
->deferred_access_checks
, i
, chk
)
344 if (chk
->decl
== decl
&& chk
->binfo
== binfo
&&
345 chk
->diag_decl
== diag_decl
)
350 /* If not, record the check. */
351 deferred_access_check new_access
= {binfo
, decl
, diag_decl
, input_location
};
352 vec_safe_push (ptr
->deferred_access_checks
, new_access
);
357 /* Returns nonzero if the current statement is a full expression,
358 i.e. temporaries created during that statement should be destroyed
359 at the end of the statement. */
362 stmts_are_full_exprs_p (void)
364 return current_stmt_tree ()->stmts_are_full_exprs_p
;
367 /* T is a statement. Add it to the statement-tree. This is the C++
368 version. The C/ObjC frontends have a slightly different version of
374 enum tree_code code
= TREE_CODE (t
);
376 if (EXPR_P (t
) && code
!= LABEL_EXPR
)
378 if (!EXPR_HAS_LOCATION (t
))
379 SET_EXPR_LOCATION (t
, input_location
);
381 /* When we expand a statement-tree, we must know whether or not the
382 statements are full-expressions. We record that fact here. */
383 STMT_IS_FULL_EXPR_P (t
) = stmts_are_full_exprs_p ();
386 /* Add T to the statement-tree. Non-side-effect statements need to be
387 recorded during statement expressions. */
388 gcc_checking_assert (!stmt_list_stack
->is_empty ());
389 append_to_statement_list_force (t
, &cur_stmt_list
);
394 /* Returns the stmt_tree to which statements are currently being added. */
397 current_stmt_tree (void)
400 ? &cfun
->language
->base
.x_stmt_tree
401 : &scope_chain
->x_stmt_tree
);
404 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
407 maybe_cleanup_point_expr (tree expr
)
409 if (!processing_template_decl
&& stmts_are_full_exprs_p ())
410 expr
= fold_build_cleanup_point_expr (TREE_TYPE (expr
), expr
);
414 /* Like maybe_cleanup_point_expr except have the type of the new expression be
415 void so we don't need to create a temporary variable to hold the inner
416 expression. The reason why we do this is because the original type might be
417 an aggregate and we cannot create a temporary variable for that type. */
420 maybe_cleanup_point_expr_void (tree expr
)
422 if (!processing_template_decl
&& stmts_are_full_exprs_p ())
423 expr
= fold_build_cleanup_point_expr (void_type_node
, expr
);
429 /* Create a declaration statement for the declaration given by the DECL. */
432 add_decl_expr (tree decl
)
434 tree r
= build_stmt (input_location
, DECL_EXPR
, decl
);
435 if (DECL_INITIAL (decl
)
436 || (DECL_SIZE (decl
) && TREE_SIDE_EFFECTS (DECL_SIZE (decl
))))
437 r
= maybe_cleanup_point_expr_void (r
);
441 /* Finish a scope. */
444 do_poplevel (tree stmt_list
)
448 if (stmts_are_full_exprs_p ())
449 block
= poplevel (kept_level_p (), 1, 0);
451 stmt_list
= pop_stmt_list (stmt_list
);
453 if (!processing_template_decl
)
455 stmt_list
= c_build_bind_expr (input_location
, block
, stmt_list
);
456 /* ??? See c_end_compound_stmt re statement expressions. */
462 /* Begin a new scope. */
465 do_pushlevel (scope_kind sk
)
467 tree ret
= push_stmt_list ();
468 if (stmts_are_full_exprs_p ())
469 begin_scope (sk
, NULL
);
473 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
474 when the current scope is exited. EH_ONLY is true when this is not
475 meant to apply to normal control flow transfer. */
478 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
480 tree stmt
= build_stmt (input_location
, CLEANUP_STMT
, NULL
, cleanup
, decl
);
481 CLEANUP_EH_ONLY (stmt
) = eh_only
;
483 CLEANUP_BODY (stmt
) = push_stmt_list ();
486 /* Begin a conditional that might contain a declaration. When generating
487 normal code, we want the declaration to appear before the statement
488 containing the conditional. When generating template code, we want the
489 conditional to be rendered as the raw DECL_EXPR. */
492 begin_cond (tree
*cond_p
)
494 if (processing_template_decl
)
495 *cond_p
= push_stmt_list ();
498 /* Finish such a conditional. */
501 finish_cond (tree
*cond_p
, tree expr
)
503 if (processing_template_decl
)
505 tree cond
= pop_stmt_list (*cond_p
);
507 if (expr
== NULL_TREE
)
508 /* Empty condition in 'for'. */
509 gcc_assert (empty_expr_stmt_p (cond
));
510 else if (check_for_bare_parameter_packs (expr
))
511 expr
= error_mark_node
;
512 else if (!empty_expr_stmt_p (cond
))
513 expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), cond
, expr
);
518 /* If *COND_P specifies a conditional with a declaration, transform the
521 for (; A x = 42;) { }
523 while (true) { A x = 42; if (!x) break; }
524 for (;;) { A x = 42; if (!x) break; }
525 The statement list for BODY will be empty if the conditional did
526 not declare anything. */
529 simplify_loop_decl_cond (tree
*cond_p
, tree body
)
533 if (!TREE_SIDE_EFFECTS (body
))
537 *cond_p
= boolean_true_node
;
539 if_stmt
= begin_if_stmt ();
540 cond
= cp_build_unary_op (TRUTH_NOT_EXPR
, cond
, 0, tf_warning_or_error
);
541 finish_if_stmt_cond (cond
, if_stmt
);
542 finish_break_stmt ();
543 finish_then_clause (if_stmt
);
544 finish_if_stmt (if_stmt
);
547 /* Finish a goto-statement. */
550 finish_goto_stmt (tree destination
)
552 if (identifier_p (destination
))
553 destination
= lookup_label (destination
);
555 /* We warn about unused labels with -Wunused. That means we have to
556 mark the used labels as used. */
557 if (TREE_CODE (destination
) == LABEL_DECL
)
558 TREE_USED (destination
) = 1;
561 destination
= mark_rvalue_use (destination
);
562 if (!processing_template_decl
)
564 destination
= cp_convert (ptr_type_node
, destination
,
565 tf_warning_or_error
);
566 if (error_operand_p (destination
))
569 = fold_build_cleanup_point_expr (TREE_TYPE (destination
),
574 check_goto (destination
);
576 return add_stmt (build_stmt (input_location
, GOTO_EXPR
, destination
));
579 /* COND is the condition-expression for an if, while, etc.,
580 statement. Convert it to a boolean value, if appropriate.
581 In addition, verify sequence points if -Wsequence-point is enabled. */
584 maybe_convert_cond (tree cond
)
586 /* Empty conditions remain empty. */
590 /* Wait until we instantiate templates before doing conversion. */
591 if (processing_template_decl
)
594 if (warn_sequence_point
)
595 verify_sequence_points (cond
);
597 /* Do the conversion. */
598 cond
= convert_from_reference (cond
);
600 if (TREE_CODE (cond
) == MODIFY_EXPR
601 && !TREE_NO_WARNING (cond
)
604 warning (OPT_Wparentheses
,
605 "suggest parentheses around assignment used as truth value");
606 TREE_NO_WARNING (cond
) = 1;
609 return condition_conversion (cond
);
612 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
615 finish_expr_stmt (tree expr
)
619 if (expr
!= NULL_TREE
)
621 if (!processing_template_decl
)
623 if (warn_sequence_point
)
624 verify_sequence_points (expr
);
625 expr
= convert_to_void (expr
, ICV_STATEMENT
, tf_warning_or_error
);
627 else if (!type_dependent_expression_p (expr
))
628 convert_to_void (build_non_dependent_expr (expr
), ICV_STATEMENT
,
629 tf_warning_or_error
);
631 if (check_for_bare_parameter_packs (expr
))
632 expr
= error_mark_node
;
634 /* Simplification of inner statement expressions, compound exprs,
635 etc can result in us already having an EXPR_STMT. */
636 if (TREE_CODE (expr
) != CLEANUP_POINT_EXPR
)
638 if (TREE_CODE (expr
) != EXPR_STMT
)
639 expr
= build_stmt (input_location
, EXPR_STMT
, expr
);
640 expr
= maybe_cleanup_point_expr_void (expr
);
650 /* Begin an if-statement. Returns a newly created IF_STMT if
657 scope
= do_pushlevel (sk_cond
);
658 r
= build_stmt (input_location
, IF_STMT
, NULL_TREE
,
659 NULL_TREE
, NULL_TREE
, scope
);
660 begin_cond (&IF_COND (r
));
664 /* Process the COND of an if-statement, which may be given by
668 finish_if_stmt_cond (tree cond
, tree if_stmt
)
670 finish_cond (&IF_COND (if_stmt
), maybe_convert_cond (cond
));
672 THEN_CLAUSE (if_stmt
) = push_stmt_list ();
675 /* Finish the then-clause of an if-statement, which may be given by
679 finish_then_clause (tree if_stmt
)
681 THEN_CLAUSE (if_stmt
) = pop_stmt_list (THEN_CLAUSE (if_stmt
));
685 /* Begin the else-clause of an if-statement. */
688 begin_else_clause (tree if_stmt
)
690 ELSE_CLAUSE (if_stmt
) = push_stmt_list ();
693 /* Finish the else-clause of an if-statement, which may be given by
697 finish_else_clause (tree if_stmt
)
699 ELSE_CLAUSE (if_stmt
) = pop_stmt_list (ELSE_CLAUSE (if_stmt
));
702 /* Finish an if-statement. */
705 finish_if_stmt (tree if_stmt
)
707 tree scope
= IF_SCOPE (if_stmt
);
708 IF_SCOPE (if_stmt
) = NULL
;
709 add_stmt (do_poplevel (scope
));
712 /* Begin a while-statement. Returns a newly created WHILE_STMT if
716 begin_while_stmt (void)
719 r
= build_stmt (input_location
, WHILE_STMT
, NULL_TREE
, NULL_TREE
);
721 WHILE_BODY (r
) = do_pushlevel (sk_block
);
722 begin_cond (&WHILE_COND (r
));
726 /* Process the COND of a while-statement, which may be given by
730 finish_while_stmt_cond (tree cond
, tree while_stmt
, bool ivdep
)
732 finish_cond (&WHILE_COND (while_stmt
), maybe_convert_cond (cond
));
733 if (ivdep
&& cond
!= error_mark_node
)
734 WHILE_COND (while_stmt
) = build2 (ANNOTATE_EXPR
,
735 TREE_TYPE (WHILE_COND (while_stmt
)),
736 WHILE_COND (while_stmt
),
737 build_int_cst (integer_type_node
,
738 annot_expr_ivdep_kind
));
739 simplify_loop_decl_cond (&WHILE_COND (while_stmt
), WHILE_BODY (while_stmt
));
742 /* Finish a while-statement, which may be given by WHILE_STMT. */
745 finish_while_stmt (tree while_stmt
)
747 WHILE_BODY (while_stmt
) = do_poplevel (WHILE_BODY (while_stmt
));
750 /* Begin a do-statement. Returns a newly created DO_STMT if
756 tree r
= build_stmt (input_location
, DO_STMT
, NULL_TREE
, NULL_TREE
);
758 DO_BODY (r
) = push_stmt_list ();
762 /* Finish the body of a do-statement, which may be given by DO_STMT. */
765 finish_do_body (tree do_stmt
)
767 tree body
= DO_BODY (do_stmt
) = pop_stmt_list (DO_BODY (do_stmt
));
769 if (TREE_CODE (body
) == STATEMENT_LIST
&& STATEMENT_LIST_TAIL (body
))
770 body
= STATEMENT_LIST_TAIL (body
)->stmt
;
772 if (IS_EMPTY_STMT (body
))
773 warning (OPT_Wempty_body
,
774 "suggest explicit braces around empty body in %<do%> statement");
777 /* Finish a do-statement, which may be given by DO_STMT, and whose
778 COND is as indicated. */
781 finish_do_stmt (tree cond
, tree do_stmt
, bool ivdep
)
783 cond
= maybe_convert_cond (cond
);
784 if (ivdep
&& cond
!= error_mark_node
)
785 cond
= build2 (ANNOTATE_EXPR
, TREE_TYPE (cond
), cond
,
786 build_int_cst (integer_type_node
, annot_expr_ivdep_kind
));
787 DO_COND (do_stmt
) = cond
;
790 /* Finish a return-statement. The EXPRESSION returned, if any, is as
794 finish_return_stmt (tree expr
)
799 expr
= check_return_expr (expr
, &no_warning
);
801 if (error_operand_p (expr
)
802 || (flag_openmp
&& !check_omp_return ()))
803 return error_mark_node
;
804 if (!processing_template_decl
)
806 if (warn_sequence_point
)
807 verify_sequence_points (expr
);
809 if (DECL_DESTRUCTOR_P (current_function_decl
)
810 || (DECL_CONSTRUCTOR_P (current_function_decl
)
811 && targetm
.cxx
.cdtor_returns_this ()))
813 /* Similarly, all destructors must run destructors for
814 base-classes before returning. So, all returns in a
815 destructor get sent to the DTOR_LABEL; finish_function emits
816 code to return a value there. */
817 return finish_goto_stmt (cdtor_label
);
821 r
= build_stmt (input_location
, RETURN_EXPR
, expr
);
822 TREE_NO_WARNING (r
) |= no_warning
;
823 r
= maybe_cleanup_point_expr_void (r
);
829 /* Begin the scope of a for-statement or a range-for-statement.
830 Both the returned trees are to be used in a call to
831 begin_for_stmt or begin_range_for_stmt. */
834 begin_for_scope (tree
*init
)
836 tree scope
= NULL_TREE
;
837 if (flag_new_for_scope
> 0)
838 scope
= do_pushlevel (sk_for
);
840 if (processing_template_decl
)
841 *init
= push_stmt_list ();
848 /* Begin a for-statement. Returns a new FOR_STMT.
849 SCOPE and INIT should be the return of begin_for_scope,
853 begin_for_stmt (tree scope
, tree init
)
857 r
= build_stmt (input_location
, FOR_STMT
, NULL_TREE
, NULL_TREE
,
858 NULL_TREE
, NULL_TREE
, NULL_TREE
);
860 if (scope
== NULL_TREE
)
862 gcc_assert (!init
|| !(flag_new_for_scope
> 0));
864 scope
= begin_for_scope (&init
);
866 FOR_INIT_STMT (r
) = init
;
867 FOR_SCOPE (r
) = scope
;
872 /* Finish the for-init-statement of a for-statement, which may be
873 given by FOR_STMT. */
876 finish_for_init_stmt (tree for_stmt
)
878 if (processing_template_decl
)
879 FOR_INIT_STMT (for_stmt
) = pop_stmt_list (FOR_INIT_STMT (for_stmt
));
881 FOR_BODY (for_stmt
) = do_pushlevel (sk_block
);
882 begin_cond (&FOR_COND (for_stmt
));
885 /* Finish the COND of a for-statement, which may be given by
889 finish_for_cond (tree cond
, tree for_stmt
, bool ivdep
)
891 finish_cond (&FOR_COND (for_stmt
), maybe_convert_cond (cond
));
892 if (ivdep
&& cond
!= error_mark_node
)
893 FOR_COND (for_stmt
) = build2 (ANNOTATE_EXPR
,
894 TREE_TYPE (FOR_COND (for_stmt
)),
896 build_int_cst (integer_type_node
,
897 annot_expr_ivdep_kind
));
898 simplify_loop_decl_cond (&FOR_COND (for_stmt
), FOR_BODY (for_stmt
));
901 /* Finish the increment-EXPRESSION in a for-statement, which may be
902 given by FOR_STMT. */
905 finish_for_expr (tree expr
, tree for_stmt
)
909 /* If EXPR is an overloaded function, issue an error; there is no
910 context available to use to perform overload resolution. */
911 if (type_unknown_p (expr
))
913 cxx_incomplete_type_error (expr
, TREE_TYPE (expr
));
914 expr
= error_mark_node
;
916 if (!processing_template_decl
)
918 if (warn_sequence_point
)
919 verify_sequence_points (expr
);
920 expr
= convert_to_void (expr
, ICV_THIRD_IN_FOR
,
921 tf_warning_or_error
);
923 else if (!type_dependent_expression_p (expr
))
924 convert_to_void (build_non_dependent_expr (expr
), ICV_THIRD_IN_FOR
,
925 tf_warning_or_error
);
926 expr
= maybe_cleanup_point_expr_void (expr
);
927 if (check_for_bare_parameter_packs (expr
))
928 expr
= error_mark_node
;
929 FOR_EXPR (for_stmt
) = expr
;
932 /* Finish the body of a for-statement, which may be given by
933 FOR_STMT. The increment-EXPR for the loop must be
935 It can also finish RANGE_FOR_STMT. */
938 finish_for_stmt (tree for_stmt
)
940 if (TREE_CODE (for_stmt
) == RANGE_FOR_STMT
)
941 RANGE_FOR_BODY (for_stmt
) = do_poplevel (RANGE_FOR_BODY (for_stmt
));
943 FOR_BODY (for_stmt
) = do_poplevel (FOR_BODY (for_stmt
));
945 /* Pop the scope for the body of the loop. */
946 if (flag_new_for_scope
> 0)
949 tree
*scope_ptr
= (TREE_CODE (for_stmt
) == RANGE_FOR_STMT
950 ? &RANGE_FOR_SCOPE (for_stmt
)
951 : &FOR_SCOPE (for_stmt
));
954 add_stmt (do_poplevel (scope
));
958 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
959 SCOPE and INIT should be the return of begin_for_scope,
961 To finish it call finish_for_stmt(). */
964 begin_range_for_stmt (tree scope
, tree init
)
968 r
= build_stmt (input_location
, RANGE_FOR_STMT
,
969 NULL_TREE
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
971 if (scope
== NULL_TREE
)
973 gcc_assert (!init
|| !(flag_new_for_scope
> 0));
975 scope
= begin_for_scope (&init
);
978 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
981 pop_stmt_list (init
);
982 RANGE_FOR_SCOPE (r
) = scope
;
987 /* Finish the head of a range-based for statement, which may
988 be given by RANGE_FOR_STMT. DECL must be the declaration
989 and EXPR must be the loop expression. */
992 finish_range_for_decl (tree range_for_stmt
, tree decl
, tree expr
)
994 RANGE_FOR_DECL (range_for_stmt
) = decl
;
995 RANGE_FOR_EXPR (range_for_stmt
) = expr
;
996 add_stmt (range_for_stmt
);
997 RANGE_FOR_BODY (range_for_stmt
) = do_pushlevel (sk_block
);
1000 /* Finish a break-statement. */
1003 finish_break_stmt (void)
1005 /* In switch statements break is sometimes stylistically used after
1006 a return statement. This can lead to spurious warnings about
1007 control reaching the end of a non-void function when it is
1008 inlined. Note that we are calling block_may_fallthru with
1009 language specific tree nodes; this works because
1010 block_may_fallthru returns true when given something it does not
1012 if (!block_may_fallthru (cur_stmt_list
))
1013 return void_zero_node
;
1014 return add_stmt (build_stmt (input_location
, BREAK_STMT
));
1017 /* Finish a continue-statement. */
1020 finish_continue_stmt (void)
1022 return add_stmt (build_stmt (input_location
, CONTINUE_STMT
));
1025 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1029 begin_switch_stmt (void)
1033 scope
= do_pushlevel (sk_cond
);
1034 r
= build_stmt (input_location
, SWITCH_STMT
, NULL_TREE
, NULL_TREE
, NULL_TREE
, scope
);
1036 begin_cond (&SWITCH_STMT_COND (r
));
1041 /* Finish the cond of a switch-statement. */
1044 finish_switch_cond (tree cond
, tree switch_stmt
)
1046 tree orig_type
= NULL
;
1047 if (!processing_template_decl
)
1049 /* Convert the condition to an integer or enumeration type. */
1050 cond
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, cond
, true);
1051 if (cond
== NULL_TREE
)
1053 error ("switch quantity not an integer");
1054 cond
= error_mark_node
;
1056 orig_type
= TREE_TYPE (cond
);
1057 if (cond
!= error_mark_node
)
1061 Integral promotions are performed. */
1062 cond
= perform_integral_promotions (cond
);
1063 cond
= maybe_cleanup_point_expr (cond
);
1066 if (check_for_bare_parameter_packs (cond
))
1067 cond
= error_mark_node
;
1068 else if (!processing_template_decl
&& warn_sequence_point
)
1069 verify_sequence_points (cond
);
1071 finish_cond (&SWITCH_STMT_COND (switch_stmt
), cond
);
1072 SWITCH_STMT_TYPE (switch_stmt
) = orig_type
;
1073 add_stmt (switch_stmt
);
1074 push_switch (switch_stmt
);
1075 SWITCH_STMT_BODY (switch_stmt
) = push_stmt_list ();
1078 /* Finish the body of a switch-statement, which may be given by
1079 SWITCH_STMT. The COND to switch on is indicated. */
1082 finish_switch_stmt (tree switch_stmt
)
1086 SWITCH_STMT_BODY (switch_stmt
) =
1087 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt
));
1090 scope
= SWITCH_STMT_SCOPE (switch_stmt
);
1091 SWITCH_STMT_SCOPE (switch_stmt
) = NULL
;
1092 add_stmt (do_poplevel (scope
));
1095 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1099 begin_try_block (void)
1101 tree r
= build_stmt (input_location
, TRY_BLOCK
, NULL_TREE
, NULL_TREE
);
1103 TRY_STMTS (r
) = push_stmt_list ();
1107 /* Likewise, for a function-try-block. The block returned in
1108 *COMPOUND_STMT is an artificial outer scope, containing the
1109 function-try-block. */
1112 begin_function_try_block (tree
*compound_stmt
)
1115 /* This outer scope does not exist in the C++ standard, but we need
1116 a place to put __FUNCTION__ and similar variables. */
1117 *compound_stmt
= begin_compound_stmt (0);
1118 r
= begin_try_block ();
1119 FN_TRY_BLOCK_P (r
) = 1;
1123 /* Finish a try-block, which may be given by TRY_BLOCK. */
1126 finish_try_block (tree try_block
)
1128 TRY_STMTS (try_block
) = pop_stmt_list (TRY_STMTS (try_block
));
1129 TRY_HANDLERS (try_block
) = push_stmt_list ();
1132 /* Finish the body of a cleanup try-block, which may be given by
1136 finish_cleanup_try_block (tree try_block
)
1138 TRY_STMTS (try_block
) = pop_stmt_list (TRY_STMTS (try_block
));
1141 /* Finish an implicitly generated try-block, with a cleanup is given
1145 finish_cleanup (tree cleanup
, tree try_block
)
1147 TRY_HANDLERS (try_block
) = cleanup
;
1148 CLEANUP_P (try_block
) = 1;
1151 /* Likewise, for a function-try-block. */
1154 finish_function_try_block (tree try_block
)
1156 finish_try_block (try_block
);
1157 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1158 the try block, but moving it inside. */
1159 in_function_try_handler
= 1;
1162 /* Finish a handler-sequence for a try-block, which may be given by
1166 finish_handler_sequence (tree try_block
)
1168 TRY_HANDLERS (try_block
) = pop_stmt_list (TRY_HANDLERS (try_block
));
1169 check_handlers (TRY_HANDLERS (try_block
));
1172 /* Finish the handler-seq for a function-try-block, given by
1173 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1174 begin_function_try_block. */
1177 finish_function_handler_sequence (tree try_block
, tree compound_stmt
)
1179 in_function_try_handler
= 0;
1180 finish_handler_sequence (try_block
);
1181 finish_compound_stmt (compound_stmt
);
1184 /* Begin a handler. Returns a HANDLER if appropriate. */
1187 begin_handler (void)
1191 r
= build_stmt (input_location
, HANDLER
, NULL_TREE
, NULL_TREE
);
1194 /* Create a binding level for the eh_info and the exception object
1196 HANDLER_BODY (r
) = do_pushlevel (sk_catch
);
1201 /* Finish the handler-parameters for a handler, which may be given by
1202 HANDLER. DECL is the declaration for the catch parameter, or NULL
1203 if this is a `catch (...)' clause. */
1206 finish_handler_parms (tree decl
, tree handler
)
1208 tree type
= NULL_TREE
;
1209 if (processing_template_decl
)
1213 decl
= pushdecl (decl
);
1214 decl
= push_template_decl (decl
);
1215 HANDLER_PARMS (handler
) = decl
;
1216 type
= TREE_TYPE (decl
);
1220 type
= expand_start_catch_block (decl
);
1221 HANDLER_TYPE (handler
) = type
;
1224 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1225 the return value from the matching call to finish_handler_parms. */
1228 finish_handler (tree handler
)
1230 if (!processing_template_decl
)
1231 expand_end_catch_block ();
1232 HANDLER_BODY (handler
) = do_poplevel (HANDLER_BODY (handler
));
1235 /* Begin a compound statement. FLAGS contains some bits that control the
1236 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1237 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1238 block of a function. If BCS_TRY_BLOCK is set, this is the block
1239 created on behalf of a TRY statement. Returns a token to be passed to
1240 finish_compound_stmt. */
1243 begin_compound_stmt (unsigned int flags
)
1247 if (flags
& BCS_NO_SCOPE
)
1249 r
= push_stmt_list ();
1250 STATEMENT_LIST_NO_SCOPE (r
) = 1;
1252 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1253 But, if it's a statement-expression with a scopeless block, there's
1254 nothing to keep, and we don't want to accidentally keep a block
1255 *inside* the scopeless block. */
1256 keep_next_level (false);
1259 r
= do_pushlevel (flags
& BCS_TRY_BLOCK
? sk_try
: sk_block
);
1261 /* When processing a template, we need to remember where the braces were,
1262 so that we can set up identical scopes when instantiating the template
1263 later. BIND_EXPR is a handy candidate for this.
1264 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1265 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1266 processing templates. */
1267 if (processing_template_decl
)
1269 r
= build3 (BIND_EXPR
, NULL
, NULL
, r
, NULL
);
1270 BIND_EXPR_TRY_BLOCK (r
) = (flags
& BCS_TRY_BLOCK
) != 0;
1271 BIND_EXPR_BODY_BLOCK (r
) = (flags
& BCS_FN_BODY
) != 0;
1272 TREE_SIDE_EFFECTS (r
) = 1;
1278 /* Finish a compound-statement, which is given by STMT. */
1281 finish_compound_stmt (tree stmt
)
1283 if (TREE_CODE (stmt
) == BIND_EXPR
)
1285 tree body
= do_poplevel (BIND_EXPR_BODY (stmt
));
1286 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1287 discard the BIND_EXPR so it can be merged with the containing
1289 if (TREE_CODE (body
) == STATEMENT_LIST
1290 && STATEMENT_LIST_HEAD (body
) == NULL
1291 && !BIND_EXPR_BODY_BLOCK (stmt
)
1292 && !BIND_EXPR_TRY_BLOCK (stmt
))
1295 BIND_EXPR_BODY (stmt
) = body
;
1297 else if (STATEMENT_LIST_NO_SCOPE (stmt
))
1298 stmt
= pop_stmt_list (stmt
);
1301 /* Destroy any ObjC "super" receivers that may have been
1303 objc_clear_super_receiver ();
1305 stmt
= do_poplevel (stmt
);
1308 /* ??? See c_end_compound_stmt wrt statement expressions. */
1312 /* Finish an asm-statement, whose components are a STRING, some
1313 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1314 LABELS. Also note whether the asm-statement should be
1315 considered volatile. */
1318 finish_asm_stmt (int volatile_p
, tree string
, tree output_operands
,
1319 tree input_operands
, tree clobbers
, tree labels
)
1323 int ninputs
= list_length (input_operands
);
1324 int noutputs
= list_length (output_operands
);
1326 if (!processing_template_decl
)
1328 const char *constraint
;
1329 const char **oconstraints
;
1330 bool allows_mem
, allows_reg
, is_inout
;
1334 oconstraints
= XALLOCAVEC (const char *, noutputs
);
1336 string
= resolve_asm_operand_names (string
, output_operands
,
1337 input_operands
, labels
);
1339 for (i
= 0, t
= output_operands
; t
; t
= TREE_CHAIN (t
), ++i
)
1341 operand
= TREE_VALUE (t
);
1343 /* ??? Really, this should not be here. Users should be using a
1344 proper lvalue, dammit. But there's a long history of using
1345 casts in the output operands. In cases like longlong.h, this
1346 becomes a primitive form of typechecking -- if the cast can be
1347 removed, then the output operand had a type of the proper width;
1348 otherwise we'll get an error. Gross, but ... */
1349 STRIP_NOPS (operand
);
1351 operand
= mark_lvalue_use (operand
);
1353 if (!lvalue_or_else (operand
, lv_asm
, tf_warning_or_error
))
1354 operand
= error_mark_node
;
1356 if (operand
!= error_mark_node
1357 && (TREE_READONLY (operand
)
1358 || CP_TYPE_CONST_P (TREE_TYPE (operand
))
1359 /* Functions are not modifiable, even though they are
1361 || TREE_CODE (TREE_TYPE (operand
)) == FUNCTION_TYPE
1362 || TREE_CODE (TREE_TYPE (operand
)) == METHOD_TYPE
1363 /* If it's an aggregate and any field is const, then it is
1364 effectively const. */
1365 || (CLASS_TYPE_P (TREE_TYPE (operand
))
1366 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand
)))))
1367 cxx_readonly_error (operand
, lv_asm
);
1369 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
1370 oconstraints
[i
] = constraint
;
1372 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
1373 &allows_mem
, &allows_reg
, &is_inout
))
1375 /* If the operand is going to end up in memory,
1376 mark it addressable. */
1377 if (!allows_reg
&& !cxx_mark_addressable (operand
))
1378 operand
= error_mark_node
;
1381 operand
= error_mark_node
;
1383 TREE_VALUE (t
) = operand
;
1386 for (i
= 0, t
= input_operands
; t
; ++i
, t
= TREE_CHAIN (t
))
1388 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
1389 bool constraint_parsed
1390 = parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
1391 oconstraints
, &allows_mem
, &allows_reg
);
1392 /* If the operand is going to end up in memory, don't call
1393 decay_conversion. */
1394 if (constraint_parsed
&& !allows_reg
&& allows_mem
)
1395 operand
= mark_lvalue_use (TREE_VALUE (t
));
1397 operand
= decay_conversion (TREE_VALUE (t
), tf_warning_or_error
);
1399 /* If the type of the operand hasn't been determined (e.g.,
1400 because it involves an overloaded function), then issue
1401 an error message. There's no context available to
1402 resolve the overloading. */
1403 if (TREE_TYPE (operand
) == unknown_type_node
)
1405 error ("type of asm operand %qE could not be determined",
1407 operand
= error_mark_node
;
1410 if (constraint_parsed
)
1412 /* If the operand is going to end up in memory,
1413 mark it addressable. */
1414 if (!allows_reg
&& allows_mem
)
1416 /* Strip the nops as we allow this case. FIXME, this really
1417 should be rejected or made deprecated. */
1418 STRIP_NOPS (operand
);
1419 if (!cxx_mark_addressable (operand
))
1420 operand
= error_mark_node
;
1422 else if (!allows_reg
&& !allows_mem
)
1424 /* If constraint allows neither register nor memory,
1425 try harder to get a constant. */
1426 tree constop
= maybe_constant_value (operand
);
1427 if (TREE_CONSTANT (constop
))
1432 operand
= error_mark_node
;
1434 TREE_VALUE (t
) = operand
;
1438 r
= build_stmt (input_location
, ASM_EXPR
, string
,
1439 output_operands
, input_operands
,
1441 ASM_VOLATILE_P (r
) = volatile_p
|| noutputs
== 0;
1442 r
= maybe_cleanup_point_expr_void (r
);
1443 return add_stmt (r
);
1446 /* Finish a label with the indicated NAME. Returns the new label. */
1449 finish_label_stmt (tree name
)
1451 tree decl
= define_label (input_location
, name
);
1453 if (decl
== error_mark_node
)
1454 return error_mark_node
;
1456 add_stmt (build_stmt (input_location
, LABEL_EXPR
, decl
));
1461 /* Finish a series of declarations for local labels. G++ allows users
1462 to declare "local" labels, i.e., labels with scope. This extension
1463 is useful when writing code involving statement-expressions. */
1466 finish_label_decl (tree name
)
1468 if (!at_function_scope_p ())
1470 error ("__label__ declarations are only allowed in function scopes");
1474 add_decl_expr (declare_local_label (name
));
1477 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1480 finish_decl_cleanup (tree decl
, tree cleanup
)
1482 push_cleanup (decl
, cleanup
, false);
1485 /* If the current scope exits with an exception, run CLEANUP. */
1488 finish_eh_cleanup (tree cleanup
)
1490 push_cleanup (NULL
, cleanup
, true);
1493 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1494 order they were written by the user. Each node is as for
1495 emit_mem_initializers. */
1498 finish_mem_initializers (tree mem_inits
)
1500 /* Reorder the MEM_INITS so that they are in the order they appeared
1501 in the source program. */
1502 mem_inits
= nreverse (mem_inits
);
1504 if (processing_template_decl
)
1508 for (mem
= mem_inits
; mem
; mem
= TREE_CHAIN (mem
))
1510 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1511 check for bare parameter packs in the TREE_VALUE, because
1512 any parameter packs in the TREE_VALUE have already been
1513 bound as part of the TREE_PURPOSE. See
1514 make_pack_expansion for more information. */
1515 if (TREE_CODE (TREE_PURPOSE (mem
)) != TYPE_PACK_EXPANSION
1516 && check_for_bare_parameter_packs (TREE_VALUE (mem
)))
1517 TREE_VALUE (mem
) = error_mark_node
;
1520 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION
,
1521 CTOR_INITIALIZER
, mem_inits
));
1524 emit_mem_initializers (mem_inits
);
1527 /* Obfuscate EXPR if it looks like an id-expression or member access so
1528 that the call to finish_decltype in do_auto_deduction will give the
1532 force_paren_expr (tree expr
)
1534 /* This is only needed for decltype(auto) in C++14. */
1535 if (cxx_dialect
< cxx1y
)
1538 if (!DECL_P (expr
) && TREE_CODE (expr
) != COMPONENT_REF
1539 && TREE_CODE (expr
) != SCOPE_REF
)
1542 if (processing_template_decl
)
1543 expr
= build1 (PAREN_EXPR
, TREE_TYPE (expr
), expr
);
1546 cp_lvalue_kind kind
= lvalue_kind (expr
);
1547 if ((kind
& ~clk_class
) != clk_none
)
1549 tree type
= unlowered_expr_type (expr
);
1550 bool rval
= !!(kind
& clk_rvalueref
);
1551 type
= cp_build_reference_type (type
, rval
);
1552 expr
= build_static_cast (type
, expr
, tf_warning_or_error
);
1559 /* Finish a parenthesized expression EXPR. */
1562 finish_parenthesized_expr (tree expr
)
1565 /* This inhibits warnings in c_common_truthvalue_conversion. */
1566 TREE_NO_WARNING (expr
) = 1;
1568 if (TREE_CODE (expr
) == OFFSET_REF
1569 || TREE_CODE (expr
) == SCOPE_REF
)
1570 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1571 enclosed in parentheses. */
1572 PTRMEM_OK_P (expr
) = 0;
1574 if (TREE_CODE (expr
) == STRING_CST
)
1575 PAREN_STRING_LITERAL_P (expr
) = 1;
1577 expr
= force_paren_expr (expr
);
1582 /* Finish a reference to a non-static data member (DECL) that is not
1583 preceded by `.' or `->'. */
1586 finish_non_static_data_member (tree decl
, tree object
, tree qualifying_scope
)
1588 gcc_assert (TREE_CODE (decl
) == FIELD_DECL
);
1592 tree scope
= qualifying_scope
;
1593 if (scope
== NULL_TREE
)
1594 scope
= context_for_name_lookup (decl
);
1595 object
= maybe_dummy_object (scope
, NULL
);
1598 object
= maybe_resolve_dummy (object
);
1599 if (object
== error_mark_node
)
1600 return error_mark_node
;
1602 /* DR 613: Can use non-static data members without an associated
1603 object in sizeof/decltype/alignof. */
1604 if (is_dummy_object (object
) && cp_unevaluated_operand
== 0
1605 && (!processing_template_decl
|| !current_class_ref
))
1607 if (current_function_decl
1608 && DECL_STATIC_FUNCTION_P (current_function_decl
))
1609 error ("invalid use of member %q+D in static member function", decl
);
1611 error ("invalid use of non-static data member %q+D", decl
);
1612 error ("from this location");
1614 return error_mark_node
;
1617 if (current_class_ptr
)
1618 TREE_USED (current_class_ptr
) = 1;
1619 if (processing_template_decl
&& !qualifying_scope
)
1621 tree type
= TREE_TYPE (decl
);
1623 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1624 /* Quals on the object don't matter. */;
1625 else if (PACK_EXPANSION_P (type
))
1626 /* Don't bother trying to represent this. */
1630 /* Set the cv qualifiers. */
1631 int quals
= cp_type_quals (TREE_TYPE (object
));
1633 if (DECL_MUTABLE_P (decl
))
1634 quals
&= ~TYPE_QUAL_CONST
;
1636 quals
|= cp_type_quals (TREE_TYPE (decl
));
1637 type
= cp_build_qualified_type (type
, quals
);
1640 return (convert_from_reference
1641 (build_min (COMPONENT_REF
, type
, object
, decl
, NULL_TREE
)));
1643 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1644 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1646 else if (processing_template_decl
)
1647 return build_qualified_name (TREE_TYPE (decl
),
1650 /*template_p=*/false);
1653 tree access_type
= TREE_TYPE (object
);
1655 perform_or_defer_access_check (TYPE_BINFO (access_type
), decl
,
1656 decl
, tf_warning_or_error
);
1658 /* If the data member was named `C::M', convert `*this' to `C'
1660 if (qualifying_scope
)
1662 tree binfo
= NULL_TREE
;
1663 object
= build_scoped_ref (object
, qualifying_scope
,
1667 return build_class_member_access_expr (object
, decl
,
1668 /*access_path=*/NULL_TREE
,
1669 /*preserve_reference=*/false,
1670 tf_warning_or_error
);
1674 /* If we are currently parsing a template and we encountered a typedef
1675 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1676 adds the typedef to a list tied to the current template.
1677 At template instantiation time, that list is walked and access check
1678 performed for each typedef.
1679 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1682 add_typedef_to_current_template_for_access_check (tree typedef_decl
,
1684 location_t location
)
1686 tree template_info
= NULL
;
1687 tree cs
= current_scope ();
1689 if (!is_typedef_decl (typedef_decl
)
1691 || !CLASS_TYPE_P (context
)
1695 if (CLASS_TYPE_P (cs
) || TREE_CODE (cs
) == FUNCTION_DECL
)
1696 template_info
= get_template_info (cs
);
1699 && TI_TEMPLATE (template_info
)
1700 && !currently_open_class (context
))
1701 append_type_to_template_for_access_check (cs
, typedef_decl
,
1705 /* DECL was the declaration to which a qualified-id resolved. Issue
1706 an error message if it is not accessible. If OBJECT_TYPE is
1707 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1708 type of `*x', or `x', respectively. If the DECL was named as
1709 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1712 check_accessibility_of_qualified_id (tree decl
,
1714 tree nested_name_specifier
)
1717 tree qualifying_type
= NULL_TREE
;
1719 /* If we are parsing a template declaration and if decl is a typedef,
1720 add it to a list tied to the template.
1721 At template instantiation time, that list will be walked and
1722 access check performed. */
1723 add_typedef_to_current_template_for_access_check (decl
,
1724 nested_name_specifier
1725 ? nested_name_specifier
1726 : DECL_CONTEXT (decl
),
1729 /* If we're not checking, return immediately. */
1730 if (deferred_access_no_check
)
1733 /* Determine the SCOPE of DECL. */
1734 scope
= context_for_name_lookup (decl
);
1735 /* If the SCOPE is not a type, then DECL is not a member. */
1736 if (!TYPE_P (scope
))
1738 /* Compute the scope through which DECL is being accessed. */
1740 /* OBJECT_TYPE might not be a class type; consider:
1742 class A { typedef int I; };
1746 In this case, we will have "A::I" as the DECL, but "I" as the
1748 && CLASS_TYPE_P (object_type
)
1749 && DERIVED_FROM_P (scope
, object_type
))
1750 /* If we are processing a `->' or `.' expression, use the type of the
1752 qualifying_type
= object_type
;
1753 else if (nested_name_specifier
)
1755 /* If the reference is to a non-static member of the
1756 current class, treat it as if it were referenced through
1758 if (DECL_NONSTATIC_MEMBER_P (decl
)
1759 && current_class_ptr
1760 && DERIVED_FROM_P (scope
, current_class_type
))
1761 qualifying_type
= current_class_type
;
1762 /* Otherwise, use the type indicated by the
1763 nested-name-specifier. */
1765 qualifying_type
= nested_name_specifier
;
1768 /* Otherwise, the name must be from the current class or one of
1770 qualifying_type
= currently_open_derived_class (scope
);
1773 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1774 or similar in a default argument value. */
1775 && CLASS_TYPE_P (qualifying_type
)
1776 && !dependent_type_p (qualifying_type
))
1777 perform_or_defer_access_check (TYPE_BINFO (qualifying_type
), decl
,
1778 decl
, tf_warning_or_error
);
1781 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1782 class named to the left of the "::" operator. DONE is true if this
1783 expression is a complete postfix-expression; it is false if this
1784 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1785 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1786 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1787 is true iff this qualified name appears as a template argument. */
1790 finish_qualified_id_expr (tree qualifying_class
,
1795 bool template_arg_p
,
1796 tsubst_flags_t complain
)
1798 gcc_assert (TYPE_P (qualifying_class
));
1800 if (error_operand_p (expr
))
1801 return error_mark_node
;
1803 if ((DECL_P (expr
) || BASELINK_P (expr
))
1804 && !mark_used (expr
, complain
))
1805 return error_mark_node
;
1808 check_template_keyword (expr
);
1810 /* If EXPR occurs as the operand of '&', use special handling that
1811 permits a pointer-to-member. */
1812 if (address_p
&& done
)
1814 if (TREE_CODE (expr
) == SCOPE_REF
)
1815 expr
= TREE_OPERAND (expr
, 1);
1816 expr
= build_offset_ref (qualifying_class
, expr
,
1817 /*address_p=*/true, complain
);
1821 /* No need to check access within an enum. */
1822 if (TREE_CODE (qualifying_class
) == ENUMERAL_TYPE
)
1825 /* Within the scope of a class, turn references to non-static
1826 members into expression of the form "this->...". */
1828 /* But, within a template argument, we do not want make the
1829 transformation, as there is no "this" pointer. */
1831 else if (TREE_CODE (expr
) == FIELD_DECL
)
1833 push_deferring_access_checks (dk_no_check
);
1834 expr
= finish_non_static_data_member (expr
, NULL_TREE
,
1836 pop_deferring_access_checks ();
1838 else if (BASELINK_P (expr
) && !processing_template_decl
)
1840 /* See if any of the functions are non-static members. */
1841 /* If so, the expression may be relative to 'this'. */
1842 if (!shared_member_p (expr
)
1843 && current_class_ptr
1844 && DERIVED_FROM_P (qualifying_class
,
1845 current_nonlambda_class_type ()))
1846 expr
= (build_class_member_access_expr
1847 (maybe_dummy_object (qualifying_class
, NULL
),
1849 BASELINK_ACCESS_BINFO (expr
),
1850 /*preserve_reference=*/false,
1853 /* The expression is a qualified name whose address is not
1855 expr
= build_offset_ref (qualifying_class
, expr
, /*address_p=*/false,
1858 else if (BASELINK_P (expr
))
1862 /* In a template, return a SCOPE_REF for most qualified-ids
1863 so that we can check access at instantiation time. But if
1864 we're looking at a member of the current instantiation, we
1865 know we have access and building up the SCOPE_REF confuses
1866 non-type template argument handling. */
1867 if (processing_template_decl
1868 && !currently_open_class (qualifying_class
))
1869 expr
= build_qualified_name (TREE_TYPE (expr
),
1870 qualifying_class
, expr
,
1873 expr
= convert_from_reference (expr
);
1879 /* Begin a statement-expression. The value returned must be passed to
1880 finish_stmt_expr. */
1883 begin_stmt_expr (void)
1885 return push_stmt_list ();
1888 /* Process the final expression of a statement expression. EXPR can be
1889 NULL, if the final expression is empty. Return a STATEMENT_LIST
1890 containing all the statements in the statement-expression, or
1891 ERROR_MARK_NODE if there was an error. */
1894 finish_stmt_expr_expr (tree expr
, tree stmt_expr
)
1896 if (error_operand_p (expr
))
1898 /* The type of the statement-expression is the type of the last
1900 TREE_TYPE (stmt_expr
) = error_mark_node
;
1901 return error_mark_node
;
1904 /* If the last statement does not have "void" type, then the value
1905 of the last statement is the value of the entire expression. */
1908 tree type
= TREE_TYPE (expr
);
1910 if (processing_template_decl
)
1912 expr
= build_stmt (input_location
, EXPR_STMT
, expr
);
1913 expr
= add_stmt (expr
);
1914 /* Mark the last statement so that we can recognize it as such at
1915 template-instantiation time. */
1916 EXPR_STMT_STMT_EXPR_RESULT (expr
) = 1;
1918 else if (VOID_TYPE_P (type
))
1920 /* Just treat this like an ordinary statement. */
1921 expr
= finish_expr_stmt (expr
);
1925 /* It actually has a value we need to deal with. First, force it
1926 to be an rvalue so that we won't need to build up a copy
1927 constructor call later when we try to assign it to something. */
1928 expr
= force_rvalue (expr
, tf_warning_or_error
);
1929 if (error_operand_p (expr
))
1930 return error_mark_node
;
1932 /* Update for array-to-pointer decay. */
1933 type
= TREE_TYPE (expr
);
1935 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1936 normal statement, but don't convert to void or actually add
1938 if (TREE_CODE (expr
) != CLEANUP_POINT_EXPR
)
1939 expr
= maybe_cleanup_point_expr (expr
);
1943 /* The type of the statement-expression is the type of the last
1945 TREE_TYPE (stmt_expr
) = type
;
1951 /* Finish a statement-expression. EXPR should be the value returned
1952 by the previous begin_stmt_expr. Returns an expression
1953 representing the statement-expression. */
1956 finish_stmt_expr (tree stmt_expr
, bool has_no_scope
)
1961 if (error_operand_p (stmt_expr
))
1963 pop_stmt_list (stmt_expr
);
1964 return error_mark_node
;
1967 gcc_assert (TREE_CODE (stmt_expr
) == STATEMENT_LIST
);
1969 type
= TREE_TYPE (stmt_expr
);
1970 result
= pop_stmt_list (stmt_expr
);
1971 TREE_TYPE (result
) = type
;
1973 if (processing_template_decl
)
1975 result
= build_min (STMT_EXPR
, type
, result
);
1976 TREE_SIDE_EFFECTS (result
) = 1;
1977 STMT_EXPR_NO_SCOPE (result
) = has_no_scope
;
1979 else if (CLASS_TYPE_P (type
))
1981 /* Wrap the statement-expression in a TARGET_EXPR so that the
1982 temporary object created by the final expression is destroyed at
1983 the end of the full-expression containing the
1984 statement-expression. */
1985 result
= force_target_expr (type
, result
, tf_warning_or_error
);
1991 /* Returns the expression which provides the value of STMT_EXPR. */
1994 stmt_expr_value_expr (tree stmt_expr
)
1996 tree t
= STMT_EXPR_STMT (stmt_expr
);
1998 if (TREE_CODE (t
) == BIND_EXPR
)
1999 t
= BIND_EXPR_BODY (t
);
2001 if (TREE_CODE (t
) == STATEMENT_LIST
&& STATEMENT_LIST_TAIL (t
))
2002 t
= STATEMENT_LIST_TAIL (t
)->stmt
;
2004 if (TREE_CODE (t
) == EXPR_STMT
)
2005 t
= EXPR_STMT_EXPR (t
);
2010 /* Return TRUE iff EXPR_STMT is an empty list of
2011 expression statements. */
2014 empty_expr_stmt_p (tree expr_stmt
)
2016 tree body
= NULL_TREE
;
2018 if (expr_stmt
== void_zero_node
)
2023 if (TREE_CODE (expr_stmt
) == EXPR_STMT
)
2024 body
= EXPR_STMT_EXPR (expr_stmt
);
2025 else if (TREE_CODE (expr_stmt
) == STATEMENT_LIST
)
2031 if (TREE_CODE (body
) == STATEMENT_LIST
)
2032 return tsi_end_p (tsi_start (body
));
2034 return empty_expr_stmt_p (body
);
2039 /* Perform Koenig lookup. FN is the postfix-expression representing
2040 the function (or functions) to call; ARGS are the arguments to the
2041 call; if INCLUDE_STD then the `std' namespace is automatically
2042 considered an associated namespace (used in range-based for loops).
2043 Returns the functions to be considered by overload resolution. */
2046 perform_koenig_lookup (tree fn
, vec
<tree
, va_gc
> *args
, bool include_std
,
2047 tsubst_flags_t complain
)
2049 tree identifier
= NULL_TREE
;
2050 tree functions
= NULL_TREE
;
2051 tree tmpl_args
= NULL_TREE
;
2052 bool template_id
= false;
2054 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2056 /* Use a separate flag to handle null args. */
2058 tmpl_args
= TREE_OPERAND (fn
, 1);
2059 fn
= TREE_OPERAND (fn
, 0);
2062 /* Find the name of the overloaded function. */
2063 if (identifier_p (fn
))
2065 else if (is_overloaded_fn (fn
))
2068 identifier
= DECL_NAME (get_first_fn (functions
));
2070 else if (DECL_P (fn
))
2073 identifier
= DECL_NAME (fn
);
2076 /* A call to a namespace-scope function using an unqualified name.
2078 Do Koenig lookup -- unless any of the arguments are
2080 if (!any_type_dependent_arguments_p (args
)
2081 && !any_dependent_template_arguments_p (tmpl_args
))
2083 fn
= lookup_arg_dependent (identifier
, functions
, args
, include_std
);
2086 /* The unqualified name could not be resolved. */
2088 fn
= unqualified_fn_lookup_error (identifier
);
2094 if (fn
&& template_id
)
2095 fn
= build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fn
, tmpl_args
);
2100 /* Generate an expression for `FN (ARGS)'. This may change the
2103 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2104 as a virtual call, even if FN is virtual. (This flag is set when
2105 encountering an expression where the function name is explicitly
2106 qualified. For example a call to `X::f' never generates a virtual
2109 Returns code for the call. */
2112 finish_call_expr (tree fn
, vec
<tree
, va_gc
> **args
, bool disallow_virtual
,
2113 bool koenig_p
, tsubst_flags_t complain
)
2117 vec
<tree
, va_gc
> *orig_args
= NULL
;
2119 if (fn
== error_mark_node
)
2120 return error_mark_node
;
2122 gcc_assert (!TYPE_P (fn
));
2126 if (processing_template_decl
)
2128 /* If the call expression is dependent, build a CALL_EXPR node
2129 with no type; type_dependent_expression_p recognizes
2130 expressions with no type as being dependent. */
2131 if (type_dependent_expression_p (fn
)
2132 || any_type_dependent_arguments_p (*args
)
2133 /* For a non-static member function that doesn't have an
2134 explicit object argument, we need to specifically
2135 test the type dependency of the "this" pointer because it
2136 is not included in *ARGS even though it is considered to
2137 be part of the list of arguments. Note that this is
2138 related to CWG issues 515 and 1005. */
2139 || (TREE_CODE (fn
) != COMPONENT_REF
2140 && non_static_member_function_p (fn
)
2141 && current_class_ref
2142 && type_dependent_expression_p (current_class_ref
)))
2144 result
= build_nt_call_vec (fn
, *args
);
2145 SET_EXPR_LOCATION (result
, EXPR_LOC_OR_HERE (fn
));
2146 KOENIG_LOOKUP_P (result
) = koenig_p
;
2151 tree fndecl
= OVL_CURRENT (fn
);
2152 if (TREE_CODE (fndecl
) != FUNCTION_DECL
2153 || !TREE_THIS_VOLATILE (fndecl
))
2159 current_function_returns_abnormally
= 1;
2163 orig_args
= make_tree_vector_copy (*args
);
2164 if (!BASELINK_P (fn
)
2165 && TREE_CODE (fn
) != PSEUDO_DTOR_EXPR
2166 && TREE_TYPE (fn
) != unknown_type_node
)
2167 fn
= build_non_dependent_expr (fn
);
2168 make_args_non_dependent (*args
);
2171 if (TREE_CODE (fn
) == COMPONENT_REF
)
2173 tree member
= TREE_OPERAND (fn
, 1);
2174 if (BASELINK_P (member
))
2176 tree object
= TREE_OPERAND (fn
, 0);
2177 return build_new_method_call (object
, member
,
2180 ? LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
2187 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2188 if (TREE_CODE (fn
) == ADDR_EXPR
2189 && TREE_CODE (TREE_OPERAND (fn
, 0)) == OVERLOAD
)
2190 fn
= TREE_OPERAND (fn
, 0);
2192 if (is_overloaded_fn (fn
))
2193 fn
= baselink_for_fns (fn
);
2196 if (BASELINK_P (fn
))
2200 /* A call to a member function. From [over.call.func]:
2202 If the keyword this is in scope and refers to the class of
2203 that member function, or a derived class thereof, then the
2204 function call is transformed into a qualified function call
2205 using (*this) as the postfix-expression to the left of the
2206 . operator.... [Otherwise] a contrived object of type T
2207 becomes the implied object argument.
2211 struct A { void f(); };
2212 struct B : public A {};
2213 struct C : public A { void g() { B::f(); }};
2215 "the class of that member function" refers to `A'. But 11.2
2216 [class.access.base] says that we need to convert 'this' to B* as
2217 part of the access, so we pass 'B' to maybe_dummy_object. */
2219 object
= maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn
)),
2222 if (processing_template_decl
)
2224 if (type_dependent_expression_p (object
))
2226 tree ret
= build_nt_call_vec (orig_fn
, orig_args
);
2227 release_tree_vector (orig_args
);
2230 object
= build_non_dependent_expr (object
);
2233 result
= build_new_method_call (object
, fn
, args
, NULL_TREE
,
2235 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
2240 else if (is_overloaded_fn (fn
))
2242 /* If the function is an overloaded builtin, resolve it. */
2243 if (TREE_CODE (fn
) == FUNCTION_DECL
2244 && (DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
2245 || DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_MD
))
2246 result
= resolve_overloaded_builtin (input_location
, fn
, *args
);
2250 if (warn_sizeof_pointer_memaccess
2251 && !vec_safe_is_empty (*args
)
2252 && !processing_template_decl
)
2254 location_t sizeof_arg_loc
[3];
2257 for (i
= 0; i
< 3; i
++)
2261 sizeof_arg_loc
[i
] = UNKNOWN_LOCATION
;
2262 sizeof_arg
[i
] = NULL_TREE
;
2263 if (i
>= (*args
)->length ())
2266 if (TREE_CODE (t
) != SIZEOF_EXPR
)
2268 if (SIZEOF_EXPR_TYPE_P (t
))
2269 sizeof_arg
[i
] = TREE_TYPE (TREE_OPERAND (t
, 0));
2271 sizeof_arg
[i
] = TREE_OPERAND (t
, 0);
2272 sizeof_arg_loc
[i
] = EXPR_LOCATION (t
);
2274 sizeof_pointer_memaccess_warning
2275 (sizeof_arg_loc
, fn
, *args
,
2276 sizeof_arg
, same_type_ignoring_top_level_qualifiers_p
);
2279 /* A call to a namespace-scope function. */
2280 result
= build_new_function_call (fn
, args
, koenig_p
, complain
);
2283 else if (TREE_CODE (fn
) == PSEUDO_DTOR_EXPR
)
2285 if (!vec_safe_is_empty (*args
))
2286 error ("arguments to destructor are not allowed");
2287 /* Mark the pseudo-destructor call as having side-effects so
2288 that we do not issue warnings about its use. */
2289 result
= build1 (NOP_EXPR
,
2291 TREE_OPERAND (fn
, 0));
2292 TREE_SIDE_EFFECTS (result
) = 1;
2294 else if (CLASS_TYPE_P (TREE_TYPE (fn
)))
2295 /* If the "function" is really an object of class type, it might
2296 have an overloaded `operator ()'. */
2297 result
= build_op_call (fn
, args
, complain
);
2300 /* A call where the function is unknown. */
2301 result
= cp_build_function_call_vec (fn
, args
, complain
);
2303 if (processing_template_decl
&& result
!= error_mark_node
)
2305 if (INDIRECT_REF_P (result
))
2306 result
= TREE_OPERAND (result
, 0);
2307 result
= build_call_vec (TREE_TYPE (result
), orig_fn
, orig_args
);
2308 SET_EXPR_LOCATION (result
, input_location
);
2309 KOENIG_LOOKUP_P (result
) = koenig_p
;
2310 release_tree_vector (orig_args
);
2311 result
= convert_from_reference (result
);
2316 /* Free garbage OVERLOADs from arg-dependent lookup. */
2317 tree next
= NULL_TREE
;
2319 fn
&& TREE_CODE (fn
) == OVERLOAD
&& OVL_ARG_DEPENDENT (fn
);
2322 if (processing_template_decl
)
2323 /* In a template, we'll re-use them at instantiation time. */
2324 OVL_ARG_DEPENDENT (fn
) = false;
2327 next
= OVL_CHAIN (fn
);
2336 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2337 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2338 POSTDECREMENT_EXPR.) */
2341 finish_increment_expr (tree expr
, enum tree_code code
)
2343 return build_x_unary_op (input_location
, code
, expr
, tf_warning_or_error
);
2346 /* Finish a use of `this'. Returns an expression for `this'. */
2349 finish_this_expr (void)
2353 if (current_class_ptr
)
2355 tree type
= TREE_TYPE (current_class_ref
);
2357 /* In a lambda expression, 'this' refers to the captured 'this'. */
2358 if (LAMBDA_TYPE_P (type
))
2359 result
= lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type
));
2361 result
= current_class_ptr
;
2363 else if (current_function_decl
2364 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2366 error ("%<this%> is unavailable for static member functions");
2367 result
= error_mark_node
;
2371 if (current_function_decl
)
2372 error ("invalid use of %<this%> in non-member function");
2374 error ("invalid use of %<this%> at top level");
2375 result
= error_mark_node
;
2378 /* The keyword 'this' is a prvalue expression. */
2379 result
= rvalue (result
);
2384 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2385 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2386 the TYPE for the type given. If SCOPE is non-NULL, the expression
2387 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2390 finish_pseudo_destructor_expr (tree object
, tree scope
, tree destructor
,
2393 if (object
== error_mark_node
|| destructor
== error_mark_node
)
2394 return error_mark_node
;
2396 gcc_assert (TYPE_P (destructor
));
2398 if (!processing_template_decl
)
2400 if (scope
== error_mark_node
)
2402 error_at (loc
, "invalid qualifying scope in pseudo-destructor name");
2403 return error_mark_node
;
2405 if (is_auto (destructor
))
2406 destructor
= TREE_TYPE (object
);
2407 if (scope
&& TYPE_P (scope
) && !check_dtor_name (scope
, destructor
))
2410 "qualified type %qT does not match destructor name ~%qT",
2412 return error_mark_node
;
2416 /* [expr.pseudo] says both:
2418 The type designated by the pseudo-destructor-name shall be
2419 the same as the object type.
2423 The cv-unqualified versions of the object type and of the
2424 type designated by the pseudo-destructor-name shall be the
2427 We implement the more generous second sentence, since that is
2428 what most other compilers do. */
2429 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object
),
2432 error_at (loc
, "%qE is not of type %qT", object
, destructor
);
2433 return error_mark_node
;
2437 return build3_loc (loc
, PSEUDO_DTOR_EXPR
, void_type_node
, object
,
2441 /* Finish an expression of the form CODE EXPR. */
2444 finish_unary_op_expr (location_t loc
, enum tree_code code
, tree expr
,
2445 tsubst_flags_t complain
)
2447 tree result
= build_x_unary_op (loc
, code
, expr
, complain
);
2448 if ((complain
& tf_warning
)
2449 && TREE_OVERFLOW_P (result
) && !TREE_OVERFLOW_P (expr
))
2450 overflow_warning (input_location
, result
);
2455 /* Finish a compound-literal expression. TYPE is the type to which
2456 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2459 finish_compound_literal (tree type
, tree compound_literal
,
2460 tsubst_flags_t complain
)
2462 if (type
== error_mark_node
)
2463 return error_mark_node
;
2465 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2468 = finish_compound_literal (TREE_TYPE (type
), compound_literal
,
2470 return cp_build_c_cast (type
, compound_literal
, complain
);
2473 if (!TYPE_OBJ_P (type
))
2475 if (complain
& tf_error
)
2476 error ("compound literal of non-object type %qT", type
);
2477 return error_mark_node
;
2480 if (processing_template_decl
)
2482 TREE_TYPE (compound_literal
) = type
;
2483 /* Mark the expression as a compound literal. */
2484 TREE_HAS_CONSTRUCTOR (compound_literal
) = 1;
2485 return compound_literal
;
2488 type
= complete_type (type
);
2490 if (TYPE_NON_AGGREGATE_CLASS (type
))
2492 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2493 everywhere that deals with function arguments would be a pain, so
2494 just wrap it in a TREE_LIST. The parser set a flag so we know
2495 that it came from T{} rather than T({}). */
2496 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal
) = 1;
2497 compound_literal
= build_tree_list (NULL_TREE
, compound_literal
);
2498 return build_functional_cast (type
, compound_literal
, complain
);
2501 if (TREE_CODE (type
) == ARRAY_TYPE
2502 && check_array_initializer (NULL_TREE
, type
, compound_literal
))
2503 return error_mark_node
;
2504 compound_literal
= reshape_init (type
, compound_literal
, complain
);
2505 if (SCALAR_TYPE_P (type
)
2506 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal
)
2507 && (complain
& tf_warning_or_error
))
2508 check_narrowing (type
, compound_literal
);
2509 if (TREE_CODE (type
) == ARRAY_TYPE
2510 && TYPE_DOMAIN (type
) == NULL_TREE
)
2512 cp_complete_array_type_or_error (&type
, compound_literal
,
2514 if (type
== error_mark_node
)
2515 return error_mark_node
;
2517 compound_literal
= digest_init (type
, compound_literal
, complain
);
2518 if (TREE_CODE (compound_literal
) == CONSTRUCTOR
)
2519 TREE_HAS_CONSTRUCTOR (compound_literal
) = true;
2520 /* Put static/constant array temporaries in static variables, but always
2521 represent class temporaries with TARGET_EXPR so we elide copies. */
2522 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type
))
2523 && TREE_CODE (type
) == ARRAY_TYPE
2524 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
2525 && !cp_unevaluated_operand
2526 && initializer_constant_valid_p (compound_literal
, type
))
2528 tree decl
= create_temporary_var (type
);
2529 DECL_INITIAL (decl
) = compound_literal
;
2530 TREE_STATIC (decl
) = 1;
2531 if (literal_type_p (type
) && CP_TYPE_CONST_NON_VOLATILE_P (type
))
2533 /* 5.19 says that a constant expression can include an
2534 lvalue-rvalue conversion applied to "a glvalue of literal type
2535 that refers to a non-volatile temporary object initialized
2536 with a constant expression". Rather than try to communicate
2537 that this VAR_DECL is a temporary, just mark it constexpr. */
2538 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
2539 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
2540 TREE_CONSTANT (decl
) = true;
2542 cp_apply_type_quals_to_decl (cp_type_quals (type
), decl
);
2543 decl
= pushdecl_top_level (decl
);
2544 DECL_NAME (decl
) = make_anon_name ();
2545 SET_DECL_ASSEMBLER_NAME (decl
, DECL_NAME (decl
));
2546 /* Make sure the destructor is callable. */
2547 tree clean
= cxx_maybe_build_cleanup (decl
, complain
);
2548 if (clean
== error_mark_node
)
2549 return error_mark_node
;
2553 return get_target_expr_sfinae (compound_literal
, complain
);
2556 /* Return the declaration for the function-name variable indicated by
2560 finish_fname (tree id
)
2564 decl
= fname_decl (input_location
, C_RID_CODE (id
), id
);
2565 if (processing_template_decl
&& current_function_decl
)
2566 decl
= DECL_NAME (decl
);
2570 /* Finish a translation unit. */
2573 finish_translation_unit (void)
2575 /* In case there were missing closebraces,
2576 get us back to the global binding level. */
2578 while (current_namespace
!= global_namespace
)
2581 /* Do file scope __FUNCTION__ et al. */
2582 finish_fname_decls ();
2585 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2586 Returns the parameter. */
2589 finish_template_type_parm (tree aggr
, tree identifier
)
2591 if (aggr
!= class_type_node
)
2593 permerror (input_location
, "template type parameters must use the keyword %<class%> or %<typename%>");
2594 aggr
= class_type_node
;
2597 return build_tree_list (aggr
, identifier
);
2600 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2601 Returns the parameter. */
2604 finish_template_template_parm (tree aggr
, tree identifier
)
2606 tree decl
= build_decl (input_location
,
2607 TYPE_DECL
, identifier
, NULL_TREE
);
2608 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, identifier
, NULL_TREE
);
2609 DECL_TEMPLATE_PARMS (tmpl
) = current_template_parms
;
2610 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2611 DECL_ARTIFICIAL (decl
) = 1;
2612 end_template_decl ();
2614 gcc_assert (DECL_TEMPLATE_PARMS (tmpl
));
2616 check_default_tmpl_args (decl
, DECL_TEMPLATE_PARMS (tmpl
),
2617 /*is_primary=*/true, /*is_partial=*/false,
2620 return finish_template_type_parm (aggr
, tmpl
);
2623 /* ARGUMENT is the default-argument value for a template template
2624 parameter. If ARGUMENT is invalid, issue error messages and return
2625 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2628 check_template_template_default_arg (tree argument
)
2630 if (TREE_CODE (argument
) != TEMPLATE_DECL
2631 && TREE_CODE (argument
) != TEMPLATE_TEMPLATE_PARM
2632 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
2634 if (TREE_CODE (argument
) == TYPE_DECL
)
2635 error ("invalid use of type %qT as a default value for a template "
2636 "template-parameter", TREE_TYPE (argument
));
2638 error ("invalid default argument for a template template parameter");
2639 return error_mark_node
;
2645 /* Begin a class definition, as indicated by T. */
2648 begin_class_definition (tree t
)
2650 if (error_operand_p (t
) || error_operand_p (TYPE_MAIN_DECL (t
)))
2651 return error_mark_node
;
2653 if (processing_template_parmlist
)
2655 error ("definition of %q#T inside template parameter list", t
);
2656 return error_mark_node
;
2659 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2660 are passed the same as decimal scalar types. */
2661 if (TREE_CODE (t
) == RECORD_TYPE
2662 && !processing_template_decl
)
2664 tree ns
= TYPE_CONTEXT (t
);
2665 if (ns
&& TREE_CODE (ns
) == NAMESPACE_DECL
2666 && DECL_CONTEXT (ns
) == std_node
2668 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns
)), "decimal"))
2670 const char *n
= TYPE_NAME_STRING (t
);
2671 if ((strcmp (n
, "decimal32") == 0)
2672 || (strcmp (n
, "decimal64") == 0)
2673 || (strcmp (n
, "decimal128") == 0))
2674 TYPE_TRANSPARENT_AGGR (t
) = 1;
2678 /* A non-implicit typename comes from code like:
2680 template <typename T> struct A {
2681 template <typename U> struct A<T>::B ...
2683 This is erroneous. */
2684 else if (TREE_CODE (t
) == TYPENAME_TYPE
)
2686 error ("invalid definition of qualified type %qT", t
);
2687 t
= error_mark_node
;
2690 if (t
== error_mark_node
|| ! MAYBE_CLASS_TYPE_P (t
))
2692 t
= make_class_type (RECORD_TYPE
);
2693 pushtag (make_anon_name (), t
, /*tag_scope=*/ts_current
);
2696 if (TYPE_BEING_DEFINED (t
))
2698 t
= make_class_type (TREE_CODE (t
));
2699 pushtag (TYPE_IDENTIFIER (t
), t
, /*tag_scope=*/ts_current
);
2701 maybe_process_partial_specialization (t
);
2703 TYPE_BEING_DEFINED (t
) = 1;
2705 if (flag_pack_struct
)
2708 TYPE_PACKED (t
) = 1;
2709 /* Even though the type is being defined for the first time
2710 here, there might have been a forward declaration, so there
2711 might be cv-qualified variants of T. */
2712 for (v
= TYPE_NEXT_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
2713 TYPE_PACKED (v
) = 1;
2715 /* Reset the interface data, at the earliest possible
2716 moment, as it might have been set via a class foo;
2718 if (! TYPE_ANONYMOUS_P (t
))
2720 struct c_fileinfo
*finfo
= get_fileinfo (input_filename
);
2721 CLASSTYPE_INTERFACE_ONLY (t
) = finfo
->interface_only
;
2722 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2723 (t
, finfo
->interface_unknown
);
2725 reset_specialization();
2727 /* Make a declaration for this class in its own scope. */
2728 build_self_reference ();
2733 /* Finish the member declaration given by DECL. */
2736 finish_member_declaration (tree decl
)
2738 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
2741 if (decl
== void_type_node
)
2742 /* The COMPONENT was a friend, not a member, and so there's
2743 nothing for us to do. */
2746 /* We should see only one DECL at a time. */
2747 gcc_assert (DECL_CHAIN (decl
) == NULL_TREE
);
2749 /* Set up access control for DECL. */
2751 = (current_access_specifier
== access_private_node
);
2752 TREE_PROTECTED (decl
)
2753 = (current_access_specifier
== access_protected_node
);
2754 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2756 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl
)) = TREE_PRIVATE (decl
);
2757 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl
)) = TREE_PROTECTED (decl
);
2760 /* Mark the DECL as a member of the current class, unless it's
2761 a member of an enumeration. */
2762 if (TREE_CODE (decl
) != CONST_DECL
)
2763 DECL_CONTEXT (decl
) = current_class_type
;
2765 /* Check for bare parameter packs in the member variable declaration. */
2766 if (TREE_CODE (decl
) == FIELD_DECL
)
2768 if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
2769 TREE_TYPE (decl
) = error_mark_node
;
2770 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl
)))
2771 DECL_ATTRIBUTES (decl
) = NULL_TREE
;
2776 A C language linkage is ignored for the names of class members
2777 and the member function type of class member functions. */
2778 if (DECL_LANG_SPECIFIC (decl
) && DECL_LANGUAGE (decl
) == lang_c
)
2779 SET_DECL_LANGUAGE (decl
, lang_cplusplus
);
2781 /* Put functions on the TYPE_METHODS list and everything else on the
2782 TYPE_FIELDS list. Note that these are built up in reverse order.
2783 We reverse them (to obtain declaration order) in finish_struct. */
2784 if (DECL_DECLARES_FUNCTION_P (decl
))
2786 /* We also need to add this function to the
2787 CLASSTYPE_METHOD_VEC. */
2788 if (add_method (current_class_type
, decl
, NULL_TREE
))
2790 DECL_CHAIN (decl
) = TYPE_METHODS (current_class_type
);
2791 TYPE_METHODS (current_class_type
) = decl
;
2793 maybe_add_class_template_decl_list (current_class_type
, decl
,
2797 /* Enter the DECL into the scope of the class, if the class
2798 isn't a closure (whose fields are supposed to be unnamed). */
2799 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type
)
2800 || pushdecl_class_level (decl
))
2802 if (TREE_CODE (decl
) == USING_DECL
)
2804 /* For now, ignore class-scope USING_DECLS, so that
2805 debugging backends do not see them. */
2806 DECL_IGNORED_P (decl
) = 1;
2809 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2810 go at the beginning. The reason is that lookup_field_1
2811 searches the list in order, and we want a field name to
2812 override a type name so that the "struct stat hack" will
2813 work. In particular:
2815 struct S { enum E { }; int E } s;
2818 is valid. In addition, the FIELD_DECLs must be maintained in
2819 declaration order so that class layout works as expected.
2820 However, we don't need that order until class layout, so we
2821 save a little time by putting FIELD_DECLs on in reverse order
2822 here, and then reversing them in finish_struct_1. (We could
2823 also keep a pointer to the correct insertion points in the
2826 if (TREE_CODE (decl
) == TYPE_DECL
)
2827 TYPE_FIELDS (current_class_type
)
2828 = chainon (TYPE_FIELDS (current_class_type
), decl
);
2831 DECL_CHAIN (decl
) = TYPE_FIELDS (current_class_type
);
2832 TYPE_FIELDS (current_class_type
) = decl
;
2835 maybe_add_class_template_decl_list (current_class_type
, decl
,
2840 note_decl_for_pch (decl
);
2843 /* DECL has been declared while we are building a PCH file. Perform
2844 actions that we might normally undertake lazily, but which can be
2845 performed now so that they do not have to be performed in
2846 translation units which include the PCH file. */
2849 note_decl_for_pch (tree decl
)
2851 gcc_assert (pch_file
);
2853 /* There's a good chance that we'll have to mangle names at some
2854 point, even if only for emission in debugging information. */
2855 if (VAR_OR_FUNCTION_DECL_P (decl
)
2856 && !processing_template_decl
)
2860 /* Finish processing a complete template declaration. The PARMS are
2861 the template parameters. */
2864 finish_template_decl (tree parms
)
2867 end_template_decl ();
2869 end_specialization ();
2872 /* Finish processing a template-id (which names a type) of the form
2873 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2874 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2875 the scope of template-id indicated. */
2878 finish_template_type (tree name
, tree args
, int entering_scope
)
2882 type
= lookup_template_class (name
, args
,
2883 NULL_TREE
, NULL_TREE
, entering_scope
,
2884 tf_warning_or_error
| tf_user
);
2885 if (type
== error_mark_node
)
2887 else if (CLASS_TYPE_P (type
) && !alias_type_or_template_p (type
))
2888 return TYPE_STUB_DECL (type
);
2890 return TYPE_NAME (type
);
2893 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2894 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2895 BASE_CLASS, or NULL_TREE if an error occurred. The
2896 ACCESS_SPECIFIER is one of
2897 access_{default,public,protected_private}_node. For a virtual base
2898 we set TREE_TYPE. */
2901 finish_base_specifier (tree base
, tree access
, bool virtual_p
)
2905 if (base
== error_mark_node
)
2907 error ("invalid base-class specification");
2910 else if (! MAYBE_CLASS_TYPE_P (base
))
2912 error ("%qT is not a class type", base
);
2917 if (cp_type_quals (base
) != 0)
2919 /* DR 484: Can a base-specifier name a cv-qualified
2921 base
= TYPE_MAIN_VARIANT (base
);
2923 result
= build_tree_list (access
, base
);
2925 TREE_TYPE (result
) = integer_type_node
;
2931 /* If FNS is a member function, a set of member functions, or a
2932 template-id referring to one or more member functions, return a
2933 BASELINK for FNS, incorporating the current access context.
2934 Otherwise, return FNS unchanged. */
2937 baselink_for_fns (tree fns
)
2942 if (BASELINK_P (fns
)
2943 || error_operand_p (fns
))
2946 scope
= ovl_scope (fns
);
2947 if (!CLASS_TYPE_P (scope
))
2950 cl
= currently_open_derived_class (scope
);
2953 cl
= TYPE_BINFO (cl
);
2954 return build_baselink (cl
, cl
, fns
, /*optype=*/NULL_TREE
);
2957 /* Returns true iff DECL is a variable from a function outside
2961 outer_var_p (tree decl
)
2963 return ((VAR_P (decl
) || TREE_CODE (decl
) == PARM_DECL
)
2964 && DECL_FUNCTION_SCOPE_P (decl
)
2965 && (DECL_CONTEXT (decl
) != current_function_decl
2966 || parsing_nsdmi ()));
2969 /* As above, but also checks that DECL is automatic. */
2972 outer_automatic_var_p (tree decl
)
2974 return (outer_var_p (decl
)
2975 && !TREE_STATIC (decl
));
2978 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2979 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2980 if non-NULL, is the type or namespace used to explicitly qualify
2981 ID_EXPRESSION. DECL is the entity to which that name has been
2984 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2985 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2986 be set to true if this expression isn't permitted in a
2987 constant-expression, but it is otherwise not set by this function.
2988 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2989 constant-expression, but a non-constant expression is also
2992 DONE is true if this expression is a complete postfix-expression;
2993 it is false if this expression is followed by '->', '[', '(', etc.
2994 ADDRESS_P is true iff this expression is the operand of '&'.
2995 TEMPLATE_P is true iff the qualified-id was of the form
2996 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2997 appears as a template argument.
2999 If an error occurs, and it is the kind of error that might cause
3000 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3001 is the caller's responsibility to issue the message. *ERROR_MSG
3002 will be a string with static storage duration, so the caller need
3005 Return an expression for the entity, after issuing appropriate
3006 diagnostics. This function is also responsible for transforming a
3007 reference to a non-static member into a COMPONENT_REF that makes
3008 the use of "this" explicit.
3010 Upon return, *IDK will be filled in appropriately. */
3012 finish_id_expression (tree id_expression
,
3016 bool integral_constant_expression_p
,
3017 bool allow_non_integral_constant_expression_p
,
3018 bool *non_integral_constant_expression_p
,
3022 bool template_arg_p
,
3023 const char **error_msg
,
3024 location_t location
)
3026 decl
= strip_using_decl (decl
);
3028 /* Initialize the output parameters. */
3029 *idk
= CP_ID_KIND_NONE
;
3032 if (id_expression
== error_mark_node
)
3033 return error_mark_node
;
3034 /* If we have a template-id, then no further lookup is
3035 required. If the template-id was for a template-class, we
3036 will sometimes have a TYPE_DECL at this point. */
3037 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
3038 || TREE_CODE (decl
) == TYPE_DECL
)
3040 /* Look up the name. */
3043 if (decl
== error_mark_node
)
3045 /* Name lookup failed. */
3048 || (!dependent_type_p (scope
)
3049 && !(identifier_p (id_expression
)
3050 && IDENTIFIER_TYPENAME_P (id_expression
)
3051 && dependent_type_p (TREE_TYPE (id_expression
))))))
3053 /* If the qualifying type is non-dependent (and the name
3054 does not name a conversion operator to a dependent
3055 type), issue an error. */
3056 qualified_name_lookup_error (scope
, id_expression
, decl
, location
);
3057 return error_mark_node
;
3061 /* It may be resolved via Koenig lookup. */
3062 *idk
= CP_ID_KIND_UNQUALIFIED
;
3063 return id_expression
;
3066 decl
= id_expression
;
3068 /* If DECL is a variable that would be out of scope under
3069 ANSI/ISO rules, but in scope in the ARM, name lookup
3070 will succeed. Issue a diagnostic here. */
3072 decl
= check_for_out_of_scope_variable (decl
);
3074 /* Remember that the name was used in the definition of
3075 the current class so that we can check later to see if
3076 the meaning would have been different after the class
3077 was entirely defined. */
3078 if (!scope
&& decl
!= error_mark_node
&& identifier_p (id_expression
))
3079 maybe_note_name_used_in_class (id_expression
, decl
);
3081 /* Disallow uses of local variables from containing functions, except
3082 within lambda-expressions. */
3083 if (!outer_var_p (decl
))
3085 else if (TREE_STATIC (decl
)
3086 /* It's not a use (3.2) if we're in an unevaluated context. */
3087 || cp_unevaluated_operand
)
3089 if (processing_template_decl
)
3090 /* For a use of an outer static/unevaluated var, return the id
3091 so that we'll look it up again in the instantiation. */
3092 return id_expression
;
3096 tree context
= DECL_CONTEXT (decl
);
3097 tree containing_function
= current_function_decl
;
3098 tree lambda_stack
= NULL_TREE
;
3099 tree lambda_expr
= NULL_TREE
;
3100 tree initializer
= convert_from_reference (decl
);
3102 /* Mark it as used now even if the use is ill-formed. */
3105 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3106 support for an approach in which a reference to a local
3107 [constant] automatic variable in a nested class or lambda body
3108 would enter the expression as an rvalue, which would reduce
3109 the complexity of the problem"
3111 FIXME update for final resolution of core issue 696. */
3112 if (decl_constant_var_p (decl
))
3114 if (processing_template_decl
)
3115 /* In a template, the constant value may not be in a usable
3116 form, so look it up again at instantiation time. */
3117 return id_expression
;
3119 return integral_constant_value (decl
);
3122 if (parsing_nsdmi ())
3123 containing_function
= NULL_TREE
;
3124 /* If we are in a lambda function, we can move out until we hit
3126 2. a non-lambda function, or
3127 3. a non-default capturing lambda function. */
3128 else while (context
!= containing_function
3129 && LAMBDA_FUNCTION_P (containing_function
))
3131 lambda_expr
= CLASSTYPE_LAMBDA_EXPR
3132 (DECL_CONTEXT (containing_function
));
3134 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
)
3138 lambda_stack
= tree_cons (NULL_TREE
,
3143 = decl_function_context (containing_function
);
3146 if (lambda_expr
&& TREE_CODE (decl
) == VAR_DECL
3147 && DECL_ANON_UNION_VAR_P (decl
))
3149 error ("cannot capture member %qD of anonymous union", decl
);
3150 return error_mark_node
;
3152 if (context
== containing_function
)
3154 decl
= add_default_capture (lambda_stack
,
3155 /*id=*/DECL_NAME (decl
),
3158 else if (lambda_expr
)
3160 error ("%qD is not captured", decl
);
3161 return error_mark_node
;
3166 ? G_("use of local variable with automatic storage from containing function")
3167 : G_("use of parameter from containing function"));
3168 inform (input_location
, "%q+#D declared here", decl
);
3169 return error_mark_node
;
3173 /* Also disallow uses of function parameters outside the function
3174 body, except inside an unevaluated context (i.e. decltype). */
3175 if (TREE_CODE (decl
) == PARM_DECL
3176 && DECL_CONTEXT (decl
) == NULL_TREE
3177 && !cp_unevaluated_operand
)
3179 error ("use of parameter %qD outside function body", decl
);
3180 return error_mark_node
;
3184 /* If we didn't find anything, or what we found was a type,
3185 then this wasn't really an id-expression. */
3186 if (TREE_CODE (decl
) == TEMPLATE_DECL
3187 && !DECL_FUNCTION_TEMPLATE_P (decl
))
3189 *error_msg
= "missing template arguments";
3190 return error_mark_node
;
3192 else if (TREE_CODE (decl
) == TYPE_DECL
3193 || TREE_CODE (decl
) == NAMESPACE_DECL
)
3195 *error_msg
= "expected primary-expression";
3196 return error_mark_node
;
3199 /* If the name resolved to a template parameter, there is no
3200 need to look it up again later. */
3201 if ((TREE_CODE (decl
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (decl
))
3202 || TREE_CODE (decl
) == TEMPLATE_PARM_INDEX
)
3206 *idk
= CP_ID_KIND_NONE
;
3207 if (TREE_CODE (decl
) == TEMPLATE_PARM_INDEX
)
3208 decl
= TEMPLATE_PARM_DECL (decl
);
3209 r
= convert_from_reference (DECL_INITIAL (decl
));
3211 if (integral_constant_expression_p
3212 && !dependent_type_p (TREE_TYPE (decl
))
3213 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r
))))
3215 if (!allow_non_integral_constant_expression_p
)
3216 error ("template parameter %qD of type %qT is not allowed in "
3217 "an integral constant expression because it is not of "
3218 "integral or enumeration type", decl
, TREE_TYPE (decl
));
3219 *non_integral_constant_expression_p
= true;
3227 /* If the declaration was explicitly qualified indicate
3228 that. The semantics of `A::f(3)' are different than
3229 `f(3)' if `f' is virtual. */
3231 ? CP_ID_KIND_QUALIFIED
3232 : (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
3233 ? CP_ID_KIND_TEMPLATE_ID
3234 : CP_ID_KIND_UNQUALIFIED
));
3239 An id-expression is type-dependent if it contains an
3240 identifier that was declared with a dependent type.
3242 The standard is not very specific about an id-expression that
3243 names a set of overloaded functions. What if some of them
3244 have dependent types and some of them do not? Presumably,
3245 such a name should be treated as a dependent name. */
3246 /* Assume the name is not dependent. */
3247 dependent_p
= false;
3248 if (!processing_template_decl
)
3249 /* No names are dependent outside a template. */
3251 else if (TREE_CODE (decl
) == CONST_DECL
)
3252 /* We don't want to treat enumerators as dependent. */
3254 /* A template-id where the name of the template was not resolved
3255 is definitely dependent. */
3256 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
3257 && (identifier_p (TREE_OPERAND (decl
, 0))))
3259 /* For anything except an overloaded function, just check its
3261 else if (!is_overloaded_fn (decl
))
3263 = dependent_type_p (TREE_TYPE (decl
));
3264 /* For a set of overloaded functions, check each of the
3270 if (BASELINK_P (fns
))
3271 fns
= BASELINK_FUNCTIONS (fns
);
3273 /* For a template-id, check to see if the template
3274 arguments are dependent. */
3275 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
3277 tree args
= TREE_OPERAND (fns
, 1);
3278 dependent_p
= any_dependent_template_arguments_p (args
);
3279 /* The functions are those referred to by the
3281 fns
= TREE_OPERAND (fns
, 0);
3284 /* If there are no dependent template arguments, go through
3285 the overloaded functions. */
3286 while (fns
&& !dependent_p
)
3288 tree fn
= OVL_CURRENT (fns
);
3290 /* Member functions of dependent classes are
3292 if (TREE_CODE (fn
) == FUNCTION_DECL
3293 && type_dependent_expression_p (fn
))
3295 else if (TREE_CODE (fn
) == TEMPLATE_DECL
3296 && dependent_template_p (fn
))
3299 fns
= OVL_NEXT (fns
);
3303 /* If the name was dependent on a template parameter, we will
3304 resolve the name at instantiation time. */
3307 /* Create a SCOPE_REF for qualified names, if the scope is
3313 if (address_p
&& done
)
3314 decl
= finish_qualified_id_expr (scope
, decl
,
3318 tf_warning_or_error
);
3321 tree type
= NULL_TREE
;
3322 if (DECL_P (decl
) && !dependent_scope_p (scope
))
3323 type
= TREE_TYPE (decl
);
3324 decl
= build_qualified_name (type
,
3330 if (TREE_TYPE (decl
))
3331 decl
= convert_from_reference (decl
);
3334 /* A TEMPLATE_ID already contains all the information we
3336 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
)
3337 return id_expression
;
3338 *idk
= CP_ID_KIND_UNQUALIFIED_DEPENDENT
;
3339 /* If we found a variable, then name lookup during the
3340 instantiation will always resolve to the same VAR_DECL
3341 (or an instantiation thereof). */
3343 || TREE_CODE (decl
) == PARM_DECL
)
3346 return convert_from_reference (decl
);
3348 /* The same is true for FIELD_DECL, but we also need to
3349 make sure that the syntax is correct. */
3350 else if (TREE_CODE (decl
) == FIELD_DECL
)
3352 /* Since SCOPE is NULL here, this is an unqualified name.
3353 Access checking has been performed during name lookup
3354 already. Turn off checking to avoid duplicate errors. */
3355 push_deferring_access_checks (dk_no_check
);
3356 decl
= finish_non_static_data_member
3358 /*qualifying_scope=*/NULL_TREE
);
3359 pop_deferring_access_checks ();
3362 return id_expression
;
3365 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3367 error ("use of namespace %qD as expression", decl
);
3368 return error_mark_node
;
3370 else if (DECL_CLASS_TEMPLATE_P (decl
))
3372 error ("use of class template %qT as expression", decl
);
3373 return error_mark_node
;
3375 else if (TREE_CODE (decl
) == TREE_LIST
)
3377 /* Ambiguous reference to base members. */
3378 error ("request for member %qD is ambiguous in "
3379 "multiple inheritance lattice", id_expression
);
3380 print_candidates (decl
);
3381 return error_mark_node
;
3384 /* Mark variable-like entities as used. Functions are similarly
3385 marked either below or after overload resolution. */
3387 || TREE_CODE (decl
) == PARM_DECL
3388 || TREE_CODE (decl
) == CONST_DECL
3389 || TREE_CODE (decl
) == RESULT_DECL
)
3390 && !mark_used (decl
))
3391 return error_mark_node
;
3393 /* Only certain kinds of names are allowed in constant
3394 expression. Template parameters have already
3395 been handled above. */
3396 if (! error_operand_p (decl
)
3397 && integral_constant_expression_p
3398 && ! decl_constant_var_p (decl
)
3399 && TREE_CODE (decl
) != CONST_DECL
3400 && ! builtin_valid_in_constant_expr_p (decl
))
3402 if (!allow_non_integral_constant_expression_p
)
3404 error ("%qD cannot appear in a constant-expression", decl
);
3405 return error_mark_node
;
3407 *non_integral_constant_expression_p
= true;
3412 && !cp_unevaluated_operand
3413 && DECL_THREAD_LOCAL_P (decl
)
3414 && (wrap
= get_tls_wrapper_fn (decl
)))
3416 /* Replace an evaluated use of the thread_local variable with
3417 a call to its wrapper. */
3418 decl
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
3422 decl
= (adjust_result_of_qualified_name_lookup
3423 (decl
, scope
, current_nonlambda_class_type()));
3425 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3429 decl
= finish_qualified_id_expr (scope
,
3435 tf_warning_or_error
);
3437 decl
= convert_from_reference (decl
);
3439 else if (TREE_CODE (decl
) == FIELD_DECL
)
3441 /* Since SCOPE is NULL here, this is an unqualified name.
3442 Access checking has been performed during name lookup
3443 already. Turn off checking to avoid duplicate errors. */
3444 push_deferring_access_checks (dk_no_check
);
3445 decl
= finish_non_static_data_member (decl
, NULL_TREE
,
3446 /*qualifying_scope=*/NULL_TREE
);
3447 pop_deferring_access_checks ();
3449 else if (is_overloaded_fn (decl
))
3453 first_fn
= get_first_fn (decl
);
3454 if (TREE_CODE (first_fn
) == TEMPLATE_DECL
)
3455 first_fn
= DECL_TEMPLATE_RESULT (first_fn
);
3457 if (!really_overloaded_fn (decl
)
3458 && !mark_used (first_fn
))
3459 return error_mark_node
;
3462 && TREE_CODE (first_fn
) == FUNCTION_DECL
3463 && DECL_FUNCTION_MEMBER_P (first_fn
)
3464 && !shared_member_p (decl
))
3466 /* A set of member functions. */
3467 decl
= maybe_dummy_object (DECL_CONTEXT (first_fn
), 0);
3468 return finish_class_member_access_expr (decl
, id_expression
,
3469 /*template_p=*/false,
3470 tf_warning_or_error
);
3473 decl
= baselink_for_fns (decl
);
3477 if (DECL_P (decl
) && DECL_NONLOCAL (decl
)
3478 && DECL_CLASS_SCOPE_P (decl
))
3480 tree context
= context_for_name_lookup (decl
);
3481 if (context
!= current_class_type
)
3483 tree path
= currently_open_derived_class (context
);
3484 perform_or_defer_access_check (TYPE_BINFO (path
),
3486 tf_warning_or_error
);
3490 decl
= convert_from_reference (decl
);
3494 /* Handle references (c++/56130). */
3495 tree t
= REFERENCE_REF_P (decl
) ? TREE_OPERAND (decl
, 0) : decl
;
3496 if (TREE_DEPRECATED (t
))
3497 warn_deprecated_use (t
, NULL_TREE
);
3502 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3503 use as a type-specifier. */
3506 finish_typeof (tree expr
)
3510 if (type_dependent_expression_p (expr
))
3512 type
= cxx_make_type (TYPEOF_TYPE
);
3513 TYPEOF_TYPE_EXPR (type
) = expr
;
3514 SET_TYPE_STRUCTURAL_EQUALITY (type
);
3519 expr
= mark_type_use (expr
);
3521 type
= unlowered_expr_type (expr
);
3523 if (!type
|| type
== unknown_type_node
)
3525 error ("type of %qE is unknown", expr
);
3526 return error_mark_node
;
3532 /* Implement the __underlying_type keyword: Return the underlying
3533 type of TYPE, suitable for use as a type-specifier. */
3536 finish_underlying_type (tree type
)
3538 tree underlying_type
;
3540 if (processing_template_decl
)
3542 underlying_type
= cxx_make_type (UNDERLYING_TYPE
);
3543 UNDERLYING_TYPE_TYPE (underlying_type
) = type
;
3544 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type
);
3546 return underlying_type
;
3549 complete_type (type
);
3551 if (TREE_CODE (type
) != ENUMERAL_TYPE
)
3553 error ("%qT is not an enumeration type", type
);
3554 return error_mark_node
;
3557 underlying_type
= ENUM_UNDERLYING_TYPE (type
);
3559 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3560 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3561 See finish_enum_value_list for details. */
3562 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type
))
3564 = c_common_type_for_mode (TYPE_MODE (underlying_type
),
3565 TYPE_UNSIGNED (underlying_type
));
3567 return underlying_type
;
3570 /* Implement the __direct_bases keyword: Return the direct base classes
3574 calculate_direct_bases (tree type
)
3576 vec
<tree
, va_gc
> *vector
= make_tree_vector();
3577 tree bases_vec
= NULL_TREE
;
3578 vec
<tree
, va_gc
> *base_binfos
;
3582 complete_type (type
);
3584 if (!NON_UNION_CLASS_TYPE_P (type
))
3585 return make_tree_vec (0);
3587 base_binfos
= BINFO_BASE_BINFOS (TYPE_BINFO (type
));
3589 /* Virtual bases are initialized first */
3590 for (i
= 0; base_binfos
->iterate (i
, &binfo
); i
++)
3592 if (BINFO_VIRTUAL_P (binfo
))
3594 vec_safe_push (vector
, binfo
);
3598 /* Now non-virtuals */
3599 for (i
= 0; base_binfos
->iterate (i
, &binfo
); i
++)
3601 if (!BINFO_VIRTUAL_P (binfo
))
3603 vec_safe_push (vector
, binfo
);
3608 bases_vec
= make_tree_vec (vector
->length ());
3610 for (i
= 0; i
< vector
->length (); ++i
)
3612 TREE_VEC_ELT (bases_vec
, i
) = BINFO_TYPE ((*vector
)[i
]);
3617 /* Implement the __bases keyword: Return the base classes
3620 /* Find morally non-virtual base classes by walking binfo hierarchy */
3621 /* Virtual base classes are handled separately in finish_bases */
3624 dfs_calculate_bases_pre (tree binfo
, void * /*data_*/)
3626 /* Don't walk bases of virtual bases */
3627 return BINFO_VIRTUAL_P (binfo
) ? dfs_skip_bases
: NULL_TREE
;
3631 dfs_calculate_bases_post (tree binfo
, void *data_
)
3633 vec
<tree
, va_gc
> **data
= ((vec
<tree
, va_gc
> **) data_
);
3634 if (!BINFO_VIRTUAL_P (binfo
))
3636 vec_safe_push (*data
, BINFO_TYPE (binfo
));
3641 /* Calculates the morally non-virtual base classes of a class */
3642 static vec
<tree
, va_gc
> *
3643 calculate_bases_helper (tree type
)
3645 vec
<tree
, va_gc
> *vector
= make_tree_vector();
3647 /* Now add non-virtual base classes in order of construction */
3648 dfs_walk_all (TYPE_BINFO (type
),
3649 dfs_calculate_bases_pre
, dfs_calculate_bases_post
, &vector
);
3654 calculate_bases (tree type
)
3656 vec
<tree
, va_gc
> *vector
= make_tree_vector();
3657 tree bases_vec
= NULL_TREE
;
3659 vec
<tree
, va_gc
> *vbases
;
3660 vec
<tree
, va_gc
> *nonvbases
;
3663 complete_type (type
);
3665 if (!NON_UNION_CLASS_TYPE_P (type
))
3666 return make_tree_vec (0);
3668 /* First go through virtual base classes */
3669 for (vbases
= CLASSTYPE_VBASECLASSES (type
), i
= 0;
3670 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
3672 vec
<tree
, va_gc
> *vbase_bases
;
3673 vbase_bases
= calculate_bases_helper (BINFO_TYPE (binfo
));
3674 vec_safe_splice (vector
, vbase_bases
);
3675 release_tree_vector (vbase_bases
);
3678 /* Now for the non-virtual bases */
3679 nonvbases
= calculate_bases_helper (type
);
3680 vec_safe_splice (vector
, nonvbases
);
3681 release_tree_vector (nonvbases
);
3683 /* Last element is entire class, so don't copy */
3684 bases_vec
= make_tree_vec (vector
->length () - 1);
3686 for (i
= 0; i
< vector
->length () - 1; ++i
)
3688 TREE_VEC_ELT (bases_vec
, i
) = (*vector
)[i
];
3690 release_tree_vector (vector
);
3695 finish_bases (tree type
, bool direct
)
3697 tree bases
= NULL_TREE
;
3699 if (!processing_template_decl
)
3701 /* Parameter packs can only be used in templates */
3702 error ("Parameter pack __bases only valid in template declaration");
3703 return error_mark_node
;
3706 bases
= cxx_make_type (BASES
);
3707 BASES_TYPE (bases
) = type
;
3708 BASES_DIRECT (bases
) = direct
;
3709 SET_TYPE_STRUCTURAL_EQUALITY (bases
);
3714 /* Perform C++-specific checks for __builtin_offsetof before calling
3718 finish_offsetof (tree expr
)
3720 if (TREE_CODE (expr
) == PSEUDO_DTOR_EXPR
)
3722 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3723 TREE_OPERAND (expr
, 2));
3724 return error_mark_node
;
3726 if (TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
3727 || TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
3728 || TREE_TYPE (expr
) == unknown_type_node
)
3730 if (INDIRECT_REF_P (expr
))
3731 error ("second operand of %<offsetof%> is neither a single "
3732 "identifier nor a sequence of member accesses and "
3733 "array references");
3736 if (TREE_CODE (expr
) == COMPONENT_REF
3737 || TREE_CODE (expr
) == COMPOUND_EXPR
)
3738 expr
= TREE_OPERAND (expr
, 1);
3739 error ("cannot apply %<offsetof%> to member function %qD", expr
);
3741 return error_mark_node
;
3743 if (REFERENCE_REF_P (expr
))
3744 expr
= TREE_OPERAND (expr
, 0);
3745 if (TREE_CODE (expr
) == COMPONENT_REF
)
3747 tree object
= TREE_OPERAND (expr
, 0);
3748 if (!complete_type_or_else (TREE_TYPE (object
), object
))
3749 return error_mark_node
;
3751 return fold_offsetof (expr
);
3754 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3755 function is broken out from the above for the benefit of the tree-ssa
3759 simplify_aggr_init_expr (tree
*tp
)
3761 tree aggr_init_expr
= *tp
;
3763 /* Form an appropriate CALL_EXPR. */
3764 tree fn
= AGGR_INIT_EXPR_FN (aggr_init_expr
);
3765 tree slot
= AGGR_INIT_EXPR_SLOT (aggr_init_expr
);
3766 tree type
= TREE_TYPE (slot
);
3769 enum style_t
{ ctor
, arg
, pcc
} style
;
3771 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr
))
3773 #ifdef PCC_STATIC_STRUCT_RETURN
3779 gcc_assert (TREE_ADDRESSABLE (type
));
3783 call_expr
= build_call_array_loc (input_location
,
3784 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))),
3786 aggr_init_expr_nargs (aggr_init_expr
),
3787 AGGR_INIT_EXPR_ARGP (aggr_init_expr
));
3788 TREE_NOTHROW (call_expr
) = TREE_NOTHROW (aggr_init_expr
);
3792 /* Replace the first argument to the ctor with the address of the
3794 cxx_mark_addressable (slot
);
3795 CALL_EXPR_ARG (call_expr
, 0) =
3796 build1 (ADDR_EXPR
, build_pointer_type (type
), slot
);
3798 else if (style
== arg
)
3800 /* Just mark it addressable here, and leave the rest to
3801 expand_call{,_inline}. */
3802 cxx_mark_addressable (slot
);
3803 CALL_EXPR_RETURN_SLOT_OPT (call_expr
) = true;
3804 call_expr
= build2 (INIT_EXPR
, TREE_TYPE (call_expr
), slot
, call_expr
);
3806 else if (style
== pcc
)
3808 /* If we're using the non-reentrant PCC calling convention, then we
3809 need to copy the returned value out of the static buffer into the
3811 push_deferring_access_checks (dk_no_check
);
3812 call_expr
= build_aggr_init (slot
, call_expr
,
3813 DIRECT_BIND
| LOOKUP_ONLYCONVERTING
,
3814 tf_warning_or_error
);
3815 pop_deferring_access_checks ();
3816 call_expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (slot
), call_expr
, slot
);
3819 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr
))
3821 tree init
= build_zero_init (type
, NULL_TREE
,
3822 /*static_storage_p=*/false);
3823 init
= build2 (INIT_EXPR
, void_type_node
, slot
, init
);
3824 call_expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (call_expr
),
3831 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3834 emit_associated_thunks (tree fn
)
3836 /* When we use vcall offsets, we emit thunks with the virtual
3837 functions to which they thunk. The whole point of vcall offsets
3838 is so that you can know statically the entire set of thunks that
3839 will ever be needed for a given virtual function, thereby
3840 enabling you to output all the thunks with the function itself. */
3841 if (DECL_VIRTUAL_P (fn
)
3842 /* Do not emit thunks for extern template instantiations. */
3843 && ! DECL_REALLY_EXTERN (fn
))
3847 for (thunk
= DECL_THUNKS (fn
); thunk
; thunk
= DECL_CHAIN (thunk
))
3849 if (!THUNK_ALIAS (thunk
))
3851 use_thunk (thunk
, /*emit_p=*/1);
3852 if (DECL_RESULT_THUNK_P (thunk
))
3856 for (probe
= DECL_THUNKS (thunk
);
3857 probe
; probe
= DECL_CHAIN (probe
))
3858 use_thunk (probe
, /*emit_p=*/1);
3862 gcc_assert (!DECL_THUNKS (thunk
));
3867 /* Returns true iff FUN is an instantiation of a constexpr function
3871 is_instantiation_of_constexpr (tree fun
)
3873 return (DECL_TEMPLOID_INSTANTIATION (fun
)
3874 && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT
3875 (DECL_TI_TEMPLATE (fun
))));
3878 /* Generate RTL for FN. */
3881 expand_or_defer_fn_1 (tree fn
)
3883 /* When the parser calls us after finishing the body of a template
3884 function, we don't really want to expand the body. */
3885 if (processing_template_decl
)
3887 /* Normally, collection only occurs in rest_of_compilation. So,
3888 if we don't collect here, we never collect junk generated
3889 during the processing of templates until we hit a
3890 non-template function. It's not safe to do this inside a
3891 nested class, though, as the parser may have local state that
3892 is not a GC root. */
3893 if (!function_depth
)
3898 gcc_assert (DECL_SAVED_TREE (fn
));
3900 /* If this is a constructor or destructor body, we have to clone
3902 if (maybe_clone_body (fn
))
3904 /* We don't want to process FN again, so pretend we've written
3905 it out, even though we haven't. */
3906 TREE_ASM_WRITTEN (fn
) = 1;
3907 /* If this is an instantiation of a constexpr function, keep
3908 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
3909 if (!is_instantiation_of_constexpr (fn
))
3910 DECL_SAVED_TREE (fn
) = NULL_TREE
;
3914 /* We make a decision about linkage for these functions at the end
3915 of the compilation. Until that point, we do not want the back
3916 end to output them -- but we do want it to see the bodies of
3917 these functions so that it can inline them as appropriate. */
3918 if (DECL_DECLARED_INLINE_P (fn
) || DECL_IMPLICIT_INSTANTIATION (fn
))
3920 if (DECL_INTERFACE_KNOWN (fn
))
3921 /* We've already made a decision as to how this function will
3925 DECL_EXTERNAL (fn
) = 1;
3926 DECL_NOT_REALLY_EXTERN (fn
) = 1;
3927 note_vague_linkage_fn (fn
);
3928 /* A non-template inline function with external linkage will
3929 always be COMDAT. As we must eventually determine the
3930 linkage of all functions, and as that causes writes to
3931 the data mapped in from the PCH file, it's advantageous
3932 to mark the functions at this point. */
3933 if (!DECL_IMPLICIT_INSTANTIATION (fn
))
3935 /* This function must have external linkage, as
3936 otherwise DECL_INTERFACE_KNOWN would have been
3938 gcc_assert (TREE_PUBLIC (fn
));
3939 comdat_linkage (fn
);
3940 DECL_INTERFACE_KNOWN (fn
) = 1;
3944 import_export_decl (fn
);
3946 /* If the user wants us to keep all inline functions, then mark
3947 this function as needed so that finish_file will make sure to
3948 output it later. Similarly, all dllexport'd functions must
3949 be emitted; there may be callers in other DLLs. */
3950 if ((flag_keep_inline_functions
3951 && DECL_DECLARED_INLINE_P (fn
)
3952 && !DECL_REALLY_EXTERN (fn
))
3953 || (flag_keep_inline_dllexport
3954 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn
))))
3957 DECL_EXTERNAL (fn
) = 0;
3961 /* There's no reason to do any of the work here if we're only doing
3962 semantic analysis; this code just generates RTL. */
3963 if (flag_syntax_only
)
3970 expand_or_defer_fn (tree fn
)
3972 if (expand_or_defer_fn_1 (fn
))
3976 /* Expand or defer, at the whim of the compilation unit manager. */
3977 cgraph_finalize_function (fn
, function_depth
> 1);
3978 emit_associated_thunks (fn
);
3988 hash_table
<pointer_hash
<tree_node
> > visited
;
3991 /* Helper function for walk_tree, used by finalize_nrv below. */
3994 finalize_nrv_r (tree
* tp
, int* walk_subtrees
, void* data
)
3996 struct nrv_data
*dp
= (struct nrv_data
*)data
;
3999 /* No need to walk into types. There wouldn't be any need to walk into
4000 non-statements, except that we have to consider STMT_EXPRs. */
4003 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4004 but differs from using NULL_TREE in that it indicates that we care
4005 about the value of the RESULT_DECL. */
4006 else if (TREE_CODE (*tp
) == RETURN_EXPR
)
4007 TREE_OPERAND (*tp
, 0) = dp
->result
;
4008 /* Change all cleanups for the NRV to only run when an exception is
4010 else if (TREE_CODE (*tp
) == CLEANUP_STMT
4011 && CLEANUP_DECL (*tp
) == dp
->var
)
4012 CLEANUP_EH_ONLY (*tp
) = 1;
4013 /* Replace the DECL_EXPR for the NRV with an initialization of the
4014 RESULT_DECL, if needed. */
4015 else if (TREE_CODE (*tp
) == DECL_EXPR
4016 && DECL_EXPR_DECL (*tp
) == dp
->var
)
4019 if (DECL_INITIAL (dp
->var
)
4020 && DECL_INITIAL (dp
->var
) != error_mark_node
)
4021 init
= build2 (INIT_EXPR
, void_type_node
, dp
->result
,
4022 DECL_INITIAL (dp
->var
));
4024 init
= build_empty_stmt (EXPR_LOCATION (*tp
));
4025 DECL_INITIAL (dp
->var
) = NULL_TREE
;
4026 SET_EXPR_LOCATION (init
, EXPR_LOCATION (*tp
));
4029 /* And replace all uses of the NRV with the RESULT_DECL. */
4030 else if (*tp
== dp
->var
)
4033 /* Avoid walking into the same tree more than once. Unfortunately, we
4034 can't just use walk_tree_without duplicates because it would only call
4035 us for the first occurrence of dp->var in the function body. */
4036 slot
= dp
->visited
.find_slot (*tp
, INSERT
);
4042 /* Keep iterating. */
4046 /* Called from finish_function to implement the named return value
4047 optimization by overriding all the RETURN_EXPRs and pertinent
4048 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4049 RESULT_DECL for the function. */
4052 finalize_nrv (tree
*tp
, tree var
, tree result
)
4054 struct nrv_data data
;
4056 /* Copy name from VAR to RESULT. */
4057 DECL_NAME (result
) = DECL_NAME (var
);
4058 /* Don't forget that we take its address. */
4059 TREE_ADDRESSABLE (result
) = TREE_ADDRESSABLE (var
);
4060 /* Finally set DECL_VALUE_EXPR to avoid assigning
4061 a stack slot at -O0 for the original var and debug info
4062 uses RESULT location for VAR. */
4063 SET_DECL_VALUE_EXPR (var
, result
);
4064 DECL_HAS_VALUE_EXPR_P (var
) = 1;
4067 data
.result
= result
;
4068 data
.visited
.create (37);
4069 cp_walk_tree (tp
, finalize_nrv_r
, &data
, 0);
4070 data
.visited
.dispose ();
4073 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4076 cxx_omp_create_clause_info (tree c
, tree type
, bool need_default_ctor
,
4077 bool need_copy_ctor
, bool need_copy_assignment
,
4080 int save_errorcount
= errorcount
;
4083 /* Always allocate 3 elements for simplicity. These are the
4084 function decls for the ctor, dtor, and assignment op.
4085 This layout is known to the three lang hooks,
4086 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4087 and cxx_omp_clause_assign_op. */
4088 info
= make_tree_vec (3);
4089 CP_OMP_CLAUSE_INFO (c
) = info
;
4091 if (need_default_ctor
|| need_copy_ctor
)
4093 if (need_default_ctor
)
4094 t
= get_default_ctor (type
);
4096 t
= get_copy_ctor (type
, tf_warning_or_error
);
4098 if (t
&& !trivial_fn_p (t
))
4099 TREE_VEC_ELT (info
, 0) = t
;
4102 if (need_dtor
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
4103 TREE_VEC_ELT (info
, 1) = get_dtor (type
, tf_warning_or_error
);
4105 if (need_copy_assignment
)
4107 t
= get_copy_assign (type
);
4109 if (t
&& !trivial_fn_p (t
))
4110 TREE_VEC_ELT (info
, 2) = t
;
4113 return errorcount
!= save_errorcount
;
4116 /* Helper function for handle_omp_array_sections. Called recursively
4117 to handle multiple array-section-subscripts. C is the clause,
4118 T current expression (initially OMP_CLAUSE_DECL), which is either
4119 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4120 expression if specified, TREE_VALUE length expression if specified,
4121 TREE_CHAIN is what it has been specified after, or some decl.
4122 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4123 set to true if any of the array-section-subscript could have length
4124 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4125 first array-section-subscript which is known not to have length
4127 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4128 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4129 all are or may have length of 1, array-section-subscript [:2] is the
4130 first one knonwn not to have length 1. For array-section-subscript
4131 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4132 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4133 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4134 case though, as some lengths could be zero. */
4137 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
4138 bool &maybe_zero_len
, unsigned int &first_non_one
)
4140 tree ret
, low_bound
, length
, type
;
4141 if (TREE_CODE (t
) != TREE_LIST
)
4143 if (error_operand_p (t
))
4144 return error_mark_node
;
4145 if (type_dependent_expression_p (t
))
4147 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
4149 if (processing_template_decl
)
4152 error_at (OMP_CLAUSE_LOCATION (c
),
4153 "%qD is not a variable in %qs clause", t
,
4154 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4156 error_at (OMP_CLAUSE_LOCATION (c
),
4157 "%qE is not a variable in %qs clause", t
,
4158 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4159 return error_mark_node
;
4161 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
4162 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
4164 error_at (OMP_CLAUSE_LOCATION (c
),
4165 "%qD is threadprivate variable in %qs clause", t
,
4166 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4167 return error_mark_node
;
4169 t
= convert_from_reference (t
);
4173 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
4174 maybe_zero_len
, first_non_one
);
4175 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
4178 type
= TREE_TYPE (ret
);
4179 low_bound
= TREE_PURPOSE (t
);
4180 length
= TREE_VALUE (t
);
4181 if ((low_bound
&& type_dependent_expression_p (low_bound
))
4182 || (length
&& type_dependent_expression_p (length
)))
4185 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
4186 return error_mark_node
;
4188 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
4190 error_at (OMP_CLAUSE_LOCATION (c
),
4191 "low bound %qE of array section does not have integral type",
4193 return error_mark_node
;
4195 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
4197 error_at (OMP_CLAUSE_LOCATION (c
),
4198 "length %qE of array section does not have integral type",
4200 return error_mark_node
;
4203 && TREE_CODE (low_bound
) == INTEGER_CST
4204 && TYPE_PRECISION (TREE_TYPE (low_bound
))
4205 > TYPE_PRECISION (sizetype
))
4206 low_bound
= fold_convert (sizetype
, low_bound
);
4208 && TREE_CODE (length
) == INTEGER_CST
4209 && TYPE_PRECISION (TREE_TYPE (length
))
4210 > TYPE_PRECISION (sizetype
))
4211 length
= fold_convert (sizetype
, length
);
4212 if (low_bound
== NULL_TREE
)
4213 low_bound
= integer_zero_node
;
4215 if (length
!= NULL_TREE
)
4217 if (!integer_nonzerop (length
))
4218 maybe_zero_len
= true;
4219 if (first_non_one
== types
.length ()
4220 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
4223 if (TREE_CODE (type
) == ARRAY_TYPE
)
4225 if (length
== NULL_TREE
4226 && (TYPE_DOMAIN (type
) == NULL_TREE
4227 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
4229 error_at (OMP_CLAUSE_LOCATION (c
),
4230 "for unknown bound array type length expression must "
4232 return error_mark_node
;
4234 if (TREE_CODE (low_bound
) == INTEGER_CST
4235 && tree_int_cst_sgn (low_bound
) == -1)
4237 error_at (OMP_CLAUSE_LOCATION (c
),
4238 "negative low bound in array section in %qs clause",
4239 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4240 return error_mark_node
;
4242 if (length
!= NULL_TREE
4243 && TREE_CODE (length
) == INTEGER_CST
4244 && tree_int_cst_sgn (length
) == -1)
4246 error_at (OMP_CLAUSE_LOCATION (c
),
4247 "negative length in array section in %qs clause",
4248 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4249 return error_mark_node
;
4251 if (TYPE_DOMAIN (type
)
4252 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
4253 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
4256 tree size
= size_binop (PLUS_EXPR
,
4257 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
4259 if (TREE_CODE (low_bound
) == INTEGER_CST
)
4261 if (tree_int_cst_lt (size
, low_bound
))
4263 error_at (OMP_CLAUSE_LOCATION (c
),
4264 "low bound %qE above array section size "
4265 "in %qs clause", low_bound
,
4266 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4267 return error_mark_node
;
4269 if (tree_int_cst_equal (size
, low_bound
))
4270 maybe_zero_len
= true;
4271 else if (length
== NULL_TREE
4272 && first_non_one
== types
.length ()
4273 && tree_int_cst_equal
4274 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
4278 else if (length
== NULL_TREE
)
4280 maybe_zero_len
= true;
4281 if (first_non_one
== types
.length ())
4284 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
4286 if (tree_int_cst_lt (size
, length
))
4288 error_at (OMP_CLAUSE_LOCATION (c
),
4289 "length %qE above array section size "
4290 "in %qs clause", length
,
4291 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4292 return error_mark_node
;
4294 if (TREE_CODE (low_bound
) == INTEGER_CST
)
4297 = size_binop (PLUS_EXPR
,
4298 fold_convert (sizetype
, low_bound
),
4299 fold_convert (sizetype
, length
));
4300 if (TREE_CODE (lbpluslen
) == INTEGER_CST
4301 && tree_int_cst_lt (size
, lbpluslen
))
4303 error_at (OMP_CLAUSE_LOCATION (c
),
4304 "high bound %qE above array section size "
4305 "in %qs clause", lbpluslen
,
4306 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4307 return error_mark_node
;
4312 else if (length
== NULL_TREE
)
4314 maybe_zero_len
= true;
4315 if (first_non_one
== types
.length ())
4319 /* For [lb:] we will need to evaluate lb more than once. */
4320 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
4322 tree lb
= cp_save_expr (low_bound
);
4323 if (lb
!= low_bound
)
4325 TREE_PURPOSE (t
) = lb
;
4330 else if (TREE_CODE (type
) == POINTER_TYPE
)
4332 if (length
== NULL_TREE
)
4334 error_at (OMP_CLAUSE_LOCATION (c
),
4335 "for pointer type length expression must be specified");
4336 return error_mark_node
;
4338 /* If there is a pointer type anywhere but in the very first
4339 array-section-subscript, the array section can't be contiguous. */
4340 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
4341 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
4343 error_at (OMP_CLAUSE_LOCATION (c
),
4344 "array section is not contiguous in %qs clause",
4345 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4346 return error_mark_node
;
4351 error_at (OMP_CLAUSE_LOCATION (c
),
4352 "%qE does not have pointer or array type", ret
);
4353 return error_mark_node
;
4355 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
4356 types
.safe_push (TREE_TYPE (ret
));
4357 /* We will need to evaluate lb more than once. */
4358 tree lb
= cp_save_expr (low_bound
);
4359 if (lb
!= low_bound
)
4361 TREE_PURPOSE (t
) = lb
;
4364 ret
= grok_array_decl (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
, false);
4368 /* Handle array sections for clause C. */
4371 handle_omp_array_sections (tree c
)
4373 bool maybe_zero_len
= false;
4374 unsigned int first_non_one
= 0;
4375 vec
<tree
> types
= vNULL
;
4376 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
4377 maybe_zero_len
, first_non_one
);
4378 if (first
== error_mark_node
)
4383 if (first
== NULL_TREE
)
4388 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
4390 tree t
= OMP_CLAUSE_DECL (c
);
4391 tree tem
= NULL_TREE
;
4393 if (processing_template_decl
)
4395 /* Need to evaluate side effects in the length expressions
4397 while (TREE_CODE (t
) == TREE_LIST
)
4399 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
4401 if (tem
== NULL_TREE
)
4402 tem
= TREE_VALUE (t
);
4404 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
4405 TREE_VALUE (t
), tem
);
4410 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
4411 OMP_CLAUSE_DECL (c
) = first
;
4415 unsigned int num
= types
.length (), i
;
4416 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
4417 tree condition
= NULL_TREE
;
4419 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
4420 maybe_zero_len
= true;
4421 if (processing_template_decl
&& maybe_zero_len
)
4427 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
4430 tree low_bound
= TREE_PURPOSE (t
);
4431 tree length
= TREE_VALUE (t
);
4435 && TREE_CODE (low_bound
) == INTEGER_CST
4436 && TYPE_PRECISION (TREE_TYPE (low_bound
))
4437 > TYPE_PRECISION (sizetype
))
4438 low_bound
= fold_convert (sizetype
, low_bound
);
4440 && TREE_CODE (length
) == INTEGER_CST
4441 && TYPE_PRECISION (TREE_TYPE (length
))
4442 > TYPE_PRECISION (sizetype
))
4443 length
= fold_convert (sizetype
, length
);
4444 if (low_bound
== NULL_TREE
)
4445 low_bound
= integer_zero_node
;
4446 if (!maybe_zero_len
&& i
> first_non_one
)
4448 if (integer_nonzerop (low_bound
))
4449 goto do_warn_noncontiguous
;
4450 if (length
!= NULL_TREE
4451 && TREE_CODE (length
) == INTEGER_CST
4452 && TYPE_DOMAIN (types
[i
])
4453 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
4454 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
4458 size
= size_binop (PLUS_EXPR
,
4459 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
4461 if (!tree_int_cst_equal (length
, size
))
4463 do_warn_noncontiguous
:
4464 error_at (OMP_CLAUSE_LOCATION (c
),
4465 "array section is not contiguous in %qs "
4467 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
4472 if (!processing_template_decl
4473 && length
!= NULL_TREE
4474 && TREE_SIDE_EFFECTS (length
))
4476 if (side_effects
== NULL_TREE
)
4477 side_effects
= length
;
4479 side_effects
= build2 (COMPOUND_EXPR
,
4480 TREE_TYPE (side_effects
),
4481 length
, side_effects
);
4484 else if (processing_template_decl
)
4490 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
4493 l
= fold_convert (sizetype
, length
);
4496 l
= size_binop (PLUS_EXPR
,
4497 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
4499 l
= size_binop (MINUS_EXPR
, l
,
4500 fold_convert (sizetype
, low_bound
));
4502 if (i
> first_non_one
)
4504 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
4506 if (condition
== NULL_TREE
)
4509 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
4512 else if (size
== NULL_TREE
)
4514 size
= size_in_bytes (TREE_TYPE (types
[i
]));
4515 size
= size_binop (MULT_EXPR
, size
, l
);
4517 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
4518 size
, size_zero_node
);
4521 size
= size_binop (MULT_EXPR
, size
, l
);
4525 if (!processing_template_decl
)
4528 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
4529 OMP_CLAUSE_DECL (c
) = first
;
4530 OMP_CLAUSE_SIZE (c
) = size
;
4531 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
4533 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
4535 OMP_CLAUSE_MAP_KIND (c2
) = OMP_CLAUSE_MAP_POINTER
;
4536 if (!cxx_mark_addressable (t
))
4538 OMP_CLAUSE_DECL (c2
) = t
;
4539 t
= build_fold_addr_expr (first
);
4540 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
4541 ptrdiff_type_node
, t
);
4542 tree ptr
= OMP_CLAUSE_DECL (c2
);
4543 ptr
= convert_from_reference (ptr
);
4544 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
4545 ptr
= build_fold_addr_expr (ptr
);
4546 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
4547 ptrdiff_type_node
, t
,
4548 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
4549 ptrdiff_type_node
, ptr
));
4550 OMP_CLAUSE_SIZE (c2
) = t
;
4551 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
4552 OMP_CLAUSE_CHAIN (c
) = c2
;
4553 ptr
= OMP_CLAUSE_DECL (c2
);
4554 if (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
4555 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr
))))
4557 tree c3
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
4559 OMP_CLAUSE_MAP_KIND (c3
) = OMP_CLAUSE_MAP_POINTER
;
4560 OMP_CLAUSE_DECL (c3
) = ptr
;
4561 OMP_CLAUSE_DECL (c2
) = convert_from_reference (ptr
);
4562 OMP_CLAUSE_SIZE (c3
) = size_zero_node
;
4563 OMP_CLAUSE_CHAIN (c3
) = OMP_CLAUSE_CHAIN (c2
);
4564 OMP_CLAUSE_CHAIN (c2
) = c3
;
4571 /* Return identifier to look up for omp declare reduction. */
4574 omp_reduction_id (enum tree_code reduction_code
, tree reduction_id
, tree type
)
4576 const char *p
= NULL
;
4577 const char *m
= NULL
;
4578 switch (reduction_code
)
4586 case TRUTH_ANDIF_EXPR
:
4587 case TRUTH_ORIF_EXPR
:
4588 reduction_id
= ansi_opname (reduction_code
);
4602 if (TREE_CODE (reduction_id
) != IDENTIFIER_NODE
)
4603 return error_mark_node
;
4604 p
= IDENTIFIER_POINTER (reduction_id
);
4607 if (type
!= NULL_TREE
)
4608 m
= mangle_type_string (TYPE_MAIN_VARIANT (type
));
4610 const char prefix
[] = "omp declare reduction ";
4611 size_t lenp
= sizeof (prefix
);
4612 if (strncmp (p
, prefix
, lenp
- 1) == 0)
4614 size_t len
= strlen (p
);
4615 size_t lenm
= m
? strlen (m
) + 1 : 0;
4616 char *name
= XALLOCAVEC (char, lenp
+ len
+ lenm
);
4618 memcpy (name
, prefix
, lenp
- 1);
4619 memcpy (name
+ lenp
- 1, p
, len
+ 1);
4622 name
[lenp
+ len
- 1] = '~';
4623 memcpy (name
+ lenp
+ len
, m
, lenm
);
4625 return get_identifier (name
);
4628 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4629 FUNCTION_DECL or NULL_TREE if not found. */
4632 omp_reduction_lookup (location_t loc
, tree id
, tree type
, tree
*baselinkp
,
4633 vec
<tree
> *ambiguousp
)
4636 tree baselink
= NULL_TREE
;
4637 if (identifier_p (id
))
4640 bool nonint_cst_expression_p
;
4641 const char *error_msg
;
4642 id
= omp_reduction_id (ERROR_MARK
, id
, type
);
4643 tree decl
= lookup_name (id
);
4644 if (decl
== NULL_TREE
)
4645 decl
= error_mark_node
;
4646 id
= finish_id_expression (id
, decl
, NULL_TREE
, &idk
, false, true,
4647 &nonint_cst_expression_p
, false, true, false,
4648 false, &error_msg
, loc
);
4649 if (idk
== CP_ID_KIND_UNQUALIFIED
4650 && identifier_p (id
))
4652 vec
<tree
, va_gc
> *args
= NULL
;
4653 vec_safe_push (args
, build_reference_type (type
));
4654 id
= perform_koenig_lookup (id
, args
, false, tf_none
);
4657 else if (TREE_CODE (id
) == SCOPE_REF
)
4658 id
= lookup_qualified_name (TREE_OPERAND (id
, 0),
4659 omp_reduction_id (ERROR_MARK
,
4660 TREE_OPERAND (id
, 1),
4664 if (id
&& is_overloaded_fn (id
))
4666 for (; id
; id
= OVL_NEXT (id
))
4668 tree fndecl
= OVL_CURRENT (id
);
4669 if (TREE_CODE (fndecl
) == FUNCTION_DECL
)
4671 tree argtype
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
4672 if (same_type_p (TREE_TYPE (argtype
), type
))
4676 if (id
&& BASELINK_P (fns
))
4683 if (id
== NULL_TREE
&& CLASS_TYPE_P (type
) && TYPE_BINFO (type
))
4685 vec
<tree
> ambiguous
= vNULL
;
4686 tree binfo
= TYPE_BINFO (type
), base_binfo
, ret
= NULL_TREE
;
4688 if (ambiguousp
== NULL
)
4689 ambiguousp
= &ambiguous
;
4690 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
4692 id
= omp_reduction_lookup (loc
, orig_id
, BINFO_TYPE (base_binfo
),
4693 baselinkp
? baselinkp
: &baselink
,
4695 if (id
== NULL_TREE
)
4697 if (!ambiguousp
->is_empty ())
4698 ambiguousp
->safe_push (id
);
4699 else if (ret
!= NULL_TREE
)
4701 ambiguousp
->safe_push (ret
);
4702 ambiguousp
->safe_push (id
);
4708 if (ambiguousp
!= &ambiguous
)
4710 if (!ambiguous
.is_empty ())
4712 const char *str
= _("candidates are:");
4715 error_at (loc
, "user defined reduction lookup is ambiguous");
4716 FOR_EACH_VEC_ELT (ambiguous
, idx
, udr
)
4718 inform (DECL_SOURCE_LOCATION (udr
), "%s %#D", str
, udr
);
4720 str
= get_spaces (str
);
4722 ambiguous
.release ();
4723 ret
= error_mark_node
;
4724 baselink
= NULL_TREE
;
4729 perform_or_defer_access_check (BASELINK_BINFO (baselink
),
4730 id
, id
, tf_warning_or_error
);
4734 /* Helper function for cp_parser_omp_declare_reduction_exprs
4736 Remove CLEANUP_STMT for data (omp_priv variable).
4737 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4741 cp_remove_omp_priv_cleanup_stmt (tree
*tp
, int *walk_subtrees
, void *data
)
4745 else if (TREE_CODE (*tp
) == CLEANUP_STMT
&& CLEANUP_DECL (*tp
) == (tree
) data
)
4746 *tp
= CLEANUP_BODY (*tp
);
4747 else if (TREE_CODE (*tp
) == DECL_EXPR
)
4749 tree decl
= DECL_EXPR_DECL (*tp
);
4750 if (!processing_template_decl
4751 && decl
== (tree
) data
4752 && DECL_INITIAL (decl
)
4753 && DECL_INITIAL (decl
) != error_mark_node
)
4755 tree list
= NULL_TREE
;
4756 append_to_statement_list_force (*tp
, &list
);
4757 tree init_expr
= build2 (INIT_EXPR
, void_type_node
,
4758 decl
, DECL_INITIAL (decl
));
4759 DECL_INITIAL (decl
) = NULL_TREE
;
4760 append_to_statement_list_force (init_expr
, &list
);
4767 /* Data passed from cp_check_omp_declare_reduction to
4768 cp_check_omp_declare_reduction_r. */
4770 struct cp_check_omp_declare_reduction_data
4777 /* Helper function for cp_check_omp_declare_reduction, called via
4781 cp_check_omp_declare_reduction_r (tree
*tp
, int *, void *data
)
4783 struct cp_check_omp_declare_reduction_data
*udr_data
4784 = (struct cp_check_omp_declare_reduction_data
*) data
;
4786 && !DECL_ARTIFICIAL (*tp
)
4787 && *tp
!= DECL_EXPR_DECL (udr_data
->stmts
[udr_data
->combiner_p
? 0 : 3])
4788 && *tp
!= DECL_EXPR_DECL (udr_data
->stmts
[udr_data
->combiner_p
? 1 : 4]))
4790 location_t loc
= udr_data
->loc
;
4791 if (udr_data
->combiner_p
)
4792 error_at (loc
, "%<#pragma omp declare reduction%> combiner refers to "
4793 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4796 error_at (loc
, "%<#pragma omp declare reduction%> initializer refers "
4797 "to variable %qD which is not %<omp_priv%> nor "
4805 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4808 cp_check_omp_declare_reduction (tree udr
)
4810 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr
)));
4811 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
4812 type
= TREE_TYPE (type
);
4814 location_t loc
= DECL_SOURCE_LOCATION (udr
);
4816 if (type
== error_mark_node
)
4818 if (ARITHMETIC_TYPE_P (type
))
4820 static enum tree_code predef_codes
[]
4821 = { PLUS_EXPR
, MULT_EXPR
, MINUS_EXPR
, BIT_AND_EXPR
, BIT_XOR_EXPR
,
4822 BIT_IOR_EXPR
, TRUTH_ANDIF_EXPR
, TRUTH_ORIF_EXPR
};
4823 for (i
= 0; i
< 8; i
++)
4825 tree id
= omp_reduction_id (predef_codes
[i
], NULL_TREE
, NULL_TREE
);
4826 const char *n1
= IDENTIFIER_POINTER (DECL_NAME (udr
));
4827 const char *n2
= IDENTIFIER_POINTER (id
);
4828 if (strncmp (n1
, n2
, IDENTIFIER_LENGTH (id
)) == 0
4829 && (n1
[IDENTIFIER_LENGTH (id
)] == '~'
4830 || n1
[IDENTIFIER_LENGTH (id
)] == '\0'))
4835 && TREE_CODE (type
) != COMPLEX_EXPR
)
4837 const char prefix_minmax
[] = "omp declare reduction m";
4838 size_t prefix_size
= sizeof (prefix_minmax
) - 1;
4839 const char *n
= IDENTIFIER_POINTER (DECL_NAME (udr
));
4840 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr
)),
4841 prefix_minmax
, prefix_size
) == 0
4842 && ((n
[prefix_size
] == 'i' && n
[prefix_size
+ 1] == 'n')
4843 || (n
[prefix_size
] == 'a' && n
[prefix_size
+ 1] == 'x'))
4844 && (n
[prefix_size
+ 2] == '~' || n
[prefix_size
+ 2] == '\0'))
4849 error_at (loc
, "predeclared arithmetic type %qT in "
4850 "%<#pragma omp declare reduction%>", type
);
4854 else if (TREE_CODE (type
) == FUNCTION_TYPE
4855 || TREE_CODE (type
) == METHOD_TYPE
4856 || TREE_CODE (type
) == ARRAY_TYPE
)
4858 error_at (loc
, "function or array type %qT in "
4859 "%<#pragma omp declare reduction%>", type
);
4862 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
4864 error_at (loc
, "reference type %qT in %<#pragma omp declare reduction%>",
4868 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
4870 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
4871 "%<#pragma omp declare reduction%>", type
);
4875 tree body
= DECL_SAVED_TREE (udr
);
4876 if (body
== NULL_TREE
|| TREE_CODE (body
) != STATEMENT_LIST
)
4879 tree_stmt_iterator tsi
;
4880 struct cp_check_omp_declare_reduction_data data
;
4881 memset (data
.stmts
, 0, sizeof data
.stmts
);
4882 for (i
= 0, tsi
= tsi_start (body
);
4883 i
< 7 && !tsi_end_p (tsi
);
4884 i
++, tsi_next (&tsi
))
4885 data
.stmts
[i
] = tsi_stmt (tsi
);
4887 gcc_assert (tsi_end_p (tsi
));
4890 gcc_assert (TREE_CODE (data
.stmts
[0]) == DECL_EXPR
4891 && TREE_CODE (data
.stmts
[1]) == DECL_EXPR
);
4892 if (TREE_NO_WARNING (DECL_EXPR_DECL (data
.stmts
[0])))
4894 data
.combiner_p
= true;
4895 if (cp_walk_tree (&data
.stmts
[2], cp_check_omp_declare_reduction_r
,
4897 TREE_NO_WARNING (DECL_EXPR_DECL (data
.stmts
[0])) = 1;
4901 gcc_assert (TREE_CODE (data
.stmts
[3]) == DECL_EXPR
4902 && TREE_CODE (data
.stmts
[4]) == DECL_EXPR
);
4903 data
.combiner_p
= false;
4904 if (cp_walk_tree (&data
.stmts
[5], cp_check_omp_declare_reduction_r
,
4906 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data
.stmts
[3])),
4907 cp_check_omp_declare_reduction_r
, &data
, NULL
))
4908 TREE_NO_WARNING (DECL_EXPR_DECL (data
.stmts
[0])) = 1;
4910 gcc_assert (TREE_CODE (data
.stmts
[6]) == DECL_EXPR
);
4914 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4915 an inline call. But, remap
4916 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4917 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4920 clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
4921 tree decl
, tree placeholder
)
4924 struct pointer_map_t
*decl_map
= pointer_map_create ();
4926 *pointer_map_insert (decl_map
, omp_decl1
) = placeholder
;
4927 *pointer_map_insert (decl_map
, omp_decl2
) = decl
;
4928 memset (&id
, 0, sizeof (id
));
4929 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
4930 id
.dst_fn
= current_function_decl
;
4931 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
4932 id
.decl_map
= decl_map
;
4934 id
.copy_decl
= copy_decl_no_change
;
4935 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
4936 id
.transform_new_cfg
= true;
4937 id
.transform_return_to_modify
= false;
4938 id
.transform_lang_insert_block
= NULL
;
4940 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
4941 pointer_map_destroy (decl_map
);
4945 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
4946 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
4949 find_omp_placeholder_r (tree
*tp
, int *, void *data
)
4951 if (*tp
== (tree
) data
)
4956 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
4957 Return true if there is some error and the clause should be removed. */
4960 finish_omp_reduction_clause (tree c
, bool *need_default_ctor
, bool *need_dtor
)
4962 tree t
= OMP_CLAUSE_DECL (c
);
4963 bool predefined
= false;
4964 tree type
= TREE_TYPE (t
);
4965 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4966 type
= TREE_TYPE (type
);
4967 if (ARITHMETIC_TYPE_P (type
))
4968 switch (OMP_CLAUSE_REDUCTION_CODE (c
))
4977 if (TREE_CODE (type
) == COMPLEX_TYPE
)
4984 case TRUTH_ANDIF_EXPR
:
4985 case TRUTH_ORIF_EXPR
:
4986 if (FLOAT_TYPE_P (type
))
4993 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TYPE_READONLY (type
))
4995 error ("%qE has invalid type for %<reduction%>", t
);
4998 else if (!processing_template_decl
)
5000 t
= require_complete_type (t
);
5001 if (t
== error_mark_node
)
5003 OMP_CLAUSE_DECL (c
) = t
;
5008 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = NULL_TREE
;
5011 else if (processing_template_decl
)
5014 tree id
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
5016 type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
5017 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5018 type
= TREE_TYPE (type
);
5019 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = NULL_TREE
;
5020 if (id
== NULL_TREE
)
5021 id
= omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c
),
5022 NULL_TREE
, NULL_TREE
);
5023 id
= omp_reduction_lookup (OMP_CLAUSE_LOCATION (c
), id
, type
, NULL
, NULL
);
5026 if (id
== error_mark_node
)
5028 id
= OVL_CURRENT (id
);
5030 tree body
= DECL_SAVED_TREE (id
);
5031 if (TREE_CODE (body
) == STATEMENT_LIST
)
5033 tree_stmt_iterator tsi
;
5034 tree placeholder
= NULL_TREE
;
5037 tree atype
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id
)));
5038 atype
= TREE_TYPE (atype
);
5039 bool need_static_cast
= !same_type_p (type
, atype
);
5040 memset (stmts
, 0, sizeof stmts
);
5041 for (i
= 0, tsi
= tsi_start (body
);
5042 i
< 7 && !tsi_end_p (tsi
);
5043 i
++, tsi_next (&tsi
))
5044 stmts
[i
] = tsi_stmt (tsi
);
5045 gcc_assert (tsi_end_p (tsi
));
5049 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
5050 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
5051 placeholder
= build_lang_decl (VAR_DECL
, NULL_TREE
, type
);
5052 DECL_ARTIFICIAL (placeholder
) = 1;
5053 DECL_IGNORED_P (placeholder
) = 1;
5054 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
5055 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts
[0])))
5056 cxx_mark_addressable (placeholder
);
5057 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts
[1]))
5058 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
)))
5060 cxx_mark_addressable (OMP_CLAUSE_DECL (c
));
5061 tree omp_out
= placeholder
;
5062 tree omp_in
= convert_from_reference (OMP_CLAUSE_DECL (c
));
5063 if (need_static_cast
)
5065 tree rtype
= build_reference_type (atype
);
5066 omp_out
= build_static_cast (rtype
, omp_out
,
5067 tf_warning_or_error
);
5068 omp_in
= build_static_cast (rtype
, omp_in
,
5069 tf_warning_or_error
);
5070 if (omp_out
== error_mark_node
|| omp_in
== error_mark_node
)
5072 omp_out
= convert_from_reference (omp_out
);
5073 omp_in
= convert_from_reference (omp_in
);
5075 OMP_CLAUSE_REDUCTION_MERGE (c
)
5076 = clone_omp_udr (stmts
[2], DECL_EXPR_DECL (stmts
[0]),
5077 DECL_EXPR_DECL (stmts
[1]), omp_in
, omp_out
);
5081 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
5082 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
5083 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts
[3])))
5084 cxx_mark_addressable (OMP_CLAUSE_DECL (c
));
5085 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts
[4])))
5086 cxx_mark_addressable (placeholder
);
5087 tree omp_priv
= convert_from_reference (OMP_CLAUSE_DECL (c
));
5088 tree omp_orig
= placeholder
;
5089 if (need_static_cast
)
5093 error_at (OMP_CLAUSE_LOCATION (c
),
5094 "user defined reduction with constructor "
5095 "initializer for base class %qT", atype
);
5098 tree rtype
= build_reference_type (atype
);
5099 omp_priv
= build_static_cast (rtype
, omp_priv
,
5100 tf_warning_or_error
);
5101 omp_orig
= build_static_cast (rtype
, omp_orig
,
5102 tf_warning_or_error
);
5103 if (omp_priv
== error_mark_node
5104 || omp_orig
== error_mark_node
)
5106 omp_priv
= convert_from_reference (omp_priv
);
5107 omp_orig
= convert_from_reference (omp_orig
);
5110 *need_default_ctor
= true;
5111 OMP_CLAUSE_REDUCTION_INIT (c
)
5112 = clone_omp_udr (stmts
[5], DECL_EXPR_DECL (stmts
[4]),
5113 DECL_EXPR_DECL (stmts
[3]),
5114 omp_priv
, omp_orig
);
5115 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
5116 find_omp_placeholder_r
, placeholder
, NULL
))
5117 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
5121 if (CLASS_TYPE_P (type
) && !pod_type_p (type
))
5122 *need_default_ctor
= true;
5126 tree v
= convert_from_reference (t
);
5127 if (AGGREGATE_TYPE_P (TREE_TYPE (v
)))
5128 init
= build_constructor (TREE_TYPE (v
), NULL
);
5130 init
= fold_convert (TREE_TYPE (v
), integer_zero_node
);
5131 OMP_CLAUSE_REDUCTION_INIT (c
)
5132 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
, init
);
5137 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
5141 error ("user defined reduction not found for %qD", t
);
5147 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5148 Remove any elements from the list that are invalid. */
5151 finish_omp_clauses (tree clauses
)
5153 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
5154 bitmap_head aligned_head
;
5155 tree c
, t
, *pc
= &clauses
;
5156 bool branch_seen
= false;
5157 bool copyprivate_seen
= false;
5159 bitmap_obstack_initialize (NULL
);
5160 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
5161 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
5162 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
5163 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
5165 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
5167 bool remove
= false;
5169 switch (OMP_CLAUSE_CODE (c
))
5171 case OMP_CLAUSE_SHARED
:
5172 goto check_dup_generic
;
5173 case OMP_CLAUSE_PRIVATE
:
5174 goto check_dup_generic
;
5175 case OMP_CLAUSE_REDUCTION
:
5176 goto check_dup_generic
;
5177 case OMP_CLAUSE_COPYPRIVATE
:
5178 copyprivate_seen
= true;
5179 goto check_dup_generic
;
5180 case OMP_CLAUSE_COPYIN
:
5181 goto check_dup_generic
;
5182 case OMP_CLAUSE_LINEAR
:
5183 t
= OMP_CLAUSE_DECL (c
);
5184 if (!type_dependent_expression_p (t
)
5185 && !INTEGRAL_TYPE_P (TREE_TYPE (t
))
5186 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
5188 error ("linear clause applied to non-integral non-pointer "
5189 "variable with %qT type", TREE_TYPE (t
));
5193 t
= OMP_CLAUSE_LINEAR_STEP (c
);
5195 t
= integer_one_node
;
5196 if (t
== error_mark_node
)
5201 else if (!type_dependent_expression_p (t
)
5202 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5204 error ("linear step expression must be integral");
5210 t
= mark_rvalue_use (t
);
5211 if (!processing_template_decl
)
5213 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5214 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
)))
5217 t
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
5218 OMP_CLAUSE_DECL (c
), t
);
5219 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
),
5220 MINUS_EXPR
, sizetype
, t
,
5221 OMP_CLAUSE_DECL (c
));
5222 if (t
== error_mark_node
)
5229 OMP_CLAUSE_LINEAR_STEP (c
) = t
;
5231 goto check_dup_generic
;
5233 t
= OMP_CLAUSE_DECL (c
);
5234 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
5236 if (processing_template_decl
)
5239 error ("%qD is not a variable in clause %qs", t
,
5240 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5242 error ("%qE is not a variable in clause %qs", t
,
5243 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5246 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
5247 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
5248 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
5250 error ("%qD appears more than once in data clauses", t
);
5254 bitmap_set_bit (&generic_head
, DECL_UID (t
));
5257 case OMP_CLAUSE_FIRSTPRIVATE
:
5258 t
= OMP_CLAUSE_DECL (c
);
5259 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
5261 if (processing_template_decl
)
5264 error ("%qD is not a variable in clause %<firstprivate%>", t
);
5266 error ("%qE is not a variable in clause %<firstprivate%>", t
);
5269 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
5270 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
5272 error ("%qD appears more than once in data clauses", t
);
5276 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
5279 case OMP_CLAUSE_LASTPRIVATE
:
5280 t
= OMP_CLAUSE_DECL (c
);
5281 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
5283 if (processing_template_decl
)
5286 error ("%qD is not a variable in clause %<lastprivate%>", t
);
5288 error ("%qE is not a variable in clause %<lastprivate%>", t
);
5291 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
5292 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
5294 error ("%qD appears more than once in data clauses", t
);
5298 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
5302 t
= OMP_CLAUSE_IF_EXPR (c
);
5303 t
= maybe_convert_cond (t
);
5304 if (t
== error_mark_node
)
5306 else if (!processing_template_decl
)
5307 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5308 OMP_CLAUSE_IF_EXPR (c
) = t
;
5311 case OMP_CLAUSE_FINAL
:
5312 t
= OMP_CLAUSE_FINAL_EXPR (c
);
5313 t
= maybe_convert_cond (t
);
5314 if (t
== error_mark_node
)
5316 else if (!processing_template_decl
)
5317 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5318 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
5321 case OMP_CLAUSE_NUM_THREADS
:
5322 t
= OMP_CLAUSE_NUM_THREADS_EXPR (c
);
5323 if (t
== error_mark_node
)
5325 else if (!type_dependent_expression_p (t
)
5326 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5328 error ("num_threads expression must be integral");
5333 t
= mark_rvalue_use (t
);
5334 if (!processing_template_decl
)
5335 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5336 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
5340 case OMP_CLAUSE_SCHEDULE
:
5341 t
= OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
);
5344 else if (t
== error_mark_node
)
5346 else if (!type_dependent_expression_p (t
)
5347 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5349 error ("schedule chunk size expression must be integral");
5354 t
= mark_rvalue_use (t
);
5355 if (!processing_template_decl
)
5356 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5357 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
5361 case OMP_CLAUSE_SIMDLEN
:
5362 case OMP_CLAUSE_SAFELEN
:
5363 t
= OMP_CLAUSE_OPERAND (c
, 0);
5364 if (t
== error_mark_node
)
5366 else if (!type_dependent_expression_p (t
)
5367 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5369 error ("%qs length expression must be integral",
5370 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5375 t
= mark_rvalue_use (t
);
5376 t
= maybe_constant_value (t
);
5377 if (!processing_template_decl
)
5379 if (TREE_CODE (t
) != INTEGER_CST
5380 || tree_int_cst_sgn (t
) != 1)
5382 error ("%qs length expression must be positive constant"
5383 " integer expression",
5384 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5388 OMP_CLAUSE_OPERAND (c
, 0) = t
;
5392 case OMP_CLAUSE_NUM_TEAMS
:
5393 t
= OMP_CLAUSE_NUM_TEAMS_EXPR (c
);
5394 if (t
== error_mark_node
)
5396 else if (!type_dependent_expression_p (t
)
5397 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5399 error ("%<num_teams%> expression must be integral");
5404 t
= mark_rvalue_use (t
);
5405 if (!processing_template_decl
)
5406 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5407 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
5411 case OMP_CLAUSE_THREAD_LIMIT
:
5412 t
= OMP_CLAUSE_THREAD_LIMIT_EXPR (c
);
5413 if (t
== error_mark_node
)
5415 else if (!type_dependent_expression_p (t
)
5416 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5418 error ("%<thread_limit%> expression must be integral");
5423 t
= mark_rvalue_use (t
);
5424 if (!processing_template_decl
)
5425 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5426 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
5430 case OMP_CLAUSE_DEVICE
:
5431 t
= OMP_CLAUSE_DEVICE_ID (c
);
5432 if (t
== error_mark_node
)
5434 else if (!type_dependent_expression_p (t
)
5435 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5437 error ("%<device%> id must be integral");
5442 t
= mark_rvalue_use (t
);
5443 if (!processing_template_decl
)
5444 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5445 OMP_CLAUSE_DEVICE_ID (c
) = t
;
5449 case OMP_CLAUSE_DIST_SCHEDULE
:
5450 t
= OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
);
5453 else if (t
== error_mark_node
)
5455 else if (!type_dependent_expression_p (t
)
5456 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5458 error ("%<dist_schedule%> chunk size expression must be "
5464 t
= mark_rvalue_use (t
);
5465 if (!processing_template_decl
)
5466 t
= fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
5467 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
5471 case OMP_CLAUSE_ALIGNED
:
5472 t
= OMP_CLAUSE_DECL (c
);
5473 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
5475 if (processing_template_decl
)
5478 error ("%qD is not a variable in %<aligned%> clause", t
);
5480 error ("%qE is not a variable in %<aligned%> clause", t
);
5483 else if (!type_dependent_expression_p (t
)
5484 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
5485 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
5486 && (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
5487 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t
)))
5488 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t
)))
5491 error_at (OMP_CLAUSE_LOCATION (c
),
5492 "%qE in %<aligned%> clause is neither a pointer nor "
5493 "an array nor a reference to pointer or array", t
);
5496 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
5498 error ("%qD appears more than once in %<aligned%> clauses", t
);
5502 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
5503 t
= OMP_CLAUSE_ALIGNED_ALIGNMENT (c
);
5504 if (t
== error_mark_node
)
5506 else if (t
== NULL_TREE
)
5508 else if (!type_dependent_expression_p (t
)
5509 && !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
5511 error ("%<aligned%> clause alignment expression must "
5517 t
= mark_rvalue_use (t
);
5518 t
= maybe_constant_value (t
);
5519 if (!processing_template_decl
)
5521 if (TREE_CODE (t
) != INTEGER_CST
5522 || tree_int_cst_sgn (t
) != 1)
5524 error ("%<aligned%> clause alignment expression must be "
5525 "positive constant integer expression");
5529 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = t
;
5533 case OMP_CLAUSE_DEPEND
:
5534 t
= OMP_CLAUSE_DECL (c
);
5535 if (TREE_CODE (t
) == TREE_LIST
)
5537 if (handle_omp_array_sections (c
))
5541 if (t
== error_mark_node
)
5543 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
5545 if (processing_template_decl
)
5548 error ("%qD is not a variable in %<depend%> clause", t
);
5550 error ("%qE is not a variable in %<depend%> clause", t
);
5553 else if (!processing_template_decl
5554 && !cxx_mark_addressable (t
))
5558 case OMP_CLAUSE_MAP
:
5560 case OMP_CLAUSE_FROM
:
5561 t
= OMP_CLAUSE_DECL (c
);
5562 if (TREE_CODE (t
) == TREE_LIST
)
5564 if (handle_omp_array_sections (c
))
5568 t
= OMP_CLAUSE_DECL (c
);
5569 if (!cp_omp_mappable_type (TREE_TYPE (t
)))
5571 error_at (OMP_CLAUSE_LOCATION (c
),
5572 "array section does not have mappable type "
5574 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5580 if (t
== error_mark_node
)
5582 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
5584 if (processing_template_decl
)
5586 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
5587 && OMP_CLAUSE_MAP_KIND (c
) == OMP_CLAUSE_MAP_POINTER
)
5590 error ("%qD is not a variable in %qs clause", t
,
5591 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5593 error ("%qE is not a variable in %qs clause", t
,
5594 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5597 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
5599 error ("%qD is threadprivate variable in %qs clause", t
,
5600 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5603 else if (!processing_template_decl
5604 && TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
5605 && !cxx_mark_addressable (t
))
5607 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
5608 && OMP_CLAUSE_MAP_KIND (c
) == OMP_CLAUSE_MAP_POINTER
)
5609 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t
))
5611 ? TREE_TYPE (TREE_TYPE (t
))
5614 error_at (OMP_CLAUSE_LOCATION (c
),
5615 "%qD does not have a mappable type in %qs clause", t
,
5616 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5619 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
5621 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
5622 error ("%qD appears more than once in motion clauses", t
);
5624 error ("%qD appears more than once in map clauses", t
);
5628 bitmap_set_bit (&generic_head
, DECL_UID (t
));
5631 case OMP_CLAUSE_UNIFORM
:
5632 t
= OMP_CLAUSE_DECL (c
);
5633 if (TREE_CODE (t
) != PARM_DECL
)
5635 if (processing_template_decl
)
5638 error ("%qD is not an argument in %<uniform%> clause", t
);
5640 error ("%qE is not an argument in %<uniform%> clause", t
);
5644 goto check_dup_generic
;
5646 case OMP_CLAUSE_NOWAIT
:
5647 case OMP_CLAUSE_ORDERED
:
5648 case OMP_CLAUSE_DEFAULT
:
5649 case OMP_CLAUSE_UNTIED
:
5650 case OMP_CLAUSE_COLLAPSE
:
5651 case OMP_CLAUSE_MERGEABLE
:
5652 case OMP_CLAUSE_PARALLEL
:
5653 case OMP_CLAUSE_FOR
:
5654 case OMP_CLAUSE_SECTIONS
:
5655 case OMP_CLAUSE_TASKGROUP
:
5656 case OMP_CLAUSE_PROC_BIND
:
5659 case OMP_CLAUSE_INBRANCH
:
5660 case OMP_CLAUSE_NOTINBRANCH
:
5663 error ("%<inbranch%> clause is incompatible with "
5675 *pc
= OMP_CLAUSE_CHAIN (c
);
5677 pc
= &OMP_CLAUSE_CHAIN (c
);
5680 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
5682 enum omp_clause_code c_kind
= OMP_CLAUSE_CODE (c
);
5683 bool remove
= false;
5684 bool need_complete_non_reference
= false;
5685 bool need_default_ctor
= false;
5686 bool need_copy_ctor
= false;
5687 bool need_copy_assignment
= false;
5688 bool need_implicitly_determined
= false;
5689 bool need_dtor
= false;
5690 tree type
, inner_type
;
5694 case OMP_CLAUSE_SHARED
:
5695 need_implicitly_determined
= true;
5697 case OMP_CLAUSE_PRIVATE
:
5698 need_complete_non_reference
= true;
5699 need_default_ctor
= true;
5701 need_implicitly_determined
= true;
5703 case OMP_CLAUSE_FIRSTPRIVATE
:
5704 need_complete_non_reference
= true;
5705 need_copy_ctor
= true;
5707 need_implicitly_determined
= true;
5709 case OMP_CLAUSE_LASTPRIVATE
:
5710 need_complete_non_reference
= true;
5711 need_copy_assignment
= true;
5712 need_implicitly_determined
= true;
5714 case OMP_CLAUSE_REDUCTION
:
5715 need_implicitly_determined
= true;
5717 case OMP_CLAUSE_COPYPRIVATE
:
5718 need_copy_assignment
= true;
5720 case OMP_CLAUSE_COPYIN
:
5721 need_copy_assignment
= true;
5723 case OMP_CLAUSE_NOWAIT
:
5724 if (copyprivate_seen
)
5726 error_at (OMP_CLAUSE_LOCATION (c
),
5727 "%<nowait%> clause must not be used together "
5728 "with %<copyprivate%>");
5729 *pc
= OMP_CLAUSE_CHAIN (c
);
5734 pc
= &OMP_CLAUSE_CHAIN (c
);
5738 t
= OMP_CLAUSE_DECL (c
);
5739 if (processing_template_decl
5740 && !VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
5742 pc
= &OMP_CLAUSE_CHAIN (c
);
5748 case OMP_CLAUSE_LASTPRIVATE
:
5749 if (!bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
5751 need_default_ctor
= true;
5756 case OMP_CLAUSE_REDUCTION
:
5757 if (finish_omp_reduction_clause (c
, &need_default_ctor
,
5761 t
= OMP_CLAUSE_DECL (c
);
5764 case OMP_CLAUSE_COPYIN
:
5765 if (!VAR_P (t
) || !DECL_THREAD_LOCAL_P (t
))
5767 error ("%qE must be %<threadprivate%> for %<copyin%>", t
);
5776 if (need_complete_non_reference
|| need_copy_assignment
)
5778 t
= require_complete_type (t
);
5779 if (t
== error_mark_node
)
5781 else if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
5782 && need_complete_non_reference
)
5784 error ("%qE has reference type for %qs", t
,
5785 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5789 if (need_implicitly_determined
)
5791 const char *share_name
= NULL
;
5793 if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
5794 share_name
= "threadprivate";
5795 else switch (cxx_omp_predetermined_sharing (t
))
5797 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
5799 case OMP_CLAUSE_DEFAULT_SHARED
:
5800 /* const vars may be specified in firstprivate clause. */
5801 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
5802 && cxx_omp_const_qual_no_mutable (t
))
5804 share_name
= "shared";
5806 case OMP_CLAUSE_DEFAULT_PRIVATE
:
5807 share_name
= "private";
5814 error ("%qE is predetermined %qs for %qs",
5815 t
, share_name
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
5820 /* We're interested in the base element, not arrays. */
5821 inner_type
= type
= TREE_TYPE (t
);
5822 while (TREE_CODE (inner_type
) == ARRAY_TYPE
)
5823 inner_type
= TREE_TYPE (inner_type
);
5825 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
5826 && TREE_CODE (inner_type
) == REFERENCE_TYPE
)
5827 inner_type
= TREE_TYPE (inner_type
);
5829 /* Check for special function availability by building a call to one.
5830 Save the results, because later we won't be in the right context
5831 for making these queries. */
5832 if (CLASS_TYPE_P (inner_type
)
5833 && COMPLETE_TYPE_P (inner_type
)
5834 && (need_default_ctor
|| need_copy_ctor
5835 || need_copy_assignment
|| need_dtor
)
5836 && !type_dependent_expression_p (t
)
5837 && cxx_omp_create_clause_info (c
, inner_type
, need_default_ctor
,
5838 need_copy_ctor
, need_copy_assignment
,
5843 *pc
= OMP_CLAUSE_CHAIN (c
);
5845 pc
= &OMP_CLAUSE_CHAIN (c
);
5848 bitmap_obstack_release (NULL
);
5852 /* For all variables in the tree_list VARS, mark them as thread local. */
5855 finish_omp_threadprivate (tree vars
)
5859 /* Mark every variable in VARS to be assigned thread local storage. */
5860 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
5862 tree v
= TREE_PURPOSE (t
);
5864 if (error_operand_p (v
))
5866 else if (!VAR_P (v
))
5867 error ("%<threadprivate%> %qD is not file, namespace "
5868 "or block scope variable", v
);
5869 /* If V had already been marked threadprivate, it doesn't matter
5870 whether it had been used prior to this point. */
5871 else if (TREE_USED (v
)
5872 && (DECL_LANG_SPECIFIC (v
) == NULL
5873 || !CP_DECL_THREADPRIVATE_P (v
)))
5874 error ("%qE declared %<threadprivate%> after first use", v
);
5875 else if (! TREE_STATIC (v
) && ! DECL_EXTERNAL (v
))
5876 error ("automatic variable %qE cannot be %<threadprivate%>", v
);
5877 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v
))))
5878 error ("%<threadprivate%> %qE has incomplete type", v
);
5879 else if (TREE_STATIC (v
) && TYPE_P (CP_DECL_CONTEXT (v
))
5880 && CP_DECL_CONTEXT (v
) != current_class_type
)
5881 error ("%<threadprivate%> %qE directive not "
5882 "in %qT definition", v
, CP_DECL_CONTEXT (v
));
5885 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5886 if (DECL_LANG_SPECIFIC (v
) == NULL
)
5888 retrofit_lang_decl (v
);
5890 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5891 after the allocation of the lang_decl structure. */
5892 if (DECL_DISCRIMINATOR_P (v
))
5893 DECL_LANG_SPECIFIC (v
)->u
.base
.u2sel
= 1;
5896 if (! DECL_THREAD_LOCAL_P (v
))
5898 DECL_TLS_MODEL (v
) = decl_default_tls_model (v
);
5899 /* If rtl has been already set for this var, call
5900 make_decl_rtl once again, so that encode_section_info
5901 has a chance to look at the new decl flags. */
5902 if (DECL_RTL_SET_P (v
))
5905 CP_DECL_THREADPRIVATE_P (v
) = 1;
5910 /* Build an OpenMP structured block. */
5913 begin_omp_structured_block (void)
5915 return do_pushlevel (sk_omp
);
5919 finish_omp_structured_block (tree block
)
5921 return do_poplevel (block
);
5924 /* Similarly, except force the retention of the BLOCK. */
5927 begin_omp_parallel (void)
5929 keep_next_level (true);
5930 return begin_omp_structured_block ();
5934 finish_omp_parallel (tree clauses
, tree body
)
5938 body
= finish_omp_structured_block (body
);
5940 stmt
= make_node (OMP_PARALLEL
);
5941 TREE_TYPE (stmt
) = void_type_node
;
5942 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
5943 OMP_PARALLEL_BODY (stmt
) = body
;
5945 return add_stmt (stmt
);
5949 begin_omp_task (void)
5951 keep_next_level (true);
5952 return begin_omp_structured_block ();
5956 finish_omp_task (tree clauses
, tree body
)
5960 body
= finish_omp_structured_block (body
);
5962 stmt
= make_node (OMP_TASK
);
5963 TREE_TYPE (stmt
) = void_type_node
;
5964 OMP_TASK_CLAUSES (stmt
) = clauses
;
5965 OMP_TASK_BODY (stmt
) = body
;
5967 return add_stmt (stmt
);
5970 /* Helper function for finish_omp_for. Convert Ith random access iterator
5971 into integral iterator. Return FALSE if successful. */
5974 handle_omp_for_class_iterator (int i
, location_t locus
, tree declv
, tree initv
,
5975 tree condv
, tree incrv
, tree
*body
,
5976 tree
*pre_body
, tree clauses
)
5978 tree diff
, iter_init
, iter_incr
= NULL
, last
;
5979 tree incr_var
= NULL
, orig_pre_body
, orig_body
, c
;
5980 tree decl
= TREE_VEC_ELT (declv
, i
);
5981 tree init
= TREE_VEC_ELT (initv
, i
);
5982 tree cond
= TREE_VEC_ELT (condv
, i
);
5983 tree incr
= TREE_VEC_ELT (incrv
, i
);
5985 location_t elocus
= locus
;
5987 if (init
&& EXPR_HAS_LOCATION (init
))
5988 elocus
= EXPR_LOCATION (init
);
5990 switch (TREE_CODE (cond
))
5996 if (TREE_OPERAND (cond
, 1) == iter
)
5997 cond
= build2 (swap_tree_comparison (TREE_CODE (cond
)),
5998 TREE_TYPE (cond
), iter
, TREE_OPERAND (cond
, 0));
5999 if (TREE_OPERAND (cond
, 0) != iter
)
6000 cond
= error_mark_node
;
6003 tree tem
= build_x_binary_op (EXPR_LOCATION (cond
),
6006 TREE_OPERAND (cond
, 1), ERROR_MARK
,
6007 NULL
, tf_warning_or_error
);
6008 if (error_operand_p (tem
))
6013 cond
= error_mark_node
;
6016 if (cond
== error_mark_node
)
6018 error_at (elocus
, "invalid controlling predicate");
6021 diff
= build_x_binary_op (elocus
, MINUS_EXPR
, TREE_OPERAND (cond
, 1),
6022 ERROR_MARK
, iter
, ERROR_MARK
, NULL
,
6023 tf_warning_or_error
);
6024 if (error_operand_p (diff
))
6026 if (TREE_CODE (TREE_TYPE (diff
)) != INTEGER_TYPE
)
6028 error_at (elocus
, "difference between %qE and %qD does not have integer type",
6029 TREE_OPERAND (cond
, 1), iter
);
6033 switch (TREE_CODE (incr
))
6035 case PREINCREMENT_EXPR
:
6036 case PREDECREMENT_EXPR
:
6037 case POSTINCREMENT_EXPR
:
6038 case POSTDECREMENT_EXPR
:
6039 if (TREE_OPERAND (incr
, 0) != iter
)
6041 incr
= error_mark_node
;
6044 iter_incr
= build_x_unary_op (EXPR_LOCATION (incr
),
6045 TREE_CODE (incr
), iter
,
6046 tf_warning_or_error
);
6047 if (error_operand_p (iter_incr
))
6049 else if (TREE_CODE (incr
) == PREINCREMENT_EXPR
6050 || TREE_CODE (incr
) == POSTINCREMENT_EXPR
)
6051 incr
= integer_one_node
;
6053 incr
= integer_minus_one_node
;
6056 if (TREE_OPERAND (incr
, 0) != iter
)
6057 incr
= error_mark_node
;
6058 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
6059 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
6061 tree rhs
= TREE_OPERAND (incr
, 1);
6062 if (TREE_OPERAND (rhs
, 0) == iter
)
6064 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs
, 1)))
6066 incr
= error_mark_node
;
6069 iter_incr
= build_x_modify_expr (EXPR_LOCATION (rhs
),
6070 iter
, TREE_CODE (rhs
),
6071 TREE_OPERAND (rhs
, 1),
6072 tf_warning_or_error
);
6073 if (error_operand_p (iter_incr
))
6075 incr
= TREE_OPERAND (rhs
, 1);
6076 incr
= cp_convert (TREE_TYPE (diff
), incr
,
6077 tf_warning_or_error
);
6078 if (TREE_CODE (rhs
) == MINUS_EXPR
)
6080 incr
= build1 (NEGATE_EXPR
, TREE_TYPE (diff
), incr
);
6081 incr
= fold_if_not_in_template (incr
);
6083 if (TREE_CODE (incr
) != INTEGER_CST
6084 && (TREE_CODE (incr
) != NOP_EXPR
6085 || (TREE_CODE (TREE_OPERAND (incr
, 0))
6090 else if (TREE_OPERAND (rhs
, 1) == iter
)
6092 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs
, 0))) != INTEGER_TYPE
6093 || TREE_CODE (rhs
) != PLUS_EXPR
)
6094 incr
= error_mark_node
;
6097 iter_incr
= build_x_binary_op (EXPR_LOCATION (rhs
),
6099 TREE_OPERAND (rhs
, 0),
6102 tf_warning_or_error
);
6103 if (error_operand_p (iter_incr
))
6105 iter_incr
= build_x_modify_expr (EXPR_LOCATION (rhs
),
6108 tf_warning_or_error
);
6109 if (error_operand_p (iter_incr
))
6111 incr
= TREE_OPERAND (rhs
, 0);
6116 incr
= error_mark_node
;
6119 incr
= error_mark_node
;
6122 incr
= error_mark_node
;
6126 if (incr
== error_mark_node
)
6128 error_at (elocus
, "invalid increment expression");
6132 incr
= cp_convert (TREE_TYPE (diff
), incr
, tf_warning_or_error
);
6133 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
6134 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
6135 && OMP_CLAUSE_DECL (c
) == iter
)
6138 decl
= create_temporary_var (TREE_TYPE (diff
));
6140 add_decl_expr (decl
);
6141 last
= create_temporary_var (TREE_TYPE (diff
));
6143 add_decl_expr (last
);
6144 if (c
&& iter_incr
== NULL
)
6146 incr_var
= create_temporary_var (TREE_TYPE (diff
));
6147 pushdecl (incr_var
);
6148 add_decl_expr (incr_var
);
6150 gcc_assert (stmts_are_full_exprs_p ());
6152 orig_pre_body
= *pre_body
;
6153 *pre_body
= push_stmt_list ();
6155 add_stmt (orig_pre_body
);
6157 finish_expr_stmt (build_x_modify_expr (elocus
,
6158 iter
, NOP_EXPR
, init
,
6159 tf_warning_or_error
));
6160 init
= build_int_cst (TREE_TYPE (diff
), 0);
6161 if (c
&& iter_incr
== NULL
)
6163 finish_expr_stmt (build_x_modify_expr (elocus
,
6165 incr
, tf_warning_or_error
));
6167 iter_incr
= build_x_modify_expr (elocus
,
6168 iter
, PLUS_EXPR
, incr
,
6169 tf_warning_or_error
);
6171 finish_expr_stmt (build_x_modify_expr (elocus
,
6172 last
, NOP_EXPR
, init
,
6173 tf_warning_or_error
));
6174 *pre_body
= pop_stmt_list (*pre_body
);
6176 cond
= cp_build_binary_op (elocus
,
6177 TREE_CODE (cond
), decl
, diff
,
6178 tf_warning_or_error
);
6179 incr
= build_modify_expr (elocus
, decl
, NULL_TREE
, PLUS_EXPR
,
6180 elocus
, incr
, NULL_TREE
);
6183 *body
= push_stmt_list ();
6184 iter_init
= build2 (MINUS_EXPR
, TREE_TYPE (diff
), decl
, last
);
6185 iter_init
= build_x_modify_expr (elocus
,
6186 iter
, PLUS_EXPR
, iter_init
,
6187 tf_warning_or_error
);
6188 iter_init
= build1 (NOP_EXPR
, void_type_node
, iter_init
);
6189 finish_expr_stmt (iter_init
);
6190 finish_expr_stmt (build_x_modify_expr (elocus
,
6191 last
, NOP_EXPR
, decl
,
6192 tf_warning_or_error
));
6193 add_stmt (orig_body
);
6194 *body
= pop_stmt_list (*body
);
6198 OMP_CLAUSE_LASTPRIVATE_STMT (c
) = push_stmt_list ();
6199 finish_expr_stmt (iter_incr
);
6200 OMP_CLAUSE_LASTPRIVATE_STMT (c
)
6201 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c
));
6204 TREE_VEC_ELT (declv
, i
) = decl
;
6205 TREE_VEC_ELT (initv
, i
) = init
;
6206 TREE_VEC_ELT (condv
, i
) = cond
;
6207 TREE_VEC_ELT (incrv
, i
) = incr
;
6212 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6213 are directly for their associated operands in the statement. DECL
6214 and INIT are a combo; if DECL is NULL then INIT ought to be a
6215 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6216 optional statements that need to go before the loop into its
6220 finish_omp_for (location_t locus
, enum tree_code code
, tree declv
, tree initv
,
6221 tree condv
, tree incrv
, tree body
, tree pre_body
, tree clauses
)
6223 tree omp_for
= NULL
, orig_incr
= NULL
;
6224 tree decl
, init
, cond
, incr
;
6228 gcc_assert (TREE_VEC_LENGTH (declv
) == TREE_VEC_LENGTH (initv
));
6229 gcc_assert (TREE_VEC_LENGTH (declv
) == TREE_VEC_LENGTH (condv
));
6230 gcc_assert (TREE_VEC_LENGTH (declv
) == TREE_VEC_LENGTH (incrv
));
6231 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
6233 decl
= TREE_VEC_ELT (declv
, i
);
6234 init
= TREE_VEC_ELT (initv
, i
);
6235 cond
= TREE_VEC_ELT (condv
, i
);
6236 incr
= TREE_VEC_ELT (incrv
, i
);
6242 switch (TREE_CODE (init
))
6245 decl
= TREE_OPERAND (init
, 0);
6246 init
= TREE_OPERAND (init
, 1);
6249 if (TREE_CODE (TREE_OPERAND (init
, 1)) == NOP_EXPR
)
6251 decl
= TREE_OPERAND (init
, 0);
6252 init
= TREE_OPERAND (init
, 2);
6262 "expected iteration declaration or initialization");
6267 if (init
&& EXPR_HAS_LOCATION (init
))
6268 elocus
= EXPR_LOCATION (init
);
6272 error_at (elocus
, "missing controlling predicate");
6278 error_at (elocus
, "missing increment expression");
6282 TREE_VEC_ELT (declv
, i
) = decl
;
6283 TREE_VEC_ELT (initv
, i
) = init
;
6286 if (dependent_omp_for_p (declv
, initv
, condv
, incrv
))
6290 stmt
= make_node (code
);
6292 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
6294 /* This is really just a place-holder. We'll be decomposing this
6295 again and going through the cp_build_modify_expr path below when
6296 we instantiate the thing. */
6297 TREE_VEC_ELT (initv
, i
)
6298 = build2 (MODIFY_EXPR
, void_type_node
, TREE_VEC_ELT (declv
, i
),
6299 TREE_VEC_ELT (initv
, i
));
6302 TREE_TYPE (stmt
) = void_type_node
;
6303 OMP_FOR_INIT (stmt
) = initv
;
6304 OMP_FOR_COND (stmt
) = condv
;
6305 OMP_FOR_INCR (stmt
) = incrv
;
6306 OMP_FOR_BODY (stmt
) = body
;
6307 OMP_FOR_PRE_BODY (stmt
) = pre_body
;
6308 OMP_FOR_CLAUSES (stmt
) = clauses
;
6310 SET_EXPR_LOCATION (stmt
, locus
);
6311 return add_stmt (stmt
);
6314 if (processing_template_decl
)
6315 orig_incr
= make_tree_vec (TREE_VEC_LENGTH (incrv
));
6317 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); )
6319 decl
= TREE_VEC_ELT (declv
, i
);
6320 init
= TREE_VEC_ELT (initv
, i
);
6321 cond
= TREE_VEC_ELT (condv
, i
);
6322 incr
= TREE_VEC_ELT (incrv
, i
);
6324 TREE_VEC_ELT (orig_incr
, i
) = incr
;
6327 if (init
&& EXPR_HAS_LOCATION (init
))
6328 elocus
= EXPR_LOCATION (init
);
6332 error_at (elocus
, "expected iteration declaration or initialization");
6336 if (incr
&& TREE_CODE (incr
) == MODOP_EXPR
)
6339 TREE_VEC_ELT (orig_incr
, i
) = incr
;
6340 incr
= cp_build_modify_expr (TREE_OPERAND (incr
, 0),
6341 TREE_CODE (TREE_OPERAND (incr
, 1)),
6342 TREE_OPERAND (incr
, 2),
6343 tf_warning_or_error
);
6346 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
6348 if (code
== OMP_SIMD
)
6350 error_at (elocus
, "%<#pragma omp simd%> used with class "
6351 "iteration variable %qE", decl
);
6354 if (handle_omp_for_class_iterator (i
, locus
, declv
, initv
, condv
,
6355 incrv
, &body
, &pre_body
, clauses
))
6360 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl
))
6361 && !TYPE_PTR_P (TREE_TYPE (decl
)))
6363 error_at (elocus
, "invalid type for iteration variable %qE", decl
);
6367 if (!processing_template_decl
)
6369 init
= fold_build_cleanup_point_expr (TREE_TYPE (init
), init
);
6370 init
= cp_build_modify_expr (decl
, NOP_EXPR
, init
, tf_warning_or_error
);
6373 init
= build2 (MODIFY_EXPR
, void_type_node
, decl
, init
);
6375 && TREE_SIDE_EFFECTS (cond
)
6376 && COMPARISON_CLASS_P (cond
)
6377 && !processing_template_decl
)
6379 tree t
= TREE_OPERAND (cond
, 0);
6380 if (TREE_SIDE_EFFECTS (t
)
6382 && (TREE_CODE (t
) != NOP_EXPR
6383 || TREE_OPERAND (t
, 0) != decl
))
6384 TREE_OPERAND (cond
, 0)
6385 = fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
6387 t
= TREE_OPERAND (cond
, 1);
6388 if (TREE_SIDE_EFFECTS (t
)
6390 && (TREE_CODE (t
) != NOP_EXPR
6391 || TREE_OPERAND (t
, 0) != decl
))
6392 TREE_OPERAND (cond
, 1)
6393 = fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
6395 if (decl
== error_mark_node
|| init
== error_mark_node
)
6398 TREE_VEC_ELT (declv
, i
) = decl
;
6399 TREE_VEC_ELT (initv
, i
) = init
;
6400 TREE_VEC_ELT (condv
, i
) = cond
;
6401 TREE_VEC_ELT (incrv
, i
) = incr
;
6405 if (IS_EMPTY_STMT (pre_body
))
6408 omp_for
= c_finish_omp_for (locus
, code
, declv
, initv
, condv
, incrv
,
6411 if (omp_for
== NULL
)
6414 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for
)); i
++)
6416 decl
= TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for
), i
), 0);
6417 incr
= TREE_VEC_ELT (OMP_FOR_INCR (omp_for
), i
);
6419 if (TREE_CODE (incr
) != MODIFY_EXPR
)
6422 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr
, 1))
6423 && BINARY_CLASS_P (TREE_OPERAND (incr
, 1))
6424 && !processing_template_decl
)
6426 tree t
= TREE_OPERAND (TREE_OPERAND (incr
, 1), 0);
6427 if (TREE_SIDE_EFFECTS (t
)
6429 && (TREE_CODE (t
) != NOP_EXPR
6430 || TREE_OPERAND (t
, 0) != decl
))
6431 TREE_OPERAND (TREE_OPERAND (incr
, 1), 0)
6432 = fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
6434 t
= TREE_OPERAND (TREE_OPERAND (incr
, 1), 1);
6435 if (TREE_SIDE_EFFECTS (t
)
6437 && (TREE_CODE (t
) != NOP_EXPR
6438 || TREE_OPERAND (t
, 0) != decl
))
6439 TREE_OPERAND (TREE_OPERAND (incr
, 1), 1)
6440 = fold_build_cleanup_point_expr (TREE_TYPE (t
), t
);
6444 TREE_VEC_ELT (OMP_FOR_INCR (omp_for
), i
) = TREE_VEC_ELT (orig_incr
, i
);
6446 if (omp_for
!= NULL
)
6447 OMP_FOR_CLAUSES (omp_for
) = clauses
;
6452 finish_omp_atomic (enum tree_code code
, enum tree_code opcode
, tree lhs
,
6453 tree rhs
, tree v
, tree lhs1
, tree rhs1
, bool seq_cst
)
6468 dependent_p
= false;
6471 /* Even in a template, we can detect invalid uses of the atomic
6472 pragma if neither LHS nor RHS is type-dependent. */
6473 if (processing_template_decl
)
6475 dependent_p
= (type_dependent_expression_p (lhs
)
6476 || (rhs
&& type_dependent_expression_p (rhs
))
6477 || (v
&& type_dependent_expression_p (v
))
6478 || (lhs1
&& type_dependent_expression_p (lhs1
))
6479 || (rhs1
&& type_dependent_expression_p (rhs1
)));
6482 lhs
= build_non_dependent_expr (lhs
);
6484 rhs
= build_non_dependent_expr (rhs
);
6486 v
= build_non_dependent_expr (v
);
6488 lhs1
= build_non_dependent_expr (lhs1
);
6490 rhs1
= build_non_dependent_expr (rhs1
);
6495 bool swapped
= false;
6496 if (rhs1
&& cp_tree_equal (lhs
, rhs
))
6501 swapped
= !commutative_tree_code (opcode
);
6503 if (rhs1
&& !cp_tree_equal (lhs
, rhs1
))
6505 if (code
== OMP_ATOMIC
)
6506 error ("%<#pragma omp atomic update%> uses two different "
6507 "expressions for memory");
6509 error ("%<#pragma omp atomic capture%> uses two different "
6510 "expressions for memory");
6513 if (lhs1
&& !cp_tree_equal (lhs
, lhs1
))
6515 if (code
== OMP_ATOMIC
)
6516 error ("%<#pragma omp atomic update%> uses two different "
6517 "expressions for memory");
6519 error ("%<#pragma omp atomic capture%> uses two different "
6520 "expressions for memory");
6523 stmt
= c_finish_omp_atomic (input_location
, code
, opcode
, lhs
, rhs
,
6524 v
, lhs1
, rhs1
, swapped
, seq_cst
);
6525 if (stmt
== error_mark_node
)
6528 if (processing_template_decl
)
6530 if (code
== OMP_ATOMIC_READ
)
6532 stmt
= build_min_nt_loc (EXPR_LOCATION (orig_lhs
),
6533 OMP_ATOMIC_READ
, orig_lhs
);
6534 OMP_ATOMIC_SEQ_CST (stmt
) = seq_cst
;
6535 stmt
= build2 (MODIFY_EXPR
, void_type_node
, orig_v
, stmt
);
6539 if (opcode
== NOP_EXPR
)
6540 stmt
= build2 (MODIFY_EXPR
, void_type_node
, orig_lhs
, orig_rhs
);
6542 stmt
= build2 (opcode
, void_type_node
, orig_lhs
, orig_rhs
);
6544 stmt
= build_min_nt_loc (EXPR_LOCATION (orig_rhs1
),
6545 COMPOUND_EXPR
, orig_rhs1
, stmt
);
6546 if (code
!= OMP_ATOMIC
)
6548 stmt
= build_min_nt_loc (EXPR_LOCATION (orig_lhs1
),
6549 code
, orig_lhs1
, stmt
);
6550 OMP_ATOMIC_SEQ_CST (stmt
) = seq_cst
;
6551 stmt
= build2 (MODIFY_EXPR
, void_type_node
, orig_v
, stmt
);
6554 stmt
= build2 (OMP_ATOMIC
, void_type_node
, integer_zero_node
, stmt
);
6555 OMP_ATOMIC_SEQ_CST (stmt
) = seq_cst
;
6561 finish_omp_barrier (void)
6563 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_BARRIER
);
6564 vec
<tree
, va_gc
> *vec
= make_tree_vector ();
6565 tree stmt
= finish_call_expr (fn
, &vec
, false, false, tf_warning_or_error
);
6566 release_tree_vector (vec
);
6567 finish_expr_stmt (stmt
);
6571 finish_omp_flush (void)
6573 tree fn
= builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE
);
6574 vec
<tree
, va_gc
> *vec
= make_tree_vector ();
6575 tree stmt
= finish_call_expr (fn
, &vec
, false, false, tf_warning_or_error
);
6576 release_tree_vector (vec
);
6577 finish_expr_stmt (stmt
);
6581 finish_omp_taskwait (void)
6583 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT
);
6584 vec
<tree
, va_gc
> *vec
= make_tree_vector ();
6585 tree stmt
= finish_call_expr (fn
, &vec
, false, false, tf_warning_or_error
);
6586 release_tree_vector (vec
);
6587 finish_expr_stmt (stmt
);
6591 finish_omp_taskyield (void)
6593 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD
);
6594 vec
<tree
, va_gc
> *vec
= make_tree_vector ();
6595 tree stmt
= finish_call_expr (fn
, &vec
, false, false, tf_warning_or_error
);
6596 release_tree_vector (vec
);
6597 finish_expr_stmt (stmt
);
6601 finish_omp_cancel (tree clauses
)
6603 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
6605 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
6607 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
6609 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
6611 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
6615 error ("%<#pragma omp cancel must specify one of "
6616 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6619 vec
<tree
, va_gc
> *vec
= make_tree_vector ();
6620 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
6621 if (ifc
!= NULL_TREE
)
6623 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
6624 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
6625 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
6626 build_zero_cst (type
));
6629 ifc
= boolean_true_node
;
6630 vec
->quick_push (build_int_cst (integer_type_node
, mask
));
6631 vec
->quick_push (ifc
);
6632 tree stmt
= finish_call_expr (fn
, &vec
, false, false, tf_warning_or_error
);
6633 release_tree_vector (vec
);
6634 finish_expr_stmt (stmt
);
6638 finish_omp_cancellation_point (tree clauses
)
6640 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
6642 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
6644 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
6646 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
6648 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
6652 error ("%<#pragma omp cancellation point must specify one of "
6653 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6656 vec
<tree
, va_gc
> *vec
6657 = make_tree_vector_single (build_int_cst (integer_type_node
, mask
));
6658 tree stmt
= finish_call_expr (fn
, &vec
, false, false, tf_warning_or_error
);
6659 release_tree_vector (vec
);
6660 finish_expr_stmt (stmt
);
6663 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6664 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6665 should create an extra compound stmt. */
6668 begin_transaction_stmt (location_t loc
, tree
*pcompound
, int flags
)
6673 *pcompound
= begin_compound_stmt (0);
6675 r
= build_stmt (loc
, TRANSACTION_EXPR
, NULL_TREE
);
6677 /* Only add the statement to the function if support enabled. */
6681 error_at (loc
, ((flags
& TM_STMT_ATTR_RELAXED
) != 0
6682 ? G_("%<__transaction_relaxed%> without "
6683 "transactional memory support enabled")
6684 : G_("%<__transaction_atomic%> without "
6685 "transactional memory support enabled")));
6687 TRANSACTION_EXPR_BODY (r
) = push_stmt_list ();
6688 TREE_SIDE_EFFECTS (r
) = 1;
6692 /* End a __transaction_atomic or __transaction_relaxed statement.
6693 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6694 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6695 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6698 finish_transaction_stmt (tree stmt
, tree compound_stmt
, int flags
, tree noex
)
6700 TRANSACTION_EXPR_BODY (stmt
) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt
));
6701 TRANSACTION_EXPR_OUTER (stmt
) = (flags
& TM_STMT_ATTR_OUTER
) != 0;
6702 TRANSACTION_EXPR_RELAXED (stmt
) = (flags
& TM_STMT_ATTR_RELAXED
) != 0;
6703 TRANSACTION_EXPR_IS_STMT (stmt
) = 1;
6705 /* noexcept specifications are not allowed for function transactions. */
6706 gcc_assert (!(noex
&& compound_stmt
));
6709 tree body
= build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt
),
6711 /* This may not be true when the STATEMENT_LIST is empty. */
6713 SET_EXPR_LOCATION (body
, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt
)));
6714 TREE_SIDE_EFFECTS (body
) = 1;
6715 TRANSACTION_EXPR_BODY (stmt
) = body
;
6719 finish_compound_stmt (compound_stmt
);
6722 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6723 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6727 build_transaction_expr (location_t loc
, tree expr
, int flags
, tree noex
)
6732 expr
= build_must_not_throw_expr (expr
, noex
);
6734 SET_EXPR_LOCATION (expr
, loc
);
6735 TREE_SIDE_EFFECTS (expr
) = 1;
6737 ret
= build1 (TRANSACTION_EXPR
, TREE_TYPE (expr
), expr
);
6738 if (flags
& TM_STMT_ATTR_RELAXED
)
6739 TRANSACTION_EXPR_RELAXED (ret
) = 1;
6740 TREE_SIDE_EFFECTS (ret
) = 1;
6741 SET_EXPR_LOCATION (ret
, loc
);
6746 init_cp_semantics (void)
6750 /* Build a STATIC_ASSERT for a static assertion with the condition
6751 CONDITION and the message text MESSAGE. LOCATION is the location
6752 of the static assertion in the source code. When MEMBER_P, this
6753 static assertion is a member of a class. */
6755 finish_static_assert (tree condition
, tree message
, location_t location
,
6758 if (message
== NULL_TREE
6759 || message
== error_mark_node
6760 || condition
== NULL_TREE
6761 || condition
== error_mark_node
)
6764 if (check_for_bare_parameter_packs (condition
))
6765 condition
= error_mark_node
;
6767 if (type_dependent_expression_p (condition
)
6768 || value_dependent_expression_p (condition
))
6770 /* We're in a template; build a STATIC_ASSERT and put it in
6774 assertion
= make_node (STATIC_ASSERT
);
6775 STATIC_ASSERT_CONDITION (assertion
) = condition
;
6776 STATIC_ASSERT_MESSAGE (assertion
) = message
;
6777 STATIC_ASSERT_SOURCE_LOCATION (assertion
) = location
;
6780 maybe_add_class_template_decl_list (current_class_type
,
6784 add_stmt (assertion
);
6789 /* Fold the expression and convert it to a boolean value. */
6790 condition
= fold_non_dependent_expr (condition
);
6791 condition
= cp_convert (boolean_type_node
, condition
, tf_warning_or_error
);
6792 condition
= maybe_constant_value (condition
);
6794 if (TREE_CODE (condition
) == INTEGER_CST
&& !integer_zerop (condition
))
6795 /* Do nothing; the condition is satisfied. */
6799 location_t saved_loc
= input_location
;
6801 input_location
= location
;
6802 if (TREE_CODE (condition
) == INTEGER_CST
6803 && integer_zerop (condition
))
6804 /* Report the error. */
6805 error ("static assertion failed: %s", TREE_STRING_POINTER (message
));
6806 else if (condition
&& condition
!= error_mark_node
)
6808 error ("non-constant condition for static assertion");
6809 cxx_constant_value (condition
);
6811 input_location
= saved_loc
;
6815 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6816 suitable for use as a type-specifier.
6818 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6819 id-expression or a class member access, FALSE when it was parsed as
6820 a full expression. */
6823 finish_decltype_type (tree expr
, bool id_expression_or_member_access_p
,
6824 tsubst_flags_t complain
)
6826 tree type
= NULL_TREE
;
6828 if (!expr
|| error_operand_p (expr
))
6829 return error_mark_node
;
6832 || TREE_CODE (expr
) == TYPE_DECL
6833 || (TREE_CODE (expr
) == BIT_NOT_EXPR
6834 && TYPE_P (TREE_OPERAND (expr
, 0))))
6836 if (complain
& tf_error
)
6837 error ("argument to decltype must be an expression");
6838 return error_mark_node
;
6841 /* Depending on the resolution of DR 1172, we may later need to distinguish
6842 instantiation-dependent but not type-dependent expressions so that, say,
6843 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6844 if (instantiation_dependent_expression_p (expr
))
6846 type
= cxx_make_type (DECLTYPE_TYPE
);
6847 DECLTYPE_TYPE_EXPR (type
) = expr
;
6848 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type
)
6849 = id_expression_or_member_access_p
;
6850 SET_TYPE_STRUCTURAL_EQUALITY (type
);
6855 /* The type denoted by decltype(e) is defined as follows: */
6857 expr
= resolve_nondeduced_context (expr
);
6859 if (invalid_nonstatic_memfn_p (expr
, complain
))
6860 return error_mark_node
;
6862 if (type_unknown_p (expr
))
6864 if (complain
& tf_error
)
6865 error ("decltype cannot resolve address of overloaded function");
6866 return error_mark_node
;
6869 /* To get the size of a static data member declared as an array of
6870 unknown bound, we need to instantiate it. */
6872 && VAR_HAD_UNKNOWN_BOUND (expr
)
6873 && DECL_TEMPLATE_INSTANTIATION (expr
))
6874 instantiate_decl (expr
, /*defer_ok*/true, /*expl_inst_mem*/false);
6876 if (id_expression_or_member_access_p
)
6878 /* If e is an id-expression or a class member access (5.2.5
6879 [expr.ref]), decltype(e) is defined as the type of the entity
6880 named by e. If there is no such entity, or e names a set of
6881 overloaded functions, the program is ill-formed. */
6882 if (identifier_p (expr
))
6883 expr
= lookup_name (expr
);
6885 if (INDIRECT_REF_P (expr
))
6886 /* This can happen when the expression is, e.g., "a.b". Just
6887 look at the underlying operand. */
6888 expr
= TREE_OPERAND (expr
, 0);
6890 if (TREE_CODE (expr
) == OFFSET_REF
6891 || TREE_CODE (expr
) == MEMBER_REF
6892 || TREE_CODE (expr
) == SCOPE_REF
)
6893 /* We're only interested in the field itself. If it is a
6894 BASELINK, we will need to see through it in the next
6896 expr
= TREE_OPERAND (expr
, 1);
6898 if (BASELINK_P (expr
))
6899 /* See through BASELINK nodes to the underlying function. */
6900 expr
= BASELINK_FUNCTIONS (expr
);
6902 switch (TREE_CODE (expr
))
6905 if (DECL_BIT_FIELD_TYPE (expr
))
6907 type
= DECL_BIT_FIELD_TYPE (expr
);
6910 /* Fall through for fields that aren't bitfields. */
6917 case TEMPLATE_PARM_INDEX
:
6918 expr
= mark_type_use (expr
);
6919 type
= TREE_TYPE (expr
);
6923 type
= error_mark_node
;
6928 mark_type_use (expr
);
6929 type
= is_bitfield_expr_with_lowered_type (expr
);
6931 type
= TREE_TYPE (TREE_OPERAND (expr
, 1));
6939 /* We can get here when the id-expression refers to an
6940 enumerator or non-type template parameter. */
6941 type
= TREE_TYPE (expr
);
6945 /* Handle instantiated template non-type arguments. */
6946 type
= TREE_TYPE (expr
);
6952 /* Within a lambda-expression:
6954 Every occurrence of decltype((x)) where x is a possibly
6955 parenthesized id-expression that names an entity of
6956 automatic storage duration is treated as if x were
6957 transformed into an access to a corresponding data member
6958 of the closure type that would have been declared if x
6959 were a use of the denoted entity. */
6960 if (outer_automatic_var_p (expr
)
6961 && current_function_decl
6962 && LAMBDA_FUNCTION_P (current_function_decl
))
6963 type
= capture_decltype (expr
);
6964 else if (error_operand_p (expr
))
6965 type
= error_mark_node
;
6966 else if (expr
== current_class_ptr
)
6967 /* If the expression is just "this", we want the
6968 cv-unqualified pointer for the "this" type. */
6969 type
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
6972 /* Otherwise, where T is the type of e, if e is an lvalue,
6973 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
6974 cp_lvalue_kind clk
= lvalue_kind (expr
);
6975 type
= unlowered_expr_type (expr
);
6976 gcc_assert (TREE_CODE (type
) != REFERENCE_TYPE
);
6978 /* For vector types, pick a non-opaque variant. */
6979 if (TREE_CODE (type
) == VECTOR_TYPE
)
6980 type
= strip_typedefs (type
);
6982 if (clk
!= clk_none
&& !(clk
& clk_class
))
6983 type
= cp_build_reference_type (type
, (clk
& clk_rvalueref
));
6987 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (type
))
6989 if (complain
& tf_warning_or_error
)
6990 pedwarn (input_location
, OPT_Wvla
,
6991 "taking decltype of array of runtime bound");
6993 return error_mark_node
;
6999 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7000 __has_nothrow_copy, depending on assign_p. */
7003 classtype_has_nothrow_assign_or_copy_p (tree type
, bool assign_p
)
7010 ix
= lookup_fnfields_1 (type
, ansi_assopname (NOP_EXPR
));
7013 fns
= (*CLASSTYPE_METHOD_VEC (type
))[ix
];
7015 else if (TYPE_HAS_COPY_CTOR (type
))
7017 /* If construction of the copy constructor was postponed, create
7019 if (CLASSTYPE_LAZY_COPY_CTOR (type
))
7020 lazily_declare_fn (sfk_copy_constructor
, type
);
7021 if (CLASSTYPE_LAZY_MOVE_CTOR (type
))
7022 lazily_declare_fn (sfk_move_constructor
, type
);
7023 fns
= CLASSTYPE_CONSTRUCTORS (type
);
7028 for (; fns
; fns
= OVL_NEXT (fns
))
7030 tree fn
= OVL_CURRENT (fns
);
7034 if (copy_fn_p (fn
) == 0)
7037 else if (copy_fn_p (fn
) <= 0)
7040 maybe_instantiate_noexcept (fn
);
7041 if (!TYPE_NOTHROW_P (TREE_TYPE (fn
)))
7048 /* Actually evaluates the trait. */
7051 trait_expr_value (cp_trait_kind kind
, tree type1
, tree type2
)
7053 enum tree_code type_code1
;
7056 type_code1
= TREE_CODE (type1
);
7060 case CPTK_HAS_NOTHROW_ASSIGN
:
7061 type1
= strip_array_types (type1
);
7062 return (!CP_TYPE_CONST_P (type1
) && type_code1
!= REFERENCE_TYPE
7063 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN
, type1
, type2
)
7064 || (CLASS_TYPE_P (type1
)
7065 && classtype_has_nothrow_assign_or_copy_p (type1
,
7068 case CPTK_HAS_TRIVIAL_ASSIGN
:
7069 /* ??? The standard seems to be missing the "or array of such a class
7070 type" wording for this trait. */
7071 type1
= strip_array_types (type1
);
7072 return (!CP_TYPE_CONST_P (type1
) && type_code1
!= REFERENCE_TYPE
7073 && (trivial_type_p (type1
)
7074 || (CLASS_TYPE_P (type1
)
7075 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1
))));
7077 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
7078 type1
= strip_array_types (type1
);
7079 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR
, type1
, type2
)
7080 || (CLASS_TYPE_P (type1
)
7081 && (t
= locate_ctor (type1
))
7082 && (maybe_instantiate_noexcept (t
),
7083 TYPE_NOTHROW_P (TREE_TYPE (t
)))));
7085 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
7086 type1
= strip_array_types (type1
);
7087 return (trivial_type_p (type1
)
7088 || (CLASS_TYPE_P (type1
) && TYPE_HAS_TRIVIAL_DFLT (type1
)));
7090 case CPTK_HAS_NOTHROW_COPY
:
7091 type1
= strip_array_types (type1
);
7092 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY
, type1
, type2
)
7093 || (CLASS_TYPE_P (type1
)
7094 && classtype_has_nothrow_assign_or_copy_p (type1
, false)));
7096 case CPTK_HAS_TRIVIAL_COPY
:
7097 /* ??? The standard seems to be missing the "or array of such a class
7098 type" wording for this trait. */
7099 type1
= strip_array_types (type1
);
7100 return (trivial_type_p (type1
) || type_code1
== REFERENCE_TYPE
7101 || (CLASS_TYPE_P (type1
) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1
)));
7103 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
7104 type1
= strip_array_types (type1
);
7105 return (trivial_type_p (type1
) || type_code1
== REFERENCE_TYPE
7106 || (CLASS_TYPE_P (type1
)
7107 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1
)));
7109 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
7110 return type_has_virtual_destructor (type1
);
7112 case CPTK_IS_ABSTRACT
:
7113 return (ABSTRACT_CLASS_TYPE_P (type1
));
7115 case CPTK_IS_BASE_OF
:
7116 return (NON_UNION_CLASS_TYPE_P (type1
) && NON_UNION_CLASS_TYPE_P (type2
)
7117 && DERIVED_FROM_P (type1
, type2
));
7120 return (NON_UNION_CLASS_TYPE_P (type1
));
7122 case CPTK_IS_CONVERTIBLE_TO
:
7127 return (NON_UNION_CLASS_TYPE_P (type1
) && CLASSTYPE_EMPTY_P (type1
));
7130 return (type_code1
== ENUMERAL_TYPE
);
7133 return (CLASS_TYPE_P (type1
) && CLASSTYPE_FINAL (type1
));
7135 case CPTK_IS_LITERAL_TYPE
:
7136 return (literal_type_p (type1
));
7139 return (pod_type_p (type1
));
7141 case CPTK_IS_POLYMORPHIC
:
7142 return (CLASS_TYPE_P (type1
) && TYPE_POLYMORPHIC_P (type1
));
7144 case CPTK_IS_STD_LAYOUT
:
7145 return (std_layout_type_p (type1
));
7147 case CPTK_IS_TRIVIAL
:
7148 return (trivial_type_p (type1
));
7151 return (type_code1
== UNION_TYPE
);
7159 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7160 void, or a complete type, returns it, otherwise NULL_TREE. */
7163 check_trait_type (tree type
)
7165 if (TREE_CODE (type
) == ARRAY_TYPE
&& !TYPE_DOMAIN (type
)
7166 && COMPLETE_TYPE_P (TREE_TYPE (type
)))
7169 if (VOID_TYPE_P (type
))
7172 return complete_type_or_else (strip_array_types (type
), NULL_TREE
);
7175 /* Process a trait expression. */
7178 finish_trait_expr (cp_trait_kind kind
, tree type1
, tree type2
)
7180 gcc_assert (kind
== CPTK_HAS_NOTHROW_ASSIGN
7181 || kind
== CPTK_HAS_NOTHROW_CONSTRUCTOR
7182 || kind
== CPTK_HAS_NOTHROW_COPY
7183 || kind
== CPTK_HAS_TRIVIAL_ASSIGN
7184 || kind
== CPTK_HAS_TRIVIAL_CONSTRUCTOR
7185 || kind
== CPTK_HAS_TRIVIAL_COPY
7186 || kind
== CPTK_HAS_TRIVIAL_DESTRUCTOR
7187 || kind
== CPTK_HAS_VIRTUAL_DESTRUCTOR
7188 || kind
== CPTK_IS_ABSTRACT
7189 || kind
== CPTK_IS_BASE_OF
7190 || kind
== CPTK_IS_CLASS
7191 || kind
== CPTK_IS_CONVERTIBLE_TO
7192 || kind
== CPTK_IS_EMPTY
7193 || kind
== CPTK_IS_ENUM
7194 || kind
== CPTK_IS_FINAL
7195 || kind
== CPTK_IS_LITERAL_TYPE
7196 || kind
== CPTK_IS_POD
7197 || kind
== CPTK_IS_POLYMORPHIC
7198 || kind
== CPTK_IS_STD_LAYOUT
7199 || kind
== CPTK_IS_TRIVIAL
7200 || kind
== CPTK_IS_UNION
);
7202 if (kind
== CPTK_IS_CONVERTIBLE_TO
)
7204 sorry ("__is_convertible_to");
7205 return error_mark_node
;
7208 if (type1
== error_mark_node
7209 || ((kind
== CPTK_IS_BASE_OF
|| kind
== CPTK_IS_CONVERTIBLE_TO
)
7210 && type2
== error_mark_node
))
7211 return error_mark_node
;
7213 if (processing_template_decl
)
7215 tree trait_expr
= make_node (TRAIT_EXPR
);
7216 TREE_TYPE (trait_expr
) = boolean_type_node
;
7217 TRAIT_EXPR_TYPE1 (trait_expr
) = type1
;
7218 TRAIT_EXPR_TYPE2 (trait_expr
) = type2
;
7219 TRAIT_EXPR_KIND (trait_expr
) = kind
;
7225 case CPTK_HAS_NOTHROW_ASSIGN
:
7226 case CPTK_HAS_TRIVIAL_ASSIGN
:
7227 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
7228 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
7229 case CPTK_HAS_NOTHROW_COPY
:
7230 case CPTK_HAS_TRIVIAL_COPY
:
7231 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
7232 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
7233 case CPTK_IS_ABSTRACT
:
7236 case CPTK_IS_LITERAL_TYPE
:
7238 case CPTK_IS_POLYMORPHIC
:
7239 case CPTK_IS_STD_LAYOUT
:
7240 case CPTK_IS_TRIVIAL
:
7241 if (!check_trait_type (type1
))
7242 return error_mark_node
;
7245 case CPTK_IS_BASE_OF
:
7246 if (NON_UNION_CLASS_TYPE_P (type1
) && NON_UNION_CLASS_TYPE_P (type2
)
7247 && !same_type_ignoring_top_level_qualifiers_p (type1
, type2
)
7248 && !complete_type_or_else (type2
, NULL_TREE
))
7249 /* We already issued an error. */
7250 return error_mark_node
;
7258 case CPTK_IS_CONVERTIBLE_TO
:
7263 return (trait_expr_value (kind
, type1
, type2
)
7264 ? boolean_true_node
: boolean_false_node
);
7267 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7268 which is ignored for C++. */
7271 set_float_const_decimal64 (void)
7276 clear_float_const_decimal64 (void)
7281 float_const_decimal64_p (void)
7287 /* Return true if T is a literal type. */
7290 literal_type_p (tree t
)
7292 if (SCALAR_TYPE_P (t
)
7293 || TREE_CODE (t
) == VECTOR_TYPE
7294 || TREE_CODE (t
) == REFERENCE_TYPE
)
7296 if (CLASS_TYPE_P (t
))
7298 t
= complete_type (t
);
7299 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
7300 return CLASSTYPE_LITERAL_P (t
);
7302 if (TREE_CODE (t
) == ARRAY_TYPE
)
7303 return literal_type_p (strip_array_types (t
));
7307 /* If DECL is a variable declared `constexpr', require its type
7308 be literal. Return the DECL if OK, otherwise NULL. */
7311 ensure_literal_type_for_constexpr_object (tree decl
)
7313 tree type
= TREE_TYPE (decl
);
7314 if (VAR_P (decl
) && DECL_DECLARED_CONSTEXPR_P (decl
)
7315 && !processing_template_decl
)
7317 if (CLASS_TYPE_P (type
) && !COMPLETE_TYPE_P (complete_type (type
)))
7318 /* Don't complain here, we'll complain about incompleteness
7319 when we try to initialize the variable. */;
7320 else if (!literal_type_p (type
))
7322 error ("the type %qT of constexpr variable %qD is not literal",
7324 explain_non_literal_class (type
);
7331 /* Representation of entries in the constexpr function definition table. */
7333 typedef struct GTY(()) constexpr_fundef
{
7338 /* This table holds all constexpr function definitions seen in
7339 the current translation unit. */
7341 static GTY ((param_is (constexpr_fundef
))) htab_t constexpr_fundef_table
;
7343 /* Utility function used for managing the constexpr function table.
7344 Return true if the entries pointed to by P and Q are for the
7345 same constexpr function. */
7348 constexpr_fundef_equal (const void *p
, const void *q
)
7350 const constexpr_fundef
*lhs
= (const constexpr_fundef
*) p
;
7351 const constexpr_fundef
*rhs
= (const constexpr_fundef
*) q
;
7352 return lhs
->decl
== rhs
->decl
;
7355 /* Utility function used for managing the constexpr function table.
7356 Return a hash value for the entry pointed to by Q. */
7358 static inline hashval_t
7359 constexpr_fundef_hash (const void *p
)
7361 const constexpr_fundef
*fundef
= (const constexpr_fundef
*) p
;
7362 return DECL_UID (fundef
->decl
);
7365 /* Return a previously saved definition of function FUN. */
7367 static constexpr_fundef
*
7368 retrieve_constexpr_fundef (tree fun
)
7370 constexpr_fundef fundef
= { NULL
, NULL
};
7371 if (constexpr_fundef_table
== NULL
)
7375 return (constexpr_fundef
*) htab_find (constexpr_fundef_table
, &fundef
);
7378 /* Check whether the parameter and return types of FUN are valid for a
7379 constexpr function, and complain if COMPLAIN. */
7382 is_valid_constexpr_fn (tree fun
, bool complain
)
7384 tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
7386 for (; parm
!= NULL
; parm
= TREE_CHAIN (parm
))
7387 if (!literal_type_p (TREE_TYPE (parm
)))
7392 error ("invalid type for parameter %d of constexpr "
7393 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
7394 explain_non_literal_class (TREE_TYPE (parm
));
7398 if (!DECL_CONSTRUCTOR_P (fun
))
7400 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
7401 if (!literal_type_p (rettype
))
7406 error ("invalid return type %qT of constexpr function %q+D",
7408 explain_non_literal_class (rettype
);
7412 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
7413 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
7418 error ("enclosing class of constexpr non-static member "
7419 "function %q+#D is not a literal type", fun
);
7420 explain_non_literal_class (DECL_CONTEXT (fun
));
7424 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
7428 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
7434 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7435 for a member of an anonymous aggregate, INIT is the initializer for that
7436 member, and VEC_OUTER is the vector of constructor elements for the class
7437 whose constructor we are processing. Add the initializer to the vector
7438 and return true to indicate success. */
7441 build_anon_member_initialization (tree member
, tree init
,
7442 vec
<constructor_elt
, va_gc
> **vec_outer
)
7444 /* MEMBER presents the relevant fields from the inside out, but we need
7445 to build up the initializer from the outside in so that we can reuse
7446 previously built CONSTRUCTORs if this is, say, the second field in an
7447 anonymous struct. So we use a vec as a stack. */
7448 stack_vec
<tree
, 2> fields
;
7451 fields
.safe_push (TREE_OPERAND (member
, 1));
7452 member
= TREE_OPERAND (member
, 0);
7454 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
)));
7456 /* VEC has the constructor elements vector for the context of FIELD.
7457 If FIELD is an anonymous aggregate, we will push inside it. */
7458 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
7460 while (field
= fields
.pop(),
7461 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
7464 /* If there is already an outer constructor entry for the anonymous
7465 aggregate FIELD, use it; otherwise, insert one. */
7466 if (vec_safe_is_empty (*vec
)
7467 || (*vec
)->last().index
!= field
)
7469 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
7470 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
7473 ctor
= (*vec
)->last().value
;
7474 vec
= &CONSTRUCTOR_ELTS (ctor
);
7477 /* Now we're at the innermost field, the one that isn't an anonymous
7478 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7479 gcc_assert (fields
.is_empty());
7480 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
7485 /* Subroutine of build_constexpr_constructor_member_initializers.
7486 The expression tree T represents a data member initialization
7487 in a (constexpr) constructor definition. Build a pairing of
7488 the data member with its initializer, and prepend that pair
7489 to the existing initialization pair INITS. */
7492 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
7495 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
7496 t
= TREE_OPERAND (t
, 0);
7497 if (TREE_CODE (t
) == EXPR_STMT
)
7498 t
= TREE_OPERAND (t
, 0);
7499 if (t
== error_mark_node
)
7501 if (TREE_CODE (t
) == STATEMENT_LIST
)
7503 tree_stmt_iterator i
;
7504 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
7506 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
7511 if (TREE_CODE (t
) == CLEANUP_STMT
)
7513 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7514 but we can in a constexpr constructor for a non-literal class. Just
7515 ignore it; either all the initialization will be constant, in which
7516 case the cleanup can't run, or it can't be constexpr.
7517 Still recurse into CLEANUP_BODY. */
7518 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
7520 if (TREE_CODE (t
) == CONVERT_EXPR
)
7521 t
= TREE_OPERAND (t
, 0);
7522 if (TREE_CODE (t
) == INIT_EXPR
7523 || TREE_CODE (t
) == MODIFY_EXPR
)
7525 member
= TREE_OPERAND (t
, 0);
7526 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
7528 else if (TREE_CODE (t
) == CALL_EXPR
)
7530 member
= CALL_EXPR_ARG (t
, 0);
7531 /* We don't use build_cplus_new here because it complains about
7532 abstract bases. Leaving the call unwrapped means that it has the
7533 wrong type, but cxx_eval_constant_expression doesn't care. */
7534 init
= break_out_target_exprs (t
);
7536 else if (TREE_CODE (t
) == DECL_EXPR
)
7537 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7541 if (INDIRECT_REF_P (member
))
7542 member
= TREE_OPERAND (member
, 0);
7543 if (TREE_CODE (member
) == NOP_EXPR
)
7547 if (TREE_CODE (op
) == ADDR_EXPR
)
7549 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7550 (TREE_TYPE (TREE_TYPE (op
)),
7551 TREE_TYPE (TREE_TYPE (member
))));
7552 /* Initializing a cv-qualified member; we need to look through
7556 else if (op
== current_class_ptr
7557 && (same_type_ignoring_top_level_qualifiers_p
7558 (TREE_TYPE (TREE_TYPE (member
)),
7559 current_class_type
)))
7560 /* Delegating constructor. */
7564 /* This is an initializer for an empty base; keep it for now so
7565 we can check it in cxx_eval_bare_aggregate. */
7566 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
7569 if (TREE_CODE (member
) == ADDR_EXPR
)
7570 member
= TREE_OPERAND (member
, 0);
7571 if (TREE_CODE (member
) == COMPONENT_REF
)
7573 tree aggr
= TREE_OPERAND (member
, 0);
7574 if (TREE_CODE (aggr
) != COMPONENT_REF
)
7575 /* Normal member initialization. */
7576 member
= TREE_OPERAND (member
, 1);
7577 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
7578 /* Initializing a member of an anonymous union. */
7579 return build_anon_member_initialization (member
, init
, vec
);
7581 /* We're initializing a vtable pointer in a base. Leave it as
7582 COMPONENT_REF so we remember the path to get to the vfield. */
7583 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
7586 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
7590 /* Make sure that there are no statements after LAST in the constructor
7591 body represented by LIST. */
7594 check_constexpr_ctor_body (tree last
, tree list
)
7597 if (TREE_CODE (list
) == STATEMENT_LIST
)
7599 tree_stmt_iterator i
= tsi_last (list
);
7600 for (; !tsi_end_p (i
); tsi_prev (&i
))
7602 tree t
= tsi_stmt (i
);
7605 if (TREE_CODE (t
) == BIND_EXPR
)
7607 if (!check_constexpr_ctor_body (last
, BIND_EXPR_BODY (t
)))
7612 /* We currently allow typedefs and static_assert.
7613 FIXME allow them in the standard, too. */
7614 if (TREE_CODE (t
) != STATIC_ASSERT
)
7621 else if (list
!= last
7622 && TREE_CODE (list
) != STATIC_ASSERT
)
7626 error ("constexpr constructor does not have empty body");
7627 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
7632 /* V is a vector of constructor elements built up for the base and member
7633 initializers of a constructor for TYPE. They need to be in increasing
7634 offset order, which they might not be yet if TYPE has a primary base
7635 which is not first in the base-clause or a vptr and at least one base
7636 all of which are non-primary. */
7638 static vec
<constructor_elt
, va_gc
> *
7639 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
7641 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
7643 constructor_elt elt
;
7647 field_type
= BINFO_TYPE (pri
);
7648 else if (TYPE_CONTAINS_VPTR_P (type
))
7649 field_type
= vtbl_ptr_type_node
;
7653 /* Find the element for the primary base or vptr and move it to the
7654 beginning of the vec. */
7655 vec
<constructor_elt
, va_gc
> &vref
= *v
;
7657 if (TREE_TYPE (vref
[i
].index
) == field_type
)
7664 vref
[i
] = vref
[i
-1];
7671 /* Build compile-time evalable representations of member-initializer list
7672 for a constexpr constructor. */
7675 build_constexpr_constructor_member_initializers (tree type
, tree body
)
7677 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
7679 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
7680 || TREE_CODE (body
) == EH_SPEC_BLOCK
)
7681 body
= TREE_OPERAND (body
, 0);
7682 if (TREE_CODE (body
) == STATEMENT_LIST
)
7683 body
= STATEMENT_LIST_HEAD (body
)->stmt
;
7684 body
= BIND_EXPR_BODY (body
);
7685 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
7687 body
= TREE_OPERAND (body
, 0);
7688 if (TREE_CODE (body
) == EXPR_STMT
)
7689 body
= TREE_OPERAND (body
, 0);
7690 if (TREE_CODE (body
) == INIT_EXPR
7691 && (same_type_ignoring_top_level_qualifiers_p
7692 (TREE_TYPE (TREE_OPERAND (body
, 0)),
7693 current_class_type
)))
7696 return TREE_OPERAND (body
, 1);
7698 ok
= build_data_member_initialization (body
, &vec
);
7700 else if (TREE_CODE (body
) == STATEMENT_LIST
)
7702 tree_stmt_iterator i
;
7703 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
7705 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
7710 else if (TREE_CODE (body
) == TRY_BLOCK
)
7712 error ("body of %<constexpr%> constructor cannot be "
7713 "a function-try-block");
7714 return error_mark_node
;
7716 else if (EXPR_P (body
))
7717 ok
= build_data_member_initialization (body
, &vec
);
7719 gcc_assert (errorcount
> 0);
7722 if (vec_safe_length (vec
) > 0)
7724 /* In a delegating constructor, return the target. */
7725 constructor_elt
*ce
= &(*vec
)[0];
7726 if (ce
->index
== current_class_ptr
)
7733 vec
= sort_constexpr_mem_initializers (type
, vec
);
7734 return build_constructor (type
, vec
);
7737 return error_mark_node
;
7740 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7741 declared to be constexpr, or a sub-statement thereof. Returns the
7742 return value if suitable, error_mark_node for a statement not allowed in
7743 a constexpr function, or NULL_TREE if no return value was found. */
7746 constexpr_fn_retval (tree body
)
7748 switch (TREE_CODE (body
))
7750 case STATEMENT_LIST
:
7752 tree_stmt_iterator i
;
7753 tree expr
= NULL_TREE
;
7754 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
7756 tree s
= constexpr_fn_retval (tsi_stmt (i
));
7757 if (s
== error_mark_node
)
7758 return error_mark_node
;
7759 else if (s
== NULL_TREE
)
7760 /* Keep iterating. */;
7762 /* Multiple return statements. */
7763 return error_mark_node
;
7771 return break_out_target_exprs (TREE_OPERAND (body
, 0));
7774 if (TREE_CODE (DECL_EXPR_DECL (body
)) == USING_DECL
)
7776 return error_mark_node
;
7778 case CLEANUP_POINT_EXPR
:
7779 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
7785 return error_mark_node
;
7789 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7790 FUN; do the necessary transformations to turn it into a single expression
7791 that we can store in the hash table. */
7794 massage_constexpr_body (tree fun
, tree body
)
7796 if (DECL_CONSTRUCTOR_P (fun
))
7797 body
= build_constexpr_constructor_member_initializers
7798 (DECL_CONTEXT (fun
), body
);
7801 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
7802 body
= EH_SPEC_STMTS (body
);
7803 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
7804 body
= TREE_OPERAND (body
, 0);
7805 if (TREE_CODE (body
) == BIND_EXPR
)
7806 body
= BIND_EXPR_BODY (body
);
7807 body
= constexpr_fn_retval (body
);
7812 /* FUN is a constexpr constructor with massaged body BODY. Return true
7813 if some bases/fields are uninitialized, and complain if COMPLAIN. */
7816 cx_check_missing_mem_inits (tree fun
, tree body
, bool complain
)
7823 if (TREE_CODE (body
) != CONSTRUCTOR
)
7826 nelts
= CONSTRUCTOR_NELTS (body
);
7827 ctype
= DECL_CONTEXT (fun
);
7828 field
= TYPE_FIELDS (ctype
);
7830 if (TREE_CODE (ctype
) == UNION_TYPE
)
7832 if (nelts
== 0 && next_initializable_field (field
))
7835 error ("%<constexpr%> constructor for union %qT must "
7836 "initialize exactly one non-static data member", ctype
);
7843 for (i
= 0; i
<= nelts
; ++i
)
7850 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
7851 /* Skip base and vtable inits. */
7852 if (TREE_CODE (index
) != FIELD_DECL
7853 || DECL_ARTIFICIAL (index
))
7856 for (; field
!= index
; field
= DECL_CHAIN (field
))
7859 if (TREE_CODE (field
) != FIELD_DECL
7860 || (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
7861 || DECL_ARTIFICIAL (field
))
7863 ftype
= strip_array_types (TREE_TYPE (field
));
7864 if (type_has_constexpr_default_constructor (ftype
))
7866 /* It's OK to skip a member with a trivial constexpr ctor.
7867 A constexpr ctor that isn't trivial should have been
7869 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
7870 || errorcount
!= 0);
7875 error ("uninitialized member %qD in %<constexpr%> constructor",
7879 if (field
== NULL_TREE
)
7881 field
= DECL_CHAIN (field
);
7887 /* We are processing the definition of the constexpr function FUN.
7888 Check that its BODY fulfills the propriate requirements and
7889 enter it in the constexpr function definition table.
7890 For constructor BODY is actually the TREE_LIST of the
7891 member-initializer list. */
7894 register_constexpr_fundef (tree fun
, tree body
)
7896 constexpr_fundef entry
;
7897 constexpr_fundef
**slot
;
7899 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
7902 body
= massage_constexpr_body (fun
, body
);
7903 if (body
== NULL_TREE
|| body
== error_mark_node
)
7905 if (!DECL_CONSTRUCTOR_P (fun
))
7906 error ("body of constexpr function %qD not a return-statement", fun
);
7910 if (!potential_rvalue_constant_expression (body
))
7912 if (!DECL_GENERATED_P (fun
))
7913 require_potential_rvalue_constant_expression (body
);
7917 if (DECL_CONSTRUCTOR_P (fun
)
7918 && cx_check_missing_mem_inits (fun
, body
, !DECL_GENERATED_P (fun
)))
7921 /* Create the constexpr function table if necessary. */
7922 if (constexpr_fundef_table
== NULL
)
7923 constexpr_fundef_table
= htab_create_ggc (101,
7924 constexpr_fundef_hash
,
7925 constexpr_fundef_equal
,
7929 slot
= (constexpr_fundef
**)
7930 htab_find_slot (constexpr_fundef_table
, &entry
, INSERT
);
7932 gcc_assert (*slot
== NULL
);
7933 *slot
= ggc_alloc_constexpr_fundef ();
7939 /* FUN is a non-constexpr function called in a context that requires a
7940 constant expression. If it comes from a constexpr template, explain why
7941 the instantiation isn't constexpr. */
7944 explain_invalid_constexpr_fn (tree fun
)
7946 static struct pointer_set_t
*diagnosed
;
7948 location_t save_loc
;
7949 /* Only diagnose defaulted functions or instantiations. */
7950 if (!DECL_DEFAULTED_FN (fun
)
7951 && !is_instantiation_of_constexpr (fun
))
7953 if (diagnosed
== NULL
)
7954 diagnosed
= pointer_set_create ();
7955 if (pointer_set_insert (diagnosed
, fun
) != 0)
7956 /* Already explained. */
7959 save_loc
= input_location
;
7960 input_location
= DECL_SOURCE_LOCATION (fun
);
7961 inform (0, "%q+D is not usable as a constexpr function because:", fun
);
7962 /* First check the declaration. */
7963 if (is_valid_constexpr_fn (fun
, true))
7965 /* Then if it's OK, the body. */
7966 if (DECL_DEFAULTED_FN (fun
))
7967 explain_implicit_non_constexpr (fun
);
7970 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
7971 require_potential_rvalue_constant_expression (body
);
7972 if (DECL_CONSTRUCTOR_P (fun
))
7973 cx_check_missing_mem_inits (fun
, body
, true);
7976 input_location
= save_loc
;
7979 /* Objects of this type represent calls to constexpr functions
7980 along with the bindings of parameters to their arguments, for
7981 the purpose of compile time evaluation. */
7983 typedef struct GTY(()) constexpr_call
{
7984 /* Description of the constexpr function definition. */
7985 constexpr_fundef
*fundef
;
7986 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
7987 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
7988 Note: This arrangement is made to accommodate the use of
7989 iterative_hash_template_arg (see pt.c). If you change this
7990 representation, also change the hash calculation in
7991 cxx_eval_call_expression. */
7993 /* Result of the call.
7994 NULL means the call is being evaluated.
7995 error_mark_node means that the evaluation was erroneous;
7996 otherwise, the actuall value of the call. */
7998 /* The hash of this call; we remember it here to avoid having to
7999 recalculate it when expanding the hash table. */
8003 /* A table of all constexpr calls that have been evaluated by the
8004 compiler in this translation unit. */
8006 static GTY ((param_is (constexpr_call
))) htab_t constexpr_call_table
;
8008 static tree
cxx_eval_constant_expression (const constexpr_call
*, tree
,
8009 bool, bool, bool *, bool *);
8011 /* Compute a hash value for a constexpr call representation. */
8014 constexpr_call_hash (const void *p
)
8016 const constexpr_call
*info
= (const constexpr_call
*) p
;
8020 /* Return 1 if the objects pointed to by P and Q represent calls
8021 to the same constexpr function with the same arguments.
8022 Otherwise, return 0. */
8025 constexpr_call_equal (const void *p
, const void *q
)
8027 const constexpr_call
*lhs
= (const constexpr_call
*) p
;
8028 const constexpr_call
*rhs
= (const constexpr_call
*) q
;
8033 if (!constexpr_fundef_equal (lhs
->fundef
, rhs
->fundef
))
8035 lhs_bindings
= lhs
->bindings
;
8036 rhs_bindings
= rhs
->bindings
;
8037 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
8039 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
8040 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
8041 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
8042 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
8044 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
8045 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
8047 return lhs_bindings
== rhs_bindings
;
8050 /* Initialize the constexpr call table, if needed. */
8053 maybe_initialize_constexpr_call_table (void)
8055 if (constexpr_call_table
== NULL
)
8056 constexpr_call_table
= htab_create_ggc (101,
8057 constexpr_call_hash
,
8058 constexpr_call_equal
,
8062 /* Return true if T designates the implied `this' parameter. */
8065 is_this_parameter (tree t
)
8067 return t
== current_class_ptr
;
8070 /* We have an expression tree T that represents a call, either CALL_EXPR
8071 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8072 retrun the _DECL for that function. */
8075 get_function_named_in_call (tree t
)
8078 switch (TREE_CODE (t
))
8081 fun
= CALL_EXPR_FN (t
);
8084 case AGGR_INIT_EXPR
:
8085 fun
= AGGR_INIT_EXPR_FN (t
);
8092 if (TREE_CODE (fun
) == ADDR_EXPR
8093 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
8094 fun
= TREE_OPERAND (fun
, 0);
8098 /* We have an expression tree T that represents a call, either CALL_EXPR
8099 or AGGR_INIT_EXPR. Return the Nth argument. */
8102 get_nth_callarg (tree t
, int n
)
8104 switch (TREE_CODE (t
))
8107 return CALL_EXPR_ARG (t
, n
);
8109 case AGGR_INIT_EXPR
:
8110 return AGGR_INIT_EXPR_ARG (t
, n
);
8118 /* Look up the binding of the function parameter T in a constexpr
8119 function call context CALL. */
8122 lookup_parameter_binding (const constexpr_call
*call
, tree t
)
8124 tree b
= purpose_member (t
, call
->bindings
);
8125 return TREE_VALUE (b
);
8128 /* Attempt to evaluate T which represents a call to a builtin function.
8129 We assume here that all builtin functions evaluate to scalar types
8130 represented by _CST nodes. */
8133 cxx_eval_builtin_function_call (const constexpr_call
*call
, tree t
,
8134 bool allow_non_constant
, bool addr
,
8135 bool *non_constant_p
, bool *overflow_p
)
8137 const int nargs
= call_expr_nargs (t
);
8138 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
8141 for (i
= 0; i
< nargs
; ++i
)
8143 args
[i
] = cxx_eval_constant_expression (call
, CALL_EXPR_ARG (t
, i
),
8144 allow_non_constant
, addr
,
8145 non_constant_p
, overflow_p
);
8146 if (allow_non_constant
&& *non_constant_p
)
8149 if (*non_constant_p
)
8151 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
8152 CALL_EXPR_FN (t
), nargs
, args
);
8153 new_call
= fold (new_call
);
8154 VERIFY_CONSTANT (new_call
);
8158 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8159 the type of the value to match. */
8162 adjust_temp_type (tree type
, tree temp
)
8164 if (TREE_TYPE (temp
) == type
)
8166 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8167 if (TREE_CODE (temp
) == CONSTRUCTOR
)
8168 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
8169 gcc_assert (scalarish_type_p (type
));
8170 return cp_fold_convert (type
, temp
);
8173 /* Subroutine of cxx_eval_call_expression.
8174 We are processing a call expression (either CALL_EXPR or
8175 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8176 all arguments and bind their values to correspondings
8177 parameters, making up the NEW_CALL context. */
8180 cxx_bind_parameters_in_call (const constexpr_call
*old_call
, tree t
,
8181 constexpr_call
*new_call
,
8182 bool allow_non_constant
,
8183 bool *non_constant_p
, bool *overflow_p
)
8185 const int nargs
= call_expr_nargs (t
);
8186 tree fun
= new_call
->fundef
->decl
;
8187 tree parms
= DECL_ARGUMENTS (fun
);
8189 for (i
= 0; i
< nargs
; ++i
)
8192 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
8193 /* For member function, the first argument is a pointer to the implied
8194 object. And for an object construction, don't bind `this' before
8195 it is fully constructed. */
8196 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
))
8198 x
= get_nth_callarg (t
, i
);
8199 if (parms
&& DECL_BY_REFERENCE (parms
))
8201 /* cp_genericize made this a reference for argument passing, but
8202 we don't want to treat it like one for constexpr evaluation. */
8203 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
8204 gcc_assert (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
);
8205 type
= TREE_TYPE (type
);
8206 x
= convert_from_reference (x
);
8208 arg
= cxx_eval_constant_expression (old_call
, x
, allow_non_constant
,
8209 TREE_CODE (type
) == REFERENCE_TYPE
,
8210 non_constant_p
, overflow_p
);
8211 /* Don't VERIFY_CONSTANT here. */
8212 if (*non_constant_p
&& allow_non_constant
)
8214 /* Just discard ellipsis args after checking their constantitude. */
8217 if (*non_constant_p
)
8218 /* Don't try to adjust the type of non-constant args. */
8221 /* Make sure the binding has the same type as the parm. */
8222 if (TREE_CODE (type
) != REFERENCE_TYPE
)
8223 arg
= adjust_temp_type (type
, arg
);
8224 new_call
->bindings
= tree_cons (parms
, arg
, new_call
->bindings
);
8226 parms
= TREE_CHAIN (parms
);
8230 /* Variables and functions to manage constexpr call expansion context.
8231 These do not need to be marked for PCH or GC. */
8233 /* FIXME remember and print actual constant arguments. */
8234 static vec
<tree
> call_stack
= vNULL
;
8235 static int call_stack_tick
;
8236 static int last_cx_error_tick
;
8239 push_cx_call_context (tree call
)
8242 if (!EXPR_HAS_LOCATION (call
))
8243 SET_EXPR_LOCATION (call
, input_location
);
8244 call_stack
.safe_push (call
);
8245 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
8251 pop_cx_call_context (void)
8258 cx_error_context (void)
8260 vec
<tree
> r
= vNULL
;
8261 if (call_stack_tick
!= last_cx_error_tick
8262 && !call_stack
.is_empty ())
8264 last_cx_error_tick
= call_stack_tick
;
8268 /* Subroutine of cxx_eval_constant_expression.
8269 Evaluate the call expression tree T in the context of OLD_CALL expression
8273 cxx_eval_call_expression (const constexpr_call
*old_call
, tree t
,
8274 bool allow_non_constant
, bool addr
,
8275 bool *non_constant_p
, bool *overflow_p
)
8277 location_t loc
= EXPR_LOC_OR_HERE (t
);
8278 tree fun
= get_function_named_in_call (t
);
8280 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
8281 constexpr_call
**slot
;
8282 constexpr_call
*entry
;
8285 if (TREE_CODE (fun
) != FUNCTION_DECL
)
8287 /* Might be a constexpr function pointer. */
8288 fun
= cxx_eval_constant_expression (old_call
, fun
, allow_non_constant
,
8289 /*addr*/false, non_constant_p
, overflow_p
);
8290 if (TREE_CODE (fun
) == ADDR_EXPR
)
8291 fun
= TREE_OPERAND (fun
, 0);
8293 if (TREE_CODE (fun
) != FUNCTION_DECL
)
8295 if (!allow_non_constant
&& !*non_constant_p
)
8296 error_at (loc
, "expression %qE does not designate a constexpr "
8298 *non_constant_p
= true;
8301 if (DECL_CLONED_FUNCTION_P (fun
))
8302 fun
= DECL_CLONED_FUNCTION (fun
);
8303 if (is_builtin_fn (fun
))
8304 return cxx_eval_builtin_function_call (old_call
, t
, allow_non_constant
,
8305 addr
, non_constant_p
, overflow_p
);
8306 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
8308 if (!allow_non_constant
)
8310 error_at (loc
, "call to non-constexpr function %qD", fun
);
8311 explain_invalid_constexpr_fn (fun
);
8313 *non_constant_p
= true;
8317 /* Shortcut trivial constructor/op=. */
8318 if (trivial_fn_p (fun
))
8320 if (call_expr_nargs (t
) == 2)
8322 tree arg
= convert_from_reference (get_nth_callarg (t
, 1));
8323 return cxx_eval_constant_expression (old_call
, arg
, allow_non_constant
,
8324 addr
, non_constant_p
, overflow_p
);
8326 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
8327 && AGGR_INIT_ZERO_FIRST (t
))
8328 return build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
8331 /* If in direct recursive call, optimize definition search. */
8332 if (old_call
!= NULL
&& old_call
->fundef
->decl
== fun
)
8333 new_call
.fundef
= old_call
->fundef
;
8336 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
8337 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
)
8339 if (!allow_non_constant
)
8341 if (DECL_INITIAL (fun
))
8343 /* The definition of fun was somehow unsuitable. */
8344 error_at (loc
, "%qD called in a constant expression", fun
);
8345 explain_invalid_constexpr_fn (fun
);
8348 error_at (loc
, "%qD used before its definition", fun
);
8350 *non_constant_p
= true;
8354 cxx_bind_parameters_in_call (old_call
, t
, &new_call
,
8355 allow_non_constant
, non_constant_p
, overflow_p
);
8356 if (*non_constant_p
)
8359 depth_ok
= push_cx_call_context (t
);
8362 = iterative_hash_template_arg (new_call
.bindings
,
8363 constexpr_fundef_hash (new_call
.fundef
));
8365 /* If we have seen this call before, we are done. */
8366 maybe_initialize_constexpr_call_table ();
8367 slot
= (constexpr_call
**)
8368 htab_find_slot (constexpr_call_table
, &new_call
, INSERT
);
8372 /* We need to keep a pointer to the entry, not just the slot, as the
8373 slot can move in the call to cxx_eval_builtin_function_call. */
8374 *slot
= entry
= ggc_alloc_constexpr_call ();
8377 /* Calls which are in progress have their result set to NULL
8378 so that we can detect circular dependencies. */
8379 else if (entry
->result
== NULL
)
8381 if (!allow_non_constant
)
8382 error ("call has circular dependency");
8383 *non_constant_p
= true;
8384 entry
->result
= result
= error_mark_node
;
8389 if (!allow_non_constant
)
8390 error ("constexpr evaluation depth exceeds maximum of %d (use "
8391 "-fconstexpr-depth= to increase the maximum)",
8392 max_constexpr_depth
);
8393 *non_constant_p
= true;
8394 entry
->result
= result
= error_mark_node
;
8398 result
= entry
->result
;
8399 if (!result
|| result
== error_mark_node
)
8400 result
= (cxx_eval_constant_expression
8401 (&new_call
, new_call
.fundef
->body
,
8402 allow_non_constant
, addr
,
8403 non_constant_p
, overflow_p
));
8404 if (result
== error_mark_node
)
8405 *non_constant_p
= true;
8406 if (*non_constant_p
)
8407 entry
->result
= result
= error_mark_node
;
8410 /* If this was a call to initialize an object, set the type of
8411 the CONSTRUCTOR to the type of that object. */
8412 if (DECL_CONSTRUCTOR_P (fun
))
8414 tree ob_arg
= get_nth_callarg (t
, 0);
8415 STRIP_NOPS (ob_arg
);
8416 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg
))
8417 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg
))));
8418 result
= adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg
)),
8421 entry
->result
= result
;
8425 pop_cx_call_context ();
8426 return unshare_expr (result
);
8429 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8432 reduced_constant_expression_p (tree t
)
8434 if (TREE_CODE (t
) == PTRMEM_CST
)
8435 /* Even if we can't lower this yet, it's constant. */
8437 /* FIXME are we calling this too much? */
8438 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
8441 /* Some expressions may have constant operands but are not constant
8442 themselves, such as 1/0. Call this function (or rather, the macro
8443 following it) to check for that condition.
8445 We only call this in places that require an arithmetic constant, not in
8446 places where we might have a non-constant expression that can be a
8447 component of a constant expression, such as the address of a constexpr
8448 variable that might be dereferenced later. */
8451 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
8454 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
8456 if (!allow_non_constant
)
8457 error ("%q+E is not a constant expression", t
);
8458 *non_constant_p
= true;
8460 if (TREE_OVERFLOW_P (t
))
8462 if (!allow_non_constant
)
8464 permerror (input_location
, "overflow in constant expression");
8465 /* If we're being permissive (and are in an enforcing
8466 context), ignore the overflow. */
8467 if (flag_permissive
)
8468 return *non_constant_p
;
8472 return *non_constant_p
;
8475 /* Subroutine of cxx_eval_constant_expression.
8476 Attempt to reduce the unary expression tree T to a compile time value.
8477 If successful, return the value. Otherwise issue a diagnostic
8478 and return error_mark_node. */
8481 cxx_eval_unary_expression (const constexpr_call
*call
, tree t
,
8482 bool allow_non_constant
, bool addr
,
8483 bool *non_constant_p
, bool *overflow_p
)
8486 tree orig_arg
= TREE_OPERAND (t
, 0);
8487 tree arg
= cxx_eval_constant_expression (call
, orig_arg
, allow_non_constant
,
8488 addr
, non_constant_p
, overflow_p
);
8489 VERIFY_CONSTANT (arg
);
8490 if (arg
== orig_arg
)
8492 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), arg
);
8493 VERIFY_CONSTANT (r
);
8497 /* Subroutine of cxx_eval_constant_expression.
8498 Like cxx_eval_unary_expression, except for binary expressions. */
8501 cxx_eval_binary_expression (const constexpr_call
*call
, tree t
,
8502 bool allow_non_constant
, bool addr
,
8503 bool *non_constant_p
, bool *overflow_p
)
8506 tree orig_lhs
= TREE_OPERAND (t
, 0);
8507 tree orig_rhs
= TREE_OPERAND (t
, 1);
8509 lhs
= cxx_eval_constant_expression (call
, orig_lhs
,
8510 allow_non_constant
, addr
,
8511 non_constant_p
, overflow_p
);
8512 VERIFY_CONSTANT (lhs
);
8513 rhs
= cxx_eval_constant_expression (call
, orig_rhs
,
8514 allow_non_constant
, addr
,
8515 non_constant_p
, overflow_p
);
8516 VERIFY_CONSTANT (rhs
);
8517 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
8519 r
= fold_build2 (TREE_CODE (t
), TREE_TYPE (t
), lhs
, rhs
);
8520 VERIFY_CONSTANT (r
);
8524 /* Subroutine of cxx_eval_constant_expression.
8525 Attempt to evaluate condition expressions. Dead branches are not
8529 cxx_eval_conditional_expression (const constexpr_call
*call
, tree t
,
8530 bool allow_non_constant
, bool addr
,
8531 bool *non_constant_p
, bool *overflow_p
)
8533 tree val
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 0),
8534 allow_non_constant
, addr
,
8535 non_constant_p
, overflow_p
);
8536 VERIFY_CONSTANT (val
);
8537 /* Don't VERIFY_CONSTANT the other operands. */
8538 if (integer_zerop (val
))
8539 return cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 2),
8540 allow_non_constant
, addr
,
8541 non_constant_p
, overflow_p
);
8542 return cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 1),
8543 allow_non_constant
, addr
,
8544 non_constant_p
, overflow_p
);
8547 /* Subroutine of cxx_eval_constant_expression.
8548 Attempt to reduce a reference to an array slot. */
8551 cxx_eval_array_reference (const constexpr_call
*call
, tree t
,
8552 bool allow_non_constant
, bool addr
,
8553 bool *non_constant_p
, bool *overflow_p
)
8555 tree oldary
= TREE_OPERAND (t
, 0);
8556 tree ary
= cxx_eval_constant_expression (call
, oldary
,
8557 allow_non_constant
, addr
,
8558 non_constant_p
, overflow_p
);
8562 unsigned len
, elem_nchars
= 1;
8563 if (*non_constant_p
)
8565 oldidx
= TREE_OPERAND (t
, 1);
8566 index
= cxx_eval_constant_expression (call
, oldidx
,
8567 allow_non_constant
, false,
8568 non_constant_p
, overflow_p
);
8569 VERIFY_CONSTANT (index
);
8570 if (addr
&& ary
== oldary
&& index
== oldidx
)
8573 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
8574 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
8575 if (TREE_CODE (ary
) == CONSTRUCTOR
)
8576 len
= CONSTRUCTOR_NELTS (ary
);
8577 else if (TREE_CODE (ary
) == STRING_CST
)
8579 elem_nchars
= (TYPE_PRECISION (elem_type
)
8580 / TYPE_PRECISION (char_type_node
));
8581 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
8585 /* We can't do anything with other tree codes, so use
8586 VERIFY_CONSTANT to complain and fail. */
8587 VERIFY_CONSTANT (ary
);
8590 if (compare_tree_int (index
, len
) >= 0)
8592 if (tree_int_cst_lt (index
, array_type_nelts_top (TREE_TYPE (ary
))))
8594 /* If it's within the array bounds but doesn't have an explicit
8595 initializer, it's value-initialized. */
8596 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
8597 return cxx_eval_constant_expression (call
, val
,
8598 allow_non_constant
, addr
,
8599 non_constant_p
, overflow_p
);
8602 if (!allow_non_constant
)
8603 error ("array subscript out of bound");
8604 *non_constant_p
= true;
8607 else if (tree_int_cst_lt (index
, integer_zero_node
))
8609 if (!allow_non_constant
)
8610 error ("negative array subscript");
8611 *non_constant_p
= true;
8614 i
= tree_low_cst (index
, 0);
8615 if (TREE_CODE (ary
) == CONSTRUCTOR
)
8616 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
8617 else if (elem_nchars
== 1)
8618 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
8619 TREE_STRING_POINTER (ary
)[i
]);
8622 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
8623 return native_interpret_expr (type
, (const unsigned char *)
8624 TREE_STRING_POINTER (ary
)
8625 + i
* elem_nchars
, elem_nchars
);
8627 /* Don't VERIFY_CONSTANT here. */
8630 /* Subroutine of cxx_eval_constant_expression.
8631 Attempt to reduce a field access of a value of class type. */
8634 cxx_eval_component_reference (const constexpr_call
*call
, tree t
,
8635 bool allow_non_constant
, bool addr
,
8636 bool *non_constant_p
, bool *overflow_p
)
8638 unsigned HOST_WIDE_INT i
;
8641 tree part
= TREE_OPERAND (t
, 1);
8642 tree orig_whole
= TREE_OPERAND (t
, 0);
8643 tree whole
= cxx_eval_constant_expression (call
, orig_whole
,
8644 allow_non_constant
, addr
,
8645 non_constant_p
, overflow_p
);
8646 if (whole
== orig_whole
)
8649 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
8650 whole
, part
, NULL_TREE
);
8651 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8653 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
8655 if (!allow_non_constant
)
8656 error ("%qE is not a constant expression", orig_whole
);
8657 *non_constant_p
= true;
8659 if (DECL_MUTABLE_P (part
))
8661 if (!allow_non_constant
)
8662 error ("mutable %qD is not usable in a constant expression", part
);
8663 *non_constant_p
= true;
8665 if (*non_constant_p
)
8667 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
8672 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
8673 && CONSTRUCTOR_NELTS (whole
) > 0)
8675 /* DR 1188 says we don't have to deal with this. */
8676 if (!allow_non_constant
)
8677 error ("accessing %qD member instead of initialized %qD member in "
8678 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
8679 *non_constant_p
= true;
8683 /* If there's no explicit init for this field, it's value-initialized. */
8684 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
8685 return cxx_eval_constant_expression (call
, value
,
8686 allow_non_constant
, addr
,
8687 non_constant_p
, overflow_p
);
8690 /* Subroutine of cxx_eval_constant_expression.
8691 Attempt to reduce a field access of a value of class type that is
8692 expressed as a BIT_FIELD_REF. */
8695 cxx_eval_bit_field_ref (const constexpr_call
*call
, tree t
,
8696 bool allow_non_constant
, bool addr
,
8697 bool *non_constant_p
, bool *overflow_p
)
8699 tree orig_whole
= TREE_OPERAND (t
, 0);
8700 tree retval
, fldval
, utype
, mask
;
8701 bool fld_seen
= false;
8702 HOST_WIDE_INT istart
, isize
;
8703 tree whole
= cxx_eval_constant_expression (call
, orig_whole
,
8704 allow_non_constant
, addr
,
8705 non_constant_p
, overflow_p
);
8706 tree start
, field
, value
;
8707 unsigned HOST_WIDE_INT i
;
8709 if (whole
== orig_whole
)
8711 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8713 if (!*non_constant_p
8714 && TREE_CODE (whole
) != VECTOR_CST
8715 && TREE_CODE (whole
) != CONSTRUCTOR
)
8717 if (!allow_non_constant
)
8718 error ("%qE is not a constant expression", orig_whole
);
8719 *non_constant_p
= true;
8721 if (*non_constant_p
)
8724 if (TREE_CODE (whole
) == VECTOR_CST
)
8725 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
8726 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
8728 start
= TREE_OPERAND (t
, 2);
8729 istart
= tree_low_cst (start
, 0);
8730 isize
= tree_low_cst (TREE_OPERAND (t
, 1), 0);
8731 utype
= TREE_TYPE (t
);
8732 if (!TYPE_UNSIGNED (utype
))
8733 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
8734 retval
= build_int_cst (utype
, 0);
8735 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
8737 tree bitpos
= bit_position (field
);
8738 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
8740 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
8741 && TREE_CODE (value
) == INTEGER_CST
8742 && tree_fits_shwi_p (bitpos
)
8743 && tree_fits_shwi_p (DECL_SIZE (field
)))
8745 HOST_WIDE_INT bit
= tree_low_cst (bitpos
, 0);
8746 HOST_WIDE_INT sz
= tree_low_cst (DECL_SIZE (field
), 0);
8747 HOST_WIDE_INT shift
;
8748 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
8750 fldval
= fold_convert (utype
, value
);
8751 mask
= build_int_cst_type (utype
, -1);
8752 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
8753 size_int (TYPE_PRECISION (utype
) - sz
));
8754 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
8755 size_int (TYPE_PRECISION (utype
) - sz
));
8756 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
8757 shift
= bit
- istart
;
8758 if (BYTES_BIG_ENDIAN
)
8759 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
8760 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
8762 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
8768 return fold_convert (TREE_TYPE (t
), retval
);
8770 return error_mark_node
;
8773 /* Subroutine of cxx_eval_constant_expression.
8774 Evaluate a short-circuited logical expression T in the context
8775 of a given constexpr CALL. BAILOUT_VALUE is the value for
8776 early return. CONTINUE_VALUE is used here purely for
8777 sanity check purposes. */
8780 cxx_eval_logical_expression (const constexpr_call
*call
, tree t
,
8781 tree bailout_value
, tree continue_value
,
8782 bool allow_non_constant
, bool addr
,
8783 bool *non_constant_p
, bool *overflow_p
)
8786 tree lhs
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 0),
8787 allow_non_constant
, addr
,
8788 non_constant_p
, overflow_p
);
8789 VERIFY_CONSTANT (lhs
);
8790 if (tree_int_cst_equal (lhs
, bailout_value
))
8792 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
8793 r
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 1),
8794 allow_non_constant
, addr
, non_constant_p
, overflow_p
);
8795 VERIFY_CONSTANT (r
);
8799 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8800 CONSTRUCTOR elements to initialize (part of) an object containing that
8801 field. Return a pointer to the constructor_elt corresponding to the
8802 initialization of the field. */
8804 static constructor_elt
*
8805 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
8807 tree aggr
= TREE_OPERAND (ref
, 0);
8808 tree field
= TREE_OPERAND (ref
, 1);
8810 constructor_elt
*ce
;
8812 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
8814 if (TREE_CODE (aggr
) == COMPONENT_REF
)
8816 constructor_elt
*base_ce
8817 = base_field_constructor_elt (v
, aggr
);
8818 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
8821 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
8822 if (ce
->index
== field
)
8829 /* Subroutine of cxx_eval_constant_expression.
8830 The expression tree T denotes a C-style array or a C-style
8831 aggregate. Reduce it to a constant expression. */
8834 cxx_eval_bare_aggregate (const constexpr_call
*call
, tree t
,
8835 bool allow_non_constant
, bool addr
,
8836 bool *non_constant_p
, bool *overflow_p
)
8838 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
8839 vec
<constructor_elt
, va_gc
> *n
;
8840 vec_alloc (n
, vec_safe_length (v
));
8841 constructor_elt
*ce
;
8843 bool changed
= false;
8844 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
8845 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
8847 tree elt
= cxx_eval_constant_expression (call
, ce
->value
,
8848 allow_non_constant
, addr
,
8849 non_constant_p
, overflow_p
);
8850 /* Don't VERIFY_CONSTANT here. */
8851 if (allow_non_constant
&& *non_constant_p
)
8853 if (elt
!= ce
->value
)
8855 if (ce
->index
&& TREE_CODE (ce
->index
) == COMPONENT_REF
)
8857 /* This is an initialization of a vfield inside a base
8858 subaggregate that we already initialized; push this
8859 initialization into the previous initialization. */
8860 constructor_elt
*inner
= base_field_constructor_elt (n
, ce
->index
);
8863 else if (ce
->index
&& TREE_CODE (ce
->index
) == NOP_EXPR
)
8865 /* This is an initializer for an empty base; now that we've
8866 checked that it's constant, we can ignore it. */
8867 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce
->index
))));
8870 CONSTRUCTOR_APPEND_ELT (n
, ce
->index
, elt
);
8872 if (*non_constant_p
|| !changed
)
8878 t
= build_constructor (TREE_TYPE (t
), n
);
8879 TREE_CONSTANT (t
) = true;
8880 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
8885 /* Subroutine of cxx_eval_constant_expression.
8886 The expression tree T is a VEC_INIT_EXPR which denotes the desired
8887 initialization of a non-static data member of array type. Reduce it to a
8890 Note that apart from value-initialization (when VALUE_INIT is true),
8891 this is only intended to support value-initialization and the
8892 initializations done by defaulted constructors for classes with
8893 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
8894 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
8895 for the copy/move constructor. */
8898 cxx_eval_vec_init_1 (const constexpr_call
*call
, tree atype
, tree init
,
8899 bool value_init
, bool allow_non_constant
, bool addr
,
8900 bool *non_constant_p
, bool *overflow_p
)
8902 tree elttype
= TREE_TYPE (atype
);
8903 int max
= tree_low_cst (array_type_nelts (atype
), 0);
8904 vec
<constructor_elt
, va_gc
> *n
;
8905 vec_alloc (n
, max
+ 1);
8906 bool pre_init
= false;
8909 /* For the default constructor, build up a call to the default
8910 constructor of the element type. We only need to handle class types
8911 here, as for a constructor to be constexpr, all members must be
8912 initialized, which for a defaulted default constructor means they must
8913 be of a class type with a constexpr default constructor. */
8914 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
8915 /* We only do this at the lowest level. */;
8916 else if (value_init
)
8918 init
= build_value_init (elttype
, tf_warning_or_error
);
8919 init
= cxx_eval_constant_expression
8920 (call
, init
, allow_non_constant
, addr
, non_constant_p
, overflow_p
);
8925 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
8926 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
8927 &argvec
, elttype
, LOOKUP_NORMAL
,
8928 tf_warning_or_error
);
8929 release_tree_vector (argvec
);
8930 init
= cxx_eval_constant_expression (call
, init
, allow_non_constant
,
8931 addr
, non_constant_p
, overflow_p
);
8935 if (*non_constant_p
&& !allow_non_constant
)
8938 for (i
= 0; i
<= max
; ++i
)
8940 tree idx
= build_int_cst (size_type_node
, i
);
8942 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
8944 /* A multidimensional array; recurse. */
8945 if (value_init
|| init
== NULL_TREE
)
8946 eltinit
= NULL_TREE
;
8948 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
8949 tf_warning_or_error
);
8950 eltinit
= cxx_eval_vec_init_1 (call
, elttype
, eltinit
, value_init
,
8951 allow_non_constant
, addr
,
8952 non_constant_p
, overflow_p
);
8956 /* Initializing an element using value or default initialization
8957 we just pre-built above. */
8961 eltinit
= unshare_expr (init
);
8965 /* Copying an element. */
8966 vec
<tree
, va_gc
> *argvec
;
8967 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8968 (atype
, TREE_TYPE (init
)));
8969 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
8970 tf_warning_or_error
);
8971 if (!real_lvalue_p (init
))
8972 eltinit
= move (eltinit
);
8973 argvec
= make_tree_vector ();
8974 argvec
->quick_push (eltinit
);
8975 eltinit
= (build_special_member_call
8976 (NULL_TREE
, complete_ctor_identifier
, &argvec
,
8977 elttype
, LOOKUP_NORMAL
, tf_warning_or_error
));
8978 release_tree_vector (argvec
);
8979 eltinit
= cxx_eval_constant_expression
8980 (call
, eltinit
, allow_non_constant
, addr
, non_constant_p
, overflow_p
);
8982 if (*non_constant_p
&& !allow_non_constant
)
8984 CONSTRUCTOR_APPEND_ELT (n
, idx
, eltinit
);
8987 if (!*non_constant_p
)
8989 init
= build_constructor (atype
, n
);
8990 TREE_CONSTANT (init
) = true;
9000 cxx_eval_vec_init (const constexpr_call
*call
, tree t
,
9001 bool allow_non_constant
, bool addr
,
9002 bool *non_constant_p
, bool *overflow_p
)
9004 tree atype
= TREE_TYPE (t
);
9005 tree init
= VEC_INIT_EXPR_INIT (t
);
9006 tree r
= cxx_eval_vec_init_1 (call
, atype
, init
,
9007 VEC_INIT_EXPR_VALUE_INIT (t
),
9008 allow_non_constant
, addr
, non_constant_p
, overflow_p
);
9009 if (*non_constant_p
)
9015 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9016 match. We want to be less strict for simple *& folding; if we have a
9017 non-const temporary that we access through a const pointer, that should
9018 work. We handle this here rather than change fold_indirect_ref_1
9019 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9020 don't really make sense outside of constant expression evaluation. Also
9021 we want to allow folding to COMPONENT_REF, which could cause trouble
9022 with TBAA in fold_indirect_ref_1.
9024 Try to keep this function synced with fold_indirect_ref_1. */
9027 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
9033 subtype
= TREE_TYPE (sub
);
9034 if (!POINTER_TYPE_P (subtype
))
9037 if (TREE_CODE (sub
) == ADDR_EXPR
)
9039 tree op
= TREE_OPERAND (sub
, 0);
9040 tree optype
= TREE_TYPE (op
);
9042 /* *&CONST_DECL -> to the value of the const decl. */
9043 if (TREE_CODE (op
) == CONST_DECL
)
9044 return DECL_INITIAL (op
);
9045 /* *&p => p; make sure to handle *&"str"[cst] here. */
9046 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
))
9048 tree fop
= fold_read_from_constant_string (op
);
9054 /* *(foo *)&fooarray => fooarray[0] */
9055 else if (TREE_CODE (optype
) == ARRAY_TYPE
9056 && (same_type_ignoring_top_level_qualifiers_p
9057 (type
, TREE_TYPE (optype
))))
9059 tree type_domain
= TYPE_DOMAIN (optype
);
9060 tree min_val
= size_zero_node
;
9061 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
9062 min_val
= TYPE_MIN_VALUE (type_domain
);
9063 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
9064 NULL_TREE
, NULL_TREE
);
9066 /* *(foo *)&complexfoo => __real__ complexfoo */
9067 else if (TREE_CODE (optype
) == COMPLEX_TYPE
9068 && (same_type_ignoring_top_level_qualifiers_p
9069 (type
, TREE_TYPE (optype
))))
9070 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
9071 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9072 else if (TREE_CODE (optype
) == VECTOR_TYPE
9073 && (same_type_ignoring_top_level_qualifiers_p
9074 (type
, TREE_TYPE (optype
))))
9076 tree part_width
= TYPE_SIZE (type
);
9077 tree index
= bitsize_int (0);
9078 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
9080 /* Also handle conversion to an empty base class, which
9081 is represented with a NOP_EXPR. */
9082 else if (is_empty_class (type
)
9083 && CLASS_TYPE_P (optype
)
9084 && DERIVED_FROM_P (type
, optype
))
9089 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9090 else if (RECORD_OR_UNION_TYPE_P (optype
))
9092 tree field
= TYPE_FIELDS (optype
);
9093 for (; field
; field
= DECL_CHAIN (field
))
9094 if (TREE_CODE (field
) == FIELD_DECL
9095 && integer_zerop (byte_position (field
))
9096 && (same_type_ignoring_top_level_qualifiers_p
9097 (TREE_TYPE (field
), type
)))
9099 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
9104 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
9105 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
9107 tree op00
= TREE_OPERAND (sub
, 0);
9108 tree op01
= TREE_OPERAND (sub
, 1);
9111 if (TREE_CODE (op00
) == ADDR_EXPR
)
9114 op00
= TREE_OPERAND (op00
, 0);
9115 op00type
= TREE_TYPE (op00
);
9117 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9118 if (TREE_CODE (op00type
) == VECTOR_TYPE
9119 && (same_type_ignoring_top_level_qualifiers_p
9120 (type
, TREE_TYPE (op00type
))))
9122 HOST_WIDE_INT offset
= tree_low_cst (op01
, 0);
9123 tree part_width
= TYPE_SIZE (type
);
9124 unsigned HOST_WIDE_INT part_widthi
= tree_low_cst (part_width
, 0)/BITS_PER_UNIT
;
9125 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
9126 tree index
= bitsize_int (indexi
);
9128 if (offset
/part_widthi
<= TYPE_VECTOR_SUBPARTS (op00type
))
9129 return fold_build3_loc (loc
,
9130 BIT_FIELD_REF
, type
, op00
,
9134 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9135 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
9136 && (same_type_ignoring_top_level_qualifiers_p
9137 (type
, TREE_TYPE (op00type
))))
9139 tree size
= TYPE_SIZE_UNIT (type
);
9140 if (tree_int_cst_equal (size
, op01
))
9141 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
9143 /* ((foo *)&fooarray)[1] => fooarray[1] */
9144 else if (TREE_CODE (op00type
) == ARRAY_TYPE
9145 && (same_type_ignoring_top_level_qualifiers_p
9146 (type
, TREE_TYPE (op00type
))))
9148 tree type_domain
= TYPE_DOMAIN (op00type
);
9149 tree min_val
= size_zero_node
;
9150 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
9151 min_val
= TYPE_MIN_VALUE (type_domain
);
9152 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
9153 TYPE_SIZE_UNIT (type
));
9154 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
9155 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
9156 NULL_TREE
, NULL_TREE
);
9158 /* Also handle conversion to an empty base class, which
9159 is represented with a NOP_EXPR. */
9160 else if (is_empty_class (type
)
9161 && CLASS_TYPE_P (op00type
)
9162 && DERIVED_FROM_P (type
, op00type
))
9167 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9168 else if (RECORD_OR_UNION_TYPE_P (op00type
))
9170 tree field
= TYPE_FIELDS (op00type
);
9171 for (; field
; field
= DECL_CHAIN (field
))
9172 if (TREE_CODE (field
) == FIELD_DECL
9173 && tree_int_cst_equal (byte_position (field
), op01
)
9174 && (same_type_ignoring_top_level_qualifiers_p
9175 (TREE_TYPE (field
), type
)))
9177 return fold_build3 (COMPONENT_REF
, type
, op00
,
9184 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9185 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
9186 && (same_type_ignoring_top_level_qualifiers_p
9187 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
9190 tree min_val
= size_zero_node
;
9191 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
9195 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
9196 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
9197 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
9198 min_val
= TYPE_MIN_VALUE (type_domain
);
9199 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
9207 cxx_eval_indirect_ref (const constexpr_call
*call
, tree t
,
9208 bool allow_non_constant
, bool addr
,
9209 bool *non_constant_p
, bool *overflow_p
)
9211 tree orig_op0
= TREE_OPERAND (t
, 0);
9212 tree op0
= cxx_eval_constant_expression (call
, orig_op0
, allow_non_constant
,
9213 /*addr*/false, non_constant_p
, overflow_p
);
9214 bool empty_base
= false;
9217 /* Don't VERIFY_CONSTANT here. */
9218 if (*non_constant_p
)
9221 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
9225 r
= cxx_eval_constant_expression (call
, r
, allow_non_constant
,
9226 addr
, non_constant_p
, overflow_p
);
9231 if (TREE_CODE (sub
) == ADDR_EXPR
)
9233 /* We couldn't fold to a constant value. Make sure it's not
9234 something we should have been able to fold. */
9235 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9236 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
9237 /* DR 1188 says we don't have to deal with this. */
9238 if (!allow_non_constant
)
9239 error ("accessing value of %qE through a %qT glvalue in a "
9240 "constant expression", build_fold_indirect_ref (sub
),
9242 *non_constant_p
= true;
9247 /* If we're pulling out the value of an empty base, make sure
9248 that the whole object is constant and then return an empty
9252 VERIFY_CONSTANT (r
);
9253 r
= build_constructor (TREE_TYPE (t
), NULL
);
9254 TREE_CONSTANT (r
) = true;
9259 if (addr
&& op0
!= orig_op0
)
9260 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
9262 VERIFY_CONSTANT (t
);
9268 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9269 Shared between potential_constant_expression and
9270 cxx_eval_constant_expression. */
9273 non_const_var_error (tree r
)
9275 tree type
= TREE_TYPE (r
);
9276 error ("the value of %qD is not usable in a constant "
9278 /* Avoid error cascade. */
9279 if (DECL_INITIAL (r
) == error_mark_node
)
9281 if (DECL_DECLARED_CONSTEXPR_P (r
))
9282 inform (DECL_SOURCE_LOCATION (r
),
9283 "%qD used in its own initializer", r
);
9284 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
9286 if (!CP_TYPE_CONST_P (type
))
9287 inform (DECL_SOURCE_LOCATION (r
),
9288 "%q#D is not const", r
);
9289 else if (CP_TYPE_VOLATILE_P (type
))
9290 inform (DECL_SOURCE_LOCATION (r
),
9291 "%q#D is volatile", r
);
9292 else if (!DECL_INITIAL (r
)
9293 || !TREE_CONSTANT (DECL_INITIAL (r
)))
9294 inform (DECL_SOURCE_LOCATION (r
),
9295 "%qD was not initialized with a constant "
9302 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
9303 inform (DECL_SOURCE_LOCATION (r
),
9304 "%qD was not declared %<constexpr%>", r
);
9306 inform (DECL_SOURCE_LOCATION (r
),
9307 "%qD does not have integral or enumeration type",
9312 /* Subroutine of cxx_eval_constant_expression.
9313 Like cxx_eval_unary_expression, except for trinary expressions. */
9316 cxx_eval_trinary_expression (const constexpr_call
*call
, tree t
,
9317 bool allow_non_constant
, bool addr
,
9318 bool *non_constant_p
, bool *overflow_p
)
9324 for (i
= 0; i
< 3; i
++)
9326 args
[i
] = cxx_eval_constant_expression (call
, TREE_OPERAND (t
, i
),
9327 allow_non_constant
, addr
,
9328 non_constant_p
, overflow_p
);
9329 VERIFY_CONSTANT (args
[i
]);
9332 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
9333 args
[0], args
[1], args
[2]);
9334 if (val
== NULL_TREE
)
9336 VERIFY_CONSTANT (val
);
9340 /* Attempt to reduce the expression T to a constant value.
9341 On failure, issue diagnostic and return error_mark_node. */
9342 /* FIXME unify with c_fully_fold */
9345 cxx_eval_constant_expression (const constexpr_call
*call
, tree t
,
9346 bool allow_non_constant
, bool addr
,
9347 bool *non_constant_p
, bool *overflow_p
)
9351 if (t
== error_mark_node
)
9353 *non_constant_p
= true;
9356 if (CONSTANT_CLASS_P (t
))
9358 if (TREE_CODE (t
) == PTRMEM_CST
)
9359 t
= cplus_expand_constant (t
);
9360 else if (TREE_OVERFLOW (t
) && (!flag_permissive
|| allow_non_constant
))
9364 if (TREE_CODE (t
) != NOP_EXPR
9365 && reduced_constant_expression_p (t
))
9368 switch (TREE_CODE (t
))
9373 /* else fall through. */
9375 r
= integral_constant_value (t
);
9376 if (TREE_CODE (r
) == TARGET_EXPR
9377 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
9378 r
= TARGET_EXPR_INITIAL (r
);
9381 if (!allow_non_constant
)
9382 non_const_var_error (r
);
9383 *non_constant_p
= true;
9393 if (call
&& DECL_CONTEXT (t
) == call
->fundef
->decl
)
9395 if (DECL_ARTIFICIAL (t
) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t
)))
9397 if (!allow_non_constant
)
9398 sorry ("use of the value of the object being constructed "
9399 "in a constant expression");
9400 *non_constant_p
= true;
9403 r
= lookup_parameter_binding (call
, t
);
9406 /* Defer in case this is only used for its type. */;
9409 if (!allow_non_constant
)
9410 error ("%qE is not a constant expression", t
);
9411 *non_constant_p
= true;
9416 case AGGR_INIT_EXPR
:
9417 r
= cxx_eval_call_expression (call
, t
, allow_non_constant
, addr
,
9418 non_constant_p
, overflow_p
);
9422 if (!literal_type_p (TREE_TYPE (t
)))
9424 if (!allow_non_constant
)
9426 error ("temporary of non-literal type %qT in a "
9427 "constant expression", TREE_TYPE (t
));
9428 explain_non_literal_class (TREE_TYPE (t
));
9430 *non_constant_p
= true;
9433 /* else fall through. */
9435 /* Pass false for 'addr' because these codes indicate
9436 initialization of a temporary. */
9437 r
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 1),
9438 allow_non_constant
, false,
9439 non_constant_p
, overflow_p
);
9440 if (!*non_constant_p
)
9441 /* Adjust the type of the result to the type of the temporary. */
9442 r
= adjust_temp_type (TREE_TYPE (t
), r
);
9446 r
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 1),
9447 allow_non_constant
, addr
,
9448 non_constant_p
, overflow_p
);
9452 case NON_LVALUE_EXPR
:
9453 case TRY_CATCH_EXPR
:
9454 case CLEANUP_POINT_EXPR
:
9455 case MUST_NOT_THROW_EXPR
:
9457 r
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 0),
9458 allow_non_constant
, addr
,
9459 non_constant_p
, overflow_p
);
9462 /* These differ from cxx_eval_unary_expression in that this doesn't
9463 check for a constant operand or result; an address can be
9464 constant without its operand being, and vice versa. */
9466 r
= cxx_eval_indirect_ref (call
, t
, allow_non_constant
, addr
,
9467 non_constant_p
, overflow_p
);
9472 tree oldop
= TREE_OPERAND (t
, 0);
9473 tree op
= cxx_eval_constant_expression (call
, oldop
,
9476 non_constant_p
, overflow_p
);
9477 /* Don't VERIFY_CONSTANT here. */
9478 if (*non_constant_p
)
9480 /* This function does more aggressive folding than fold itself. */
9481 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
9482 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
9490 case FIX_TRUNC_EXPR
:
9495 case TRUTH_NOT_EXPR
:
9496 case FIXED_CONVERT_EXPR
:
9497 r
= cxx_eval_unary_expression (call
, t
, allow_non_constant
, addr
,
9498 non_constant_p
, overflow_p
);
9502 if (SIZEOF_EXPR_TYPE_P (t
))
9503 r
= cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t
, 0)),
9504 SIZEOF_EXPR
, false);
9505 else if (TYPE_P (TREE_OPERAND (t
, 0)))
9506 r
= cxx_sizeof_or_alignof_type (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
9509 r
= cxx_sizeof_or_alignof_expr (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
9511 if (r
== error_mark_node
)
9513 VERIFY_CONSTANT (r
);
9518 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9519 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9520 introduced by build_call_a. */
9521 tree op0
= TREE_OPERAND (t
, 0);
9522 tree op1
= TREE_OPERAND (t
, 1);
9524 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
9525 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
9526 r
= cxx_eval_constant_expression (call
, op0
, allow_non_constant
,
9527 addr
, non_constant_p
, overflow_p
);
9530 /* Check that the LHS is constant and then discard it. */
9531 cxx_eval_constant_expression (call
, op0
, allow_non_constant
,
9532 false, non_constant_p
, overflow_p
);
9533 op1
= TREE_OPERAND (t
, 1);
9534 r
= cxx_eval_constant_expression (call
, op1
, allow_non_constant
,
9535 addr
, non_constant_p
, overflow_p
);
9540 case POINTER_PLUS_EXPR
:
9544 case TRUNC_DIV_EXPR
:
9546 case FLOOR_DIV_EXPR
:
9547 case ROUND_DIV_EXPR
:
9548 case TRUNC_MOD_EXPR
:
9550 case ROUND_MOD_EXPR
:
9552 case EXACT_DIV_EXPR
:
9562 case TRUTH_XOR_EXPR
:
9569 case UNORDERED_EXPR
:
9579 r
= cxx_eval_binary_expression (call
, t
, allow_non_constant
, addr
,
9580 non_constant_p
, overflow_p
);
9583 /* fold can introduce non-IF versions of these; still treat them as
9584 short-circuiting. */
9585 case TRUTH_AND_EXPR
:
9586 case TRUTH_ANDIF_EXPR
:
9587 r
= cxx_eval_logical_expression (call
, t
, boolean_false_node
,
9589 allow_non_constant
, addr
,
9590 non_constant_p
, overflow_p
);
9594 case TRUTH_ORIF_EXPR
:
9595 r
= cxx_eval_logical_expression (call
, t
, boolean_true_node
,
9597 allow_non_constant
, addr
,
9598 non_constant_p
, overflow_p
);
9602 r
= cxx_eval_array_reference (call
, t
, allow_non_constant
, addr
,
9603 non_constant_p
, overflow_p
);
9607 r
= cxx_eval_component_reference (call
, t
, allow_non_constant
, addr
,
9608 non_constant_p
, overflow_p
);
9612 r
= cxx_eval_bit_field_ref (call
, t
, allow_non_constant
, addr
,
9613 non_constant_p
, overflow_p
);
9618 r
= cxx_eval_conditional_expression (call
, t
, allow_non_constant
, addr
,
9619 non_constant_p
, overflow_p
);
9623 r
= cxx_eval_bare_aggregate (call
, t
, allow_non_constant
, addr
,
9624 non_constant_p
, overflow_p
);
9628 /* We can get this in a defaulted constructor for a class with a
9629 non-static data member of array type. Either the initializer will
9630 be NULL, meaning default-initialization, or it will be an lvalue
9631 or xvalue of the same type, meaning direct-initialization from the
9632 corresponding member. */
9633 r
= cxx_eval_vec_init (call
, t
, allow_non_constant
, addr
,
9634 non_constant_p
, overflow_p
);
9639 r
= cxx_eval_trinary_expression (call
, t
, allow_non_constant
, addr
,
9640 non_constant_p
, overflow_p
);
9644 case VIEW_CONVERT_EXPR
:
9647 tree oldop
= TREE_OPERAND (t
, 0);
9648 tree op
= cxx_eval_constant_expression (call
, oldop
,
9649 allow_non_constant
, addr
,
9650 non_constant_p
, overflow_p
);
9651 if (*non_constant_p
)
9653 if (POINTER_TYPE_P (TREE_TYPE (t
))
9654 && TREE_CODE (op
) == INTEGER_CST
9655 && !integer_zerop (op
))
9657 if (!allow_non_constant
)
9658 error_at (EXPR_LOC_OR_HERE (t
),
9659 "reinterpret_cast from integer to pointer");
9660 *non_constant_p
= true;
9664 /* We didn't fold at the top so we could check for ptr-int
9667 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), op
);
9668 /* Conversion of an out-of-range value has implementation-defined
9669 behavior; the language considers it different from arithmetic
9670 overflow, which is undefined. */
9671 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
9672 TREE_OVERFLOW (r
) = false;
9676 case EMPTY_CLASS_EXPR
:
9677 /* This is good enough for a function argument that might not get
9678 used, and they can't do anything with it, so just return it. */
9682 case PREINCREMENT_EXPR
:
9683 case POSTINCREMENT_EXPR
:
9684 case PREDECREMENT_EXPR
:
9685 case POSTDECREMENT_EXPR
:
9689 case VEC_DELETE_EXPR
:
9693 /* GCC internal stuff. */
9696 case WITH_CLEANUP_EXPR
:
9697 case STATEMENT_LIST
:
9699 case NON_DEPENDENT_EXPR
:
9703 if (!allow_non_constant
)
9704 error_at (EXPR_LOC_OR_HERE (t
),
9705 "expression %qE is not a constant-expression", t
);
9706 *non_constant_p
= true;
9710 internal_error ("unexpected expression %qE of kind %s", t
,
9711 get_tree_code_name (TREE_CODE (t
)));
9712 *non_constant_p
= true;
9716 if (r
== error_mark_node
)
9717 *non_constant_p
= true;
9719 if (*non_constant_p
)
9726 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
)
9728 bool non_constant_p
= false;
9729 bool overflow_p
= false;
9730 tree r
= cxx_eval_constant_expression (NULL
, t
, allow_non_constant
,
9731 false, &non_constant_p
, &overflow_p
);
9733 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
9735 if (TREE_CODE (t
) != CONSTRUCTOR
9736 && cp_has_mutable_p (TREE_TYPE (t
)))
9738 /* We allow a mutable type if the original expression was a
9739 CONSTRUCTOR so that we can do aggregate initialization of
9740 constexpr variables. */
9741 if (!allow_non_constant
)
9742 error ("%qT cannot be the type of a complete constant expression "
9743 "because it has mutable sub-objects", TREE_TYPE (t
));
9744 non_constant_p
= true;
9747 /* Technically we should check this for all subexpressions, but that
9748 runs into problems with our internal representation of pointer
9749 subtraction and the 5.19 rules are still in flux. */
9750 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
9751 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
9752 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
9754 if (!allow_non_constant
)
9755 error ("conversion from pointer type %qT "
9756 "to arithmetic type %qT in a constant-expression",
9757 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
9758 non_constant_p
= true;
9761 if (!non_constant_p
&& overflow_p
)
9762 non_constant_p
= true;
9764 if (non_constant_p
&& !allow_non_constant
)
9765 return error_mark_node
;
9766 else if (non_constant_p
&& TREE_CONSTANT (r
))
9768 /* This isn't actually constant, so unset TREE_CONSTANT. */
9771 else if (TREE_CODE (r
) == CONSTRUCTOR
)
9772 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
9774 r
= build_nop (TREE_TYPE (r
), r
);
9775 TREE_CONSTANT (r
) = false;
9777 else if (non_constant_p
|| r
== t
)
9780 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
9782 if (TREE_CODE (t
) == TARGET_EXPR
9783 && TARGET_EXPR_INITIAL (t
) == r
)
9787 r
= get_target_expr (r
);
9788 TREE_CONSTANT (r
) = true;
9796 /* Returns true if T is a valid subexpression of a constant expression,
9797 even if it isn't itself a constant expression. */
9800 is_sub_constant_expr (tree t
)
9802 bool non_constant_p
= false;
9803 bool overflow_p
= false;
9804 cxx_eval_constant_expression (NULL
, t
, true, false, &non_constant_p
,
9806 return !non_constant_p
&& !overflow_p
;
9809 /* If T represents a constant expression returns its reduced value.
9810 Otherwise return error_mark_node. If T is dependent, then
9814 cxx_constant_value (tree t
)
9816 return cxx_eval_outermost_constant_expr (t
, false);
9819 /* If T is a constant expression, returns its reduced value.
9820 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9821 Otherwise, returns a version of T without TREE_CONSTANT. */
9824 maybe_constant_value (tree t
)
9828 if (instantiation_dependent_expression_p (t
)
9829 || type_unknown_p (t
)
9830 || BRACE_ENCLOSED_INITIALIZER_P (t
)
9831 || !potential_constant_expression (t
))
9833 if (TREE_OVERFLOW_P (t
))
9835 t
= build_nop (TREE_TYPE (t
), t
);
9836 TREE_CONSTANT (t
) = false;
9841 r
= cxx_eval_outermost_constant_expr (t
, true);
9842 #ifdef ENABLE_CHECKING
9843 /* cp_tree_equal looks through NOPs, so allow them. */
9845 || CONVERT_EXPR_P (t
)
9846 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
9847 || !cp_tree_equal (r
, t
));
9852 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9853 than wrapped in a TARGET_EXPR. */
9856 maybe_constant_init (tree t
)
9858 t
= maybe_constant_value (t
);
9859 if (TREE_CODE (t
) == TARGET_EXPR
)
9861 tree init
= TARGET_EXPR_INITIAL (t
);
9862 if (TREE_CODE (init
) == CONSTRUCTOR
)
9869 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9870 /* Return true if the object referred to by REF has automatic or thread
9873 enum { ck_ok
, ck_bad
, ck_unknown
};
9875 check_automatic_or_tls (tree ref
)
9877 enum machine_mode mode
;
9878 HOST_WIDE_INT bitsize
, bitpos
;
9880 int volatilep
= 0, unsignedp
= 0;
9881 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
9882 &mode
, &unsignedp
, &volatilep
, false);
9885 /* If there isn't a decl in the middle, we don't know the linkage here,
9886 and this isn't a constant expression anyway. */
9889 dk
= decl_storage_duration (decl
);
9890 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
9894 /* Return true if T denotes a potentially constant expression. Issue
9895 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9896 an lvalue-rvalue conversion is implied.
9898 C++0x [expr.const] used to say
9900 6 An expression is a potential constant expression if it is
9901 a constant expression where all occurrences of function
9902 parameters are replaced by arbitrary constant expressions
9903 of the appropriate type.
9905 2 A conditional expression is a constant expression unless it
9906 involves one of the following as a potentially evaluated
9907 subexpression (3.2), but subexpressions of logical AND (5.14),
9908 logical OR (5.15), and conditional (5.16) operations that are
9909 not evaluated are not considered. */
9912 potential_constant_expression_1 (tree t
, bool want_rval
, tsubst_flags_t flags
)
9914 enum { any
= false, rval
= true };
9918 if (t
== error_mark_node
)
9922 if (TREE_THIS_VOLATILE (t
))
9924 if (flags
& tf_error
)
9925 error ("expression %qE has side-effects", t
);
9928 if (CONSTANT_CLASS_P (t
))
9931 switch (TREE_CODE (t
))
9937 case TEMPLATE_ID_EXPR
:
9945 case TEMPLATE_PARM_INDEX
:
9947 case IDENTIFIER_NODE
:
9948 case USERDEF_LITERAL
:
9949 /* We can see a FIELD_DECL in a pointer-to-member expression. */
9955 case AGGR_INIT_EXPR
:
9957 /* -- an invocation of a function other than a constexpr function
9958 or a constexpr constructor. */
9960 tree fun
= get_function_named_in_call (t
);
9961 const int nargs
= call_expr_nargs (t
);
9964 if (is_overloaded_fn (fun
))
9966 if (TREE_CODE (fun
) == FUNCTION_DECL
)
9968 if (builtin_valid_in_constant_expr_p (fun
))
9970 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
9971 /* Allow any built-in function; if the expansion
9972 isn't constant, we'll deal with that then. */
9973 && !is_builtin_fn (fun
))
9975 if (flags
& tf_error
)
9977 error_at (EXPR_LOC_OR_HERE (t
),
9978 "call to non-constexpr function %qD", fun
);
9979 explain_invalid_constexpr_fn (fun
);
9983 /* A call to a non-static member function takes the address
9984 of the object as the first argument. But in a constant
9985 expression the address will be folded away, so look
9987 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
9988 && !DECL_CONSTRUCTOR_P (fun
))
9990 tree x
= get_nth_callarg (t
, 0);
9991 if (is_this_parameter (x
))
9993 if (DECL_CONTEXT (x
) == NULL_TREE
9994 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x
)))
9996 if (flags
& tf_error
)
9997 sorry ("calling a member function of the "
9998 "object being constructed in a constant "
10002 /* Otherwise OK. */;
10004 else if (!potential_constant_expression_1 (x
, rval
, flags
))
10011 if (!potential_constant_expression_1 (fun
, true, flags
))
10013 fun
= get_first_fn (fun
);
10015 /* Skip initial arguments to base constructors. */
10016 if (DECL_BASE_CONSTRUCTOR_P (fun
))
10017 i
= num_artificial_parms_for (fun
);
10018 fun
= DECL_ORIGIN (fun
);
10022 if (potential_constant_expression_1 (fun
, rval
, flags
))
10023 /* Might end up being a constant function pointer. */;
10027 for (; i
< nargs
; ++i
)
10029 tree x
= get_nth_callarg (t
, i
);
10030 if (!potential_constant_expression_1 (x
, rval
, flags
))
10036 case NON_LVALUE_EXPR
:
10037 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10038 -- an lvalue of integral type that refers to a non-volatile
10039 const variable or static data member initialized with
10040 constant expressions, or
10042 -- an lvalue of literal type that refers to non-volatile
10043 object defined with constexpr, or that refers to a
10044 sub-object of such an object; */
10045 return potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
, flags
);
10048 if (want_rval
&& !decl_constant_var_p (t
)
10049 && !dependent_type_p (TREE_TYPE (t
)))
10051 if (flags
& tf_error
)
10052 non_const_var_error (t
);
10059 case VIEW_CONVERT_EXPR
:
10060 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10061 may change to something more specific to type-punning (DR 1312). */
10063 tree from
= TREE_OPERAND (t
, 0);
10064 if (POINTER_TYPE_P (TREE_TYPE (t
))
10065 && TREE_CODE (from
) == INTEGER_CST
10066 && !integer_zerop (from
))
10068 if (flags
& tf_error
)
10069 error_at (EXPR_LOC_OR_HERE (t
),
10070 "reinterpret_cast from integer to pointer");
10073 return (potential_constant_expression_1
10074 (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
, flags
));
10078 /* -- a unary operator & that is applied to an lvalue that
10079 designates an object with thread or automatic storage
10081 t
= TREE_OPERAND (t
, 0);
10083 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10084 any checking here, as we might dereference the pointer later. If
10085 we remove this code, also remove check_automatic_or_tls. */
10086 i
= check_automatic_or_tls (t
);
10091 if (flags
& tf_error
)
10092 error ("address-of an object %qE with thread local or "
10093 "automatic storage is not a constant expression", t
);
10097 return potential_constant_expression_1 (t
, any
, flags
);
10099 case COMPONENT_REF
:
10100 case BIT_FIELD_REF
:
10103 /* -- a class member access unless its postfix-expression is
10104 of literal type or of pointer to literal type. */
10105 /* This test would be redundant, as it follows from the
10106 postfix-expression being a potential constant expression. */
10107 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
10110 case EXPR_PACK_EXPANSION
:
10111 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t
),
10116 tree x
= TREE_OPERAND (t
, 0);
10118 if (is_this_parameter (x
))
10120 if (DECL_CONTEXT (x
)
10121 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
10123 if (flags
& tf_error
)
10124 error ("use of %<this%> in a constant expression");
10127 if (want_rval
&& DECL_CONTEXT (x
)
10128 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x
)))
10130 if (flags
& tf_error
)
10131 sorry ("use of the value of the object being constructed "
10132 "in a constant expression");
10137 return potential_constant_expression_1 (x
, rval
, flags
);
10141 case DYNAMIC_CAST_EXPR
:
10142 case PSEUDO_DTOR_EXPR
:
10143 case PREINCREMENT_EXPR
:
10144 case POSTINCREMENT_EXPR
:
10145 case PREDECREMENT_EXPR
:
10146 case POSTDECREMENT_EXPR
:
10150 case VEC_DELETE_EXPR
:
10155 case OMP_ATOMIC_READ
:
10156 case OMP_ATOMIC_CAPTURE_OLD
:
10157 case OMP_ATOMIC_CAPTURE_NEW
:
10158 /* GCC internal stuff. */
10161 case WITH_CLEANUP_EXPR
:
10162 case CLEANUP_POINT_EXPR
:
10163 case MUST_NOT_THROW_EXPR
:
10164 case TRY_CATCH_EXPR
:
10165 case STATEMENT_LIST
:
10166 /* Don't bother trying to define a subset of statement-expressions to
10167 be constant-expressions, at least for now. */
10171 case TRANSACTION_EXPR
:
10176 if (flags
& tf_error
)
10177 error ("expression %qE is not a constant-expression", t
);
10181 /* -- a typeid expression whose operand is of polymorphic
10184 tree e
= TREE_OPERAND (t
, 0);
10185 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
10186 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
10188 if (flags
& tf_error
)
10189 error ("typeid-expression is not a constant expression "
10190 "because %qE is of polymorphic type", e
);
10197 /* -- a subtraction where both operands are pointers. */
10198 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
10199 && TYPE_PTR_P (TREE_OPERAND (t
, 1)))
10201 if (flags
& tf_error
)
10202 error ("difference of two pointer expressions is not "
10203 "a constant expression");
10215 /* -- a relational or equality operator where at least
10216 one of the operands is a pointer. */
10217 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
10218 || TYPE_PTR_P (TREE_OPERAND (t
, 1)))
10220 if (flags
& tf_error
)
10221 error ("pointer comparison expression is not a "
10222 "constant expression");
10229 /* A destructor. */
10230 if (TYPE_P (TREE_OPERAND (t
, 0)))
10232 /* else fall through. */
10234 case REALPART_EXPR
:
10235 case IMAGPART_EXPR
:
10238 case FIX_TRUNC_EXPR
:
10242 case TRUTH_NOT_EXPR
:
10243 case FIXED_CONVERT_EXPR
:
10244 case UNARY_PLUS_EXPR
:
10245 return potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
,
10249 case CONST_CAST_EXPR
:
10250 case STATIC_CAST_EXPR
:
10251 case REINTERPRET_CAST_EXPR
:
10252 case IMPLICIT_CONV_EXPR
:
10253 if (cxx_dialect
< cxx11
10254 && !dependent_type_p (TREE_TYPE (t
))
10255 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
10256 /* In C++98, a conversion to non-integral type can't be part of a
10257 constant expression. */
10259 if (flags
& tf_error
)
10260 error ("cast to non-integral type %qT in a constant expression",
10265 return (potential_constant_expression_1
10266 (TREE_OPERAND (t
, 0),
10267 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
, flags
));
10270 case NON_DEPENDENT_EXPR
:
10271 /* For convenience. */
10273 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
10277 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
10281 if (!literal_type_p (TREE_TYPE (t
)))
10283 if (flags
& tf_error
)
10285 error ("temporary of non-literal type %qT in a "
10286 "constant expression", TREE_TYPE (t
));
10287 explain_non_literal_class (TREE_TYPE (t
));
10292 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
10297 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
10298 constructor_elt
*ce
;
10299 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
10300 if (!potential_constant_expression_1 (ce
->value
, want_rval
, flags
))
10307 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
10308 || DECL_P (TREE_PURPOSE (t
)));
10309 if (!potential_constant_expression_1 (TREE_VALUE (t
), want_rval
,
10312 if (TREE_CHAIN (t
) == NULL_TREE
)
10314 return potential_constant_expression_1 (TREE_CHAIN (t
), want_rval
,
10318 case TRUNC_DIV_EXPR
:
10319 case CEIL_DIV_EXPR
:
10320 case FLOOR_DIV_EXPR
:
10321 case ROUND_DIV_EXPR
:
10322 case TRUNC_MOD_EXPR
:
10323 case CEIL_MOD_EXPR
:
10324 case ROUND_MOD_EXPR
:
10326 tree denom
= TREE_OPERAND (t
, 1);
10327 if (!potential_constant_expression_1 (denom
, rval
, flags
))
10329 /* We can't call cxx_eval_outermost_constant_expr on an expression
10330 that hasn't been through fold_non_dependent_expr yet. */
10331 if (!processing_template_decl
)
10332 denom
= cxx_eval_outermost_constant_expr (denom
, true);
10333 if (integer_zerop (denom
))
10335 if (flags
& tf_error
)
10336 error ("division by zero is not a constant-expression");
10342 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
10347 case COMPOUND_EXPR
:
10349 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10350 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10351 introduced by build_call_a. */
10352 tree op0
= TREE_OPERAND (t
, 0);
10353 tree op1
= TREE_OPERAND (t
, 1);
10355 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
10356 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
10357 return potential_constant_expression_1 (op0
, want_rval
, flags
);
10362 /* If the first operand is the non-short-circuit constant, look at
10363 the second operand; otherwise we only care about the first one for
10365 case TRUTH_AND_EXPR
:
10366 case TRUTH_ANDIF_EXPR
:
10367 tmp
= boolean_true_node
;
10369 case TRUTH_OR_EXPR
:
10370 case TRUTH_ORIF_EXPR
:
10371 tmp
= boolean_false_node
;
10374 tree op
= TREE_OPERAND (t
, 0);
10375 if (!potential_constant_expression_1 (op
, rval
, flags
))
10377 if (!processing_template_decl
)
10378 op
= cxx_eval_outermost_constant_expr (op
, true);
10379 if (tree_int_cst_equal (op
, tmp
))
10380 return potential_constant_expression_1 (TREE_OPERAND (t
, 1), rval
, flags
);
10387 case POINTER_PLUS_EXPR
:
10389 case EXACT_DIV_EXPR
:
10399 case TRUTH_XOR_EXPR
:
10400 case UNORDERED_EXPR
:
10411 /* Fall through. */
10413 case ARRAY_RANGE_REF
:
10417 for (i
= 0; i
< 2; ++i
)
10418 if (!potential_constant_expression_1 (TREE_OPERAND (t
, i
),
10423 case ARRAY_NOTATION_REF
:
10427 case VEC_PERM_EXPR
:
10428 for (i
= 0; i
< 3; ++i
)
10429 if (!potential_constant_expression_1 (TREE_OPERAND (t
, i
),
10435 case VEC_COND_EXPR
:
10436 /* If the condition is a known constant, we know which of the legs we
10437 care about; otherwise we only require that the condition and
10438 either of the legs be potentially constant. */
10439 tmp
= TREE_OPERAND (t
, 0);
10440 if (!potential_constant_expression_1 (tmp
, rval
, flags
))
10442 if (!processing_template_decl
)
10443 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
10444 if (integer_zerop (tmp
))
10445 return potential_constant_expression_1 (TREE_OPERAND (t
, 2),
10447 else if (TREE_CODE (tmp
) == INTEGER_CST
)
10448 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
10450 for (i
= 1; i
< 3; ++i
)
10451 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
10452 want_rval
, tf_none
))
10454 if (flags
& tf_error
)
10455 error ("expression %qE is not a constant-expression", t
);
10458 case VEC_INIT_EXPR
:
10459 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
10461 if (flags
& tf_error
)
10463 error ("non-constant array initialization");
10464 diagnose_non_constexpr_vec_init (t
);
10469 if (objc_is_property_ref (t
))
10472 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
10478 /* The main entry point to the above. */
10481 potential_constant_expression (tree t
)
10483 return potential_constant_expression_1 (t
, false, tf_none
);
10486 /* As above, but require a constant rvalue. */
10489 potential_rvalue_constant_expression (tree t
)
10491 return potential_constant_expression_1 (t
, true, tf_none
);
10494 /* Like above, but complain about non-constant expressions. */
10497 require_potential_constant_expression (tree t
)
10499 return potential_constant_expression_1 (t
, false, tf_warning_or_error
);
10502 /* Cross product of the above. */
10505 require_potential_rvalue_constant_expression (tree t
)
10507 return potential_constant_expression_1 (t
, true, tf_warning_or_error
);
10510 /* Insert the deduced return type for an auto function. */
10513 apply_deduced_return_type (tree fco
, tree return_type
)
10517 if (return_type
== error_mark_node
)
10520 if (LAMBDA_FUNCTION_P (fco
))
10522 tree lambda
= CLASSTYPE_LAMBDA_EXPR (current_class_type
);
10523 LAMBDA_EXPR_RETURN_TYPE (lambda
) = return_type
;
10526 if (DECL_CONV_FN_P (fco
))
10527 DECL_NAME (fco
) = mangle_conv_op_name_for_type (return_type
);
10529 TREE_TYPE (fco
) = change_return_type (return_type
, TREE_TYPE (fco
));
10531 result
= DECL_RESULT (fco
);
10532 if (result
== NULL_TREE
)
10534 if (TREE_TYPE (result
) == return_type
)
10537 /* We already have a DECL_RESULT from start_preparsed_function.
10538 Now we need to redo the work it and allocate_struct_function
10539 did to reflect the new type. */
10540 gcc_assert (current_function_decl
== fco
);
10541 result
= build_decl (input_location
, RESULT_DECL
, NULL_TREE
,
10542 TYPE_MAIN_VARIANT (return_type
));
10543 DECL_ARTIFICIAL (result
) = 1;
10544 DECL_IGNORED_P (result
) = 1;
10545 cp_apply_type_quals_to_decl (cp_type_quals (return_type
),
10548 DECL_RESULT (fco
) = result
;
10550 if (!processing_template_decl
)
10552 bool aggr
= aggregate_value_p (result
, fco
);
10553 #ifdef PCC_STATIC_STRUCT_RETURN
10554 cfun
->returns_pcc_struct
= aggr
;
10556 cfun
->returns_struct
= aggr
;
10561 /* DECL is a local variable or parameter from the surrounding scope of a
10562 lambda-expression. Returns the decltype for a use of the capture field
10563 for DECL even if it hasn't been captured yet. */
10566 capture_decltype (tree decl
)
10568 tree lam
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl
));
10569 /* FIXME do lookup instead of list walk? */
10570 tree cap
= value_member (decl
, LAMBDA_EXPR_CAPTURE_LIST (lam
));
10574 type
= TREE_TYPE (TREE_PURPOSE (cap
));
10576 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
))
10579 error ("%qD is not captured", decl
);
10580 return error_mark_node
;
10583 type
= TREE_TYPE (decl
);
10584 if (TREE_CODE (type
) == REFERENCE_TYPE
10585 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
10586 type
= TREE_TYPE (type
);
10589 case CPLD_REFERENCE
:
10590 type
= TREE_TYPE (decl
);
10591 if (TREE_CODE (type
) != REFERENCE_TYPE
)
10592 type
= build_reference_type (TREE_TYPE (decl
));
10596 gcc_unreachable ();
10599 if (TREE_CODE (type
) != REFERENCE_TYPE
)
10601 if (!LAMBDA_EXPR_MUTABLE_P (lam
))
10602 type
= cp_build_qualified_type (type
, (cp_type_quals (type
)
10603 |TYPE_QUAL_CONST
));
10604 type
= build_reference_type (type
);
10609 #include "gt-cp-semantics.h"