[AArch64 Testsuite] Add a test of vldN_dup intrinsics
[official-gcc.git] / gcc / cp / semantics.c
blobaee92ddc1d0adac69600d79fd05c94e9b7f314b6
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-2014 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)
15 any later version.
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/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stmt.h"
32 #include "varasm.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "timevar.h"
43 #include "diagnostic.h"
44 #include "cgraph.h"
45 #include "tree-iterator.h"
46 #include "target.h"
47 #include "hash-table.h"
48 #include "gimplify.h"
49 #include "bitmap.h"
50 #include "omp-low.h"
51 #include "builtins.h"
52 #include "convert.h"
54 static bool verify_constant (tree, bool, bool *, bool *);
55 #define VERIFY_CONSTANT(X) \
56 do { \
57 if (verify_constant ((X), allow_non_constant, non_constant_p, overflow_p)) \
58 return t; \
59 } while (0)
61 /* There routines provide a modular interface to perform many parsing
62 operations. They may therefore be used during actual parsing, or
63 during template instantiation, which may be regarded as a
64 degenerate form of parsing. */
66 static tree maybe_convert_cond (tree);
67 static tree finalize_nrv_r (tree *, int *, void *);
68 static tree capture_decltype (tree);
71 /* Deferred Access Checking Overview
72 ---------------------------------
74 Most C++ expressions and declarations require access checking
75 to be performed during parsing. However, in several cases,
76 this has to be treated differently.
78 For member declarations, access checking has to be deferred
79 until more information about the declaration is known. For
80 example:
82 class A {
83 typedef int X;
84 public:
85 X f();
88 A::X A::f();
89 A::X g();
91 When we are parsing the function return type `A::X', we don't
92 really know if this is allowed until we parse the function name.
94 Furthermore, some contexts require that access checking is
95 never performed at all. These include class heads, and template
96 instantiations.
98 Typical use of access checking functions is described here:
100 1. When we enter a context that requires certain access checking
101 mode, the function `push_deferring_access_checks' is called with
102 DEFERRING argument specifying the desired mode. Access checking
103 may be performed immediately (dk_no_deferred), deferred
104 (dk_deferred), or not performed (dk_no_check).
106 2. When a declaration such as a type, or a variable, is encountered,
107 the function `perform_or_defer_access_check' is called. It
108 maintains a vector of all deferred checks.
110 3. The global `current_class_type' or `current_function_decl' is then
111 setup by the parser. `enforce_access' relies on these information
112 to check access.
114 4. Upon exiting the context mentioned in step 1,
115 `perform_deferred_access_checks' is called to check all declaration
116 stored in the vector. `pop_deferring_access_checks' is then
117 called to restore the previous access checking mode.
119 In case of parsing error, we simply call `pop_deferring_access_checks'
120 without `perform_deferred_access_checks'. */
122 typedef struct GTY(()) deferred_access {
123 /* A vector representing name-lookups for which we have deferred
124 checking access controls. We cannot check the accessibility of
125 names used in a decl-specifier-seq until we know what is being
126 declared because code like:
128 class A {
129 class B {};
130 B* f();
133 A::B* A::f() { return 0; }
135 is valid, even though `A::B' is not generally accessible. */
136 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
138 /* The current mode of access checks. */
139 enum deferring_kind deferring_access_checks_kind;
141 } deferred_access;
143 /* Data for deferred access checking. */
144 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
145 static GTY(()) unsigned deferred_access_no_check;
147 /* Save the current deferred access states and start deferred
148 access checking iff DEFER_P is true. */
150 void
151 push_deferring_access_checks (deferring_kind deferring)
153 /* For context like template instantiation, access checking
154 disabling applies to all nested context. */
155 if (deferred_access_no_check || deferring == dk_no_check)
156 deferred_access_no_check++;
157 else
159 deferred_access e = {NULL, deferring};
160 vec_safe_push (deferred_access_stack, e);
164 /* Save the current deferred access states and start deferred access
165 checking, continuing the set of deferred checks in CHECKS. */
167 void
168 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
170 push_deferring_access_checks (dk_deferred);
171 if (!deferred_access_no_check)
172 deferred_access_stack->last().deferred_access_checks = checks;
175 /* Resume deferring access checks again after we stopped doing
176 this previously. */
178 void
179 resume_deferring_access_checks (void)
181 if (!deferred_access_no_check)
182 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
185 /* Stop deferring access checks. */
187 void
188 stop_deferring_access_checks (void)
190 if (!deferred_access_no_check)
191 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
194 /* Discard the current deferred access checks and restore the
195 previous states. */
197 void
198 pop_deferring_access_checks (void)
200 if (deferred_access_no_check)
201 deferred_access_no_check--;
202 else
203 deferred_access_stack->pop ();
206 /* Returns a TREE_LIST representing the deferred checks.
207 The TREE_PURPOSE of each node is the type through which the
208 access occurred; the TREE_VALUE is the declaration named.
211 vec<deferred_access_check, va_gc> *
212 get_deferred_access_checks (void)
214 if (deferred_access_no_check)
215 return NULL;
216 else
217 return (deferred_access_stack->last().deferred_access_checks);
220 /* Take current deferred checks and combine with the
221 previous states if we also defer checks previously.
222 Otherwise perform checks now. */
224 void
225 pop_to_parent_deferring_access_checks (void)
227 if (deferred_access_no_check)
228 deferred_access_no_check--;
229 else
231 vec<deferred_access_check, va_gc> *checks;
232 deferred_access *ptr;
234 checks = (deferred_access_stack->last ().deferred_access_checks);
236 deferred_access_stack->pop ();
237 ptr = &deferred_access_stack->last ();
238 if (ptr->deferring_access_checks_kind == dk_no_deferred)
240 /* Check access. */
241 perform_access_checks (checks, tf_warning_or_error);
243 else
245 /* Merge with parent. */
246 int i, j;
247 deferred_access_check *chk, *probe;
249 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
251 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
253 if (probe->binfo == chk->binfo &&
254 probe->decl == chk->decl &&
255 probe->diag_decl == chk->diag_decl)
256 goto found;
258 /* Insert into parent's checks. */
259 vec_safe_push (ptr->deferred_access_checks, *chk);
260 found:;
266 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
267 is the BINFO indicating the qualifying scope used to access the
268 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
269 or we aren't in SFINAE context or all the checks succeed return TRUE,
270 otherwise FALSE. */
272 bool
273 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
274 tsubst_flags_t complain)
276 int i;
277 deferred_access_check *chk;
278 location_t loc = input_location;
279 bool ok = true;
281 if (!checks)
282 return true;
284 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
286 input_location = chk->loc;
287 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
290 input_location = loc;
291 return (complain & tf_error) ? true : ok;
294 /* Perform the deferred access checks.
296 After performing the checks, we still have to keep the list
297 `deferred_access_stack->deferred_access_checks' since we may want
298 to check access for them again later in a different context.
299 For example:
301 class A {
302 typedef int X;
303 static X a;
305 A::X A::a, x; // No error for `A::a', error for `x'
307 We have to perform deferred access of `A::X', first with `A::a',
308 next with `x'. Return value like perform_access_checks above. */
310 bool
311 perform_deferred_access_checks (tsubst_flags_t complain)
313 return perform_access_checks (get_deferred_access_checks (), complain);
316 /* Defer checking the accessibility of DECL, when looked up in
317 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
318 Return value like perform_access_checks above. */
320 bool
321 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
322 tsubst_flags_t complain)
324 int i;
325 deferred_access *ptr;
326 deferred_access_check *chk;
329 /* Exit if we are in a context that no access checking is performed.
331 if (deferred_access_no_check)
332 return true;
334 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
336 ptr = &deferred_access_stack->last ();
338 /* If we are not supposed to defer access checks, just check now. */
339 if (ptr->deferring_access_checks_kind == dk_no_deferred)
341 bool ok = enforce_access (binfo, decl, diag_decl, complain);
342 return (complain & tf_error) ? true : ok;
345 /* See if we are already going to perform this check. */
346 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
348 if (chk->decl == decl && chk->binfo == binfo &&
349 chk->diag_decl == diag_decl)
351 return true;
354 /* If not, record the check. */
355 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
356 vec_safe_push (ptr->deferred_access_checks, new_access);
358 return true;
361 /* Returns nonzero if the current statement is a full expression,
362 i.e. temporaries created during that statement should be destroyed
363 at the end of the statement. */
366 stmts_are_full_exprs_p (void)
368 return current_stmt_tree ()->stmts_are_full_exprs_p;
371 /* T is a statement. Add it to the statement-tree. This is the C++
372 version. The C/ObjC frontends have a slightly different version of
373 this function. */
375 tree
376 add_stmt (tree t)
378 enum tree_code code = TREE_CODE (t);
380 if (EXPR_P (t) && code != LABEL_EXPR)
382 if (!EXPR_HAS_LOCATION (t))
383 SET_EXPR_LOCATION (t, input_location);
385 /* When we expand a statement-tree, we must know whether or not the
386 statements are full-expressions. We record that fact here. */
387 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
390 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
391 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
393 /* Add T to the statement-tree. Non-side-effect statements need to be
394 recorded during statement expressions. */
395 gcc_checking_assert (!stmt_list_stack->is_empty ());
396 append_to_statement_list_force (t, &cur_stmt_list);
398 return t;
401 /* Returns the stmt_tree to which statements are currently being added. */
403 stmt_tree
404 current_stmt_tree (void)
406 return (cfun
407 ? &cfun->language->base.x_stmt_tree
408 : &scope_chain->x_stmt_tree);
411 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
413 static tree
414 maybe_cleanup_point_expr (tree expr)
416 if (!processing_template_decl && stmts_are_full_exprs_p ())
417 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
418 return expr;
421 /* Like maybe_cleanup_point_expr except have the type of the new expression be
422 void so we don't need to create a temporary variable to hold the inner
423 expression. The reason why we do this is because the original type might be
424 an aggregate and we cannot create a temporary variable for that type. */
426 tree
427 maybe_cleanup_point_expr_void (tree expr)
429 if (!processing_template_decl && stmts_are_full_exprs_p ())
430 expr = fold_build_cleanup_point_expr (void_type_node, expr);
431 return expr;
436 /* Create a declaration statement for the declaration given by the DECL. */
438 void
439 add_decl_expr (tree decl)
441 tree r = build_stmt (input_location, DECL_EXPR, decl);
442 if (DECL_INITIAL (decl)
443 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
444 r = maybe_cleanup_point_expr_void (r);
445 add_stmt (r);
448 /* Finish a scope. */
450 tree
451 do_poplevel (tree stmt_list)
453 tree block = NULL;
455 if (stmts_are_full_exprs_p ())
456 block = poplevel (kept_level_p (), 1, 0);
458 stmt_list = pop_stmt_list (stmt_list);
460 if (!processing_template_decl)
462 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
463 /* ??? See c_end_compound_stmt re statement expressions. */
466 return stmt_list;
469 /* Begin a new scope. */
471 static tree
472 do_pushlevel (scope_kind sk)
474 tree ret = push_stmt_list ();
475 if (stmts_are_full_exprs_p ())
476 begin_scope (sk, NULL);
477 return ret;
480 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
481 when the current scope is exited. EH_ONLY is true when this is not
482 meant to apply to normal control flow transfer. */
484 void
485 push_cleanup (tree decl, tree cleanup, bool eh_only)
487 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
488 CLEANUP_EH_ONLY (stmt) = eh_only;
489 add_stmt (stmt);
490 CLEANUP_BODY (stmt) = push_stmt_list ();
493 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
494 the current loops, represented by 'NULL_TREE' if we've seen a possible
495 exit, and 'error_mark_node' if not. This is currently used only to
496 suppress the warning about a function with no return statements, and
497 therefore we don't bother noting returns as possible exits. We also
498 don't bother with gotos. */
500 static void
501 begin_maybe_infinite_loop (tree cond)
503 /* Only track this while parsing a function, not during instantiation. */
504 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
505 && !processing_template_decl))
506 return;
507 bool maybe_infinite = true;
508 if (cond)
510 cond = fold_non_dependent_expr_sfinae (cond, tf_none);
511 cond = maybe_constant_value (cond);
512 maybe_infinite = integer_nonzerop (cond);
514 vec_safe_push (cp_function_chain->infinite_loops,
515 maybe_infinite ? error_mark_node : NULL_TREE);
519 /* A break is a possible exit for the current loop. */
521 void
522 break_maybe_infinite_loop (void)
524 if (!cfun)
525 return;
526 cp_function_chain->infinite_loops->last() = NULL_TREE;
529 /* If we reach the end of the loop without seeing a possible exit, we have
530 an infinite loop. */
532 static void
533 end_maybe_infinite_loop (tree cond)
535 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
536 && !processing_template_decl))
537 return;
538 tree current = cp_function_chain->infinite_loops->pop();
539 if (current != NULL_TREE)
541 cond = fold_non_dependent_expr (cond);
542 cond = maybe_constant_value (cond);
543 if (integer_nonzerop (cond))
544 current_function_infinite_loop = 1;
549 /* Begin a conditional that might contain a declaration. When generating
550 normal code, we want the declaration to appear before the statement
551 containing the conditional. When generating template code, we want the
552 conditional to be rendered as the raw DECL_EXPR. */
554 static void
555 begin_cond (tree *cond_p)
557 if (processing_template_decl)
558 *cond_p = push_stmt_list ();
561 /* Finish such a conditional. */
563 static void
564 finish_cond (tree *cond_p, tree expr)
566 if (processing_template_decl)
568 tree cond = pop_stmt_list (*cond_p);
570 if (expr == NULL_TREE)
571 /* Empty condition in 'for'. */
572 gcc_assert (empty_expr_stmt_p (cond));
573 else if (check_for_bare_parameter_packs (expr))
574 expr = error_mark_node;
575 else if (!empty_expr_stmt_p (cond))
576 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
578 *cond_p = expr;
581 /* If *COND_P specifies a conditional with a declaration, transform the
582 loop such that
583 while (A x = 42) { }
584 for (; A x = 42;) { }
585 becomes
586 while (true) { A x = 42; if (!x) break; }
587 for (;;) { A x = 42; if (!x) break; }
588 The statement list for BODY will be empty if the conditional did
589 not declare anything. */
591 static void
592 simplify_loop_decl_cond (tree *cond_p, tree body)
594 tree cond, if_stmt;
596 if (!TREE_SIDE_EFFECTS (body))
597 return;
599 cond = *cond_p;
600 *cond_p = boolean_true_node;
602 if_stmt = begin_if_stmt ();
603 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
604 finish_if_stmt_cond (cond, if_stmt);
605 finish_break_stmt ();
606 finish_then_clause (if_stmt);
607 finish_if_stmt (if_stmt);
610 /* Finish a goto-statement. */
612 tree
613 finish_goto_stmt (tree destination)
615 if (identifier_p (destination))
616 destination = lookup_label (destination);
618 /* We warn about unused labels with -Wunused. That means we have to
619 mark the used labels as used. */
620 if (TREE_CODE (destination) == LABEL_DECL)
621 TREE_USED (destination) = 1;
622 else
624 destination = mark_rvalue_use (destination);
625 if (!processing_template_decl)
627 destination = cp_convert (ptr_type_node, destination,
628 tf_warning_or_error);
629 if (error_operand_p (destination))
630 return NULL_TREE;
631 destination
632 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
633 destination);
637 check_goto (destination);
639 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
642 /* COND is the condition-expression for an if, while, etc.,
643 statement. Convert it to a boolean value, if appropriate.
644 In addition, verify sequence points if -Wsequence-point is enabled. */
646 static tree
647 maybe_convert_cond (tree cond)
649 /* Empty conditions remain empty. */
650 if (!cond)
651 return NULL_TREE;
653 /* Wait until we instantiate templates before doing conversion. */
654 if (processing_template_decl)
655 return cond;
657 if (warn_sequence_point)
658 verify_sequence_points (cond);
660 /* Do the conversion. */
661 cond = convert_from_reference (cond);
663 if (TREE_CODE (cond) == MODIFY_EXPR
664 && !TREE_NO_WARNING (cond)
665 && warn_parentheses)
667 warning (OPT_Wparentheses,
668 "suggest parentheses around assignment used as truth value");
669 TREE_NO_WARNING (cond) = 1;
672 return condition_conversion (cond);
675 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
677 tree
678 finish_expr_stmt (tree expr)
680 tree r = NULL_TREE;
682 if (expr != NULL_TREE)
684 if (!processing_template_decl)
686 if (warn_sequence_point)
687 verify_sequence_points (expr);
688 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
690 else if (!type_dependent_expression_p (expr))
691 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
692 tf_warning_or_error);
694 if (check_for_bare_parameter_packs (expr))
695 expr = error_mark_node;
697 /* Simplification of inner statement expressions, compound exprs,
698 etc can result in us already having an EXPR_STMT. */
699 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
701 if (TREE_CODE (expr) != EXPR_STMT)
702 expr = build_stmt (input_location, EXPR_STMT, expr);
703 expr = maybe_cleanup_point_expr_void (expr);
706 r = add_stmt (expr);
709 return r;
713 /* Begin an if-statement. Returns a newly created IF_STMT if
714 appropriate. */
716 tree
717 begin_if_stmt (void)
719 tree r, scope;
720 scope = do_pushlevel (sk_cond);
721 r = build_stmt (input_location, IF_STMT, NULL_TREE,
722 NULL_TREE, NULL_TREE, scope);
723 begin_cond (&IF_COND (r));
724 return r;
727 /* Process the COND of an if-statement, which may be given by
728 IF_STMT. */
730 void
731 finish_if_stmt_cond (tree cond, tree if_stmt)
733 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
734 add_stmt (if_stmt);
735 THEN_CLAUSE (if_stmt) = push_stmt_list ();
738 /* Finish the then-clause of an if-statement, which may be given by
739 IF_STMT. */
741 tree
742 finish_then_clause (tree if_stmt)
744 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
745 return if_stmt;
748 /* Begin the else-clause of an if-statement. */
750 void
751 begin_else_clause (tree if_stmt)
753 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
756 /* Finish the else-clause of an if-statement, which may be given by
757 IF_STMT. */
759 void
760 finish_else_clause (tree if_stmt)
762 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
765 /* Finish an if-statement. */
767 void
768 finish_if_stmt (tree if_stmt)
770 tree scope = IF_SCOPE (if_stmt);
771 IF_SCOPE (if_stmt) = NULL;
772 add_stmt (do_poplevel (scope));
775 /* Begin a while-statement. Returns a newly created WHILE_STMT if
776 appropriate. */
778 tree
779 begin_while_stmt (void)
781 tree r;
782 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
783 add_stmt (r);
784 WHILE_BODY (r) = do_pushlevel (sk_block);
785 begin_cond (&WHILE_COND (r));
786 return r;
789 /* Process the COND of a while-statement, which may be given by
790 WHILE_STMT. */
792 void
793 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
795 cond = maybe_convert_cond (cond);
796 finish_cond (&WHILE_COND (while_stmt), cond);
797 begin_maybe_infinite_loop (cond);
798 if (ivdep && cond != error_mark_node)
799 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
800 TREE_TYPE (WHILE_COND (while_stmt)),
801 WHILE_COND (while_stmt),
802 build_int_cst (integer_type_node,
803 annot_expr_ivdep_kind));
804 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
807 /* Finish a while-statement, which may be given by WHILE_STMT. */
809 void
810 finish_while_stmt (tree while_stmt)
812 end_maybe_infinite_loop (boolean_true_node);
813 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
816 /* Begin a do-statement. Returns a newly created DO_STMT if
817 appropriate. */
819 tree
820 begin_do_stmt (void)
822 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
823 begin_maybe_infinite_loop (boolean_true_node);
824 add_stmt (r);
825 DO_BODY (r) = push_stmt_list ();
826 return r;
829 /* Finish the body of a do-statement, which may be given by DO_STMT. */
831 void
832 finish_do_body (tree do_stmt)
834 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
836 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
837 body = STATEMENT_LIST_TAIL (body)->stmt;
839 if (IS_EMPTY_STMT (body))
840 warning (OPT_Wempty_body,
841 "suggest explicit braces around empty body in %<do%> statement");
844 /* Finish a do-statement, which may be given by DO_STMT, and whose
845 COND is as indicated. */
847 void
848 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
850 cond = maybe_convert_cond (cond);
851 end_maybe_infinite_loop (cond);
852 if (ivdep && cond != error_mark_node)
853 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
854 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
855 DO_COND (do_stmt) = cond;
858 /* Finish a return-statement. The EXPRESSION returned, if any, is as
859 indicated. */
861 tree
862 finish_return_stmt (tree expr)
864 tree r;
865 bool no_warning;
867 expr = check_return_expr (expr, &no_warning);
869 if (error_operand_p (expr)
870 || (flag_openmp && !check_omp_return ()))
871 return error_mark_node;
872 if (!processing_template_decl)
874 if (warn_sequence_point)
875 verify_sequence_points (expr);
877 if (DECL_DESTRUCTOR_P (current_function_decl)
878 || (DECL_CONSTRUCTOR_P (current_function_decl)
879 && targetm.cxx.cdtor_returns_this ()))
881 /* Similarly, all destructors must run destructors for
882 base-classes before returning. So, all returns in a
883 destructor get sent to the DTOR_LABEL; finish_function emits
884 code to return a value there. */
885 return finish_goto_stmt (cdtor_label);
889 r = build_stmt (input_location, RETURN_EXPR, expr);
890 TREE_NO_WARNING (r) |= no_warning;
891 r = maybe_cleanup_point_expr_void (r);
892 r = add_stmt (r);
894 return r;
897 /* Begin the scope of a for-statement or a range-for-statement.
898 Both the returned trees are to be used in a call to
899 begin_for_stmt or begin_range_for_stmt. */
901 tree
902 begin_for_scope (tree *init)
904 tree scope = NULL_TREE;
905 if (flag_new_for_scope > 0)
906 scope = do_pushlevel (sk_for);
908 if (processing_template_decl)
909 *init = push_stmt_list ();
910 else
911 *init = NULL_TREE;
913 return scope;
916 /* Begin a for-statement. Returns a new FOR_STMT.
917 SCOPE and INIT should be the return of begin_for_scope,
918 or both NULL_TREE */
920 tree
921 begin_for_stmt (tree scope, tree init)
923 tree r;
925 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
926 NULL_TREE, NULL_TREE, NULL_TREE);
928 if (scope == NULL_TREE)
930 gcc_assert (!init || !(flag_new_for_scope > 0));
931 if (!init)
932 scope = begin_for_scope (&init);
934 FOR_INIT_STMT (r) = init;
935 FOR_SCOPE (r) = scope;
937 return r;
940 /* Finish the for-init-statement of a for-statement, which may be
941 given by FOR_STMT. */
943 void
944 finish_for_init_stmt (tree for_stmt)
946 if (processing_template_decl)
947 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
948 add_stmt (for_stmt);
949 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
950 begin_cond (&FOR_COND (for_stmt));
953 /* Finish the COND of a for-statement, which may be given by
954 FOR_STMT. */
956 void
957 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
959 cond = maybe_convert_cond (cond);
960 finish_cond (&FOR_COND (for_stmt), cond);
961 begin_maybe_infinite_loop (cond);
962 if (ivdep && cond != error_mark_node)
963 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
964 TREE_TYPE (FOR_COND (for_stmt)),
965 FOR_COND (for_stmt),
966 build_int_cst (integer_type_node,
967 annot_expr_ivdep_kind));
968 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
971 /* Finish the increment-EXPRESSION in a for-statement, which may be
972 given by FOR_STMT. */
974 void
975 finish_for_expr (tree expr, tree for_stmt)
977 if (!expr)
978 return;
979 /* If EXPR is an overloaded function, issue an error; there is no
980 context available to use to perform overload resolution. */
981 if (type_unknown_p (expr))
983 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
984 expr = error_mark_node;
986 if (!processing_template_decl)
988 if (warn_sequence_point)
989 verify_sequence_points (expr);
990 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
991 tf_warning_or_error);
993 else if (!type_dependent_expression_p (expr))
994 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
995 tf_warning_or_error);
996 expr = maybe_cleanup_point_expr_void (expr);
997 if (check_for_bare_parameter_packs (expr))
998 expr = error_mark_node;
999 FOR_EXPR (for_stmt) = expr;
1002 /* Finish the body of a for-statement, which may be given by
1003 FOR_STMT. The increment-EXPR for the loop must be
1004 provided.
1005 It can also finish RANGE_FOR_STMT. */
1007 void
1008 finish_for_stmt (tree for_stmt)
1010 end_maybe_infinite_loop (boolean_true_node);
1012 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1013 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1014 else
1015 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1017 /* Pop the scope for the body of the loop. */
1018 if (flag_new_for_scope > 0)
1020 tree scope;
1021 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1022 ? &RANGE_FOR_SCOPE (for_stmt)
1023 : &FOR_SCOPE (for_stmt));
1024 scope = *scope_ptr;
1025 *scope_ptr = NULL;
1026 add_stmt (do_poplevel (scope));
1030 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1031 SCOPE and INIT should be the return of begin_for_scope,
1032 or both NULL_TREE .
1033 To finish it call finish_for_stmt(). */
1035 tree
1036 begin_range_for_stmt (tree scope, tree init)
1038 tree r;
1040 begin_maybe_infinite_loop (boolean_false_node);
1042 r = build_stmt (input_location, RANGE_FOR_STMT,
1043 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1045 if (scope == NULL_TREE)
1047 gcc_assert (!init || !(flag_new_for_scope > 0));
1048 if (!init)
1049 scope = begin_for_scope (&init);
1052 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1053 pop it now. */
1054 if (init)
1055 pop_stmt_list (init);
1056 RANGE_FOR_SCOPE (r) = scope;
1058 return r;
1061 /* Finish the head of a range-based for statement, which may
1062 be given by RANGE_FOR_STMT. DECL must be the declaration
1063 and EXPR must be the loop expression. */
1065 void
1066 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1068 RANGE_FOR_DECL (range_for_stmt) = decl;
1069 RANGE_FOR_EXPR (range_for_stmt) = expr;
1070 add_stmt (range_for_stmt);
1071 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1074 /* Finish a break-statement. */
1076 tree
1077 finish_break_stmt (void)
1079 /* In switch statements break is sometimes stylistically used after
1080 a return statement. This can lead to spurious warnings about
1081 control reaching the end of a non-void function when it is
1082 inlined. Note that we are calling block_may_fallthru with
1083 language specific tree nodes; this works because
1084 block_may_fallthru returns true when given something it does not
1085 understand. */
1086 if (!block_may_fallthru (cur_stmt_list))
1087 return void_node;
1088 return add_stmt (build_stmt (input_location, BREAK_STMT));
1091 /* Finish a continue-statement. */
1093 tree
1094 finish_continue_stmt (void)
1096 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1099 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1100 appropriate. */
1102 tree
1103 begin_switch_stmt (void)
1105 tree r, scope;
1107 scope = do_pushlevel (sk_cond);
1108 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1110 begin_cond (&SWITCH_STMT_COND (r));
1112 return r;
1115 /* Finish the cond of a switch-statement. */
1117 void
1118 finish_switch_cond (tree cond, tree switch_stmt)
1120 tree orig_type = NULL;
1121 if (!processing_template_decl)
1123 /* Convert the condition to an integer or enumeration type. */
1124 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1125 if (cond == NULL_TREE)
1127 error ("switch quantity not an integer");
1128 cond = error_mark_node;
1130 orig_type = TREE_TYPE (cond);
1131 if (cond != error_mark_node)
1133 /* Warn if the condition has boolean value. */
1134 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1135 warning_at (input_location, OPT_Wswitch_bool,
1136 "switch condition has type bool");
1138 /* [stmt.switch]
1140 Integral promotions are performed. */
1141 cond = perform_integral_promotions (cond);
1142 cond = maybe_cleanup_point_expr (cond);
1145 if (check_for_bare_parameter_packs (cond))
1146 cond = error_mark_node;
1147 else if (!processing_template_decl && warn_sequence_point)
1148 verify_sequence_points (cond);
1150 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1151 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1152 add_stmt (switch_stmt);
1153 push_switch (switch_stmt);
1154 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1157 /* Finish the body of a switch-statement, which may be given by
1158 SWITCH_STMT. The COND to switch on is indicated. */
1160 void
1161 finish_switch_stmt (tree switch_stmt)
1163 tree scope;
1165 SWITCH_STMT_BODY (switch_stmt) =
1166 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1167 pop_switch ();
1169 scope = SWITCH_STMT_SCOPE (switch_stmt);
1170 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1171 add_stmt (do_poplevel (scope));
1174 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1175 appropriate. */
1177 tree
1178 begin_try_block (void)
1180 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1181 add_stmt (r);
1182 TRY_STMTS (r) = push_stmt_list ();
1183 return r;
1186 /* Likewise, for a function-try-block. The block returned in
1187 *COMPOUND_STMT is an artificial outer scope, containing the
1188 function-try-block. */
1190 tree
1191 begin_function_try_block (tree *compound_stmt)
1193 tree r;
1194 /* This outer scope does not exist in the C++ standard, but we need
1195 a place to put __FUNCTION__ and similar variables. */
1196 *compound_stmt = begin_compound_stmt (0);
1197 r = begin_try_block ();
1198 FN_TRY_BLOCK_P (r) = 1;
1199 return r;
1202 /* Finish a try-block, which may be given by TRY_BLOCK. */
1204 void
1205 finish_try_block (tree try_block)
1207 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1208 TRY_HANDLERS (try_block) = push_stmt_list ();
1211 /* Finish the body of a cleanup try-block, which may be given by
1212 TRY_BLOCK. */
1214 void
1215 finish_cleanup_try_block (tree try_block)
1217 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1220 /* Finish an implicitly generated try-block, with a cleanup is given
1221 by CLEANUP. */
1223 void
1224 finish_cleanup (tree cleanup, tree try_block)
1226 TRY_HANDLERS (try_block) = cleanup;
1227 CLEANUP_P (try_block) = 1;
1230 /* Likewise, for a function-try-block. */
1232 void
1233 finish_function_try_block (tree try_block)
1235 finish_try_block (try_block);
1236 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1237 the try block, but moving it inside. */
1238 in_function_try_handler = 1;
1241 /* Finish a handler-sequence for a try-block, which may be given by
1242 TRY_BLOCK. */
1244 void
1245 finish_handler_sequence (tree try_block)
1247 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1248 check_handlers (TRY_HANDLERS (try_block));
1251 /* Finish the handler-seq for a function-try-block, given by
1252 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1253 begin_function_try_block. */
1255 void
1256 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1258 in_function_try_handler = 0;
1259 finish_handler_sequence (try_block);
1260 finish_compound_stmt (compound_stmt);
1263 /* Begin a handler. Returns a HANDLER if appropriate. */
1265 tree
1266 begin_handler (void)
1268 tree r;
1270 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1271 add_stmt (r);
1273 /* Create a binding level for the eh_info and the exception object
1274 cleanup. */
1275 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1277 return r;
1280 /* Finish the handler-parameters for a handler, which may be given by
1281 HANDLER. DECL is the declaration for the catch parameter, or NULL
1282 if this is a `catch (...)' clause. */
1284 void
1285 finish_handler_parms (tree decl, tree handler)
1287 tree type = NULL_TREE;
1288 if (processing_template_decl)
1290 if (decl)
1292 decl = pushdecl (decl);
1293 decl = push_template_decl (decl);
1294 HANDLER_PARMS (handler) = decl;
1295 type = TREE_TYPE (decl);
1298 else
1299 type = expand_start_catch_block (decl);
1300 HANDLER_TYPE (handler) = type;
1303 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1304 the return value from the matching call to finish_handler_parms. */
1306 void
1307 finish_handler (tree handler)
1309 if (!processing_template_decl)
1310 expand_end_catch_block ();
1311 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1314 /* Begin a compound statement. FLAGS contains some bits that control the
1315 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1316 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1317 block of a function. If BCS_TRY_BLOCK is set, this is the block
1318 created on behalf of a TRY statement. Returns a token to be passed to
1319 finish_compound_stmt. */
1321 tree
1322 begin_compound_stmt (unsigned int flags)
1324 tree r;
1326 if (flags & BCS_NO_SCOPE)
1328 r = push_stmt_list ();
1329 STATEMENT_LIST_NO_SCOPE (r) = 1;
1331 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1332 But, if it's a statement-expression with a scopeless block, there's
1333 nothing to keep, and we don't want to accidentally keep a block
1334 *inside* the scopeless block. */
1335 keep_next_level (false);
1337 else
1338 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1340 /* When processing a template, we need to remember where the braces were,
1341 so that we can set up identical scopes when instantiating the template
1342 later. BIND_EXPR is a handy candidate for this.
1343 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1344 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1345 processing templates. */
1346 if (processing_template_decl)
1348 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1349 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1350 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1351 TREE_SIDE_EFFECTS (r) = 1;
1354 return r;
1357 /* Finish a compound-statement, which is given by STMT. */
1359 void
1360 finish_compound_stmt (tree stmt)
1362 if (TREE_CODE (stmt) == BIND_EXPR)
1364 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1365 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1366 discard the BIND_EXPR so it can be merged with the containing
1367 STATEMENT_LIST. */
1368 if (TREE_CODE (body) == STATEMENT_LIST
1369 && STATEMENT_LIST_HEAD (body) == NULL
1370 && !BIND_EXPR_BODY_BLOCK (stmt)
1371 && !BIND_EXPR_TRY_BLOCK (stmt))
1372 stmt = body;
1373 else
1374 BIND_EXPR_BODY (stmt) = body;
1376 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1377 stmt = pop_stmt_list (stmt);
1378 else
1380 /* Destroy any ObjC "super" receivers that may have been
1381 created. */
1382 objc_clear_super_receiver ();
1384 stmt = do_poplevel (stmt);
1387 /* ??? See c_end_compound_stmt wrt statement expressions. */
1388 add_stmt (stmt);
1391 /* Finish an asm-statement, whose components are a STRING, some
1392 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1393 LABELS. Also note whether the asm-statement should be
1394 considered volatile. */
1396 tree
1397 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1398 tree input_operands, tree clobbers, tree labels)
1400 tree r;
1401 tree t;
1402 int ninputs = list_length (input_operands);
1403 int noutputs = list_length (output_operands);
1405 if (!processing_template_decl)
1407 const char *constraint;
1408 const char **oconstraints;
1409 bool allows_mem, allows_reg, is_inout;
1410 tree operand;
1411 int i;
1413 oconstraints = XALLOCAVEC (const char *, noutputs);
1415 string = resolve_asm_operand_names (string, output_operands,
1416 input_operands, labels);
1418 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1420 operand = TREE_VALUE (t);
1422 /* ??? Really, this should not be here. Users should be using a
1423 proper lvalue, dammit. But there's a long history of using
1424 casts in the output operands. In cases like longlong.h, this
1425 becomes a primitive form of typechecking -- if the cast can be
1426 removed, then the output operand had a type of the proper width;
1427 otherwise we'll get an error. Gross, but ... */
1428 STRIP_NOPS (operand);
1430 operand = mark_lvalue_use (operand);
1432 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1433 operand = error_mark_node;
1435 if (operand != error_mark_node
1436 && (TREE_READONLY (operand)
1437 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1438 /* Functions are not modifiable, even though they are
1439 lvalues. */
1440 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1441 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1442 /* If it's an aggregate and any field is const, then it is
1443 effectively const. */
1444 || (CLASS_TYPE_P (TREE_TYPE (operand))
1445 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1446 cxx_readonly_error (operand, lv_asm);
1448 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1449 oconstraints[i] = constraint;
1451 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1452 &allows_mem, &allows_reg, &is_inout))
1454 /* If the operand is going to end up in memory,
1455 mark it addressable. */
1456 if (!allows_reg && !cxx_mark_addressable (operand))
1457 operand = error_mark_node;
1459 else
1460 operand = error_mark_node;
1462 TREE_VALUE (t) = operand;
1465 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1467 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1468 bool constraint_parsed
1469 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1470 oconstraints, &allows_mem, &allows_reg);
1471 /* If the operand is going to end up in memory, don't call
1472 decay_conversion. */
1473 if (constraint_parsed && !allows_reg && allows_mem)
1474 operand = mark_lvalue_use (TREE_VALUE (t));
1475 else
1476 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1478 /* If the type of the operand hasn't been determined (e.g.,
1479 because it involves an overloaded function), then issue
1480 an error message. There's no context available to
1481 resolve the overloading. */
1482 if (TREE_TYPE (operand) == unknown_type_node)
1484 error ("type of asm operand %qE could not be determined",
1485 TREE_VALUE (t));
1486 operand = error_mark_node;
1489 if (constraint_parsed)
1491 /* If the operand is going to end up in memory,
1492 mark it addressable. */
1493 if (!allows_reg && allows_mem)
1495 /* Strip the nops as we allow this case. FIXME, this really
1496 should be rejected or made deprecated. */
1497 STRIP_NOPS (operand);
1498 if (!cxx_mark_addressable (operand))
1499 operand = error_mark_node;
1501 else if (!allows_reg && !allows_mem)
1503 /* If constraint allows neither register nor memory,
1504 try harder to get a constant. */
1505 tree constop = maybe_constant_value (operand);
1506 if (TREE_CONSTANT (constop))
1507 operand = constop;
1510 else
1511 operand = error_mark_node;
1513 TREE_VALUE (t) = operand;
1517 r = build_stmt (input_location, ASM_EXPR, string,
1518 output_operands, input_operands,
1519 clobbers, labels);
1520 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1521 r = maybe_cleanup_point_expr_void (r);
1522 return add_stmt (r);
1525 /* Finish a label with the indicated NAME. Returns the new label. */
1527 tree
1528 finish_label_stmt (tree name)
1530 tree decl = define_label (input_location, name);
1532 if (decl == error_mark_node)
1533 return error_mark_node;
1535 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1537 return decl;
1540 /* Finish a series of declarations for local labels. G++ allows users
1541 to declare "local" labels, i.e., labels with scope. This extension
1542 is useful when writing code involving statement-expressions. */
1544 void
1545 finish_label_decl (tree name)
1547 if (!at_function_scope_p ())
1549 error ("__label__ declarations are only allowed in function scopes");
1550 return;
1553 add_decl_expr (declare_local_label (name));
1556 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1558 void
1559 finish_decl_cleanup (tree decl, tree cleanup)
1561 push_cleanup (decl, cleanup, false);
1564 /* If the current scope exits with an exception, run CLEANUP. */
1566 void
1567 finish_eh_cleanup (tree cleanup)
1569 push_cleanup (NULL, cleanup, true);
1572 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1573 order they were written by the user. Each node is as for
1574 emit_mem_initializers. */
1576 void
1577 finish_mem_initializers (tree mem_inits)
1579 /* Reorder the MEM_INITS so that they are in the order they appeared
1580 in the source program. */
1581 mem_inits = nreverse (mem_inits);
1583 if (processing_template_decl)
1585 tree mem;
1587 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1589 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1590 check for bare parameter packs in the TREE_VALUE, because
1591 any parameter packs in the TREE_VALUE have already been
1592 bound as part of the TREE_PURPOSE. See
1593 make_pack_expansion for more information. */
1594 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1595 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1596 TREE_VALUE (mem) = error_mark_node;
1599 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1600 CTOR_INITIALIZER, mem_inits));
1602 else
1603 emit_mem_initializers (mem_inits);
1606 /* Obfuscate EXPR if it looks like an id-expression or member access so
1607 that the call to finish_decltype in do_auto_deduction will give the
1608 right result. */
1610 tree
1611 force_paren_expr (tree expr)
1613 /* This is only needed for decltype(auto) in C++14. */
1614 if (cxx_dialect < cxx14)
1615 return expr;
1617 /* If we're in unevaluated context, we can't be deducing a
1618 return/initializer type, so we don't need to mess with this. */
1619 if (cp_unevaluated_operand)
1620 return expr;
1622 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1623 && TREE_CODE (expr) != SCOPE_REF)
1624 return expr;
1626 if (TREE_CODE (expr) == COMPONENT_REF)
1627 REF_PARENTHESIZED_P (expr) = true;
1628 else if (type_dependent_expression_p (expr))
1629 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1630 else
1632 cp_lvalue_kind kind = lvalue_kind (expr);
1633 if ((kind & ~clk_class) != clk_none)
1635 tree type = unlowered_expr_type (expr);
1636 bool rval = !!(kind & clk_rvalueref);
1637 type = cp_build_reference_type (type, rval);
1638 expr = build_static_cast (type, expr, tf_error);
1642 return expr;
1645 /* Finish a parenthesized expression EXPR. */
1647 tree
1648 finish_parenthesized_expr (tree expr)
1650 if (EXPR_P (expr))
1651 /* This inhibits warnings in c_common_truthvalue_conversion. */
1652 TREE_NO_WARNING (expr) = 1;
1654 if (TREE_CODE (expr) == OFFSET_REF
1655 || TREE_CODE (expr) == SCOPE_REF)
1656 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1657 enclosed in parentheses. */
1658 PTRMEM_OK_P (expr) = 0;
1660 if (TREE_CODE (expr) == STRING_CST)
1661 PAREN_STRING_LITERAL_P (expr) = 1;
1663 expr = force_paren_expr (expr);
1665 return expr;
1668 /* Finish a reference to a non-static data member (DECL) that is not
1669 preceded by `.' or `->'. */
1671 tree
1672 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1674 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1676 if (!object)
1678 tree scope = qualifying_scope;
1679 if (scope == NULL_TREE)
1680 scope = context_for_name_lookup (decl);
1681 object = maybe_dummy_object (scope, NULL);
1684 object = maybe_resolve_dummy (object, true);
1685 if (object == error_mark_node)
1686 return error_mark_node;
1688 /* DR 613: Can use non-static data members without an associated
1689 object in sizeof/decltype/alignof. */
1690 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1691 && (!processing_template_decl || !current_class_ref))
1693 if (current_function_decl
1694 && DECL_STATIC_FUNCTION_P (current_function_decl))
1695 error ("invalid use of member %q+D in static member function", decl);
1696 else
1697 error ("invalid use of non-static data member %q+D", decl);
1698 error ("from this location");
1700 return error_mark_node;
1703 if (current_class_ptr)
1704 TREE_USED (current_class_ptr) = 1;
1705 if (processing_template_decl && !qualifying_scope)
1707 tree type = TREE_TYPE (decl);
1709 if (TREE_CODE (type) == REFERENCE_TYPE)
1710 /* Quals on the object don't matter. */;
1711 else if (PACK_EXPANSION_P (type))
1712 /* Don't bother trying to represent this. */
1713 type = NULL_TREE;
1714 else
1716 /* Set the cv qualifiers. */
1717 int quals = cp_type_quals (TREE_TYPE (object));
1719 if (DECL_MUTABLE_P (decl))
1720 quals &= ~TYPE_QUAL_CONST;
1722 quals |= cp_type_quals (TREE_TYPE (decl));
1723 type = cp_build_qualified_type (type, quals);
1726 return (convert_from_reference
1727 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1729 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1730 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1731 for now. */
1732 else if (processing_template_decl)
1733 return build_qualified_name (TREE_TYPE (decl),
1734 qualifying_scope,
1735 decl,
1736 /*template_p=*/false);
1737 else
1739 tree access_type = TREE_TYPE (object);
1741 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1742 decl, tf_warning_or_error);
1744 /* If the data member was named `C::M', convert `*this' to `C'
1745 first. */
1746 if (qualifying_scope)
1748 tree binfo = NULL_TREE;
1749 object = build_scoped_ref (object, qualifying_scope,
1750 &binfo);
1753 return build_class_member_access_expr (object, decl,
1754 /*access_path=*/NULL_TREE,
1755 /*preserve_reference=*/false,
1756 tf_warning_or_error);
1760 /* If we are currently parsing a template and we encountered a typedef
1761 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1762 adds the typedef to a list tied to the current template.
1763 At template instantiation time, that list is walked and access check
1764 performed for each typedef.
1765 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1767 void
1768 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1769 tree context,
1770 location_t location)
1772 tree template_info = NULL;
1773 tree cs = current_scope ();
1775 if (!is_typedef_decl (typedef_decl)
1776 || !context
1777 || !CLASS_TYPE_P (context)
1778 || !cs)
1779 return;
1781 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1782 template_info = get_template_info (cs);
1784 if (template_info
1785 && TI_TEMPLATE (template_info)
1786 && !currently_open_class (context))
1787 append_type_to_template_for_access_check (cs, typedef_decl,
1788 context, location);
1791 /* DECL was the declaration to which a qualified-id resolved. Issue
1792 an error message if it is not accessible. If OBJECT_TYPE is
1793 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1794 type of `*x', or `x', respectively. If the DECL was named as
1795 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1797 void
1798 check_accessibility_of_qualified_id (tree decl,
1799 tree object_type,
1800 tree nested_name_specifier)
1802 tree scope;
1803 tree qualifying_type = NULL_TREE;
1805 /* If we are parsing a template declaration and if decl is a typedef,
1806 add it to a list tied to the template.
1807 At template instantiation time, that list will be walked and
1808 access check performed. */
1809 add_typedef_to_current_template_for_access_check (decl,
1810 nested_name_specifier
1811 ? nested_name_specifier
1812 : DECL_CONTEXT (decl),
1813 input_location);
1815 /* If we're not checking, return immediately. */
1816 if (deferred_access_no_check)
1817 return;
1819 /* Determine the SCOPE of DECL. */
1820 scope = context_for_name_lookup (decl);
1821 /* If the SCOPE is not a type, then DECL is not a member. */
1822 if (!TYPE_P (scope))
1823 return;
1824 /* Compute the scope through which DECL is being accessed. */
1825 if (object_type
1826 /* OBJECT_TYPE might not be a class type; consider:
1828 class A { typedef int I; };
1829 I *p;
1830 p->A::I::~I();
1832 In this case, we will have "A::I" as the DECL, but "I" as the
1833 OBJECT_TYPE. */
1834 && CLASS_TYPE_P (object_type)
1835 && DERIVED_FROM_P (scope, object_type))
1836 /* If we are processing a `->' or `.' expression, use the type of the
1837 left-hand side. */
1838 qualifying_type = object_type;
1839 else if (nested_name_specifier)
1841 /* If the reference is to a non-static member of the
1842 current class, treat it as if it were referenced through
1843 `this'. */
1844 tree ct;
1845 if (DECL_NONSTATIC_MEMBER_P (decl)
1846 && current_class_ptr
1847 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1848 qualifying_type = ct;
1849 /* Otherwise, use the type indicated by the
1850 nested-name-specifier. */
1851 else
1852 qualifying_type = nested_name_specifier;
1854 else
1855 /* Otherwise, the name must be from the current class or one of
1856 its bases. */
1857 qualifying_type = currently_open_derived_class (scope);
1859 if (qualifying_type
1860 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1861 or similar in a default argument value. */
1862 && CLASS_TYPE_P (qualifying_type)
1863 && !dependent_type_p (qualifying_type))
1864 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1865 decl, tf_warning_or_error);
1868 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1869 class named to the left of the "::" operator. DONE is true if this
1870 expression is a complete postfix-expression; it is false if this
1871 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1872 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1873 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1874 is true iff this qualified name appears as a template argument. */
1876 tree
1877 finish_qualified_id_expr (tree qualifying_class,
1878 tree expr,
1879 bool done,
1880 bool address_p,
1881 bool template_p,
1882 bool template_arg_p,
1883 tsubst_flags_t complain)
1885 gcc_assert (TYPE_P (qualifying_class));
1887 if (error_operand_p (expr))
1888 return error_mark_node;
1890 if ((DECL_P (expr) || BASELINK_P (expr))
1891 && !mark_used (expr, complain))
1892 return error_mark_node;
1894 if (template_p)
1895 check_template_keyword (expr);
1897 /* If EXPR occurs as the operand of '&', use special handling that
1898 permits a pointer-to-member. */
1899 if (address_p && done)
1901 if (TREE_CODE (expr) == SCOPE_REF)
1902 expr = TREE_OPERAND (expr, 1);
1903 expr = build_offset_ref (qualifying_class, expr,
1904 /*address_p=*/true, complain);
1905 return expr;
1908 /* No need to check access within an enum. */
1909 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1910 return expr;
1912 /* Within the scope of a class, turn references to non-static
1913 members into expression of the form "this->...". */
1914 if (template_arg_p)
1915 /* But, within a template argument, we do not want make the
1916 transformation, as there is no "this" pointer. */
1918 else if (TREE_CODE (expr) == FIELD_DECL)
1920 push_deferring_access_checks (dk_no_check);
1921 expr = finish_non_static_data_member (expr, NULL_TREE,
1922 qualifying_class);
1923 pop_deferring_access_checks ();
1925 else if (BASELINK_P (expr) && !processing_template_decl)
1927 /* See if any of the functions are non-static members. */
1928 /* If so, the expression may be relative to 'this'. */
1929 if (!shared_member_p (expr)
1930 && current_class_ptr
1931 && DERIVED_FROM_P (qualifying_class,
1932 current_nonlambda_class_type ()))
1933 expr = (build_class_member_access_expr
1934 (maybe_dummy_object (qualifying_class, NULL),
1935 expr,
1936 BASELINK_ACCESS_BINFO (expr),
1937 /*preserve_reference=*/false,
1938 complain));
1939 else if (done)
1940 /* The expression is a qualified name whose address is not
1941 being taken. */
1942 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1943 complain);
1945 else if (BASELINK_P (expr))
1947 else
1949 /* In a template, return a SCOPE_REF for most qualified-ids
1950 so that we can check access at instantiation time. But if
1951 we're looking at a member of the current instantiation, we
1952 know we have access and building up the SCOPE_REF confuses
1953 non-type template argument handling. */
1954 if (processing_template_decl
1955 && !currently_open_class (qualifying_class))
1956 expr = build_qualified_name (TREE_TYPE (expr),
1957 qualifying_class, expr,
1958 template_p);
1960 expr = convert_from_reference (expr);
1963 return expr;
1966 /* Begin a statement-expression. The value returned must be passed to
1967 finish_stmt_expr. */
1969 tree
1970 begin_stmt_expr (void)
1972 return push_stmt_list ();
1975 /* Process the final expression of a statement expression. EXPR can be
1976 NULL, if the final expression is empty. Return a STATEMENT_LIST
1977 containing all the statements in the statement-expression, or
1978 ERROR_MARK_NODE if there was an error. */
1980 tree
1981 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1983 if (error_operand_p (expr))
1985 /* The type of the statement-expression is the type of the last
1986 expression. */
1987 TREE_TYPE (stmt_expr) = error_mark_node;
1988 return error_mark_node;
1991 /* If the last statement does not have "void" type, then the value
1992 of the last statement is the value of the entire expression. */
1993 if (expr)
1995 tree type = TREE_TYPE (expr);
1997 if (processing_template_decl)
1999 expr = build_stmt (input_location, EXPR_STMT, expr);
2000 expr = add_stmt (expr);
2001 /* Mark the last statement so that we can recognize it as such at
2002 template-instantiation time. */
2003 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2005 else if (VOID_TYPE_P (type))
2007 /* Just treat this like an ordinary statement. */
2008 expr = finish_expr_stmt (expr);
2010 else
2012 /* It actually has a value we need to deal with. First, force it
2013 to be an rvalue so that we won't need to build up a copy
2014 constructor call later when we try to assign it to something. */
2015 expr = force_rvalue (expr, tf_warning_or_error);
2016 if (error_operand_p (expr))
2017 return error_mark_node;
2019 /* Update for array-to-pointer decay. */
2020 type = TREE_TYPE (expr);
2022 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2023 normal statement, but don't convert to void or actually add
2024 the EXPR_STMT. */
2025 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2026 expr = maybe_cleanup_point_expr (expr);
2027 add_stmt (expr);
2030 /* The type of the statement-expression is the type of the last
2031 expression. */
2032 TREE_TYPE (stmt_expr) = type;
2035 return stmt_expr;
2038 /* Finish a statement-expression. EXPR should be the value returned
2039 by the previous begin_stmt_expr. Returns an expression
2040 representing the statement-expression. */
2042 tree
2043 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2045 tree type;
2046 tree result;
2048 if (error_operand_p (stmt_expr))
2050 pop_stmt_list (stmt_expr);
2051 return error_mark_node;
2054 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2056 type = TREE_TYPE (stmt_expr);
2057 result = pop_stmt_list (stmt_expr);
2058 TREE_TYPE (result) = type;
2060 if (processing_template_decl)
2062 result = build_min (STMT_EXPR, type, result);
2063 TREE_SIDE_EFFECTS (result) = 1;
2064 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2066 else if (CLASS_TYPE_P (type))
2068 /* Wrap the statement-expression in a TARGET_EXPR so that the
2069 temporary object created by the final expression is destroyed at
2070 the end of the full-expression containing the
2071 statement-expression. */
2072 result = force_target_expr (type, result, tf_warning_or_error);
2075 return result;
2078 /* Returns the expression which provides the value of STMT_EXPR. */
2080 tree
2081 stmt_expr_value_expr (tree stmt_expr)
2083 tree t = STMT_EXPR_STMT (stmt_expr);
2085 if (TREE_CODE (t) == BIND_EXPR)
2086 t = BIND_EXPR_BODY (t);
2088 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2089 t = STATEMENT_LIST_TAIL (t)->stmt;
2091 if (TREE_CODE (t) == EXPR_STMT)
2092 t = EXPR_STMT_EXPR (t);
2094 return t;
2097 /* Return TRUE iff EXPR_STMT is an empty list of
2098 expression statements. */
2100 bool
2101 empty_expr_stmt_p (tree expr_stmt)
2103 tree body = NULL_TREE;
2105 if (expr_stmt == void_node)
2106 return true;
2108 if (expr_stmt)
2110 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2111 body = EXPR_STMT_EXPR (expr_stmt);
2112 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2113 body = expr_stmt;
2116 if (body)
2118 if (TREE_CODE (body) == STATEMENT_LIST)
2119 return tsi_end_p (tsi_start (body));
2120 else
2121 return empty_expr_stmt_p (body);
2123 return false;
2126 /* Perform Koenig lookup. FN is the postfix-expression representing
2127 the function (or functions) to call; ARGS are the arguments to the
2128 call. Returns the functions to be considered by overload resolution. */
2130 tree
2131 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2132 tsubst_flags_t complain)
2134 tree identifier = NULL_TREE;
2135 tree functions = NULL_TREE;
2136 tree tmpl_args = NULL_TREE;
2137 bool template_id = false;
2139 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2141 /* Use a separate flag to handle null args. */
2142 template_id = true;
2143 tmpl_args = TREE_OPERAND (fn, 1);
2144 fn = TREE_OPERAND (fn, 0);
2147 /* Find the name of the overloaded function. */
2148 if (identifier_p (fn))
2149 identifier = fn;
2150 else if (is_overloaded_fn (fn))
2152 functions = fn;
2153 identifier = DECL_NAME (get_first_fn (functions));
2155 else if (DECL_P (fn))
2157 functions = fn;
2158 identifier = DECL_NAME (fn);
2161 /* A call to a namespace-scope function using an unqualified name.
2163 Do Koenig lookup -- unless any of the arguments are
2164 type-dependent. */
2165 if (!any_type_dependent_arguments_p (args)
2166 && !any_dependent_template_arguments_p (tmpl_args))
2168 fn = lookup_arg_dependent (identifier, functions, args);
2169 if (!fn)
2171 /* The unqualified name could not be resolved. */
2172 if (complain)
2173 fn = unqualified_fn_lookup_error (identifier);
2174 else
2175 fn = identifier;
2179 if (fn && template_id)
2180 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2182 return fn;
2185 /* Generate an expression for `FN (ARGS)'. This may change the
2186 contents of ARGS.
2188 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2189 as a virtual call, even if FN is virtual. (This flag is set when
2190 encountering an expression where the function name is explicitly
2191 qualified. For example a call to `X::f' never generates a virtual
2192 call.)
2194 Returns code for the call. */
2196 tree
2197 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2198 bool koenig_p, tsubst_flags_t complain)
2200 tree result;
2201 tree orig_fn;
2202 vec<tree, va_gc> *orig_args = NULL;
2204 if (fn == error_mark_node)
2205 return error_mark_node;
2207 gcc_assert (!TYPE_P (fn));
2209 orig_fn = fn;
2211 if (processing_template_decl)
2213 /* If the call expression is dependent, build a CALL_EXPR node
2214 with no type; type_dependent_expression_p recognizes
2215 expressions with no type as being dependent. */
2216 if (type_dependent_expression_p (fn)
2217 || any_type_dependent_arguments_p (*args)
2218 /* For a non-static member function that doesn't have an
2219 explicit object argument, we need to specifically
2220 test the type dependency of the "this" pointer because it
2221 is not included in *ARGS even though it is considered to
2222 be part of the list of arguments. Note that this is
2223 related to CWG issues 515 and 1005. */
2224 || (TREE_CODE (fn) != COMPONENT_REF
2225 && non_static_member_function_p (fn)
2226 && current_class_ref
2227 && type_dependent_expression_p (current_class_ref)))
2229 result = build_nt_call_vec (fn, *args);
2230 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2231 KOENIG_LOOKUP_P (result) = koenig_p;
2232 if (cfun)
2236 tree fndecl = OVL_CURRENT (fn);
2237 if (TREE_CODE (fndecl) != FUNCTION_DECL
2238 || !TREE_THIS_VOLATILE (fndecl))
2239 break;
2240 fn = OVL_NEXT (fn);
2242 while (fn);
2243 if (!fn)
2244 current_function_returns_abnormally = 1;
2246 return result;
2248 orig_args = make_tree_vector_copy (*args);
2249 if (!BASELINK_P (fn)
2250 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2251 && TREE_TYPE (fn) != unknown_type_node)
2252 fn = build_non_dependent_expr (fn);
2253 make_args_non_dependent (*args);
2256 if (TREE_CODE (fn) == COMPONENT_REF)
2258 tree member = TREE_OPERAND (fn, 1);
2259 if (BASELINK_P (member))
2261 tree object = TREE_OPERAND (fn, 0);
2262 return build_new_method_call (object, member,
2263 args, NULL_TREE,
2264 (disallow_virtual
2265 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2266 : LOOKUP_NORMAL),
2267 /*fn_p=*/NULL,
2268 complain);
2272 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2273 if (TREE_CODE (fn) == ADDR_EXPR
2274 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2275 fn = TREE_OPERAND (fn, 0);
2277 if (is_overloaded_fn (fn))
2278 fn = baselink_for_fns (fn);
2280 result = NULL_TREE;
2281 if (BASELINK_P (fn))
2283 tree object;
2285 /* A call to a member function. From [over.call.func]:
2287 If the keyword this is in scope and refers to the class of
2288 that member function, or a derived class thereof, then the
2289 function call is transformed into a qualified function call
2290 using (*this) as the postfix-expression to the left of the
2291 . operator.... [Otherwise] a contrived object of type T
2292 becomes the implied object argument.
2294 In this situation:
2296 struct A { void f(); };
2297 struct B : public A {};
2298 struct C : public A { void g() { B::f(); }};
2300 "the class of that member function" refers to `A'. But 11.2
2301 [class.access.base] says that we need to convert 'this' to B* as
2302 part of the access, so we pass 'B' to maybe_dummy_object. */
2304 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2305 NULL);
2307 if (processing_template_decl)
2309 if (type_dependent_expression_p (object))
2311 tree ret = build_nt_call_vec (orig_fn, orig_args);
2312 release_tree_vector (orig_args);
2313 return ret;
2315 object = build_non_dependent_expr (object);
2318 result = build_new_method_call (object, fn, args, NULL_TREE,
2319 (disallow_virtual
2320 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2321 : LOOKUP_NORMAL),
2322 /*fn_p=*/NULL,
2323 complain);
2325 else if (is_overloaded_fn (fn))
2327 /* If the function is an overloaded builtin, resolve it. */
2328 if (TREE_CODE (fn) == FUNCTION_DECL
2329 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2330 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2331 result = resolve_overloaded_builtin (input_location, fn, *args);
2333 if (!result)
2335 if (warn_sizeof_pointer_memaccess
2336 && !vec_safe_is_empty (*args)
2337 && !processing_template_decl)
2339 location_t sizeof_arg_loc[3];
2340 tree sizeof_arg[3];
2341 unsigned int i;
2342 for (i = 0; i < 3; i++)
2344 tree t;
2346 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2347 sizeof_arg[i] = NULL_TREE;
2348 if (i >= (*args)->length ())
2349 continue;
2350 t = (**args)[i];
2351 if (TREE_CODE (t) != SIZEOF_EXPR)
2352 continue;
2353 if (SIZEOF_EXPR_TYPE_P (t))
2354 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2355 else
2356 sizeof_arg[i] = TREE_OPERAND (t, 0);
2357 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2359 sizeof_pointer_memaccess_warning
2360 (sizeof_arg_loc, fn, *args,
2361 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2364 /* A call to a namespace-scope function. */
2365 result = build_new_function_call (fn, args, koenig_p, complain);
2368 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2370 if (!vec_safe_is_empty (*args))
2371 error ("arguments to destructor are not allowed");
2372 /* Mark the pseudo-destructor call as having side-effects so
2373 that we do not issue warnings about its use. */
2374 result = build1 (NOP_EXPR,
2375 void_type_node,
2376 TREE_OPERAND (fn, 0));
2377 TREE_SIDE_EFFECTS (result) = 1;
2379 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2380 /* If the "function" is really an object of class type, it might
2381 have an overloaded `operator ()'. */
2382 result = build_op_call (fn, args, complain);
2384 if (!result)
2385 /* A call where the function is unknown. */
2386 result = cp_build_function_call_vec (fn, args, complain);
2388 if (processing_template_decl && result != error_mark_node)
2390 if (INDIRECT_REF_P (result))
2391 result = TREE_OPERAND (result, 0);
2392 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2393 SET_EXPR_LOCATION (result, input_location);
2394 KOENIG_LOOKUP_P (result) = koenig_p;
2395 release_tree_vector (orig_args);
2396 result = convert_from_reference (result);
2399 if (koenig_p)
2401 /* Free garbage OVERLOADs from arg-dependent lookup. */
2402 tree next = NULL_TREE;
2403 for (fn = orig_fn;
2404 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2405 fn = next)
2407 if (processing_template_decl)
2408 /* In a template, we'll re-use them at instantiation time. */
2409 OVL_ARG_DEPENDENT (fn) = false;
2410 else
2412 next = OVL_CHAIN (fn);
2413 ggc_free (fn);
2418 return result;
2421 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
2423 tree
2424 finish_template_variable (tree var)
2426 return instantiate_template (TREE_OPERAND (var, 0), TREE_OPERAND (var, 1),
2427 tf_error);
2430 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2431 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2432 POSTDECREMENT_EXPR.) */
2434 tree
2435 finish_increment_expr (tree expr, enum tree_code code)
2437 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2440 /* Finish a use of `this'. Returns an expression for `this'. */
2442 tree
2443 finish_this_expr (void)
2445 tree result;
2447 if (current_class_ptr)
2449 tree type = TREE_TYPE (current_class_ref);
2451 /* In a lambda expression, 'this' refers to the captured 'this'. */
2452 if (LAMBDA_TYPE_P (type))
2453 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2454 else
2455 result = current_class_ptr;
2457 else if (current_function_decl
2458 && DECL_STATIC_FUNCTION_P (current_function_decl))
2460 error ("%<this%> is unavailable for static member functions");
2461 result = error_mark_node;
2463 else
2465 if (current_function_decl)
2466 error ("invalid use of %<this%> in non-member function");
2467 else
2468 error ("invalid use of %<this%> at top level");
2469 result = error_mark_node;
2472 /* The keyword 'this' is a prvalue expression. */
2473 result = rvalue (result);
2475 return result;
2478 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2479 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2480 the TYPE for the type given. If SCOPE is non-NULL, the expression
2481 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2483 tree
2484 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2485 location_t loc)
2487 if (object == error_mark_node || destructor == error_mark_node)
2488 return error_mark_node;
2490 gcc_assert (TYPE_P (destructor));
2492 if (!processing_template_decl)
2494 if (scope == error_mark_node)
2496 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2497 return error_mark_node;
2499 if (is_auto (destructor))
2500 destructor = TREE_TYPE (object);
2501 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2503 error_at (loc,
2504 "qualified type %qT does not match destructor name ~%qT",
2505 scope, destructor);
2506 return error_mark_node;
2510 /* [expr.pseudo] says both:
2512 The type designated by the pseudo-destructor-name shall be
2513 the same as the object type.
2515 and:
2517 The cv-unqualified versions of the object type and of the
2518 type designated by the pseudo-destructor-name shall be the
2519 same type.
2521 We implement the more generous second sentence, since that is
2522 what most other compilers do. */
2523 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2524 destructor))
2526 error_at (loc, "%qE is not of type %qT", object, destructor);
2527 return error_mark_node;
2531 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2532 scope, destructor);
2535 /* Finish an expression of the form CODE EXPR. */
2537 tree
2538 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2539 tsubst_flags_t complain)
2541 tree result = build_x_unary_op (loc, code, expr, complain);
2542 if ((complain & tf_warning)
2543 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2544 overflow_warning (input_location, result);
2546 return result;
2549 /* Finish a compound-literal expression. TYPE is the type to which
2550 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2552 tree
2553 finish_compound_literal (tree type, tree compound_literal,
2554 tsubst_flags_t complain)
2556 if (type == error_mark_node)
2557 return error_mark_node;
2559 if (TREE_CODE (type) == REFERENCE_TYPE)
2561 compound_literal
2562 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2563 complain);
2564 return cp_build_c_cast (type, compound_literal, complain);
2567 if (!TYPE_OBJ_P (type))
2569 if (complain & tf_error)
2570 error ("compound literal of non-object type %qT", type);
2571 return error_mark_node;
2574 if (processing_template_decl)
2576 TREE_TYPE (compound_literal) = type;
2577 /* Mark the expression as a compound literal. */
2578 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2579 return compound_literal;
2582 type = complete_type (type);
2584 if (TYPE_NON_AGGREGATE_CLASS (type))
2586 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2587 everywhere that deals with function arguments would be a pain, so
2588 just wrap it in a TREE_LIST. The parser set a flag so we know
2589 that it came from T{} rather than T({}). */
2590 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2591 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2592 return build_functional_cast (type, compound_literal, complain);
2595 if (TREE_CODE (type) == ARRAY_TYPE
2596 && check_array_initializer (NULL_TREE, type, compound_literal))
2597 return error_mark_node;
2598 compound_literal = reshape_init (type, compound_literal, complain);
2599 if (SCALAR_TYPE_P (type)
2600 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2601 && !check_narrowing (type, compound_literal, complain))
2602 return error_mark_node;
2603 if (TREE_CODE (type) == ARRAY_TYPE
2604 && TYPE_DOMAIN (type) == NULL_TREE)
2606 cp_complete_array_type_or_error (&type, compound_literal,
2607 false, complain);
2608 if (type == error_mark_node)
2609 return error_mark_node;
2611 compound_literal = digest_init (type, compound_literal, complain);
2612 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2613 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2614 /* Put static/constant array temporaries in static variables, but always
2615 represent class temporaries with TARGET_EXPR so we elide copies. */
2616 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2617 && TREE_CODE (type) == ARRAY_TYPE
2618 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2619 && initializer_constant_valid_p (compound_literal, type))
2621 tree decl = create_temporary_var (type);
2622 DECL_INITIAL (decl) = compound_literal;
2623 TREE_STATIC (decl) = 1;
2624 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2626 /* 5.19 says that a constant expression can include an
2627 lvalue-rvalue conversion applied to "a glvalue of literal type
2628 that refers to a non-volatile temporary object initialized
2629 with a constant expression". Rather than try to communicate
2630 that this VAR_DECL is a temporary, just mark it constexpr. */
2631 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2632 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2633 TREE_CONSTANT (decl) = true;
2635 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2636 decl = pushdecl_top_level (decl);
2637 DECL_NAME (decl) = make_anon_name ();
2638 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2639 /* Make sure the destructor is callable. */
2640 tree clean = cxx_maybe_build_cleanup (decl, complain);
2641 if (clean == error_mark_node)
2642 return error_mark_node;
2643 return decl;
2645 else
2646 return get_target_expr_sfinae (compound_literal, complain);
2649 /* Return the declaration for the function-name variable indicated by
2650 ID. */
2652 tree
2653 finish_fname (tree id)
2655 tree decl;
2657 decl = fname_decl (input_location, C_RID_CODE (id), id);
2658 if (processing_template_decl && current_function_decl
2659 && decl != error_mark_node)
2660 decl = DECL_NAME (decl);
2661 return decl;
2664 /* Finish a translation unit. */
2666 void
2667 finish_translation_unit (void)
2669 /* In case there were missing closebraces,
2670 get us back to the global binding level. */
2671 pop_everything ();
2672 while (current_namespace != global_namespace)
2673 pop_namespace ();
2675 /* Do file scope __FUNCTION__ et al. */
2676 finish_fname_decls ();
2679 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2680 Returns the parameter. */
2682 tree
2683 finish_template_type_parm (tree aggr, tree identifier)
2685 if (aggr != class_type_node)
2687 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2688 aggr = class_type_node;
2691 return build_tree_list (aggr, identifier);
2694 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2695 Returns the parameter. */
2697 tree
2698 finish_template_template_parm (tree aggr, tree identifier)
2700 tree decl = build_decl (input_location,
2701 TYPE_DECL, identifier, NULL_TREE);
2702 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2703 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2704 DECL_TEMPLATE_RESULT (tmpl) = decl;
2705 DECL_ARTIFICIAL (decl) = 1;
2706 end_template_decl ();
2708 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2710 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2711 /*is_primary=*/true, /*is_partial=*/false,
2712 /*is_friend=*/0);
2714 return finish_template_type_parm (aggr, tmpl);
2717 /* ARGUMENT is the default-argument value for a template template
2718 parameter. If ARGUMENT is invalid, issue error messages and return
2719 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2721 tree
2722 check_template_template_default_arg (tree argument)
2724 if (TREE_CODE (argument) != TEMPLATE_DECL
2725 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2726 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2728 if (TREE_CODE (argument) == TYPE_DECL)
2729 error ("invalid use of type %qT as a default value for a template "
2730 "template-parameter", TREE_TYPE (argument));
2731 else
2732 error ("invalid default argument for a template template parameter");
2733 return error_mark_node;
2736 return argument;
2739 /* Begin a class definition, as indicated by T. */
2741 tree
2742 begin_class_definition (tree t)
2744 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2745 return error_mark_node;
2747 if (processing_template_parmlist)
2749 error ("definition of %q#T inside template parameter list", t);
2750 return error_mark_node;
2753 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2754 are passed the same as decimal scalar types. */
2755 if (TREE_CODE (t) == RECORD_TYPE
2756 && !processing_template_decl)
2758 tree ns = TYPE_CONTEXT (t);
2759 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2760 && DECL_CONTEXT (ns) == std_node
2761 && DECL_NAME (ns)
2762 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2764 const char *n = TYPE_NAME_STRING (t);
2765 if ((strcmp (n, "decimal32") == 0)
2766 || (strcmp (n, "decimal64") == 0)
2767 || (strcmp (n, "decimal128") == 0))
2768 TYPE_TRANSPARENT_AGGR (t) = 1;
2772 /* A non-implicit typename comes from code like:
2774 template <typename T> struct A {
2775 template <typename U> struct A<T>::B ...
2777 This is erroneous. */
2778 else if (TREE_CODE (t) == TYPENAME_TYPE)
2780 error ("invalid definition of qualified type %qT", t);
2781 t = error_mark_node;
2784 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2786 t = make_class_type (RECORD_TYPE);
2787 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2790 if (TYPE_BEING_DEFINED (t))
2792 t = make_class_type (TREE_CODE (t));
2793 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2795 maybe_process_partial_specialization (t);
2796 pushclass (t);
2797 TYPE_BEING_DEFINED (t) = 1;
2798 class_binding_level->defining_class_p = 1;
2800 if (flag_pack_struct)
2802 tree v;
2803 TYPE_PACKED (t) = 1;
2804 /* Even though the type is being defined for the first time
2805 here, there might have been a forward declaration, so there
2806 might be cv-qualified variants of T. */
2807 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2808 TYPE_PACKED (v) = 1;
2810 /* Reset the interface data, at the earliest possible
2811 moment, as it might have been set via a class foo;
2812 before. */
2813 if (! TYPE_ANONYMOUS_P (t))
2815 struct c_fileinfo *finfo = \
2816 get_fileinfo (LOCATION_FILE (input_location));
2817 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2818 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2819 (t, finfo->interface_unknown);
2821 reset_specialization();
2823 /* Make a declaration for this class in its own scope. */
2824 build_self_reference ();
2826 return t;
2829 /* Finish the member declaration given by DECL. */
2831 void
2832 finish_member_declaration (tree decl)
2834 if (decl == error_mark_node || decl == NULL_TREE)
2835 return;
2837 if (decl == void_type_node)
2838 /* The COMPONENT was a friend, not a member, and so there's
2839 nothing for us to do. */
2840 return;
2842 /* We should see only one DECL at a time. */
2843 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2845 /* Set up access control for DECL. */
2846 TREE_PRIVATE (decl)
2847 = (current_access_specifier == access_private_node);
2848 TREE_PROTECTED (decl)
2849 = (current_access_specifier == access_protected_node);
2850 if (TREE_CODE (decl) == TEMPLATE_DECL)
2852 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2853 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2856 /* Mark the DECL as a member of the current class, unless it's
2857 a member of an enumeration. */
2858 if (TREE_CODE (decl) != CONST_DECL)
2859 DECL_CONTEXT (decl) = current_class_type;
2861 /* Check for bare parameter packs in the member variable declaration. */
2862 if (TREE_CODE (decl) == FIELD_DECL)
2864 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2865 TREE_TYPE (decl) = error_mark_node;
2866 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2867 DECL_ATTRIBUTES (decl) = NULL_TREE;
2870 /* [dcl.link]
2872 A C language linkage is ignored for the names of class members
2873 and the member function type of class member functions. */
2874 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2875 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2877 /* Put functions on the TYPE_METHODS list and everything else on the
2878 TYPE_FIELDS list. Note that these are built up in reverse order.
2879 We reverse them (to obtain declaration order) in finish_struct. */
2880 if (DECL_DECLARES_FUNCTION_P (decl))
2882 /* We also need to add this function to the
2883 CLASSTYPE_METHOD_VEC. */
2884 if (add_method (current_class_type, decl, NULL_TREE))
2886 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2887 TYPE_METHODS (current_class_type) = decl;
2889 maybe_add_class_template_decl_list (current_class_type, decl,
2890 /*friend_p=*/0);
2893 /* Enter the DECL into the scope of the class, if the class
2894 isn't a closure (whose fields are supposed to be unnamed). */
2895 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2896 || pushdecl_class_level (decl))
2898 if (TREE_CODE (decl) == USING_DECL)
2900 /* For now, ignore class-scope USING_DECLS, so that
2901 debugging backends do not see them. */
2902 DECL_IGNORED_P (decl) = 1;
2905 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2906 go at the beginning. The reason is that lookup_field_1
2907 searches the list in order, and we want a field name to
2908 override a type name so that the "struct stat hack" will
2909 work. In particular:
2911 struct S { enum E { }; int E } s;
2912 s.E = 3;
2914 is valid. In addition, the FIELD_DECLs must be maintained in
2915 declaration order so that class layout works as expected.
2916 However, we don't need that order until class layout, so we
2917 save a little time by putting FIELD_DECLs on in reverse order
2918 here, and then reversing them in finish_struct_1. (We could
2919 also keep a pointer to the correct insertion points in the
2920 list.) */
2922 if (TREE_CODE (decl) == TYPE_DECL)
2923 TYPE_FIELDS (current_class_type)
2924 = chainon (TYPE_FIELDS (current_class_type), decl);
2925 else
2927 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2928 TYPE_FIELDS (current_class_type) = decl;
2931 maybe_add_class_template_decl_list (current_class_type, decl,
2932 /*friend_p=*/0);
2935 if (pch_file)
2936 note_decl_for_pch (decl);
2939 /* DECL has been declared while we are building a PCH file. Perform
2940 actions that we might normally undertake lazily, but which can be
2941 performed now so that they do not have to be performed in
2942 translation units which include the PCH file. */
2944 void
2945 note_decl_for_pch (tree decl)
2947 gcc_assert (pch_file);
2949 /* There's a good chance that we'll have to mangle names at some
2950 point, even if only for emission in debugging information. */
2951 if (VAR_OR_FUNCTION_DECL_P (decl)
2952 && !processing_template_decl)
2953 mangle_decl (decl);
2956 /* Finish processing a complete template declaration. The PARMS are
2957 the template parameters. */
2959 void
2960 finish_template_decl (tree parms)
2962 if (parms)
2963 end_template_decl ();
2964 else
2965 end_specialization ();
2968 /* Finish processing a template-id (which names a type) of the form
2969 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2970 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2971 the scope of template-id indicated. */
2973 tree
2974 finish_template_type (tree name, tree args, int entering_scope)
2976 tree type;
2978 type = lookup_template_class (name, args,
2979 NULL_TREE, NULL_TREE, entering_scope,
2980 tf_warning_or_error | tf_user);
2981 if (type == error_mark_node)
2982 return type;
2983 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2984 return TYPE_STUB_DECL (type);
2985 else
2986 return TYPE_NAME (type);
2989 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2990 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2991 BASE_CLASS, or NULL_TREE if an error occurred. The
2992 ACCESS_SPECIFIER is one of
2993 access_{default,public,protected_private}_node. For a virtual base
2994 we set TREE_TYPE. */
2996 tree
2997 finish_base_specifier (tree base, tree access, bool virtual_p)
2999 tree result;
3001 if (base == error_mark_node)
3003 error ("invalid base-class specification");
3004 result = NULL_TREE;
3006 else if (! MAYBE_CLASS_TYPE_P (base))
3008 error ("%qT is not a class type", base);
3009 result = NULL_TREE;
3011 else
3013 if (cp_type_quals (base) != 0)
3015 /* DR 484: Can a base-specifier name a cv-qualified
3016 class type? */
3017 base = TYPE_MAIN_VARIANT (base);
3019 result = build_tree_list (access, base);
3020 if (virtual_p)
3021 TREE_TYPE (result) = integer_type_node;
3024 return result;
3027 /* If FNS is a member function, a set of member functions, or a
3028 template-id referring to one or more member functions, return a
3029 BASELINK for FNS, incorporating the current access context.
3030 Otherwise, return FNS unchanged. */
3032 tree
3033 baselink_for_fns (tree fns)
3035 tree scope;
3036 tree cl;
3038 if (BASELINK_P (fns)
3039 || error_operand_p (fns))
3040 return fns;
3042 scope = ovl_scope (fns);
3043 if (!CLASS_TYPE_P (scope))
3044 return fns;
3046 cl = currently_open_derived_class (scope);
3047 if (!cl)
3048 cl = scope;
3049 cl = TYPE_BINFO (cl);
3050 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3053 /* Returns true iff DECL is a variable from a function outside
3054 the current one. */
3056 static bool
3057 outer_var_p (tree decl)
3059 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3060 && DECL_FUNCTION_SCOPE_P (decl)
3061 && (DECL_CONTEXT (decl) != current_function_decl
3062 || parsing_nsdmi ()));
3065 /* As above, but also checks that DECL is automatic. */
3067 static bool
3068 outer_automatic_var_p (tree decl)
3070 return (outer_var_p (decl)
3071 && !TREE_STATIC (decl));
3074 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3075 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3076 if non-NULL, is the type or namespace used to explicitly qualify
3077 ID_EXPRESSION. DECL is the entity to which that name has been
3078 resolved.
3080 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3081 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3082 be set to true if this expression isn't permitted in a
3083 constant-expression, but it is otherwise not set by this function.
3084 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3085 constant-expression, but a non-constant expression is also
3086 permissible.
3088 DONE is true if this expression is a complete postfix-expression;
3089 it is false if this expression is followed by '->', '[', '(', etc.
3090 ADDRESS_P is true iff this expression is the operand of '&'.
3091 TEMPLATE_P is true iff the qualified-id was of the form
3092 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3093 appears as a template argument.
3095 If an error occurs, and it is the kind of error that might cause
3096 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3097 is the caller's responsibility to issue the message. *ERROR_MSG
3098 will be a string with static storage duration, so the caller need
3099 not "free" it.
3101 Return an expression for the entity, after issuing appropriate
3102 diagnostics. This function is also responsible for transforming a
3103 reference to a non-static member into a COMPONENT_REF that makes
3104 the use of "this" explicit.
3106 Upon return, *IDK will be filled in appropriately. */
3107 tree
3108 finish_id_expression (tree id_expression,
3109 tree decl,
3110 tree scope,
3111 cp_id_kind *idk,
3112 bool integral_constant_expression_p,
3113 bool allow_non_integral_constant_expression_p,
3114 bool *non_integral_constant_expression_p,
3115 bool template_p,
3116 bool done,
3117 bool address_p,
3118 bool template_arg_p,
3119 const char **error_msg,
3120 location_t location)
3122 decl = strip_using_decl (decl);
3124 /* Initialize the output parameters. */
3125 *idk = CP_ID_KIND_NONE;
3126 *error_msg = NULL;
3128 if (id_expression == error_mark_node)
3129 return error_mark_node;
3130 /* If we have a template-id, then no further lookup is
3131 required. If the template-id was for a template-class, we
3132 will sometimes have a TYPE_DECL at this point. */
3133 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3134 || TREE_CODE (decl) == TYPE_DECL)
3136 /* Look up the name. */
3137 else
3139 if (decl == error_mark_node)
3141 /* Name lookup failed. */
3142 if (scope
3143 && (!TYPE_P (scope)
3144 || (!dependent_type_p (scope)
3145 && !(identifier_p (id_expression)
3146 && IDENTIFIER_TYPENAME_P (id_expression)
3147 && dependent_type_p (TREE_TYPE (id_expression))))))
3149 /* If the qualifying type is non-dependent (and the name
3150 does not name a conversion operator to a dependent
3151 type), issue an error. */
3152 qualified_name_lookup_error (scope, id_expression, decl, location);
3153 return error_mark_node;
3155 else if (!scope)
3157 /* It may be resolved via Koenig lookup. */
3158 *idk = CP_ID_KIND_UNQUALIFIED;
3159 return id_expression;
3161 else
3162 decl = id_expression;
3164 /* If DECL is a variable that would be out of scope under
3165 ANSI/ISO rules, but in scope in the ARM, name lookup
3166 will succeed. Issue a diagnostic here. */
3167 else
3168 decl = check_for_out_of_scope_variable (decl);
3170 /* Remember that the name was used in the definition of
3171 the current class so that we can check later to see if
3172 the meaning would have been different after the class
3173 was entirely defined. */
3174 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3175 maybe_note_name_used_in_class (id_expression, decl);
3177 /* Disallow uses of local variables from containing functions, except
3178 within lambda-expressions. */
3179 if (!outer_var_p (decl))
3180 /* OK */;
3181 else if (TREE_STATIC (decl)
3182 /* It's not a use (3.2) if we're in an unevaluated context. */
3183 || cp_unevaluated_operand)
3184 /* OK */;
3185 else
3187 tree context = DECL_CONTEXT (decl);
3188 tree containing_function = current_function_decl;
3189 tree lambda_stack = NULL_TREE;
3190 tree lambda_expr = NULL_TREE;
3191 tree initializer = convert_from_reference (decl);
3193 /* Mark it as used now even if the use is ill-formed. */
3194 mark_used (decl);
3196 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3197 support for an approach in which a reference to a local
3198 [constant] automatic variable in a nested class or lambda body
3199 would enter the expression as an rvalue, which would reduce
3200 the complexity of the problem"
3202 FIXME update for final resolution of core issue 696. */
3203 if (decl_maybe_constant_var_p (decl))
3205 if (processing_template_decl)
3206 /* In a template, the constant value may not be in a usable
3207 form, so wait until instantiation time. */
3208 return decl;
3209 else if (decl_constant_var_p (decl))
3210 return integral_constant_value (decl);
3213 if (parsing_nsdmi ())
3214 containing_function = NULL_TREE;
3215 /* If we are in a lambda function, we can move out until we hit
3216 1. the context,
3217 2. a non-lambda function, or
3218 3. a non-default capturing lambda function. */
3219 else while (context != containing_function
3220 && LAMBDA_FUNCTION_P (containing_function))
3222 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3223 (DECL_CONTEXT (containing_function));
3225 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3226 == CPLD_NONE)
3227 break;
3229 lambda_stack = tree_cons (NULL_TREE,
3230 lambda_expr,
3231 lambda_stack);
3233 containing_function
3234 = decl_function_context (containing_function);
3237 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3238 && DECL_ANON_UNION_VAR_P (decl))
3240 error ("cannot capture member %qD of anonymous union", decl);
3241 return error_mark_node;
3243 if (context == containing_function)
3245 decl = add_default_capture (lambda_stack,
3246 /*id=*/DECL_NAME (decl),
3247 initializer);
3249 else if (lambda_expr)
3251 error ("%qD is not captured", decl);
3252 return error_mark_node;
3254 else
3256 error (VAR_P (decl)
3257 ? G_("use of local variable with automatic storage from containing function")
3258 : G_("use of parameter from containing function"));
3259 inform (input_location, "%q+#D declared here", decl);
3260 return error_mark_node;
3264 /* Also disallow uses of function parameters outside the function
3265 body, except inside an unevaluated context (i.e. decltype). */
3266 if (TREE_CODE (decl) == PARM_DECL
3267 && DECL_CONTEXT (decl) == NULL_TREE
3268 && !cp_unevaluated_operand)
3270 *error_msg = "use of parameter outside function body";
3271 return error_mark_node;
3275 /* If we didn't find anything, or what we found was a type,
3276 then this wasn't really an id-expression. */
3277 if (TREE_CODE (decl) == TEMPLATE_DECL
3278 && !DECL_FUNCTION_TEMPLATE_P (decl))
3280 *error_msg = "missing template arguments";
3281 return error_mark_node;
3283 else if (TREE_CODE (decl) == TYPE_DECL
3284 || TREE_CODE (decl) == NAMESPACE_DECL)
3286 *error_msg = "expected primary-expression";
3287 return error_mark_node;
3290 /* If the name resolved to a template parameter, there is no
3291 need to look it up again later. */
3292 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3293 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3295 tree r;
3297 *idk = CP_ID_KIND_NONE;
3298 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3299 decl = TEMPLATE_PARM_DECL (decl);
3300 r = convert_from_reference (DECL_INITIAL (decl));
3302 if (integral_constant_expression_p
3303 && !dependent_type_p (TREE_TYPE (decl))
3304 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3306 if (!allow_non_integral_constant_expression_p)
3307 error ("template parameter %qD of type %qT is not allowed in "
3308 "an integral constant expression because it is not of "
3309 "integral or enumeration type", decl, TREE_TYPE (decl));
3310 *non_integral_constant_expression_p = true;
3312 return r;
3314 else
3316 bool dependent_p;
3318 /* If the declaration was explicitly qualified indicate
3319 that. The semantics of `A::f(3)' are different than
3320 `f(3)' if `f' is virtual. */
3321 *idk = (scope
3322 ? CP_ID_KIND_QUALIFIED
3323 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3324 ? CP_ID_KIND_TEMPLATE_ID
3325 : CP_ID_KIND_UNQUALIFIED));
3328 /* [temp.dep.expr]
3330 An id-expression is type-dependent if it contains an
3331 identifier that was declared with a dependent type.
3333 The standard is not very specific about an id-expression that
3334 names a set of overloaded functions. What if some of them
3335 have dependent types and some of them do not? Presumably,
3336 such a name should be treated as a dependent name. */
3337 /* Assume the name is not dependent. */
3338 dependent_p = false;
3339 if (!processing_template_decl)
3340 /* No names are dependent outside a template. */
3342 else if (TREE_CODE (decl) == CONST_DECL)
3343 /* We don't want to treat enumerators as dependent. */
3345 /* A template-id where the name of the template was not resolved
3346 is definitely dependent. */
3347 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3348 && (identifier_p (TREE_OPERAND (decl, 0))))
3349 dependent_p = true;
3350 /* For anything except an overloaded function, just check its
3351 type. */
3352 else if (!is_overloaded_fn (decl))
3353 dependent_p
3354 = dependent_type_p (TREE_TYPE (decl));
3355 /* For a set of overloaded functions, check each of the
3356 functions. */
3357 else
3359 tree fns = decl;
3361 if (BASELINK_P (fns))
3362 fns = BASELINK_FUNCTIONS (fns);
3364 /* For a template-id, check to see if the template
3365 arguments are dependent. */
3366 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3368 tree args = TREE_OPERAND (fns, 1);
3369 dependent_p = any_dependent_template_arguments_p (args);
3370 /* The functions are those referred to by the
3371 template-id. */
3372 fns = TREE_OPERAND (fns, 0);
3375 /* If there are no dependent template arguments, go through
3376 the overloaded functions. */
3377 while (fns && !dependent_p)
3379 tree fn = OVL_CURRENT (fns);
3381 /* Member functions of dependent classes are
3382 dependent. */
3383 if (TREE_CODE (fn) == FUNCTION_DECL
3384 && type_dependent_expression_p (fn))
3385 dependent_p = true;
3386 else if (TREE_CODE (fn) == TEMPLATE_DECL
3387 && dependent_template_p (fn))
3388 dependent_p = true;
3390 fns = OVL_NEXT (fns);
3394 /* If the name was dependent on a template parameter, we will
3395 resolve the name at instantiation time. */
3396 if (dependent_p)
3398 /* Create a SCOPE_REF for qualified names, if the scope is
3399 dependent. */
3400 if (scope)
3402 if (TYPE_P (scope))
3404 if (address_p && done)
3405 decl = finish_qualified_id_expr (scope, decl,
3406 done, address_p,
3407 template_p,
3408 template_arg_p,
3409 tf_warning_or_error);
3410 else
3412 tree type = NULL_TREE;
3413 if (DECL_P (decl) && !dependent_scope_p (scope))
3414 type = TREE_TYPE (decl);
3415 decl = build_qualified_name (type,
3416 scope,
3417 id_expression,
3418 template_p);
3421 if (TREE_TYPE (decl))
3422 decl = convert_from_reference (decl);
3423 return decl;
3425 /* A TEMPLATE_ID already contains all the information we
3426 need. */
3427 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3428 return id_expression;
3429 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3430 /* If we found a variable, then name lookup during the
3431 instantiation will always resolve to the same VAR_DECL
3432 (or an instantiation thereof). */
3433 if (VAR_P (decl)
3434 || TREE_CODE (decl) == PARM_DECL)
3436 mark_used (decl);
3437 return convert_from_reference (decl);
3439 /* The same is true for FIELD_DECL, but we also need to
3440 make sure that the syntax is correct. */
3441 else if (TREE_CODE (decl) == FIELD_DECL)
3443 /* Since SCOPE is NULL here, this is an unqualified name.
3444 Access checking has been performed during name lookup
3445 already. Turn off checking to avoid duplicate errors. */
3446 push_deferring_access_checks (dk_no_check);
3447 decl = finish_non_static_data_member
3448 (decl, NULL_TREE,
3449 /*qualifying_scope=*/NULL_TREE);
3450 pop_deferring_access_checks ();
3451 return decl;
3453 return id_expression;
3456 if (TREE_CODE (decl) == NAMESPACE_DECL)
3458 error ("use of namespace %qD as expression", decl);
3459 return error_mark_node;
3461 else if (DECL_CLASS_TEMPLATE_P (decl))
3463 error ("use of class template %qT as expression", decl);
3464 return error_mark_node;
3466 else if (TREE_CODE (decl) == TREE_LIST)
3468 /* Ambiguous reference to base members. */
3469 error ("request for member %qD is ambiguous in "
3470 "multiple inheritance lattice", id_expression);
3471 print_candidates (decl);
3472 return error_mark_node;
3475 /* Mark variable-like entities as used. Functions are similarly
3476 marked either below or after overload resolution. */
3477 if ((VAR_P (decl)
3478 || TREE_CODE (decl) == PARM_DECL
3479 || TREE_CODE (decl) == CONST_DECL
3480 || TREE_CODE (decl) == RESULT_DECL)
3481 && !mark_used (decl))
3482 return error_mark_node;
3484 /* Only certain kinds of names are allowed in constant
3485 expression. Template parameters have already
3486 been handled above. */
3487 if (! error_operand_p (decl)
3488 && integral_constant_expression_p
3489 && ! decl_constant_var_p (decl)
3490 && TREE_CODE (decl) != CONST_DECL
3491 && ! builtin_valid_in_constant_expr_p (decl))
3493 if (!allow_non_integral_constant_expression_p)
3495 error ("%qD cannot appear in a constant-expression", decl);
3496 return error_mark_node;
3498 *non_integral_constant_expression_p = true;
3501 tree wrap;
3502 if (VAR_P (decl)
3503 && !cp_unevaluated_operand
3504 && !processing_template_decl
3505 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3506 && DECL_THREAD_LOCAL_P (decl)
3507 && (wrap = get_tls_wrapper_fn (decl)))
3509 /* Replace an evaluated use of the thread_local variable with
3510 a call to its wrapper. */
3511 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3513 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3514 && variable_template_p (TREE_OPERAND (decl, 0)))
3516 decl = finish_template_variable (decl);
3517 mark_used (decl);
3519 else if (scope)
3521 decl = (adjust_result_of_qualified_name_lookup
3522 (decl, scope, current_nonlambda_class_type()));
3524 if (TREE_CODE (decl) == FUNCTION_DECL)
3525 mark_used (decl);
3527 if (TYPE_P (scope))
3528 decl = finish_qualified_id_expr (scope,
3529 decl,
3530 done,
3531 address_p,
3532 template_p,
3533 template_arg_p,
3534 tf_warning_or_error);
3535 else
3536 decl = convert_from_reference (decl);
3538 else if (TREE_CODE (decl) == FIELD_DECL)
3540 /* Since SCOPE is NULL here, this is an unqualified name.
3541 Access checking has been performed during name lookup
3542 already. Turn off checking to avoid duplicate errors. */
3543 push_deferring_access_checks (dk_no_check);
3544 decl = finish_non_static_data_member (decl, NULL_TREE,
3545 /*qualifying_scope=*/NULL_TREE);
3546 pop_deferring_access_checks ();
3548 else if (is_overloaded_fn (decl))
3550 tree first_fn;
3552 first_fn = get_first_fn (decl);
3553 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3554 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3556 if (!really_overloaded_fn (decl)
3557 && !mark_used (first_fn))
3558 return error_mark_node;
3560 if (!template_arg_p
3561 && TREE_CODE (first_fn) == FUNCTION_DECL
3562 && DECL_FUNCTION_MEMBER_P (first_fn)
3563 && !shared_member_p (decl))
3565 /* A set of member functions. */
3566 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3567 return finish_class_member_access_expr (decl, id_expression,
3568 /*template_p=*/false,
3569 tf_warning_or_error);
3572 decl = baselink_for_fns (decl);
3574 else
3576 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3577 && DECL_CLASS_SCOPE_P (decl))
3579 tree context = context_for_name_lookup (decl);
3580 if (context != current_class_type)
3582 tree path = currently_open_derived_class (context);
3583 perform_or_defer_access_check (TYPE_BINFO (path),
3584 decl, decl,
3585 tf_warning_or_error);
3589 decl = convert_from_reference (decl);
3593 /* Handle references (c++/56130). */
3594 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3595 if (TREE_DEPRECATED (t))
3596 warn_deprecated_use (t, NULL_TREE);
3598 return decl;
3601 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3602 use as a type-specifier. */
3604 tree
3605 finish_typeof (tree expr)
3607 tree type;
3609 if (type_dependent_expression_p (expr))
3611 type = cxx_make_type (TYPEOF_TYPE);
3612 TYPEOF_TYPE_EXPR (type) = expr;
3613 SET_TYPE_STRUCTURAL_EQUALITY (type);
3615 return type;
3618 expr = mark_type_use (expr);
3620 type = unlowered_expr_type (expr);
3622 if (!type || type == unknown_type_node)
3624 error ("type of %qE is unknown", expr);
3625 return error_mark_node;
3628 return type;
3631 /* Implement the __underlying_type keyword: Return the underlying
3632 type of TYPE, suitable for use as a type-specifier. */
3634 tree
3635 finish_underlying_type (tree type)
3637 tree underlying_type;
3639 if (processing_template_decl)
3641 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3642 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3643 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3645 return underlying_type;
3648 complete_type (type);
3650 if (TREE_CODE (type) != ENUMERAL_TYPE)
3652 error ("%qT is not an enumeration type", type);
3653 return error_mark_node;
3656 underlying_type = ENUM_UNDERLYING_TYPE (type);
3658 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3659 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3660 See finish_enum_value_list for details. */
3661 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3662 underlying_type
3663 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3664 TYPE_UNSIGNED (underlying_type));
3666 return underlying_type;
3669 /* Implement the __direct_bases keyword: Return the direct base classes
3670 of type */
3672 tree
3673 calculate_direct_bases (tree type)
3675 vec<tree, va_gc> *vector = make_tree_vector();
3676 tree bases_vec = NULL_TREE;
3677 vec<tree, va_gc> *base_binfos;
3678 tree binfo;
3679 unsigned i;
3681 complete_type (type);
3683 if (!NON_UNION_CLASS_TYPE_P (type))
3684 return make_tree_vec (0);
3686 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3688 /* Virtual bases are initialized first */
3689 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3691 if (BINFO_VIRTUAL_P (binfo))
3693 vec_safe_push (vector, binfo);
3697 /* Now non-virtuals */
3698 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3700 if (!BINFO_VIRTUAL_P (binfo))
3702 vec_safe_push (vector, binfo);
3707 bases_vec = make_tree_vec (vector->length ());
3709 for (i = 0; i < vector->length (); ++i)
3711 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3713 return bases_vec;
3716 /* Implement the __bases keyword: Return the base classes
3717 of type */
3719 /* Find morally non-virtual base classes by walking binfo hierarchy */
3720 /* Virtual base classes are handled separately in finish_bases */
3722 static tree
3723 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3725 /* Don't walk bases of virtual bases */
3726 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3729 static tree
3730 dfs_calculate_bases_post (tree binfo, void *data_)
3732 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3733 if (!BINFO_VIRTUAL_P (binfo))
3735 vec_safe_push (*data, BINFO_TYPE (binfo));
3737 return NULL_TREE;
3740 /* Calculates the morally non-virtual base classes of a class */
3741 static vec<tree, va_gc> *
3742 calculate_bases_helper (tree type)
3744 vec<tree, va_gc> *vector = make_tree_vector();
3746 /* Now add non-virtual base classes in order of construction */
3747 dfs_walk_all (TYPE_BINFO (type),
3748 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3749 return vector;
3752 tree
3753 calculate_bases (tree type)
3755 vec<tree, va_gc> *vector = make_tree_vector();
3756 tree bases_vec = NULL_TREE;
3757 unsigned i;
3758 vec<tree, va_gc> *vbases;
3759 vec<tree, va_gc> *nonvbases;
3760 tree binfo;
3762 complete_type (type);
3764 if (!NON_UNION_CLASS_TYPE_P (type))
3765 return make_tree_vec (0);
3767 /* First go through virtual base classes */
3768 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3769 vec_safe_iterate (vbases, i, &binfo); i++)
3771 vec<tree, va_gc> *vbase_bases;
3772 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3773 vec_safe_splice (vector, vbase_bases);
3774 release_tree_vector (vbase_bases);
3777 /* Now for the non-virtual bases */
3778 nonvbases = calculate_bases_helper (type);
3779 vec_safe_splice (vector, nonvbases);
3780 release_tree_vector (nonvbases);
3782 /* Last element is entire class, so don't copy */
3783 bases_vec = make_tree_vec (vector->length () - 1);
3785 for (i = 0; i < vector->length () - 1; ++i)
3787 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3789 release_tree_vector (vector);
3790 return bases_vec;
3793 tree
3794 finish_bases (tree type, bool direct)
3796 tree bases = NULL_TREE;
3798 if (!processing_template_decl)
3800 /* Parameter packs can only be used in templates */
3801 error ("Parameter pack __bases only valid in template declaration");
3802 return error_mark_node;
3805 bases = cxx_make_type (BASES);
3806 BASES_TYPE (bases) = type;
3807 BASES_DIRECT (bases) = direct;
3808 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3810 return bases;
3813 /* Perform C++-specific checks for __builtin_offsetof before calling
3814 fold_offsetof. */
3816 tree
3817 finish_offsetof (tree expr, location_t loc)
3819 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3821 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3822 TREE_OPERAND (expr, 2));
3823 return error_mark_node;
3825 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3826 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3827 || TREE_TYPE (expr) == unknown_type_node)
3829 if (INDIRECT_REF_P (expr))
3830 error ("second operand of %<offsetof%> is neither a single "
3831 "identifier nor a sequence of member accesses and "
3832 "array references");
3833 else
3835 if (TREE_CODE (expr) == COMPONENT_REF
3836 || TREE_CODE (expr) == COMPOUND_EXPR)
3837 expr = TREE_OPERAND (expr, 1);
3838 error ("cannot apply %<offsetof%> to member function %qD", expr);
3840 return error_mark_node;
3842 if (REFERENCE_REF_P (expr))
3843 expr = TREE_OPERAND (expr, 0);
3844 if (TREE_CODE (expr) == COMPONENT_REF)
3846 tree object = TREE_OPERAND (expr, 0);
3847 if (!complete_type_or_else (TREE_TYPE (object), object))
3848 return error_mark_node;
3849 if (warn_invalid_offsetof
3850 && CLASS_TYPE_P (TREE_TYPE (object))
3851 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3852 && cp_unevaluated_operand == 0)
3853 pedwarn (loc, OPT_Winvalid_offsetof,
3854 "offsetof within non-standard-layout type %qT is undefined",
3855 TREE_TYPE (object));
3857 return fold_offsetof (expr);
3860 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3861 function is broken out from the above for the benefit of the tree-ssa
3862 project. */
3864 void
3865 simplify_aggr_init_expr (tree *tp)
3867 tree aggr_init_expr = *tp;
3869 /* Form an appropriate CALL_EXPR. */
3870 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3871 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3872 tree type = TREE_TYPE (slot);
3874 tree call_expr;
3875 enum style_t { ctor, arg, pcc } style;
3877 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3878 style = ctor;
3879 #ifdef PCC_STATIC_STRUCT_RETURN
3880 else if (1)
3881 style = pcc;
3882 #endif
3883 else
3885 gcc_assert (TREE_ADDRESSABLE (type));
3886 style = arg;
3889 call_expr = build_call_array_loc (input_location,
3890 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3892 aggr_init_expr_nargs (aggr_init_expr),
3893 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3894 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3895 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3897 if (style == ctor)
3899 /* Replace the first argument to the ctor with the address of the
3900 slot. */
3901 cxx_mark_addressable (slot);
3902 CALL_EXPR_ARG (call_expr, 0) =
3903 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3905 else if (style == arg)
3907 /* Just mark it addressable here, and leave the rest to
3908 expand_call{,_inline}. */
3909 cxx_mark_addressable (slot);
3910 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3911 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3913 else if (style == pcc)
3915 /* If we're using the non-reentrant PCC calling convention, then we
3916 need to copy the returned value out of the static buffer into the
3917 SLOT. */
3918 push_deferring_access_checks (dk_no_check);
3919 call_expr = build_aggr_init (slot, call_expr,
3920 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3921 tf_warning_or_error);
3922 pop_deferring_access_checks ();
3923 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3926 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3928 tree init = build_zero_init (type, NULL_TREE,
3929 /*static_storage_p=*/false);
3930 init = build2 (INIT_EXPR, void_type_node, slot, init);
3931 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3932 init, call_expr);
3935 *tp = call_expr;
3938 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3940 void
3941 emit_associated_thunks (tree fn)
3943 /* When we use vcall offsets, we emit thunks with the virtual
3944 functions to which they thunk. The whole point of vcall offsets
3945 is so that you can know statically the entire set of thunks that
3946 will ever be needed for a given virtual function, thereby
3947 enabling you to output all the thunks with the function itself. */
3948 if (DECL_VIRTUAL_P (fn)
3949 /* Do not emit thunks for extern template instantiations. */
3950 && ! DECL_REALLY_EXTERN (fn))
3952 tree thunk;
3954 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3956 if (!THUNK_ALIAS (thunk))
3958 use_thunk (thunk, /*emit_p=*/1);
3959 if (DECL_RESULT_THUNK_P (thunk))
3961 tree probe;
3963 for (probe = DECL_THUNKS (thunk);
3964 probe; probe = DECL_CHAIN (probe))
3965 use_thunk (probe, /*emit_p=*/1);
3968 else
3969 gcc_assert (!DECL_THUNKS (thunk));
3974 /* Returns true iff FUN is an instantiation of a constexpr function
3975 template or a defaulted constexpr function. */
3977 static inline bool
3978 is_instantiation_of_constexpr (tree fun)
3980 return ((DECL_TEMPLOID_INSTANTIATION (fun)
3981 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
3982 || (DECL_DEFAULTED_FN (fun)
3983 && DECL_DECLARED_CONSTEXPR_P (fun)));
3987 /* Generate RTL for FN. */
3989 bool
3990 expand_or_defer_fn_1 (tree fn)
3992 /* When the parser calls us after finishing the body of a template
3993 function, we don't really want to expand the body. */
3994 if (processing_template_decl)
3996 /* Normally, collection only occurs in rest_of_compilation. So,
3997 if we don't collect here, we never collect junk generated
3998 during the processing of templates until we hit a
3999 non-template function. It's not safe to do this inside a
4000 nested class, though, as the parser may have local state that
4001 is not a GC root. */
4002 if (!function_depth)
4003 ggc_collect ();
4004 return false;
4007 gcc_assert (DECL_SAVED_TREE (fn));
4009 /* We make a decision about linkage for these functions at the end
4010 of the compilation. Until that point, we do not want the back
4011 end to output them -- but we do want it to see the bodies of
4012 these functions so that it can inline them as appropriate. */
4013 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4015 if (DECL_INTERFACE_KNOWN (fn))
4016 /* We've already made a decision as to how this function will
4017 be handled. */;
4018 else if (!at_eof)
4019 tentative_decl_linkage (fn);
4020 else
4021 import_export_decl (fn);
4023 /* If the user wants us to keep all inline functions, then mark
4024 this function as needed so that finish_file will make sure to
4025 output it later. Similarly, all dllexport'd functions must
4026 be emitted; there may be callers in other DLLs. */
4027 if (DECL_DECLARED_INLINE_P (fn)
4028 && !DECL_REALLY_EXTERN (fn)
4029 && (flag_keep_inline_functions
4030 || (flag_keep_inline_dllexport
4031 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4033 mark_needed (fn);
4034 DECL_EXTERNAL (fn) = 0;
4038 /* If this is a constructor or destructor body, we have to clone
4039 it. */
4040 if (maybe_clone_body (fn))
4042 /* We don't want to process FN again, so pretend we've written
4043 it out, even though we haven't. */
4044 TREE_ASM_WRITTEN (fn) = 1;
4045 /* If this is an instantiation of a constexpr function, keep
4046 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4047 if (!is_instantiation_of_constexpr (fn))
4048 DECL_SAVED_TREE (fn) = NULL_TREE;
4049 return false;
4052 /* There's no reason to do any of the work here if we're only doing
4053 semantic analysis; this code just generates RTL. */
4054 if (flag_syntax_only)
4055 return false;
4057 return true;
4060 void
4061 expand_or_defer_fn (tree fn)
4063 if (expand_or_defer_fn_1 (fn))
4065 function_depth++;
4067 /* Expand or defer, at the whim of the compilation unit manager. */
4068 cgraph_node::finalize_function (fn, function_depth > 1);
4069 emit_associated_thunks (fn);
4071 function_depth--;
4075 struct nrv_data
4077 nrv_data () : visited (37) {}
4079 tree var;
4080 tree result;
4081 hash_table<pointer_hash <tree_node> > visited;
4084 /* Helper function for walk_tree, used by finalize_nrv below. */
4086 static tree
4087 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4089 struct nrv_data *dp = (struct nrv_data *)data;
4090 tree_node **slot;
4092 /* No need to walk into types. There wouldn't be any need to walk into
4093 non-statements, except that we have to consider STMT_EXPRs. */
4094 if (TYPE_P (*tp))
4095 *walk_subtrees = 0;
4096 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4097 but differs from using NULL_TREE in that it indicates that we care
4098 about the value of the RESULT_DECL. */
4099 else if (TREE_CODE (*tp) == RETURN_EXPR)
4100 TREE_OPERAND (*tp, 0) = dp->result;
4101 /* Change all cleanups for the NRV to only run when an exception is
4102 thrown. */
4103 else if (TREE_CODE (*tp) == CLEANUP_STMT
4104 && CLEANUP_DECL (*tp) == dp->var)
4105 CLEANUP_EH_ONLY (*tp) = 1;
4106 /* Replace the DECL_EXPR for the NRV with an initialization of the
4107 RESULT_DECL, if needed. */
4108 else if (TREE_CODE (*tp) == DECL_EXPR
4109 && DECL_EXPR_DECL (*tp) == dp->var)
4111 tree init;
4112 if (DECL_INITIAL (dp->var)
4113 && DECL_INITIAL (dp->var) != error_mark_node)
4114 init = build2 (INIT_EXPR, void_type_node, dp->result,
4115 DECL_INITIAL (dp->var));
4116 else
4117 init = build_empty_stmt (EXPR_LOCATION (*tp));
4118 DECL_INITIAL (dp->var) = NULL_TREE;
4119 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4120 *tp = init;
4122 /* And replace all uses of the NRV with the RESULT_DECL. */
4123 else if (*tp == dp->var)
4124 *tp = dp->result;
4126 /* Avoid walking into the same tree more than once. Unfortunately, we
4127 can't just use walk_tree_without duplicates because it would only call
4128 us for the first occurrence of dp->var in the function body. */
4129 slot = dp->visited.find_slot (*tp, INSERT);
4130 if (*slot)
4131 *walk_subtrees = 0;
4132 else
4133 *slot = *tp;
4135 /* Keep iterating. */
4136 return NULL_TREE;
4139 /* Called from finish_function to implement the named return value
4140 optimization by overriding all the RETURN_EXPRs and pertinent
4141 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4142 RESULT_DECL for the function. */
4144 void
4145 finalize_nrv (tree *tp, tree var, tree result)
4147 struct nrv_data data;
4149 /* Copy name from VAR to RESULT. */
4150 DECL_NAME (result) = DECL_NAME (var);
4151 /* Don't forget that we take its address. */
4152 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4153 /* Finally set DECL_VALUE_EXPR to avoid assigning
4154 a stack slot at -O0 for the original var and debug info
4155 uses RESULT location for VAR. */
4156 SET_DECL_VALUE_EXPR (var, result);
4157 DECL_HAS_VALUE_EXPR_P (var) = 1;
4159 data.var = var;
4160 data.result = result;
4161 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4164 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4166 bool
4167 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4168 bool need_copy_ctor, bool need_copy_assignment,
4169 bool need_dtor)
4171 int save_errorcount = errorcount;
4172 tree info, t;
4174 /* Always allocate 3 elements for simplicity. These are the
4175 function decls for the ctor, dtor, and assignment op.
4176 This layout is known to the three lang hooks,
4177 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4178 and cxx_omp_clause_assign_op. */
4179 info = make_tree_vec (3);
4180 CP_OMP_CLAUSE_INFO (c) = info;
4182 if (need_default_ctor || need_copy_ctor)
4184 if (need_default_ctor)
4185 t = get_default_ctor (type);
4186 else
4187 t = get_copy_ctor (type, tf_warning_or_error);
4189 if (t && !trivial_fn_p (t))
4190 TREE_VEC_ELT (info, 0) = t;
4193 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4194 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4196 if (need_copy_assignment)
4198 t = get_copy_assign (type);
4200 if (t && !trivial_fn_p (t))
4201 TREE_VEC_ELT (info, 2) = t;
4204 return errorcount != save_errorcount;
4207 /* Helper function for handle_omp_array_sections. Called recursively
4208 to handle multiple array-section-subscripts. C is the clause,
4209 T current expression (initially OMP_CLAUSE_DECL), which is either
4210 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4211 expression if specified, TREE_VALUE length expression if specified,
4212 TREE_CHAIN is what it has been specified after, or some decl.
4213 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4214 set to true if any of the array-section-subscript could have length
4215 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4216 first array-section-subscript which is known not to have length
4217 of one. Given say:
4218 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4219 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4220 all are or may have length of 1, array-section-subscript [:2] is the
4221 first one knonwn not to have length 1. For array-section-subscript
4222 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4223 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4224 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4225 case though, as some lengths could be zero. */
4227 static tree
4228 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4229 bool &maybe_zero_len, unsigned int &first_non_one)
4231 tree ret, low_bound, length, type;
4232 if (TREE_CODE (t) != TREE_LIST)
4234 if (error_operand_p (t))
4235 return error_mark_node;
4236 if (type_dependent_expression_p (t))
4237 return NULL_TREE;
4238 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4240 if (processing_template_decl)
4241 return NULL_TREE;
4242 if (DECL_P (t))
4243 error_at (OMP_CLAUSE_LOCATION (c),
4244 "%qD is not a variable in %qs clause", t,
4245 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4246 else
4247 error_at (OMP_CLAUSE_LOCATION (c),
4248 "%qE is not a variable in %qs clause", t,
4249 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4250 return error_mark_node;
4252 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4253 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4255 error_at (OMP_CLAUSE_LOCATION (c),
4256 "%qD is threadprivate variable in %qs clause", t,
4257 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4258 return error_mark_node;
4260 t = convert_from_reference (t);
4261 return t;
4264 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4265 maybe_zero_len, first_non_one);
4266 if (ret == error_mark_node || ret == NULL_TREE)
4267 return ret;
4269 type = TREE_TYPE (ret);
4270 low_bound = TREE_PURPOSE (t);
4271 length = TREE_VALUE (t);
4272 if ((low_bound && type_dependent_expression_p (low_bound))
4273 || (length && type_dependent_expression_p (length)))
4274 return NULL_TREE;
4276 if (low_bound == error_mark_node || length == error_mark_node)
4277 return error_mark_node;
4279 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4281 error_at (OMP_CLAUSE_LOCATION (c),
4282 "low bound %qE of array section does not have integral type",
4283 low_bound);
4284 return error_mark_node;
4286 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4288 error_at (OMP_CLAUSE_LOCATION (c),
4289 "length %qE of array section does not have integral type",
4290 length);
4291 return error_mark_node;
4293 if (low_bound
4294 && TREE_CODE (low_bound) == INTEGER_CST
4295 && TYPE_PRECISION (TREE_TYPE (low_bound))
4296 > TYPE_PRECISION (sizetype))
4297 low_bound = fold_convert (sizetype, low_bound);
4298 if (length
4299 && TREE_CODE (length) == INTEGER_CST
4300 && TYPE_PRECISION (TREE_TYPE (length))
4301 > TYPE_PRECISION (sizetype))
4302 length = fold_convert (sizetype, length);
4303 if (low_bound == NULL_TREE)
4304 low_bound = integer_zero_node;
4306 if (length != NULL_TREE)
4308 if (!integer_nonzerop (length))
4309 maybe_zero_len = true;
4310 if (first_non_one == types.length ()
4311 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4312 first_non_one++;
4314 if (TREE_CODE (type) == ARRAY_TYPE)
4316 if (length == NULL_TREE
4317 && (TYPE_DOMAIN (type) == NULL_TREE
4318 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4320 error_at (OMP_CLAUSE_LOCATION (c),
4321 "for unknown bound array type length expression must "
4322 "be specified");
4323 return error_mark_node;
4325 if (TREE_CODE (low_bound) == INTEGER_CST
4326 && tree_int_cst_sgn (low_bound) == -1)
4328 error_at (OMP_CLAUSE_LOCATION (c),
4329 "negative low bound in array section in %qs clause",
4330 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4331 return error_mark_node;
4333 if (length != NULL_TREE
4334 && TREE_CODE (length) == INTEGER_CST
4335 && tree_int_cst_sgn (length) == -1)
4337 error_at (OMP_CLAUSE_LOCATION (c),
4338 "negative length in array section in %qs clause",
4339 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4340 return error_mark_node;
4342 if (TYPE_DOMAIN (type)
4343 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4344 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4345 == INTEGER_CST)
4347 tree size = size_binop (PLUS_EXPR,
4348 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4349 size_one_node);
4350 if (TREE_CODE (low_bound) == INTEGER_CST)
4352 if (tree_int_cst_lt (size, low_bound))
4354 error_at (OMP_CLAUSE_LOCATION (c),
4355 "low bound %qE above array section size "
4356 "in %qs clause", low_bound,
4357 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4358 return error_mark_node;
4360 if (tree_int_cst_equal (size, low_bound))
4361 maybe_zero_len = true;
4362 else if (length == NULL_TREE
4363 && first_non_one == types.length ()
4364 && tree_int_cst_equal
4365 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4366 low_bound))
4367 first_non_one++;
4369 else if (length == NULL_TREE)
4371 maybe_zero_len = true;
4372 if (first_non_one == types.length ())
4373 first_non_one++;
4375 if (length && TREE_CODE (length) == INTEGER_CST)
4377 if (tree_int_cst_lt (size, length))
4379 error_at (OMP_CLAUSE_LOCATION (c),
4380 "length %qE above array section size "
4381 "in %qs clause", length,
4382 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4383 return error_mark_node;
4385 if (TREE_CODE (low_bound) == INTEGER_CST)
4387 tree lbpluslen
4388 = size_binop (PLUS_EXPR,
4389 fold_convert (sizetype, low_bound),
4390 fold_convert (sizetype, length));
4391 if (TREE_CODE (lbpluslen) == INTEGER_CST
4392 && tree_int_cst_lt (size, lbpluslen))
4394 error_at (OMP_CLAUSE_LOCATION (c),
4395 "high bound %qE above array section size "
4396 "in %qs clause", lbpluslen,
4397 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4398 return error_mark_node;
4403 else if (length == NULL_TREE)
4405 maybe_zero_len = true;
4406 if (first_non_one == types.length ())
4407 first_non_one++;
4410 /* For [lb:] we will need to evaluate lb more than once. */
4411 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4413 tree lb = cp_save_expr (low_bound);
4414 if (lb != low_bound)
4416 TREE_PURPOSE (t) = lb;
4417 low_bound = lb;
4421 else if (TREE_CODE (type) == POINTER_TYPE)
4423 if (length == NULL_TREE)
4425 error_at (OMP_CLAUSE_LOCATION (c),
4426 "for pointer type length expression must be specified");
4427 return error_mark_node;
4429 /* If there is a pointer type anywhere but in the very first
4430 array-section-subscript, the array section can't be contiguous. */
4431 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4432 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4434 error_at (OMP_CLAUSE_LOCATION (c),
4435 "array section is not contiguous in %qs clause",
4436 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4437 return error_mark_node;
4440 else
4442 error_at (OMP_CLAUSE_LOCATION (c),
4443 "%qE does not have pointer or array type", ret);
4444 return error_mark_node;
4446 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4447 types.safe_push (TREE_TYPE (ret));
4448 /* We will need to evaluate lb more than once. */
4449 tree lb = cp_save_expr (low_bound);
4450 if (lb != low_bound)
4452 TREE_PURPOSE (t) = lb;
4453 low_bound = lb;
4455 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4456 return ret;
4459 /* Handle array sections for clause C. */
4461 static bool
4462 handle_omp_array_sections (tree c)
4464 bool maybe_zero_len = false;
4465 unsigned int first_non_one = 0;
4466 auto_vec<tree> types;
4467 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4468 maybe_zero_len, first_non_one);
4469 if (first == error_mark_node)
4470 return true;
4471 if (first == NULL_TREE)
4472 return false;
4473 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4475 tree t = OMP_CLAUSE_DECL (c);
4476 tree tem = NULL_TREE;
4477 if (processing_template_decl)
4478 return false;
4479 /* Need to evaluate side effects in the length expressions
4480 if any. */
4481 while (TREE_CODE (t) == TREE_LIST)
4483 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4485 if (tem == NULL_TREE)
4486 tem = TREE_VALUE (t);
4487 else
4488 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4489 TREE_VALUE (t), tem);
4491 t = TREE_CHAIN (t);
4493 if (tem)
4494 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4495 OMP_CLAUSE_DECL (c) = first;
4497 else
4499 unsigned int num = types.length (), i;
4500 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4501 tree condition = NULL_TREE;
4503 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4504 maybe_zero_len = true;
4505 if (processing_template_decl && maybe_zero_len)
4506 return false;
4508 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4509 t = TREE_CHAIN (t))
4511 tree low_bound = TREE_PURPOSE (t);
4512 tree length = TREE_VALUE (t);
4514 i--;
4515 if (low_bound
4516 && TREE_CODE (low_bound) == INTEGER_CST
4517 && TYPE_PRECISION (TREE_TYPE (low_bound))
4518 > TYPE_PRECISION (sizetype))
4519 low_bound = fold_convert (sizetype, low_bound);
4520 if (length
4521 && TREE_CODE (length) == INTEGER_CST
4522 && TYPE_PRECISION (TREE_TYPE (length))
4523 > TYPE_PRECISION (sizetype))
4524 length = fold_convert (sizetype, length);
4525 if (low_bound == NULL_TREE)
4526 low_bound = integer_zero_node;
4527 if (!maybe_zero_len && i > first_non_one)
4529 if (integer_nonzerop (low_bound))
4530 goto do_warn_noncontiguous;
4531 if (length != NULL_TREE
4532 && TREE_CODE (length) == INTEGER_CST
4533 && TYPE_DOMAIN (types[i])
4534 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4535 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4536 == INTEGER_CST)
4538 tree size;
4539 size = size_binop (PLUS_EXPR,
4540 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4541 size_one_node);
4542 if (!tree_int_cst_equal (length, size))
4544 do_warn_noncontiguous:
4545 error_at (OMP_CLAUSE_LOCATION (c),
4546 "array section is not contiguous in %qs "
4547 "clause",
4548 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4549 return true;
4552 if (!processing_template_decl
4553 && length != NULL_TREE
4554 && TREE_SIDE_EFFECTS (length))
4556 if (side_effects == NULL_TREE)
4557 side_effects = length;
4558 else
4559 side_effects = build2 (COMPOUND_EXPR,
4560 TREE_TYPE (side_effects),
4561 length, side_effects);
4564 else if (processing_template_decl)
4565 continue;
4566 else
4568 tree l;
4570 if (i > first_non_one && length && integer_nonzerop (length))
4571 continue;
4572 if (length)
4573 l = fold_convert (sizetype, length);
4574 else
4576 l = size_binop (PLUS_EXPR,
4577 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4578 size_one_node);
4579 l = size_binop (MINUS_EXPR, l,
4580 fold_convert (sizetype, low_bound));
4582 if (i > first_non_one)
4584 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4585 size_zero_node);
4586 if (condition == NULL_TREE)
4587 condition = l;
4588 else
4589 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4590 l, condition);
4592 else if (size == NULL_TREE)
4594 size = size_in_bytes (TREE_TYPE (types[i]));
4595 size = size_binop (MULT_EXPR, size, l);
4596 if (condition)
4597 size = fold_build3 (COND_EXPR, sizetype, condition,
4598 size, size_zero_node);
4600 else
4601 size = size_binop (MULT_EXPR, size, l);
4604 if (!processing_template_decl)
4606 if (side_effects)
4607 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4608 OMP_CLAUSE_DECL (c) = first;
4609 OMP_CLAUSE_SIZE (c) = size;
4610 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4611 return false;
4612 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4613 OMP_CLAUSE_MAP);
4614 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4615 if (!cxx_mark_addressable (t))
4616 return false;
4617 OMP_CLAUSE_DECL (c2) = t;
4618 t = build_fold_addr_expr (first);
4619 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4620 ptrdiff_type_node, t);
4621 tree ptr = OMP_CLAUSE_DECL (c2);
4622 ptr = convert_from_reference (ptr);
4623 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4624 ptr = build_fold_addr_expr (ptr);
4625 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4626 ptrdiff_type_node, t,
4627 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4628 ptrdiff_type_node, ptr));
4629 OMP_CLAUSE_SIZE (c2) = t;
4630 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4631 OMP_CLAUSE_CHAIN (c) = c2;
4632 ptr = OMP_CLAUSE_DECL (c2);
4633 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4634 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4636 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4637 OMP_CLAUSE_MAP);
4638 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4639 OMP_CLAUSE_DECL (c3) = ptr;
4640 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4641 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4642 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4643 OMP_CLAUSE_CHAIN (c2) = c3;
4647 return false;
4650 /* Return identifier to look up for omp declare reduction. */
4652 tree
4653 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4655 const char *p = NULL;
4656 const char *m = NULL;
4657 switch (reduction_code)
4659 case PLUS_EXPR:
4660 case MULT_EXPR:
4661 case MINUS_EXPR:
4662 case BIT_AND_EXPR:
4663 case BIT_XOR_EXPR:
4664 case BIT_IOR_EXPR:
4665 case TRUTH_ANDIF_EXPR:
4666 case TRUTH_ORIF_EXPR:
4667 reduction_id = ansi_opname (reduction_code);
4668 break;
4669 case MIN_EXPR:
4670 p = "min";
4671 break;
4672 case MAX_EXPR:
4673 p = "max";
4674 break;
4675 default:
4676 break;
4679 if (p == NULL)
4681 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4682 return error_mark_node;
4683 p = IDENTIFIER_POINTER (reduction_id);
4686 if (type != NULL_TREE)
4687 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4689 const char prefix[] = "omp declare reduction ";
4690 size_t lenp = sizeof (prefix);
4691 if (strncmp (p, prefix, lenp - 1) == 0)
4692 lenp = 1;
4693 size_t len = strlen (p);
4694 size_t lenm = m ? strlen (m) + 1 : 0;
4695 char *name = XALLOCAVEC (char, lenp + len + lenm);
4696 if (lenp > 1)
4697 memcpy (name, prefix, lenp - 1);
4698 memcpy (name + lenp - 1, p, len + 1);
4699 if (m)
4701 name[lenp + len - 1] = '~';
4702 memcpy (name + lenp + len, m, lenm);
4704 return get_identifier (name);
4707 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4708 FUNCTION_DECL or NULL_TREE if not found. */
4710 static tree
4711 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4712 vec<tree> *ambiguousp)
4714 tree orig_id = id;
4715 tree baselink = NULL_TREE;
4716 if (identifier_p (id))
4718 cp_id_kind idk;
4719 bool nonint_cst_expression_p;
4720 const char *error_msg;
4721 id = omp_reduction_id (ERROR_MARK, id, type);
4722 tree decl = lookup_name (id);
4723 if (decl == NULL_TREE)
4724 decl = error_mark_node;
4725 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4726 &nonint_cst_expression_p, false, true, false,
4727 false, &error_msg, loc);
4728 if (idk == CP_ID_KIND_UNQUALIFIED
4729 && identifier_p (id))
4731 vec<tree, va_gc> *args = NULL;
4732 vec_safe_push (args, build_reference_type (type));
4733 id = perform_koenig_lookup (id, args, tf_none);
4736 else if (TREE_CODE (id) == SCOPE_REF)
4737 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4738 omp_reduction_id (ERROR_MARK,
4739 TREE_OPERAND (id, 1),
4740 type),
4741 false, false);
4742 tree fns = id;
4743 if (id && is_overloaded_fn (id))
4744 id = get_fns (id);
4745 for (; id; id = OVL_NEXT (id))
4747 tree fndecl = OVL_CURRENT (id);
4748 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4750 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4751 if (same_type_p (TREE_TYPE (argtype), type))
4752 break;
4755 if (id && BASELINK_P (fns))
4757 if (baselinkp)
4758 *baselinkp = fns;
4759 else
4760 baselink = fns;
4762 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4764 vec<tree> ambiguous = vNULL;
4765 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4766 unsigned int ix;
4767 if (ambiguousp == NULL)
4768 ambiguousp = &ambiguous;
4769 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4771 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4772 baselinkp ? baselinkp : &baselink,
4773 ambiguousp);
4774 if (id == NULL_TREE)
4775 continue;
4776 if (!ambiguousp->is_empty ())
4777 ambiguousp->safe_push (id);
4778 else if (ret != NULL_TREE)
4780 ambiguousp->safe_push (ret);
4781 ambiguousp->safe_push (id);
4782 ret = NULL_TREE;
4784 else
4785 ret = id;
4787 if (ambiguousp != &ambiguous)
4788 return ret;
4789 if (!ambiguous.is_empty ())
4791 const char *str = _("candidates are:");
4792 unsigned int idx;
4793 tree udr;
4794 error_at (loc, "user defined reduction lookup is ambiguous");
4795 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4797 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4798 if (idx == 0)
4799 str = get_spaces (str);
4801 ambiguous.release ();
4802 ret = error_mark_node;
4803 baselink = NULL_TREE;
4805 id = ret;
4807 if (id && baselink)
4808 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4809 id, id, tf_warning_or_error);
4810 return id;
4813 /* Helper function for cp_parser_omp_declare_reduction_exprs
4814 and tsubst_omp_udr.
4815 Remove CLEANUP_STMT for data (omp_priv variable).
4816 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4817 DECL_EXPR. */
4819 tree
4820 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4822 if (TYPE_P (*tp))
4823 *walk_subtrees = 0;
4824 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4825 *tp = CLEANUP_BODY (*tp);
4826 else if (TREE_CODE (*tp) == DECL_EXPR)
4828 tree decl = DECL_EXPR_DECL (*tp);
4829 if (!processing_template_decl
4830 && decl == (tree) data
4831 && DECL_INITIAL (decl)
4832 && DECL_INITIAL (decl) != error_mark_node)
4834 tree list = NULL_TREE;
4835 append_to_statement_list_force (*tp, &list);
4836 tree init_expr = build2 (INIT_EXPR, void_type_node,
4837 decl, DECL_INITIAL (decl));
4838 DECL_INITIAL (decl) = NULL_TREE;
4839 append_to_statement_list_force (init_expr, &list);
4840 *tp = list;
4843 return NULL_TREE;
4846 /* Data passed from cp_check_omp_declare_reduction to
4847 cp_check_omp_declare_reduction_r. */
4849 struct cp_check_omp_declare_reduction_data
4851 location_t loc;
4852 tree stmts[7];
4853 bool combiner_p;
4856 /* Helper function for cp_check_omp_declare_reduction, called via
4857 cp_walk_tree. */
4859 static tree
4860 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4862 struct cp_check_omp_declare_reduction_data *udr_data
4863 = (struct cp_check_omp_declare_reduction_data *) data;
4864 if (SSA_VAR_P (*tp)
4865 && !DECL_ARTIFICIAL (*tp)
4866 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4867 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4869 location_t loc = udr_data->loc;
4870 if (udr_data->combiner_p)
4871 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4872 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4873 *tp);
4874 else
4875 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4876 "to variable %qD which is not %<omp_priv%> nor "
4877 "%<omp_orig%>",
4878 *tp);
4879 return *tp;
4881 return NULL_TREE;
4884 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4886 void
4887 cp_check_omp_declare_reduction (tree udr)
4889 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4890 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4891 type = TREE_TYPE (type);
4892 int i;
4893 location_t loc = DECL_SOURCE_LOCATION (udr);
4895 if (type == error_mark_node)
4896 return;
4897 if (ARITHMETIC_TYPE_P (type))
4899 static enum tree_code predef_codes[]
4900 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4901 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4902 for (i = 0; i < 8; i++)
4904 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4905 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4906 const char *n2 = IDENTIFIER_POINTER (id);
4907 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4908 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4909 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4910 break;
4913 if (i == 8
4914 && TREE_CODE (type) != COMPLEX_EXPR)
4916 const char prefix_minmax[] = "omp declare reduction m";
4917 size_t prefix_size = sizeof (prefix_minmax) - 1;
4918 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4919 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4920 prefix_minmax, prefix_size) == 0
4921 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4922 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4923 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4924 i = 0;
4926 if (i < 8)
4928 error_at (loc, "predeclared arithmetic type %qT in "
4929 "%<#pragma omp declare reduction%>", type);
4930 return;
4933 else if (TREE_CODE (type) == FUNCTION_TYPE
4934 || TREE_CODE (type) == METHOD_TYPE
4935 || TREE_CODE (type) == ARRAY_TYPE)
4937 error_at (loc, "function or array type %qT in "
4938 "%<#pragma omp declare reduction%>", type);
4939 return;
4941 else if (TREE_CODE (type) == REFERENCE_TYPE)
4943 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4944 type);
4945 return;
4947 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4949 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4950 "%<#pragma omp declare reduction%>", type);
4951 return;
4954 tree body = DECL_SAVED_TREE (udr);
4955 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4956 return;
4958 tree_stmt_iterator tsi;
4959 struct cp_check_omp_declare_reduction_data data;
4960 memset (data.stmts, 0, sizeof data.stmts);
4961 for (i = 0, tsi = tsi_start (body);
4962 i < 7 && !tsi_end_p (tsi);
4963 i++, tsi_next (&tsi))
4964 data.stmts[i] = tsi_stmt (tsi);
4965 data.loc = loc;
4966 gcc_assert (tsi_end_p (tsi));
4967 if (i >= 3)
4969 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4970 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4971 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4972 return;
4973 data.combiner_p = true;
4974 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4975 &data, NULL))
4976 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4978 if (i >= 6)
4980 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4981 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4982 data.combiner_p = false;
4983 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4984 &data, NULL)
4985 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4986 cp_check_omp_declare_reduction_r, &data, NULL))
4987 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4988 if (i == 7)
4989 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4993 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4994 an inline call. But, remap
4995 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4996 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4998 static tree
4999 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5000 tree decl, tree placeholder)
5002 copy_body_data id;
5003 hash_map<tree, tree> decl_map;
5005 decl_map.put (omp_decl1, placeholder);
5006 decl_map.put (omp_decl2, decl);
5007 memset (&id, 0, sizeof (id));
5008 id.src_fn = DECL_CONTEXT (omp_decl1);
5009 id.dst_fn = current_function_decl;
5010 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5011 id.decl_map = &decl_map;
5013 id.copy_decl = copy_decl_no_change;
5014 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5015 id.transform_new_cfg = true;
5016 id.transform_return_to_modify = false;
5017 id.transform_lang_insert_block = NULL;
5018 id.eh_lp_nr = 0;
5019 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5020 return stmt;
5023 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5024 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5026 static tree
5027 find_omp_placeholder_r (tree *tp, int *, void *data)
5029 if (*tp == (tree) data)
5030 return *tp;
5031 return NULL_TREE;
5034 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5035 Return true if there is some error and the clause should be removed. */
5037 static bool
5038 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5040 tree t = OMP_CLAUSE_DECL (c);
5041 bool predefined = false;
5042 tree type = TREE_TYPE (t);
5043 if (TREE_CODE (type) == REFERENCE_TYPE)
5044 type = TREE_TYPE (type);
5045 if (type == error_mark_node)
5046 return true;
5047 else if (ARITHMETIC_TYPE_P (type))
5048 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5050 case PLUS_EXPR:
5051 case MULT_EXPR:
5052 case MINUS_EXPR:
5053 predefined = true;
5054 break;
5055 case MIN_EXPR:
5056 case MAX_EXPR:
5057 if (TREE_CODE (type) == COMPLEX_TYPE)
5058 break;
5059 predefined = true;
5060 break;
5061 case BIT_AND_EXPR:
5062 case BIT_IOR_EXPR:
5063 case BIT_XOR_EXPR:
5064 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5065 break;
5066 predefined = true;
5067 break;
5068 case TRUTH_ANDIF_EXPR:
5069 case TRUTH_ORIF_EXPR:
5070 if (FLOAT_TYPE_P (type))
5071 break;
5072 predefined = true;
5073 break;
5074 default:
5075 break;
5077 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5079 error ("%qE has invalid type for %<reduction%>", t);
5080 return true;
5082 else if (!processing_template_decl)
5084 t = require_complete_type (t);
5085 if (t == error_mark_node)
5086 return true;
5087 OMP_CLAUSE_DECL (c) = t;
5090 if (predefined)
5092 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5093 return false;
5095 else if (processing_template_decl)
5096 return false;
5098 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5100 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5101 if (TREE_CODE (type) == REFERENCE_TYPE)
5102 type = TREE_TYPE (type);
5103 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5104 if (id == NULL_TREE)
5105 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5106 NULL_TREE, NULL_TREE);
5107 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5108 if (id)
5110 if (id == error_mark_node)
5111 return true;
5112 id = OVL_CURRENT (id);
5113 mark_used (id);
5114 tree body = DECL_SAVED_TREE (id);
5115 if (TREE_CODE (body) == STATEMENT_LIST)
5117 tree_stmt_iterator tsi;
5118 tree placeholder = NULL_TREE;
5119 int i;
5120 tree stmts[7];
5121 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5122 atype = TREE_TYPE (atype);
5123 bool need_static_cast = !same_type_p (type, atype);
5124 memset (stmts, 0, sizeof stmts);
5125 for (i = 0, tsi = tsi_start (body);
5126 i < 7 && !tsi_end_p (tsi);
5127 i++, tsi_next (&tsi))
5128 stmts[i] = tsi_stmt (tsi);
5129 gcc_assert (tsi_end_p (tsi));
5131 if (i >= 3)
5133 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5134 && TREE_CODE (stmts[1]) == DECL_EXPR);
5135 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5136 DECL_ARTIFICIAL (placeholder) = 1;
5137 DECL_IGNORED_P (placeholder) = 1;
5138 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5139 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5140 cxx_mark_addressable (placeholder);
5141 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5142 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5143 != REFERENCE_TYPE)
5144 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5145 tree omp_out = placeholder;
5146 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5147 if (need_static_cast)
5149 tree rtype = build_reference_type (atype);
5150 omp_out = build_static_cast (rtype, omp_out,
5151 tf_warning_or_error);
5152 omp_in = build_static_cast (rtype, omp_in,
5153 tf_warning_or_error);
5154 if (omp_out == error_mark_node || omp_in == error_mark_node)
5155 return true;
5156 omp_out = convert_from_reference (omp_out);
5157 omp_in = convert_from_reference (omp_in);
5159 OMP_CLAUSE_REDUCTION_MERGE (c)
5160 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5161 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5163 if (i >= 6)
5165 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5166 && TREE_CODE (stmts[4]) == DECL_EXPR);
5167 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5168 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5169 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5170 cxx_mark_addressable (placeholder);
5171 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5172 tree omp_orig = placeholder;
5173 if (need_static_cast)
5175 if (i == 7)
5177 error_at (OMP_CLAUSE_LOCATION (c),
5178 "user defined reduction with constructor "
5179 "initializer for base class %qT", atype);
5180 return true;
5182 tree rtype = build_reference_type (atype);
5183 omp_priv = build_static_cast (rtype, omp_priv,
5184 tf_warning_or_error);
5185 omp_orig = build_static_cast (rtype, omp_orig,
5186 tf_warning_or_error);
5187 if (omp_priv == error_mark_node
5188 || omp_orig == error_mark_node)
5189 return true;
5190 omp_priv = convert_from_reference (omp_priv);
5191 omp_orig = convert_from_reference (omp_orig);
5193 if (i == 6)
5194 *need_default_ctor = true;
5195 OMP_CLAUSE_REDUCTION_INIT (c)
5196 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5197 DECL_EXPR_DECL (stmts[3]),
5198 omp_priv, omp_orig);
5199 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5200 find_omp_placeholder_r, placeholder, NULL))
5201 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5203 else if (i >= 3)
5205 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5206 *need_default_ctor = true;
5207 else
5209 tree init;
5210 tree v = convert_from_reference (t);
5211 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5212 init = build_constructor (TREE_TYPE (v), NULL);
5213 else
5214 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5215 OMP_CLAUSE_REDUCTION_INIT (c)
5216 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5221 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5222 *need_dtor = true;
5223 else
5225 error ("user defined reduction not found for %qD", t);
5226 return true;
5228 return false;
5231 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5232 Remove any elements from the list that are invalid. */
5234 tree
5235 finish_omp_clauses (tree clauses)
5237 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5238 bitmap_head aligned_head;
5239 tree c, t, *pc;
5240 bool branch_seen = false;
5241 bool copyprivate_seen = false;
5243 bitmap_obstack_initialize (NULL);
5244 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5245 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5246 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5247 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5249 for (pc = &clauses, c = clauses; c ; c = *pc)
5251 bool remove = false;
5253 switch (OMP_CLAUSE_CODE (c))
5255 case OMP_CLAUSE_SHARED:
5256 goto check_dup_generic;
5257 case OMP_CLAUSE_PRIVATE:
5258 goto check_dup_generic;
5259 case OMP_CLAUSE_REDUCTION:
5260 goto check_dup_generic;
5261 case OMP_CLAUSE_COPYPRIVATE:
5262 copyprivate_seen = true;
5263 goto check_dup_generic;
5264 case OMP_CLAUSE_COPYIN:
5265 goto check_dup_generic;
5266 case OMP_CLAUSE_LINEAR:
5267 t = OMP_CLAUSE_DECL (c);
5268 if (!type_dependent_expression_p (t)
5269 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5270 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5272 error ("linear clause applied to non-integral non-pointer "
5273 "variable with %qT type", TREE_TYPE (t));
5274 remove = true;
5275 break;
5277 t = OMP_CLAUSE_LINEAR_STEP (c);
5278 if (t == NULL_TREE)
5279 t = integer_one_node;
5280 if (t == error_mark_node)
5282 remove = true;
5283 break;
5285 else if (!type_dependent_expression_p (t)
5286 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5288 error ("linear step expression must be integral");
5289 remove = true;
5290 break;
5292 else
5294 t = mark_rvalue_use (t);
5295 if (!processing_template_decl)
5297 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5298 t = maybe_constant_value (t);
5299 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5300 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5301 == POINTER_TYPE)
5303 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5304 OMP_CLAUSE_DECL (c), t);
5305 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5306 MINUS_EXPR, sizetype, t,
5307 OMP_CLAUSE_DECL (c));
5308 if (t == error_mark_node)
5310 remove = true;
5311 break;
5314 else
5315 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5317 OMP_CLAUSE_LINEAR_STEP (c) = t;
5319 goto check_dup_generic;
5320 check_dup_generic:
5321 t = OMP_CLAUSE_DECL (c);
5322 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5324 if (processing_template_decl)
5325 break;
5326 if (DECL_P (t))
5327 error ("%qD is not a variable in clause %qs", t,
5328 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5329 else
5330 error ("%qE is not a variable in clause %qs", t,
5331 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5332 remove = true;
5334 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5335 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5336 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5338 error ("%qD appears more than once in data clauses", t);
5339 remove = true;
5341 else
5342 bitmap_set_bit (&generic_head, DECL_UID (t));
5343 break;
5345 case OMP_CLAUSE_FIRSTPRIVATE:
5346 t = OMP_CLAUSE_DECL (c);
5347 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5349 if (processing_template_decl)
5350 break;
5351 if (DECL_P (t))
5352 error ("%qD is not a variable in clause %<firstprivate%>", t);
5353 else
5354 error ("%qE is not a variable in clause %<firstprivate%>", t);
5355 remove = true;
5357 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5358 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5360 error ("%qD appears more than once in data clauses", t);
5361 remove = true;
5363 else
5364 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5365 break;
5367 case OMP_CLAUSE_LASTPRIVATE:
5368 t = OMP_CLAUSE_DECL (c);
5369 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5371 if (processing_template_decl)
5372 break;
5373 if (DECL_P (t))
5374 error ("%qD is not a variable in clause %<lastprivate%>", t);
5375 else
5376 error ("%qE is not a variable in clause %<lastprivate%>", t);
5377 remove = true;
5379 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5380 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5382 error ("%qD appears more than once in data clauses", t);
5383 remove = true;
5385 else
5386 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5387 break;
5389 case OMP_CLAUSE_IF:
5390 t = OMP_CLAUSE_IF_EXPR (c);
5391 t = maybe_convert_cond (t);
5392 if (t == error_mark_node)
5393 remove = true;
5394 else if (!processing_template_decl)
5395 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5396 OMP_CLAUSE_IF_EXPR (c) = t;
5397 break;
5399 case OMP_CLAUSE_FINAL:
5400 t = OMP_CLAUSE_FINAL_EXPR (c);
5401 t = maybe_convert_cond (t);
5402 if (t == error_mark_node)
5403 remove = true;
5404 else if (!processing_template_decl)
5405 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5406 OMP_CLAUSE_FINAL_EXPR (c) = t;
5407 break;
5409 case OMP_CLAUSE_NUM_THREADS:
5410 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5411 if (t == error_mark_node)
5412 remove = true;
5413 else if (!type_dependent_expression_p (t)
5414 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5416 error ("num_threads expression must be integral");
5417 remove = true;
5419 else
5421 t = mark_rvalue_use (t);
5422 if (!processing_template_decl)
5423 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5424 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5426 break;
5428 case OMP_CLAUSE_SCHEDULE:
5429 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5430 if (t == NULL)
5432 else if (t == error_mark_node)
5433 remove = true;
5434 else if (!type_dependent_expression_p (t)
5435 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5436 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5437 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5439 error ("schedule chunk size expression must be integral");
5440 remove = true;
5442 else
5444 t = mark_rvalue_use (t);
5445 if (!processing_template_decl)
5447 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5448 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5450 t = convert_to_integer (long_integer_type_node, t);
5451 if (t == error_mark_node)
5453 remove = true;
5454 break;
5457 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5459 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5461 break;
5463 case OMP_CLAUSE_SIMDLEN:
5464 case OMP_CLAUSE_SAFELEN:
5465 t = OMP_CLAUSE_OPERAND (c, 0);
5466 if (t == error_mark_node)
5467 remove = true;
5468 else if (!type_dependent_expression_p (t)
5469 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5471 error ("%qs length expression must be integral",
5472 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5473 remove = true;
5475 else
5477 t = mark_rvalue_use (t);
5478 t = maybe_constant_value (t);
5479 if (!processing_template_decl)
5481 if (TREE_CODE (t) != INTEGER_CST
5482 || tree_int_cst_sgn (t) != 1)
5484 error ("%qs length expression must be positive constant"
5485 " integer expression",
5486 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5487 remove = true;
5490 OMP_CLAUSE_OPERAND (c, 0) = t;
5492 break;
5494 case OMP_CLAUSE_NUM_TEAMS:
5495 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5496 if (t == error_mark_node)
5497 remove = true;
5498 else if (!type_dependent_expression_p (t)
5499 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5501 error ("%<num_teams%> expression must be integral");
5502 remove = true;
5504 else
5506 t = mark_rvalue_use (t);
5507 if (!processing_template_decl)
5508 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5509 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5511 break;
5513 case OMP_CLAUSE_THREAD_LIMIT:
5514 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5515 if (t == error_mark_node)
5516 remove = true;
5517 else if (!type_dependent_expression_p (t)
5518 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5520 error ("%<thread_limit%> expression must be integral");
5521 remove = true;
5523 else
5525 t = mark_rvalue_use (t);
5526 if (!processing_template_decl)
5527 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5528 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5530 break;
5532 case OMP_CLAUSE_DEVICE:
5533 t = OMP_CLAUSE_DEVICE_ID (c);
5534 if (t == error_mark_node)
5535 remove = true;
5536 else if (!type_dependent_expression_p (t)
5537 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5539 error ("%<device%> id must be integral");
5540 remove = true;
5542 else
5544 t = mark_rvalue_use (t);
5545 if (!processing_template_decl)
5546 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5547 OMP_CLAUSE_DEVICE_ID (c) = t;
5549 break;
5551 case OMP_CLAUSE_DIST_SCHEDULE:
5552 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5553 if (t == NULL)
5555 else if (t == error_mark_node)
5556 remove = true;
5557 else if (!type_dependent_expression_p (t)
5558 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5560 error ("%<dist_schedule%> chunk size expression must be "
5561 "integral");
5562 remove = true;
5564 else
5566 t = mark_rvalue_use (t);
5567 if (!processing_template_decl)
5568 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5569 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5571 break;
5573 case OMP_CLAUSE_ALIGNED:
5574 t = OMP_CLAUSE_DECL (c);
5575 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5577 if (processing_template_decl)
5578 break;
5579 if (DECL_P (t))
5580 error ("%qD is not a variable in %<aligned%> clause", t);
5581 else
5582 error ("%qE is not a variable in %<aligned%> clause", t);
5583 remove = true;
5585 else if (!type_dependent_expression_p (t)
5586 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5587 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5588 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5589 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5590 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5591 != ARRAY_TYPE))))
5593 error_at (OMP_CLAUSE_LOCATION (c),
5594 "%qE in %<aligned%> clause is neither a pointer nor "
5595 "an array nor a reference to pointer or array", t);
5596 remove = true;
5598 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5600 error ("%qD appears more than once in %<aligned%> clauses", t);
5601 remove = true;
5603 else
5604 bitmap_set_bit (&aligned_head, DECL_UID (t));
5605 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5606 if (t == error_mark_node)
5607 remove = true;
5608 else if (t == NULL_TREE)
5609 break;
5610 else if (!type_dependent_expression_p (t)
5611 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5613 error ("%<aligned%> clause alignment expression must "
5614 "be integral");
5615 remove = true;
5617 else
5619 t = mark_rvalue_use (t);
5620 t = maybe_constant_value (t);
5621 if (!processing_template_decl)
5623 if (TREE_CODE (t) != INTEGER_CST
5624 || tree_int_cst_sgn (t) != 1)
5626 error ("%<aligned%> clause alignment expression must be "
5627 "positive constant integer expression");
5628 remove = true;
5631 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5633 break;
5635 case OMP_CLAUSE_DEPEND:
5636 t = OMP_CLAUSE_DECL (c);
5637 if (TREE_CODE (t) == TREE_LIST)
5639 if (handle_omp_array_sections (c))
5640 remove = true;
5641 break;
5643 if (t == error_mark_node)
5644 remove = true;
5645 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5647 if (processing_template_decl)
5648 break;
5649 if (DECL_P (t))
5650 error ("%qD is not a variable in %<depend%> clause", t);
5651 else
5652 error ("%qE is not a variable in %<depend%> clause", t);
5653 remove = true;
5655 else if (!processing_template_decl
5656 && !cxx_mark_addressable (t))
5657 remove = true;
5658 break;
5660 case OMP_CLAUSE_MAP:
5661 case OMP_CLAUSE_TO:
5662 case OMP_CLAUSE_FROM:
5663 t = OMP_CLAUSE_DECL (c);
5664 if (TREE_CODE (t) == TREE_LIST)
5666 if (handle_omp_array_sections (c))
5667 remove = true;
5668 else
5670 t = OMP_CLAUSE_DECL (c);
5671 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5673 error_at (OMP_CLAUSE_LOCATION (c),
5674 "array section does not have mappable type "
5675 "in %qs clause",
5676 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5677 remove = true;
5680 break;
5682 if (t == error_mark_node)
5683 remove = true;
5684 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5686 if (processing_template_decl)
5687 break;
5688 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5689 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5690 break;
5691 if (DECL_P (t))
5692 error ("%qD is not a variable in %qs clause", t,
5693 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5694 else
5695 error ("%qE is not a variable in %qs clause", t,
5696 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5697 remove = true;
5699 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5701 error ("%qD is threadprivate variable in %qs clause", t,
5702 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5703 remove = true;
5705 else if (!processing_template_decl
5706 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5707 && !cxx_mark_addressable (t))
5708 remove = true;
5709 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5710 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5711 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5712 == REFERENCE_TYPE)
5713 ? TREE_TYPE (TREE_TYPE (t))
5714 : TREE_TYPE (t)))
5716 error_at (OMP_CLAUSE_LOCATION (c),
5717 "%qD does not have a mappable type in %qs clause", t,
5718 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5719 remove = true;
5721 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5723 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5724 error ("%qD appears more than once in motion clauses", t);
5725 else
5726 error ("%qD appears more than once in map clauses", t);
5727 remove = true;
5729 else
5730 bitmap_set_bit (&generic_head, DECL_UID (t));
5731 break;
5733 case OMP_CLAUSE_UNIFORM:
5734 t = OMP_CLAUSE_DECL (c);
5735 if (TREE_CODE (t) != PARM_DECL)
5737 if (processing_template_decl)
5738 break;
5739 if (DECL_P (t))
5740 error ("%qD is not an argument in %<uniform%> clause", t);
5741 else
5742 error ("%qE is not an argument in %<uniform%> clause", t);
5743 remove = true;
5744 break;
5746 goto check_dup_generic;
5748 case OMP_CLAUSE_NOWAIT:
5749 case OMP_CLAUSE_ORDERED:
5750 case OMP_CLAUSE_DEFAULT:
5751 case OMP_CLAUSE_UNTIED:
5752 case OMP_CLAUSE_COLLAPSE:
5753 case OMP_CLAUSE_MERGEABLE:
5754 case OMP_CLAUSE_PARALLEL:
5755 case OMP_CLAUSE_FOR:
5756 case OMP_CLAUSE_SECTIONS:
5757 case OMP_CLAUSE_TASKGROUP:
5758 case OMP_CLAUSE_PROC_BIND:
5759 case OMP_CLAUSE__CILK_FOR_COUNT_:
5760 break;
5762 case OMP_CLAUSE_INBRANCH:
5763 case OMP_CLAUSE_NOTINBRANCH:
5764 if (branch_seen)
5766 error ("%<inbranch%> clause is incompatible with "
5767 "%<notinbranch%>");
5768 remove = true;
5770 branch_seen = true;
5771 break;
5773 default:
5774 gcc_unreachable ();
5777 if (remove)
5778 *pc = OMP_CLAUSE_CHAIN (c);
5779 else
5780 pc = &OMP_CLAUSE_CHAIN (c);
5783 for (pc = &clauses, c = clauses; c ; c = *pc)
5785 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5786 bool remove = false;
5787 bool need_complete_non_reference = false;
5788 bool need_default_ctor = false;
5789 bool need_copy_ctor = false;
5790 bool need_copy_assignment = false;
5791 bool need_implicitly_determined = false;
5792 bool need_dtor = false;
5793 tree type, inner_type;
5795 switch (c_kind)
5797 case OMP_CLAUSE_SHARED:
5798 need_implicitly_determined = true;
5799 break;
5800 case OMP_CLAUSE_PRIVATE:
5801 need_complete_non_reference = true;
5802 need_default_ctor = true;
5803 need_dtor = true;
5804 need_implicitly_determined = true;
5805 break;
5806 case OMP_CLAUSE_FIRSTPRIVATE:
5807 need_complete_non_reference = true;
5808 need_copy_ctor = true;
5809 need_dtor = true;
5810 need_implicitly_determined = true;
5811 break;
5812 case OMP_CLAUSE_LASTPRIVATE:
5813 need_complete_non_reference = true;
5814 need_copy_assignment = true;
5815 need_implicitly_determined = true;
5816 break;
5817 case OMP_CLAUSE_REDUCTION:
5818 need_implicitly_determined = true;
5819 break;
5820 case OMP_CLAUSE_COPYPRIVATE:
5821 need_copy_assignment = true;
5822 break;
5823 case OMP_CLAUSE_COPYIN:
5824 need_copy_assignment = true;
5825 break;
5826 case OMP_CLAUSE_NOWAIT:
5827 if (copyprivate_seen)
5829 error_at (OMP_CLAUSE_LOCATION (c),
5830 "%<nowait%> clause must not be used together "
5831 "with %<copyprivate%>");
5832 *pc = OMP_CLAUSE_CHAIN (c);
5833 continue;
5835 /* FALLTHRU */
5836 default:
5837 pc = &OMP_CLAUSE_CHAIN (c);
5838 continue;
5841 t = OMP_CLAUSE_DECL (c);
5842 if (processing_template_decl
5843 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5845 pc = &OMP_CLAUSE_CHAIN (c);
5846 continue;
5849 switch (c_kind)
5851 case OMP_CLAUSE_LASTPRIVATE:
5852 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5854 need_default_ctor = true;
5855 need_dtor = true;
5857 break;
5859 case OMP_CLAUSE_REDUCTION:
5860 if (finish_omp_reduction_clause (c, &need_default_ctor,
5861 &need_dtor))
5862 remove = true;
5863 else
5864 t = OMP_CLAUSE_DECL (c);
5865 break;
5867 case OMP_CLAUSE_COPYIN:
5868 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5870 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5871 remove = true;
5873 break;
5875 default:
5876 break;
5879 if (need_complete_non_reference || need_copy_assignment)
5881 t = require_complete_type (t);
5882 if (t == error_mark_node)
5883 remove = true;
5884 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5885 && need_complete_non_reference)
5887 error ("%qE has reference type for %qs", t,
5888 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5889 remove = true;
5892 if (need_implicitly_determined)
5894 const char *share_name = NULL;
5896 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5897 share_name = "threadprivate";
5898 else switch (cxx_omp_predetermined_sharing (t))
5900 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5901 break;
5902 case OMP_CLAUSE_DEFAULT_SHARED:
5903 /* const vars may be specified in firstprivate clause. */
5904 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5905 && cxx_omp_const_qual_no_mutable (t))
5906 break;
5907 share_name = "shared";
5908 break;
5909 case OMP_CLAUSE_DEFAULT_PRIVATE:
5910 share_name = "private";
5911 break;
5912 default:
5913 gcc_unreachable ();
5915 if (share_name)
5917 error ("%qE is predetermined %qs for %qs",
5918 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5919 remove = true;
5923 /* We're interested in the base element, not arrays. */
5924 inner_type = type = TREE_TYPE (t);
5925 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5926 inner_type = TREE_TYPE (inner_type);
5928 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5929 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5930 inner_type = TREE_TYPE (inner_type);
5932 /* Check for special function availability by building a call to one.
5933 Save the results, because later we won't be in the right context
5934 for making these queries. */
5935 if (CLASS_TYPE_P (inner_type)
5936 && COMPLETE_TYPE_P (inner_type)
5937 && (need_default_ctor || need_copy_ctor
5938 || need_copy_assignment || need_dtor)
5939 && !type_dependent_expression_p (t)
5940 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5941 need_copy_ctor, need_copy_assignment,
5942 need_dtor))
5943 remove = true;
5945 if (remove)
5946 *pc = OMP_CLAUSE_CHAIN (c);
5947 else
5948 pc = &OMP_CLAUSE_CHAIN (c);
5951 bitmap_obstack_release (NULL);
5952 return clauses;
5955 /* For all variables in the tree_list VARS, mark them as thread local. */
5957 void
5958 finish_omp_threadprivate (tree vars)
5960 tree t;
5962 /* Mark every variable in VARS to be assigned thread local storage. */
5963 for (t = vars; t; t = TREE_CHAIN (t))
5965 tree v = TREE_PURPOSE (t);
5967 if (error_operand_p (v))
5969 else if (!VAR_P (v))
5970 error ("%<threadprivate%> %qD is not file, namespace "
5971 "or block scope variable", v);
5972 /* If V had already been marked threadprivate, it doesn't matter
5973 whether it had been used prior to this point. */
5974 else if (TREE_USED (v)
5975 && (DECL_LANG_SPECIFIC (v) == NULL
5976 || !CP_DECL_THREADPRIVATE_P (v)))
5977 error ("%qE declared %<threadprivate%> after first use", v);
5978 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
5979 error ("automatic variable %qE cannot be %<threadprivate%>", v);
5980 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
5981 error ("%<threadprivate%> %qE has incomplete type", v);
5982 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
5983 && CP_DECL_CONTEXT (v) != current_class_type)
5984 error ("%<threadprivate%> %qE directive not "
5985 "in %qT definition", v, CP_DECL_CONTEXT (v));
5986 else
5988 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5989 if (DECL_LANG_SPECIFIC (v) == NULL)
5991 retrofit_lang_decl (v);
5993 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5994 after the allocation of the lang_decl structure. */
5995 if (DECL_DISCRIMINATOR_P (v))
5996 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
5999 if (! DECL_THREAD_LOCAL_P (v))
6001 set_decl_tls_model (v, decl_default_tls_model (v));
6002 /* If rtl has been already set for this var, call
6003 make_decl_rtl once again, so that encode_section_info
6004 has a chance to look at the new decl flags. */
6005 if (DECL_RTL_SET_P (v))
6006 make_decl_rtl (v);
6008 CP_DECL_THREADPRIVATE_P (v) = 1;
6013 /* Build an OpenMP structured block. */
6015 tree
6016 begin_omp_structured_block (void)
6018 return do_pushlevel (sk_omp);
6021 tree
6022 finish_omp_structured_block (tree block)
6024 return do_poplevel (block);
6027 /* Similarly, except force the retention of the BLOCK. */
6029 tree
6030 begin_omp_parallel (void)
6032 keep_next_level (true);
6033 return begin_omp_structured_block ();
6036 tree
6037 finish_omp_parallel (tree clauses, tree body)
6039 tree stmt;
6041 body = finish_omp_structured_block (body);
6043 stmt = make_node (OMP_PARALLEL);
6044 TREE_TYPE (stmt) = void_type_node;
6045 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6046 OMP_PARALLEL_BODY (stmt) = body;
6048 return add_stmt (stmt);
6051 tree
6052 begin_omp_task (void)
6054 keep_next_level (true);
6055 return begin_omp_structured_block ();
6058 tree
6059 finish_omp_task (tree clauses, tree body)
6061 tree stmt;
6063 body = finish_omp_structured_block (body);
6065 stmt = make_node (OMP_TASK);
6066 TREE_TYPE (stmt) = void_type_node;
6067 OMP_TASK_CLAUSES (stmt) = clauses;
6068 OMP_TASK_BODY (stmt) = body;
6070 return add_stmt (stmt);
6073 /* Helper function for finish_omp_for. Convert Ith random access iterator
6074 into integral iterator. Return FALSE if successful. */
6076 static bool
6077 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6078 tree condv, tree incrv, tree *body,
6079 tree *pre_body, tree clauses, tree *lastp)
6081 tree diff, iter_init, iter_incr = NULL, last;
6082 tree incr_var = NULL, orig_pre_body, orig_body, c;
6083 tree decl = TREE_VEC_ELT (declv, i);
6084 tree init = TREE_VEC_ELT (initv, i);
6085 tree cond = TREE_VEC_ELT (condv, i);
6086 tree incr = TREE_VEC_ELT (incrv, i);
6087 tree iter = decl;
6088 location_t elocus = locus;
6090 if (init && EXPR_HAS_LOCATION (init))
6091 elocus = EXPR_LOCATION (init);
6093 switch (TREE_CODE (cond))
6095 case GT_EXPR:
6096 case GE_EXPR:
6097 case LT_EXPR:
6098 case LE_EXPR:
6099 case NE_EXPR:
6100 if (TREE_OPERAND (cond, 1) == iter)
6101 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6102 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6103 if (TREE_OPERAND (cond, 0) != iter)
6104 cond = error_mark_node;
6105 else
6107 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6108 TREE_CODE (cond),
6109 iter, ERROR_MARK,
6110 TREE_OPERAND (cond, 1), ERROR_MARK,
6111 NULL, tf_warning_or_error);
6112 if (error_operand_p (tem))
6113 return true;
6115 break;
6116 default:
6117 cond = error_mark_node;
6118 break;
6120 if (cond == error_mark_node)
6122 error_at (elocus, "invalid controlling predicate");
6123 return true;
6125 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6126 ERROR_MARK, iter, ERROR_MARK, NULL,
6127 tf_warning_or_error);
6128 if (error_operand_p (diff))
6129 return true;
6130 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6132 error_at (elocus, "difference between %qE and %qD does not have integer type",
6133 TREE_OPERAND (cond, 1), iter);
6134 return true;
6137 switch (TREE_CODE (incr))
6139 case PREINCREMENT_EXPR:
6140 case PREDECREMENT_EXPR:
6141 case POSTINCREMENT_EXPR:
6142 case POSTDECREMENT_EXPR:
6143 if (TREE_OPERAND (incr, 0) != iter)
6145 incr = error_mark_node;
6146 break;
6148 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6149 TREE_CODE (incr), iter,
6150 tf_warning_or_error);
6151 if (error_operand_p (iter_incr))
6152 return true;
6153 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6154 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6155 incr = integer_one_node;
6156 else
6157 incr = integer_minus_one_node;
6158 break;
6159 case MODIFY_EXPR:
6160 if (TREE_OPERAND (incr, 0) != iter)
6161 incr = error_mark_node;
6162 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6163 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6165 tree rhs = TREE_OPERAND (incr, 1);
6166 if (TREE_OPERAND (rhs, 0) == iter)
6168 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6169 != INTEGER_TYPE)
6170 incr = error_mark_node;
6171 else
6173 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6174 iter, TREE_CODE (rhs),
6175 TREE_OPERAND (rhs, 1),
6176 tf_warning_or_error);
6177 if (error_operand_p (iter_incr))
6178 return true;
6179 incr = TREE_OPERAND (rhs, 1);
6180 incr = cp_convert (TREE_TYPE (diff), incr,
6181 tf_warning_or_error);
6182 if (TREE_CODE (rhs) == MINUS_EXPR)
6184 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6185 incr = fold_if_not_in_template (incr);
6187 if (TREE_CODE (incr) != INTEGER_CST
6188 && (TREE_CODE (incr) != NOP_EXPR
6189 || (TREE_CODE (TREE_OPERAND (incr, 0))
6190 != INTEGER_CST)))
6191 iter_incr = NULL;
6194 else if (TREE_OPERAND (rhs, 1) == iter)
6196 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6197 || TREE_CODE (rhs) != PLUS_EXPR)
6198 incr = error_mark_node;
6199 else
6201 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6202 PLUS_EXPR,
6203 TREE_OPERAND (rhs, 0),
6204 ERROR_MARK, iter,
6205 ERROR_MARK, NULL,
6206 tf_warning_or_error);
6207 if (error_operand_p (iter_incr))
6208 return true;
6209 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6210 iter, NOP_EXPR,
6211 iter_incr,
6212 tf_warning_or_error);
6213 if (error_operand_p (iter_incr))
6214 return true;
6215 incr = TREE_OPERAND (rhs, 0);
6216 iter_incr = NULL;
6219 else
6220 incr = error_mark_node;
6222 else
6223 incr = error_mark_node;
6224 break;
6225 default:
6226 incr = error_mark_node;
6227 break;
6230 if (incr == error_mark_node)
6232 error_at (elocus, "invalid increment expression");
6233 return true;
6236 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6237 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6238 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6239 && OMP_CLAUSE_DECL (c) == iter)
6240 break;
6242 decl = create_temporary_var (TREE_TYPE (diff));
6243 pushdecl (decl);
6244 add_decl_expr (decl);
6245 last = create_temporary_var (TREE_TYPE (diff));
6246 pushdecl (last);
6247 add_decl_expr (last);
6248 if (c && iter_incr == NULL)
6250 incr_var = create_temporary_var (TREE_TYPE (diff));
6251 pushdecl (incr_var);
6252 add_decl_expr (incr_var);
6254 gcc_assert (stmts_are_full_exprs_p ());
6256 orig_pre_body = *pre_body;
6257 *pre_body = push_stmt_list ();
6258 if (orig_pre_body)
6259 add_stmt (orig_pre_body);
6260 if (init != NULL)
6261 finish_expr_stmt (build_x_modify_expr (elocus,
6262 iter, NOP_EXPR, init,
6263 tf_warning_or_error));
6264 init = build_int_cst (TREE_TYPE (diff), 0);
6265 if (c && iter_incr == NULL)
6267 finish_expr_stmt (build_x_modify_expr (elocus,
6268 incr_var, NOP_EXPR,
6269 incr, tf_warning_or_error));
6270 incr = incr_var;
6271 iter_incr = build_x_modify_expr (elocus,
6272 iter, PLUS_EXPR, incr,
6273 tf_warning_or_error);
6275 finish_expr_stmt (build_x_modify_expr (elocus,
6276 last, NOP_EXPR, init,
6277 tf_warning_or_error));
6278 *pre_body = pop_stmt_list (*pre_body);
6280 cond = cp_build_binary_op (elocus,
6281 TREE_CODE (cond), decl, diff,
6282 tf_warning_or_error);
6283 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6284 elocus, incr, NULL_TREE);
6286 orig_body = *body;
6287 *body = push_stmt_list ();
6288 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6289 iter_init = build_x_modify_expr (elocus,
6290 iter, PLUS_EXPR, iter_init,
6291 tf_warning_or_error);
6292 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6293 finish_expr_stmt (iter_init);
6294 finish_expr_stmt (build_x_modify_expr (elocus,
6295 last, NOP_EXPR, decl,
6296 tf_warning_or_error));
6297 add_stmt (orig_body);
6298 *body = pop_stmt_list (*body);
6300 if (c)
6302 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6303 finish_expr_stmt (iter_incr);
6304 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6305 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6308 TREE_VEC_ELT (declv, i) = decl;
6309 TREE_VEC_ELT (initv, i) = init;
6310 TREE_VEC_ELT (condv, i) = cond;
6311 TREE_VEC_ELT (incrv, i) = incr;
6312 *lastp = last;
6314 return false;
6317 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6318 are directly for their associated operands in the statement. DECL
6319 and INIT are a combo; if DECL is NULL then INIT ought to be a
6320 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6321 optional statements that need to go before the loop into its
6322 sk_omp scope. */
6324 tree
6325 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6326 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6328 tree omp_for = NULL, orig_incr = NULL;
6329 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6330 tree last = NULL_TREE;
6331 location_t elocus;
6332 int i;
6334 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6335 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6336 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6337 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6339 decl = TREE_VEC_ELT (declv, i);
6340 init = TREE_VEC_ELT (initv, i);
6341 cond = TREE_VEC_ELT (condv, i);
6342 incr = TREE_VEC_ELT (incrv, i);
6343 elocus = locus;
6345 if (decl == NULL)
6347 if (init != NULL)
6348 switch (TREE_CODE (init))
6350 case MODIFY_EXPR:
6351 decl = TREE_OPERAND (init, 0);
6352 init = TREE_OPERAND (init, 1);
6353 break;
6354 case MODOP_EXPR:
6355 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6357 decl = TREE_OPERAND (init, 0);
6358 init = TREE_OPERAND (init, 2);
6360 break;
6361 default:
6362 break;
6365 if (decl == NULL)
6367 error_at (locus,
6368 "expected iteration declaration or initialization");
6369 return NULL;
6373 if (init && EXPR_HAS_LOCATION (init))
6374 elocus = EXPR_LOCATION (init);
6376 if (cond == NULL)
6378 error_at (elocus, "missing controlling predicate");
6379 return NULL;
6382 if (incr == NULL)
6384 error_at (elocus, "missing increment expression");
6385 return NULL;
6388 TREE_VEC_ELT (declv, i) = decl;
6389 TREE_VEC_ELT (initv, i) = init;
6392 if (dependent_omp_for_p (declv, initv, condv, incrv))
6394 tree stmt;
6396 stmt = make_node (code);
6398 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6400 /* This is really just a place-holder. We'll be decomposing this
6401 again and going through the cp_build_modify_expr path below when
6402 we instantiate the thing. */
6403 TREE_VEC_ELT (initv, i)
6404 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6405 TREE_VEC_ELT (initv, i));
6408 TREE_TYPE (stmt) = void_type_node;
6409 OMP_FOR_INIT (stmt) = initv;
6410 OMP_FOR_COND (stmt) = condv;
6411 OMP_FOR_INCR (stmt) = incrv;
6412 OMP_FOR_BODY (stmt) = body;
6413 OMP_FOR_PRE_BODY (stmt) = pre_body;
6414 OMP_FOR_CLAUSES (stmt) = clauses;
6416 SET_EXPR_LOCATION (stmt, locus);
6417 return add_stmt (stmt);
6420 if (processing_template_decl)
6421 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6423 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6425 decl = TREE_VEC_ELT (declv, i);
6426 init = TREE_VEC_ELT (initv, i);
6427 cond = TREE_VEC_ELT (condv, i);
6428 incr = TREE_VEC_ELT (incrv, i);
6429 if (orig_incr)
6430 TREE_VEC_ELT (orig_incr, i) = incr;
6431 elocus = locus;
6433 if (init && EXPR_HAS_LOCATION (init))
6434 elocus = EXPR_LOCATION (init);
6436 if (!DECL_P (decl))
6438 error_at (elocus, "expected iteration declaration or initialization");
6439 return NULL;
6442 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6444 if (orig_incr)
6445 TREE_VEC_ELT (orig_incr, i) = incr;
6446 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6447 TREE_CODE (TREE_OPERAND (incr, 1)),
6448 TREE_OPERAND (incr, 2),
6449 tf_warning_or_error);
6452 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6454 if (code == OMP_SIMD)
6456 error_at (elocus, "%<#pragma omp simd%> used with class "
6457 "iteration variable %qE", decl);
6458 return NULL;
6460 if (code == CILK_FOR && i == 0)
6461 orig_decl = decl;
6462 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6463 incrv, &body, &pre_body,
6464 clauses, &last))
6465 return NULL;
6466 continue;
6469 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6470 && !TYPE_PTR_P (TREE_TYPE (decl)))
6472 error_at (elocus, "invalid type for iteration variable %qE", decl);
6473 return NULL;
6476 if (!processing_template_decl)
6478 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6479 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6481 else
6482 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6483 if (cond
6484 && TREE_SIDE_EFFECTS (cond)
6485 && COMPARISON_CLASS_P (cond)
6486 && !processing_template_decl)
6488 tree t = TREE_OPERAND (cond, 0);
6489 if (TREE_SIDE_EFFECTS (t)
6490 && t != decl
6491 && (TREE_CODE (t) != NOP_EXPR
6492 || TREE_OPERAND (t, 0) != decl))
6493 TREE_OPERAND (cond, 0)
6494 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6496 t = TREE_OPERAND (cond, 1);
6497 if (TREE_SIDE_EFFECTS (t)
6498 && t != decl
6499 && (TREE_CODE (t) != NOP_EXPR
6500 || TREE_OPERAND (t, 0) != decl))
6501 TREE_OPERAND (cond, 1)
6502 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6504 if (decl == error_mark_node || init == error_mark_node)
6505 return NULL;
6507 TREE_VEC_ELT (declv, i) = decl;
6508 TREE_VEC_ELT (initv, i) = init;
6509 TREE_VEC_ELT (condv, i) = cond;
6510 TREE_VEC_ELT (incrv, i) = incr;
6511 i++;
6514 if (IS_EMPTY_STMT (pre_body))
6515 pre_body = NULL;
6517 if (code == CILK_FOR && !processing_template_decl)
6518 block = push_stmt_list ();
6520 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6521 body, pre_body);
6523 if (omp_for == NULL)
6525 if (block)
6526 pop_stmt_list (block);
6527 return NULL;
6530 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6532 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6533 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6535 if (TREE_CODE (incr) != MODIFY_EXPR)
6536 continue;
6538 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6539 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6540 && !processing_template_decl)
6542 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6543 if (TREE_SIDE_EFFECTS (t)
6544 && t != decl
6545 && (TREE_CODE (t) != NOP_EXPR
6546 || TREE_OPERAND (t, 0) != decl))
6547 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6548 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6550 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6551 if (TREE_SIDE_EFFECTS (t)
6552 && t != decl
6553 && (TREE_CODE (t) != NOP_EXPR
6554 || TREE_OPERAND (t, 0) != decl))
6555 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6556 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6559 if (orig_incr)
6560 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6562 OMP_FOR_CLAUSES (omp_for) = clauses;
6564 if (block)
6566 tree omp_par = make_node (OMP_PARALLEL);
6567 TREE_TYPE (omp_par) = void_type_node;
6568 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6569 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6570 TREE_SIDE_EFFECTS (bind) = 1;
6571 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6572 OMP_PARALLEL_BODY (omp_par) = bind;
6573 if (OMP_FOR_PRE_BODY (omp_for))
6575 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6576 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6578 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6579 decl = TREE_OPERAND (init, 0);
6580 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6581 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6582 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6583 clauses = OMP_FOR_CLAUSES (omp_for);
6584 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6585 for (pc = &clauses; *pc; )
6586 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6588 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6589 OMP_FOR_CLAUSES (omp_for) = *pc;
6590 *pc = OMP_CLAUSE_CHAIN (*pc);
6591 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6593 else
6595 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6596 pc = &OMP_CLAUSE_CHAIN (*pc);
6598 if (TREE_CODE (t) != INTEGER_CST)
6600 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6601 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6602 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6603 OMP_CLAUSE_CHAIN (c) = clauses;
6604 clauses = c;
6606 if (TREE_CODE (incr) == MODIFY_EXPR)
6608 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6609 if (TREE_CODE (t) != INTEGER_CST)
6611 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6612 = get_temp_regvar (TREE_TYPE (t), t);
6613 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6614 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6615 OMP_CLAUSE_CHAIN (c) = clauses;
6616 clauses = c;
6619 t = TREE_OPERAND (init, 1);
6620 if (TREE_CODE (t) != INTEGER_CST)
6622 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6623 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6624 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6625 OMP_CLAUSE_CHAIN (c) = clauses;
6626 clauses = c;
6628 if (orig_decl && orig_decl != decl)
6630 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6631 OMP_CLAUSE_DECL (c) = orig_decl;
6632 OMP_CLAUSE_CHAIN (c) = clauses;
6633 clauses = c;
6635 if (last)
6637 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6638 OMP_CLAUSE_DECL (c) = last;
6639 OMP_CLAUSE_CHAIN (c) = clauses;
6640 clauses = c;
6642 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6643 OMP_CLAUSE_DECL (c) = decl;
6644 OMP_CLAUSE_CHAIN (c) = clauses;
6645 clauses = c;
6646 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6647 OMP_CLAUSE_OPERAND (c, 0)
6648 = cilk_for_number_of_iterations (omp_for);
6649 OMP_CLAUSE_CHAIN (c) = clauses;
6650 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6651 add_stmt (omp_par);
6652 return omp_par;
6654 else if (code == CILK_FOR && processing_template_decl)
6656 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6657 if (orig_decl && orig_decl != decl)
6659 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6660 OMP_CLAUSE_DECL (c) = orig_decl;
6661 OMP_CLAUSE_CHAIN (c) = clauses;
6662 clauses = c;
6664 if (last)
6666 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6667 OMP_CLAUSE_DECL (c) = last;
6668 OMP_CLAUSE_CHAIN (c) = clauses;
6669 clauses = c;
6671 OMP_FOR_CLAUSES (omp_for) = clauses;
6673 return omp_for;
6676 void
6677 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6678 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6680 tree orig_lhs;
6681 tree orig_rhs;
6682 tree orig_v;
6683 tree orig_lhs1;
6684 tree orig_rhs1;
6685 bool dependent_p;
6686 tree stmt;
6688 orig_lhs = lhs;
6689 orig_rhs = rhs;
6690 orig_v = v;
6691 orig_lhs1 = lhs1;
6692 orig_rhs1 = rhs1;
6693 dependent_p = false;
6694 stmt = NULL_TREE;
6696 /* Even in a template, we can detect invalid uses of the atomic
6697 pragma if neither LHS nor RHS is type-dependent. */
6698 if (processing_template_decl)
6700 dependent_p = (type_dependent_expression_p (lhs)
6701 || (rhs && type_dependent_expression_p (rhs))
6702 || (v && type_dependent_expression_p (v))
6703 || (lhs1 && type_dependent_expression_p (lhs1))
6704 || (rhs1 && type_dependent_expression_p (rhs1)));
6705 if (!dependent_p)
6707 lhs = build_non_dependent_expr (lhs);
6708 if (rhs)
6709 rhs = build_non_dependent_expr (rhs);
6710 if (v)
6711 v = build_non_dependent_expr (v);
6712 if (lhs1)
6713 lhs1 = build_non_dependent_expr (lhs1);
6714 if (rhs1)
6715 rhs1 = build_non_dependent_expr (rhs1);
6718 if (!dependent_p)
6720 bool swapped = false;
6721 if (rhs1 && cp_tree_equal (lhs, rhs))
6723 tree tem = rhs;
6724 rhs = rhs1;
6725 rhs1 = tem;
6726 swapped = !commutative_tree_code (opcode);
6728 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6730 if (code == OMP_ATOMIC)
6731 error ("%<#pragma omp atomic update%> uses two different "
6732 "expressions for memory");
6733 else
6734 error ("%<#pragma omp atomic capture%> uses two different "
6735 "expressions for memory");
6736 return;
6738 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6740 if (code == OMP_ATOMIC)
6741 error ("%<#pragma omp atomic update%> uses two different "
6742 "expressions for memory");
6743 else
6744 error ("%<#pragma omp atomic capture%> uses two different "
6745 "expressions for memory");
6746 return;
6748 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6749 v, lhs1, rhs1, swapped, seq_cst);
6750 if (stmt == error_mark_node)
6751 return;
6753 if (processing_template_decl)
6755 if (code == OMP_ATOMIC_READ)
6757 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6758 OMP_ATOMIC_READ, orig_lhs);
6759 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6760 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6762 else
6764 if (opcode == NOP_EXPR)
6765 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6766 else
6767 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6768 if (orig_rhs1)
6769 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6770 COMPOUND_EXPR, orig_rhs1, stmt);
6771 if (code != OMP_ATOMIC)
6773 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6774 code, orig_lhs1, stmt);
6775 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6776 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6779 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6780 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6782 finish_expr_stmt (stmt);
6785 void
6786 finish_omp_barrier (void)
6788 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6789 vec<tree, va_gc> *vec = make_tree_vector ();
6790 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6791 release_tree_vector (vec);
6792 finish_expr_stmt (stmt);
6795 void
6796 finish_omp_flush (void)
6798 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6799 vec<tree, va_gc> *vec = make_tree_vector ();
6800 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6801 release_tree_vector (vec);
6802 finish_expr_stmt (stmt);
6805 void
6806 finish_omp_taskwait (void)
6808 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6809 vec<tree, va_gc> *vec = make_tree_vector ();
6810 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6811 release_tree_vector (vec);
6812 finish_expr_stmt (stmt);
6815 void
6816 finish_omp_taskyield (void)
6818 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6819 vec<tree, va_gc> *vec = make_tree_vector ();
6820 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6821 release_tree_vector (vec);
6822 finish_expr_stmt (stmt);
6825 void
6826 finish_omp_cancel (tree clauses)
6828 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6829 int mask = 0;
6830 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6831 mask = 1;
6832 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6833 mask = 2;
6834 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6835 mask = 4;
6836 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6837 mask = 8;
6838 else
6840 error ("%<#pragma omp cancel must specify one of "
6841 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6842 return;
6844 vec<tree, va_gc> *vec = make_tree_vector ();
6845 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6846 if (ifc != NULL_TREE)
6848 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6849 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6850 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6851 build_zero_cst (type));
6853 else
6854 ifc = boolean_true_node;
6855 vec->quick_push (build_int_cst (integer_type_node, mask));
6856 vec->quick_push (ifc);
6857 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6858 release_tree_vector (vec);
6859 finish_expr_stmt (stmt);
6862 void
6863 finish_omp_cancellation_point (tree clauses)
6865 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6866 int mask = 0;
6867 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6868 mask = 1;
6869 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6870 mask = 2;
6871 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6872 mask = 4;
6873 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6874 mask = 8;
6875 else
6877 error ("%<#pragma omp cancellation point must specify one of "
6878 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6879 return;
6881 vec<tree, va_gc> *vec
6882 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6883 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6884 release_tree_vector (vec);
6885 finish_expr_stmt (stmt);
6888 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6889 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6890 should create an extra compound stmt. */
6892 tree
6893 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6895 tree r;
6897 if (pcompound)
6898 *pcompound = begin_compound_stmt (0);
6900 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6902 /* Only add the statement to the function if support enabled. */
6903 if (flag_tm)
6904 add_stmt (r);
6905 else
6906 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6907 ? G_("%<__transaction_relaxed%> without "
6908 "transactional memory support enabled")
6909 : G_("%<__transaction_atomic%> without "
6910 "transactional memory support enabled")));
6912 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6913 TREE_SIDE_EFFECTS (r) = 1;
6914 return r;
6917 /* End a __transaction_atomic or __transaction_relaxed statement.
6918 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6919 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6920 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6922 void
6923 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6925 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6926 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6927 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6928 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6930 /* noexcept specifications are not allowed for function transactions. */
6931 gcc_assert (!(noex && compound_stmt));
6932 if (noex)
6934 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6935 noex);
6936 /* This may not be true when the STATEMENT_LIST is empty. */
6937 if (EXPR_P (body))
6938 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6939 TREE_SIDE_EFFECTS (body) = 1;
6940 TRANSACTION_EXPR_BODY (stmt) = body;
6943 if (compound_stmt)
6944 finish_compound_stmt (compound_stmt);
6947 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6948 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6949 condition. */
6951 tree
6952 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6954 tree ret;
6955 if (noex)
6957 expr = build_must_not_throw_expr (expr, noex);
6958 if (EXPR_P (expr))
6959 SET_EXPR_LOCATION (expr, loc);
6960 TREE_SIDE_EFFECTS (expr) = 1;
6962 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6963 if (flags & TM_STMT_ATTR_RELAXED)
6964 TRANSACTION_EXPR_RELAXED (ret) = 1;
6965 TREE_SIDE_EFFECTS (ret) = 1;
6966 SET_EXPR_LOCATION (ret, loc);
6967 return ret;
6970 void
6971 init_cp_semantics (void)
6975 /* Build a STATIC_ASSERT for a static assertion with the condition
6976 CONDITION and the message text MESSAGE. LOCATION is the location
6977 of the static assertion in the source code. When MEMBER_P, this
6978 static assertion is a member of a class. */
6979 void
6980 finish_static_assert (tree condition, tree message, location_t location,
6981 bool member_p)
6983 if (message == NULL_TREE
6984 || message == error_mark_node
6985 || condition == NULL_TREE
6986 || condition == error_mark_node)
6987 return;
6989 if (check_for_bare_parameter_packs (condition))
6990 condition = error_mark_node;
6992 if (type_dependent_expression_p (condition)
6993 || value_dependent_expression_p (condition))
6995 /* We're in a template; build a STATIC_ASSERT and put it in
6996 the right place. */
6997 tree assertion;
6999 assertion = make_node (STATIC_ASSERT);
7000 STATIC_ASSERT_CONDITION (assertion) = condition;
7001 STATIC_ASSERT_MESSAGE (assertion) = message;
7002 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7004 if (member_p)
7005 maybe_add_class_template_decl_list (current_class_type,
7006 assertion,
7007 /*friend_p=*/0);
7008 else
7009 add_stmt (assertion);
7011 return;
7014 /* Fold the expression and convert it to a boolean value. */
7015 condition = fold_non_dependent_expr (condition);
7016 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7017 condition = maybe_constant_value (condition);
7019 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7020 /* Do nothing; the condition is satisfied. */
7022 else
7024 location_t saved_loc = input_location;
7026 input_location = location;
7027 if (TREE_CODE (condition) == INTEGER_CST
7028 && integer_zerop (condition))
7029 /* Report the error. */
7030 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
7031 else if (condition && condition != error_mark_node)
7033 error ("non-constant condition for static assertion");
7034 if (require_potential_rvalue_constant_expression (condition))
7035 cxx_constant_value (condition);
7037 input_location = saved_loc;
7041 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7042 suitable for use as a type-specifier.
7044 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7045 id-expression or a class member access, FALSE when it was parsed as
7046 a full expression. */
7048 tree
7049 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7050 tsubst_flags_t complain)
7052 tree type = NULL_TREE;
7054 if (!expr || error_operand_p (expr))
7055 return error_mark_node;
7057 if (TYPE_P (expr)
7058 || TREE_CODE (expr) == TYPE_DECL
7059 || (TREE_CODE (expr) == BIT_NOT_EXPR
7060 && TYPE_P (TREE_OPERAND (expr, 0))))
7062 if (complain & tf_error)
7063 error ("argument to decltype must be an expression");
7064 return error_mark_node;
7067 /* Depending on the resolution of DR 1172, we may later need to distinguish
7068 instantiation-dependent but not type-dependent expressions so that, say,
7069 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7070 if (instantiation_dependent_expression_p (expr))
7072 type = cxx_make_type (DECLTYPE_TYPE);
7073 DECLTYPE_TYPE_EXPR (type) = expr;
7074 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7075 = id_expression_or_member_access_p;
7076 SET_TYPE_STRUCTURAL_EQUALITY (type);
7078 return type;
7081 /* The type denoted by decltype(e) is defined as follows: */
7083 expr = resolve_nondeduced_context (expr);
7085 if (invalid_nonstatic_memfn_p (expr, complain))
7086 return error_mark_node;
7088 if (type_unknown_p (expr))
7090 if (complain & tf_error)
7091 error ("decltype cannot resolve address of overloaded function");
7092 return error_mark_node;
7095 /* To get the size of a static data member declared as an array of
7096 unknown bound, we need to instantiate it. */
7097 if (VAR_P (expr)
7098 && VAR_HAD_UNKNOWN_BOUND (expr)
7099 && DECL_TEMPLATE_INSTANTIATION (expr))
7100 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7102 if (id_expression_or_member_access_p)
7104 /* If e is an id-expression or a class member access (5.2.5
7105 [expr.ref]), decltype(e) is defined as the type of the entity
7106 named by e. If there is no such entity, or e names a set of
7107 overloaded functions, the program is ill-formed. */
7108 if (identifier_p (expr))
7109 expr = lookup_name (expr);
7111 if (INDIRECT_REF_P (expr))
7112 /* This can happen when the expression is, e.g., "a.b". Just
7113 look at the underlying operand. */
7114 expr = TREE_OPERAND (expr, 0);
7116 if (TREE_CODE (expr) == OFFSET_REF
7117 || TREE_CODE (expr) == MEMBER_REF
7118 || TREE_CODE (expr) == SCOPE_REF)
7119 /* We're only interested in the field itself. If it is a
7120 BASELINK, we will need to see through it in the next
7121 step. */
7122 expr = TREE_OPERAND (expr, 1);
7124 if (BASELINK_P (expr))
7125 /* See through BASELINK nodes to the underlying function. */
7126 expr = BASELINK_FUNCTIONS (expr);
7128 switch (TREE_CODE (expr))
7130 case FIELD_DECL:
7131 if (DECL_BIT_FIELD_TYPE (expr))
7133 type = DECL_BIT_FIELD_TYPE (expr);
7134 break;
7136 /* Fall through for fields that aren't bitfields. */
7138 case FUNCTION_DECL:
7139 case VAR_DECL:
7140 case CONST_DECL:
7141 case PARM_DECL:
7142 case RESULT_DECL:
7143 case TEMPLATE_PARM_INDEX:
7144 expr = mark_type_use (expr);
7145 type = TREE_TYPE (expr);
7146 break;
7148 case ERROR_MARK:
7149 type = error_mark_node;
7150 break;
7152 case COMPONENT_REF:
7153 case COMPOUND_EXPR:
7154 mark_type_use (expr);
7155 type = is_bitfield_expr_with_lowered_type (expr);
7156 if (!type)
7157 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7158 break;
7160 case BIT_FIELD_REF:
7161 gcc_unreachable ();
7163 case INTEGER_CST:
7164 case PTRMEM_CST:
7165 /* We can get here when the id-expression refers to an
7166 enumerator or non-type template parameter. */
7167 type = TREE_TYPE (expr);
7168 break;
7170 default:
7171 /* Handle instantiated template non-type arguments. */
7172 type = TREE_TYPE (expr);
7173 break;
7176 else
7178 /* Within a lambda-expression:
7180 Every occurrence of decltype((x)) where x is a possibly
7181 parenthesized id-expression that names an entity of
7182 automatic storage duration is treated as if x were
7183 transformed into an access to a corresponding data member
7184 of the closure type that would have been declared if x
7185 were a use of the denoted entity. */
7186 if (outer_automatic_var_p (expr)
7187 && current_function_decl
7188 && LAMBDA_FUNCTION_P (current_function_decl))
7189 type = capture_decltype (expr);
7190 else if (error_operand_p (expr))
7191 type = error_mark_node;
7192 else if (expr == current_class_ptr)
7193 /* If the expression is just "this", we want the
7194 cv-unqualified pointer for the "this" type. */
7195 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7196 else
7198 /* Otherwise, where T is the type of e, if e is an lvalue,
7199 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7200 cp_lvalue_kind clk = lvalue_kind (expr);
7201 type = unlowered_expr_type (expr);
7202 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7204 /* For vector types, pick a non-opaque variant. */
7205 if (TREE_CODE (type) == VECTOR_TYPE)
7206 type = strip_typedefs (type);
7208 if (clk != clk_none && !(clk & clk_class))
7209 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7213 if (cxx_dialect >= cxx14 && array_of_runtime_bound_p (type)
7214 && (flag_iso || warn_vla > 0))
7216 if (complain & tf_warning_or_error)
7217 pedwarn (input_location, OPT_Wvla,
7218 "taking decltype of array of runtime bound");
7219 else
7220 return error_mark_node;
7223 return type;
7226 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7227 __has_nothrow_copy, depending on assign_p. */
7229 static bool
7230 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7232 tree fns;
7234 if (assign_p)
7236 int ix;
7237 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7238 if (ix < 0)
7239 return false;
7240 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7242 else if (TYPE_HAS_COPY_CTOR (type))
7244 /* If construction of the copy constructor was postponed, create
7245 it now. */
7246 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7247 lazily_declare_fn (sfk_copy_constructor, type);
7248 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7249 lazily_declare_fn (sfk_move_constructor, type);
7250 fns = CLASSTYPE_CONSTRUCTORS (type);
7252 else
7253 return false;
7255 for (; fns; fns = OVL_NEXT (fns))
7257 tree fn = OVL_CURRENT (fns);
7259 if (assign_p)
7261 if (copy_fn_p (fn) == 0)
7262 continue;
7264 else if (copy_fn_p (fn) <= 0)
7265 continue;
7267 maybe_instantiate_noexcept (fn);
7268 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7269 return false;
7272 return true;
7275 /* Actually evaluates the trait. */
7277 static bool
7278 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7280 enum tree_code type_code1;
7281 tree t;
7283 type_code1 = TREE_CODE (type1);
7285 switch (kind)
7287 case CPTK_HAS_NOTHROW_ASSIGN:
7288 type1 = strip_array_types (type1);
7289 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7290 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7291 || (CLASS_TYPE_P (type1)
7292 && classtype_has_nothrow_assign_or_copy_p (type1,
7293 true))));
7295 case CPTK_HAS_TRIVIAL_ASSIGN:
7296 /* ??? The standard seems to be missing the "or array of such a class
7297 type" wording for this trait. */
7298 type1 = strip_array_types (type1);
7299 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7300 && (trivial_type_p (type1)
7301 || (CLASS_TYPE_P (type1)
7302 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7304 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7305 type1 = strip_array_types (type1);
7306 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7307 || (CLASS_TYPE_P (type1)
7308 && (t = locate_ctor (type1))
7309 && (maybe_instantiate_noexcept (t),
7310 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7312 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7313 type1 = strip_array_types (type1);
7314 return (trivial_type_p (type1)
7315 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7317 case CPTK_HAS_NOTHROW_COPY:
7318 type1 = strip_array_types (type1);
7319 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7320 || (CLASS_TYPE_P (type1)
7321 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7323 case CPTK_HAS_TRIVIAL_COPY:
7324 /* ??? The standard seems to be missing the "or array of such a class
7325 type" wording for this trait. */
7326 type1 = strip_array_types (type1);
7327 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7328 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7330 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7331 type1 = strip_array_types (type1);
7332 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7333 || (CLASS_TYPE_P (type1)
7334 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7336 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7337 return type_has_virtual_destructor (type1);
7339 case CPTK_IS_ABSTRACT:
7340 return (ABSTRACT_CLASS_TYPE_P (type1));
7342 case CPTK_IS_BASE_OF:
7343 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7344 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7345 || DERIVED_FROM_P (type1, type2)));
7347 case CPTK_IS_CLASS:
7348 return (NON_UNION_CLASS_TYPE_P (type1));
7350 case CPTK_IS_CONVERTIBLE_TO:
7351 /* TODO */
7352 return false;
7354 case CPTK_IS_EMPTY:
7355 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7357 case CPTK_IS_ENUM:
7358 return (type_code1 == ENUMERAL_TYPE);
7360 case CPTK_IS_FINAL:
7361 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7363 case CPTK_IS_LITERAL_TYPE:
7364 return (literal_type_p (type1));
7366 case CPTK_IS_POD:
7367 return (pod_type_p (type1));
7369 case CPTK_IS_POLYMORPHIC:
7370 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7372 case CPTK_IS_STD_LAYOUT:
7373 return (std_layout_type_p (type1));
7375 case CPTK_IS_TRIVIAL:
7376 return (trivial_type_p (type1));
7378 case CPTK_IS_UNION:
7379 return (type_code1 == UNION_TYPE);
7381 default:
7382 gcc_unreachable ();
7383 return false;
7387 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7388 void, or a complete type, returns it, otherwise NULL_TREE. */
7390 static tree
7391 check_trait_type (tree type)
7393 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7394 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7395 return type;
7397 if (VOID_TYPE_P (type))
7398 return type;
7400 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7403 /* Process a trait expression. */
7405 tree
7406 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7408 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7409 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7410 || kind == CPTK_HAS_NOTHROW_COPY
7411 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7412 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7413 || kind == CPTK_HAS_TRIVIAL_COPY
7414 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7415 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7416 || kind == CPTK_IS_ABSTRACT
7417 || kind == CPTK_IS_BASE_OF
7418 || kind == CPTK_IS_CLASS
7419 || kind == CPTK_IS_CONVERTIBLE_TO
7420 || kind == CPTK_IS_EMPTY
7421 || kind == CPTK_IS_ENUM
7422 || kind == CPTK_IS_FINAL
7423 || kind == CPTK_IS_LITERAL_TYPE
7424 || kind == CPTK_IS_POD
7425 || kind == CPTK_IS_POLYMORPHIC
7426 || kind == CPTK_IS_STD_LAYOUT
7427 || kind == CPTK_IS_TRIVIAL
7428 || kind == CPTK_IS_UNION);
7430 if (kind == CPTK_IS_CONVERTIBLE_TO)
7432 sorry ("__is_convertible_to");
7433 return error_mark_node;
7436 if (type1 == error_mark_node
7437 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
7438 && type2 == error_mark_node))
7439 return error_mark_node;
7441 if (processing_template_decl)
7443 tree trait_expr = make_node (TRAIT_EXPR);
7444 TREE_TYPE (trait_expr) = boolean_type_node;
7445 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7446 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7447 TRAIT_EXPR_KIND (trait_expr) = kind;
7448 return trait_expr;
7451 switch (kind)
7453 case CPTK_HAS_NOTHROW_ASSIGN:
7454 case CPTK_HAS_TRIVIAL_ASSIGN:
7455 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7456 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7457 case CPTK_HAS_NOTHROW_COPY:
7458 case CPTK_HAS_TRIVIAL_COPY:
7459 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7460 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7461 case CPTK_IS_ABSTRACT:
7462 case CPTK_IS_EMPTY:
7463 case CPTK_IS_FINAL:
7464 case CPTK_IS_LITERAL_TYPE:
7465 case CPTK_IS_POD:
7466 case CPTK_IS_POLYMORPHIC:
7467 case CPTK_IS_STD_LAYOUT:
7468 case CPTK_IS_TRIVIAL:
7469 if (!check_trait_type (type1))
7470 return error_mark_node;
7471 break;
7473 case CPTK_IS_BASE_OF:
7474 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7475 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7476 && !complete_type_or_else (type2, NULL_TREE))
7477 /* We already issued an error. */
7478 return error_mark_node;
7479 break;
7481 case CPTK_IS_CLASS:
7482 case CPTK_IS_ENUM:
7483 case CPTK_IS_UNION:
7484 break;
7486 case CPTK_IS_CONVERTIBLE_TO:
7487 default:
7488 gcc_unreachable ();
7491 return (trait_expr_value (kind, type1, type2)
7492 ? boolean_true_node : boolean_false_node);
7495 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7496 which is ignored for C++. */
7498 void
7499 set_float_const_decimal64 (void)
7503 void
7504 clear_float_const_decimal64 (void)
7508 bool
7509 float_const_decimal64_p (void)
7511 return 0;
7515 /* Return true if T is a literal type. */
7517 bool
7518 literal_type_p (tree t)
7520 if (SCALAR_TYPE_P (t)
7521 || TREE_CODE (t) == VECTOR_TYPE
7522 || TREE_CODE (t) == REFERENCE_TYPE)
7523 return true;
7524 if (CLASS_TYPE_P (t))
7526 t = complete_type (t);
7527 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7528 return CLASSTYPE_LITERAL_P (t);
7530 if (TREE_CODE (t) == ARRAY_TYPE)
7531 return literal_type_p (strip_array_types (t));
7532 return false;
7535 /* If DECL is a variable declared `constexpr', require its type
7536 be literal. Return the DECL if OK, otherwise NULL. */
7538 tree
7539 ensure_literal_type_for_constexpr_object (tree decl)
7541 tree type = TREE_TYPE (decl);
7542 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7543 && !processing_template_decl)
7545 tree stype = strip_array_types (type);
7546 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
7547 /* Don't complain here, we'll complain about incompleteness
7548 when we try to initialize the variable. */;
7549 else if (!literal_type_p (type))
7551 error ("the type %qT of constexpr variable %qD is not literal",
7552 type, decl);
7553 explain_non_literal_class (type);
7554 return NULL;
7557 return decl;
7560 /* Representation of entries in the constexpr function definition table. */
7562 typedef struct GTY(()) constexpr_fundef {
7563 tree decl;
7564 tree body;
7565 } constexpr_fundef;
7567 /* This table holds all constexpr function definitions seen in
7568 the current translation unit. */
7570 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7572 /* Utility function used for managing the constexpr function table.
7573 Return true if the entries pointed to by P and Q are for the
7574 same constexpr function. */
7576 static inline int
7577 constexpr_fundef_equal (const void *p, const void *q)
7579 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7580 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7581 return lhs->decl == rhs->decl;
7584 /* Utility function used for managing the constexpr function table.
7585 Return a hash value for the entry pointed to by Q. */
7587 static inline hashval_t
7588 constexpr_fundef_hash (const void *p)
7590 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7591 return DECL_UID (fundef->decl);
7594 /* Return a previously saved definition of function FUN. */
7596 static constexpr_fundef *
7597 retrieve_constexpr_fundef (tree fun)
7599 constexpr_fundef fundef = { NULL, NULL };
7600 if (constexpr_fundef_table == NULL)
7601 return NULL;
7603 fundef.decl = fun;
7604 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7607 /* Check whether the parameter and return types of FUN are valid for a
7608 constexpr function, and complain if COMPLAIN. */
7610 static bool
7611 is_valid_constexpr_fn (tree fun, bool complain)
7613 bool ret = true;
7615 if (DECL_INHERITED_CTOR_BASE (fun)
7616 && TREE_CODE (fun) == TEMPLATE_DECL)
7618 ret = false;
7619 if (complain)
7620 error ("inherited constructor %qD is not constexpr",
7621 get_inherited_ctor (fun));
7623 else
7625 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
7626 parm != NULL_TREE; parm = TREE_CHAIN (parm))
7627 if (!literal_type_p (TREE_TYPE (parm)))
7629 ret = false;
7630 if (complain)
7632 error ("invalid type for parameter %d of constexpr "
7633 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7634 explain_non_literal_class (TREE_TYPE (parm));
7639 if (!DECL_CONSTRUCTOR_P (fun))
7641 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7642 if (!literal_type_p (rettype))
7644 ret = false;
7645 if (complain)
7647 error ("invalid return type %qT of constexpr function %q+D",
7648 rettype, fun);
7649 explain_non_literal_class (rettype);
7653 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7654 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7656 ret = false;
7657 if (complain)
7659 error ("enclosing class of constexpr non-static member "
7660 "function %q+#D is not a literal type", fun);
7661 explain_non_literal_class (DECL_CONTEXT (fun));
7665 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7667 ret = false;
7668 if (complain)
7669 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7672 return ret;
7675 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7676 for a member of an anonymous aggregate, INIT is the initializer for that
7677 member, and VEC_OUTER is the vector of constructor elements for the class
7678 whose constructor we are processing. Add the initializer to the vector
7679 and return true to indicate success. */
7681 static bool
7682 build_anon_member_initialization (tree member, tree init,
7683 vec<constructor_elt, va_gc> **vec_outer)
7685 /* MEMBER presents the relevant fields from the inside out, but we need
7686 to build up the initializer from the outside in so that we can reuse
7687 previously built CONSTRUCTORs if this is, say, the second field in an
7688 anonymous struct. So we use a vec as a stack. */
7689 auto_vec<tree, 2> fields;
7692 fields.safe_push (TREE_OPERAND (member, 1));
7693 member = TREE_OPERAND (member, 0);
7695 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
7696 && TREE_CODE (member) == COMPONENT_REF);
7698 /* VEC has the constructor elements vector for the context of FIELD.
7699 If FIELD is an anonymous aggregate, we will push inside it. */
7700 vec<constructor_elt, va_gc> **vec = vec_outer;
7701 tree field;
7702 while (field = fields.pop(),
7703 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7705 tree ctor;
7706 /* If there is already an outer constructor entry for the anonymous
7707 aggregate FIELD, use it; otherwise, insert one. */
7708 if (vec_safe_is_empty (*vec)
7709 || (*vec)->last().index != field)
7711 ctor = build_constructor (TREE_TYPE (field), NULL);
7712 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7714 else
7715 ctor = (*vec)->last().value;
7716 vec = &CONSTRUCTOR_ELTS (ctor);
7719 /* Now we're at the innermost field, the one that isn't an anonymous
7720 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7721 gcc_assert (fields.is_empty());
7722 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7724 return true;
7727 /* Subroutine of build_constexpr_constructor_member_initializers.
7728 The expression tree T represents a data member initialization
7729 in a (constexpr) constructor definition. Build a pairing of
7730 the data member with its initializer, and prepend that pair
7731 to the existing initialization pair INITS. */
7733 static bool
7734 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7736 tree member, init;
7737 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7738 t = TREE_OPERAND (t, 0);
7739 if (TREE_CODE (t) == EXPR_STMT)
7740 t = TREE_OPERAND (t, 0);
7741 if (t == error_mark_node)
7742 return false;
7743 if (TREE_CODE (t) == STATEMENT_LIST)
7745 tree_stmt_iterator i;
7746 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7748 if (! build_data_member_initialization (tsi_stmt (i), vec))
7749 return false;
7751 return true;
7753 if (TREE_CODE (t) == CLEANUP_STMT)
7755 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7756 but we can in a constexpr constructor for a non-literal class. Just
7757 ignore it; either all the initialization will be constant, in which
7758 case the cleanup can't run, or it can't be constexpr.
7759 Still recurse into CLEANUP_BODY. */
7760 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7762 if (TREE_CODE (t) == CONVERT_EXPR)
7763 t = TREE_OPERAND (t, 0);
7764 if (TREE_CODE (t) == INIT_EXPR
7765 || TREE_CODE (t) == MODIFY_EXPR)
7767 member = TREE_OPERAND (t, 0);
7768 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7770 else if (TREE_CODE (t) == CALL_EXPR)
7772 member = CALL_EXPR_ARG (t, 0);
7773 /* We don't use build_cplus_new here because it complains about
7774 abstract bases. Leaving the call unwrapped means that it has the
7775 wrong type, but cxx_eval_constant_expression doesn't care. */
7776 init = break_out_target_exprs (t);
7778 else if (TREE_CODE (t) == DECL_EXPR)
7779 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7780 return true;
7781 else
7782 gcc_unreachable ();
7783 if (INDIRECT_REF_P (member))
7784 member = TREE_OPERAND (member, 0);
7785 if (TREE_CODE (member) == NOP_EXPR)
7787 tree op = member;
7788 STRIP_NOPS (op);
7789 if (TREE_CODE (op) == ADDR_EXPR)
7791 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7792 (TREE_TYPE (TREE_TYPE (op)),
7793 TREE_TYPE (TREE_TYPE (member))));
7794 /* Initializing a cv-qualified member; we need to look through
7795 the const_cast. */
7796 member = op;
7798 else if (op == current_class_ptr
7799 && (same_type_ignoring_top_level_qualifiers_p
7800 (TREE_TYPE (TREE_TYPE (member)),
7801 current_class_type)))
7802 /* Delegating constructor. */
7803 member = op;
7804 else
7806 /* This is an initializer for an empty base; keep it for now so
7807 we can check it in cxx_eval_bare_aggregate. */
7808 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7811 if (TREE_CODE (member) == ADDR_EXPR)
7812 member = TREE_OPERAND (member, 0);
7813 if (TREE_CODE (member) == COMPONENT_REF)
7815 tree aggr = TREE_OPERAND (member, 0);
7816 if (TREE_CODE (aggr) != COMPONENT_REF)
7817 /* Normal member initialization. */
7818 member = TREE_OPERAND (member, 1);
7819 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7820 /* Initializing a member of an anonymous union. */
7821 return build_anon_member_initialization (member, init, vec);
7822 else
7823 /* We're initializing a vtable pointer in a base. Leave it as
7824 COMPONENT_REF so we remember the path to get to the vfield. */
7825 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7828 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7829 return true;
7832 /* Make sure that there are no statements after LAST in the constructor
7833 body represented by LIST. */
7835 bool
7836 check_constexpr_ctor_body (tree last, tree list)
7838 bool ok = true;
7839 if (TREE_CODE (list) == STATEMENT_LIST)
7841 tree_stmt_iterator i = tsi_last (list);
7842 for (; !tsi_end_p (i); tsi_prev (&i))
7844 tree t = tsi_stmt (i);
7845 if (t == last)
7846 break;
7847 if (TREE_CODE (t) == BIND_EXPR)
7849 if (BIND_EXPR_VARS (t))
7851 ok = false;
7852 break;
7854 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7855 return false;
7856 else
7857 continue;
7859 /* We currently allow typedefs and static_assert.
7860 FIXME allow them in the standard, too. */
7861 if (TREE_CODE (t) != STATIC_ASSERT)
7863 ok = false;
7864 break;
7868 else if (list != last
7869 && TREE_CODE (list) != STATIC_ASSERT)
7870 ok = false;
7871 if (!ok)
7873 error ("constexpr constructor does not have empty body");
7874 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7876 return ok;
7879 /* V is a vector of constructor elements built up for the base and member
7880 initializers of a constructor for TYPE. They need to be in increasing
7881 offset order, which they might not be yet if TYPE has a primary base
7882 which is not first in the base-clause or a vptr and at least one base
7883 all of which are non-primary. */
7885 static vec<constructor_elt, va_gc> *
7886 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7888 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7889 tree field_type;
7890 unsigned i;
7891 constructor_elt *ce;
7893 if (pri)
7894 field_type = BINFO_TYPE (pri);
7895 else if (TYPE_CONTAINS_VPTR_P (type))
7896 field_type = vtbl_ptr_type_node;
7897 else
7898 return v;
7900 /* Find the element for the primary base or vptr and move it to the
7901 beginning of the vec. */
7902 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7903 if (TREE_TYPE (ce->index) == field_type)
7904 break;
7906 if (i > 0 && i < vec_safe_length (v))
7908 vec<constructor_elt, va_gc> &vref = *v;
7909 constructor_elt elt = vref[i];
7910 for (; i > 0; --i)
7911 vref[i] = vref[i-1];
7912 vref[0] = elt;
7915 return v;
7918 /* Build compile-time evalable representations of member-initializer list
7919 for a constexpr constructor. */
7921 static tree
7922 build_constexpr_constructor_member_initializers (tree type, tree body)
7924 vec<constructor_elt, va_gc> *vec = NULL;
7925 bool ok = true;
7926 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7927 || TREE_CODE (body) == EH_SPEC_BLOCK)
7928 body = TREE_OPERAND (body, 0);
7929 if (TREE_CODE (body) == STATEMENT_LIST)
7930 body = STATEMENT_LIST_HEAD (body)->stmt;
7931 body = BIND_EXPR_BODY (body);
7932 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7934 body = TREE_OPERAND (body, 0);
7935 if (TREE_CODE (body) == EXPR_STMT)
7936 body = TREE_OPERAND (body, 0);
7937 if (TREE_CODE (body) == INIT_EXPR
7938 && (same_type_ignoring_top_level_qualifiers_p
7939 (TREE_TYPE (TREE_OPERAND (body, 0)),
7940 current_class_type)))
7942 /* Trivial copy. */
7943 return TREE_OPERAND (body, 1);
7945 ok = build_data_member_initialization (body, &vec);
7947 else if (TREE_CODE (body) == STATEMENT_LIST)
7949 tree_stmt_iterator i;
7950 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7952 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7953 if (!ok)
7954 break;
7957 else if (TREE_CODE (body) == TRY_BLOCK)
7959 error ("body of %<constexpr%> constructor cannot be "
7960 "a function-try-block");
7961 return error_mark_node;
7963 else if (EXPR_P (body))
7964 ok = build_data_member_initialization (body, &vec);
7965 else
7966 gcc_assert (errorcount > 0);
7967 if (ok)
7969 if (vec_safe_length (vec) > 0)
7971 /* In a delegating constructor, return the target. */
7972 constructor_elt *ce = &(*vec)[0];
7973 if (ce->index == current_class_ptr)
7975 body = ce->value;
7976 vec_free (vec);
7977 return body;
7980 vec = sort_constexpr_mem_initializers (type, vec);
7981 return build_constructor (type, vec);
7983 else
7984 return error_mark_node;
7987 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7988 declared to be constexpr, or a sub-statement thereof. Returns the
7989 return value if suitable, error_mark_node for a statement not allowed in
7990 a constexpr function, or NULL_TREE if no return value was found. */
7992 static tree
7993 constexpr_fn_retval (tree body)
7995 switch (TREE_CODE (body))
7997 case STATEMENT_LIST:
7999 tree_stmt_iterator i;
8000 tree expr = NULL_TREE;
8001 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
8003 tree s = constexpr_fn_retval (tsi_stmt (i));
8004 if (s == error_mark_node)
8005 return error_mark_node;
8006 else if (s == NULL_TREE)
8007 /* Keep iterating. */;
8008 else if (expr)
8009 /* Multiple return statements. */
8010 return error_mark_node;
8011 else
8012 expr = s;
8014 return expr;
8017 case RETURN_EXPR:
8018 return break_out_target_exprs (TREE_OPERAND (body, 0));
8020 case DECL_EXPR:
8021 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
8022 return NULL_TREE;
8023 return error_mark_node;
8025 case CLEANUP_POINT_EXPR:
8026 return constexpr_fn_retval (TREE_OPERAND (body, 0));
8028 case USING_STMT:
8029 return NULL_TREE;
8031 default:
8032 return error_mark_node;
8036 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
8037 FUN; do the necessary transformations to turn it into a single expression
8038 that we can store in the hash table. */
8040 static tree
8041 massage_constexpr_body (tree fun, tree body)
8043 if (DECL_CONSTRUCTOR_P (fun))
8044 body = build_constexpr_constructor_member_initializers
8045 (DECL_CONTEXT (fun), body);
8046 else
8048 if (TREE_CODE (body) == EH_SPEC_BLOCK)
8049 body = EH_SPEC_STMTS (body);
8050 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
8051 body = TREE_OPERAND (body, 0);
8052 if (TREE_CODE (body) == BIND_EXPR)
8053 body = BIND_EXPR_BODY (body);
8054 body = constexpr_fn_retval (body);
8056 return body;
8059 /* FUN is a constexpr constructor with massaged body BODY. Return true
8060 if some bases/fields are uninitialized, and complain if COMPLAIN. */
8062 static bool
8063 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
8065 bool bad;
8066 tree field;
8067 unsigned i, nelts;
8068 tree ctype;
8070 if (TREE_CODE (body) != CONSTRUCTOR)
8071 return false;
8073 nelts = CONSTRUCTOR_NELTS (body);
8074 ctype = DECL_CONTEXT (fun);
8075 field = TYPE_FIELDS (ctype);
8077 if (TREE_CODE (ctype) == UNION_TYPE)
8079 if (nelts == 0 && next_initializable_field (field))
8081 if (complain)
8082 error ("%<constexpr%> constructor for union %qT must "
8083 "initialize exactly one non-static data member", ctype);
8084 return true;
8086 return false;
8089 bad = false;
8090 for (i = 0; i <= nelts; ++i)
8092 tree index;
8093 if (i == nelts)
8094 index = NULL_TREE;
8095 else
8097 index = CONSTRUCTOR_ELT (body, i)->index;
8098 /* Skip base and vtable inits. */
8099 if (TREE_CODE (index) != FIELD_DECL
8100 || DECL_ARTIFICIAL (index))
8101 continue;
8103 for (; field != index; field = DECL_CHAIN (field))
8105 tree ftype;
8106 if (TREE_CODE (field) != FIELD_DECL
8107 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
8108 || DECL_ARTIFICIAL (field))
8109 continue;
8110 ftype = strip_array_types (TREE_TYPE (field));
8111 if (type_has_constexpr_default_constructor (ftype))
8113 /* It's OK to skip a member with a trivial constexpr ctor.
8114 A constexpr ctor that isn't trivial should have been
8115 added in by now. */
8116 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
8117 || errorcount != 0);
8118 continue;
8120 if (!complain)
8121 return true;
8122 error ("uninitialized member %qD in %<constexpr%> constructor",
8123 field);
8124 bad = true;
8126 if (field == NULL_TREE)
8127 break;
8128 field = DECL_CHAIN (field);
8131 return bad;
8134 /* We are processing the definition of the constexpr function FUN.
8135 Check that its BODY fulfills the propriate requirements and
8136 enter it in the constexpr function definition table.
8137 For constructor BODY is actually the TREE_LIST of the
8138 member-initializer list. */
8140 tree
8141 register_constexpr_fundef (tree fun, tree body)
8143 constexpr_fundef entry;
8144 constexpr_fundef **slot;
8146 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
8147 return NULL;
8149 body = massage_constexpr_body (fun, body);
8150 if (body == NULL_TREE || body == error_mark_node)
8152 if (!DECL_CONSTRUCTOR_P (fun))
8153 error ("body of constexpr function %qD not a return-statement", fun);
8154 return NULL;
8157 if (!potential_rvalue_constant_expression (body))
8159 if (!DECL_GENERATED_P (fun))
8160 require_potential_rvalue_constant_expression (body);
8161 return NULL;
8164 if (DECL_CONSTRUCTOR_P (fun)
8165 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
8166 return NULL;
8168 /* Create the constexpr function table if necessary. */
8169 if (constexpr_fundef_table == NULL)
8170 constexpr_fundef_table = htab_create_ggc (101,
8171 constexpr_fundef_hash,
8172 constexpr_fundef_equal,
8173 ggc_free);
8174 entry.decl = fun;
8175 entry.body = body;
8176 slot = (constexpr_fundef **)
8177 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
8179 gcc_assert (*slot == NULL);
8180 *slot = ggc_alloc<constexpr_fundef> ();
8181 **slot = entry;
8183 return fun;
8186 /* FUN is a non-constexpr function called in a context that requires a
8187 constant expression. If it comes from a constexpr template, explain why
8188 the instantiation isn't constexpr. */
8190 void
8191 explain_invalid_constexpr_fn (tree fun)
8193 static hash_set<tree> *diagnosed;
8194 tree body;
8195 location_t save_loc;
8196 /* Only diagnose defaulted functions or instantiations. */
8197 if (!DECL_DEFAULTED_FN (fun)
8198 && !is_instantiation_of_constexpr (fun))
8199 return;
8200 if (diagnosed == NULL)
8201 diagnosed = new hash_set<tree>;
8202 if (diagnosed->add (fun))
8203 /* Already explained. */
8204 return;
8206 save_loc = input_location;
8207 input_location = DECL_SOURCE_LOCATION (fun);
8208 inform (0, "%q+D is not usable as a constexpr function because:", fun);
8209 /* First check the declaration. */
8210 if (is_valid_constexpr_fn (fun, true))
8212 /* Then if it's OK, the body. */
8213 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8214 explain_implicit_non_constexpr (fun);
8215 else
8217 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
8218 require_potential_rvalue_constant_expression (body);
8219 if (DECL_CONSTRUCTOR_P (fun))
8220 cx_check_missing_mem_inits (fun, body, true);
8223 input_location = save_loc;
8226 /* Objects of this type represent calls to constexpr functions
8227 along with the bindings of parameters to their arguments, for
8228 the purpose of compile time evaluation. */
8230 typedef struct GTY(()) constexpr_call {
8231 /* Description of the constexpr function definition. */
8232 constexpr_fundef *fundef;
8233 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
8234 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
8235 Note: This arrangement is made to accommodate the use of
8236 iterative_hash_template_arg (see pt.c). If you change this
8237 representation, also change the hash calculation in
8238 cxx_eval_call_expression. */
8239 tree bindings;
8240 /* Result of the call.
8241 NULL means the call is being evaluated.
8242 error_mark_node means that the evaluation was erroneous;
8243 otherwise, the actuall value of the call. */
8244 tree result;
8245 /* The hash of this call; we remember it here to avoid having to
8246 recalculate it when expanding the hash table. */
8247 hashval_t hash;
8248 } constexpr_call;
8250 /* A table of all constexpr calls that have been evaluated by the
8251 compiler in this translation unit. */
8253 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8255 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8256 bool, bool, bool *, bool *);
8258 /* Compute a hash value for a constexpr call representation. */
8260 static hashval_t
8261 constexpr_call_hash (const void *p)
8263 const constexpr_call *info = (const constexpr_call *) p;
8264 return info->hash;
8267 /* Return 1 if the objects pointed to by P and Q represent calls
8268 to the same constexpr function with the same arguments.
8269 Otherwise, return 0. */
8271 static int
8272 constexpr_call_equal (const void *p, const void *q)
8274 const constexpr_call *lhs = (const constexpr_call *) p;
8275 const constexpr_call *rhs = (const constexpr_call *) q;
8276 tree lhs_bindings;
8277 tree rhs_bindings;
8278 if (lhs == rhs)
8279 return 1;
8280 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8281 return 0;
8282 lhs_bindings = lhs->bindings;
8283 rhs_bindings = rhs->bindings;
8284 while (lhs_bindings != NULL && rhs_bindings != NULL)
8286 tree lhs_arg = TREE_VALUE (lhs_bindings);
8287 tree rhs_arg = TREE_VALUE (rhs_bindings);
8288 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8289 if (!cp_tree_equal (lhs_arg, rhs_arg))
8290 return 0;
8291 lhs_bindings = TREE_CHAIN (lhs_bindings);
8292 rhs_bindings = TREE_CHAIN (rhs_bindings);
8294 return lhs_bindings == rhs_bindings;
8297 /* Initialize the constexpr call table, if needed. */
8299 static void
8300 maybe_initialize_constexpr_call_table (void)
8302 if (constexpr_call_table == NULL)
8303 constexpr_call_table = htab_create_ggc (101,
8304 constexpr_call_hash,
8305 constexpr_call_equal,
8306 ggc_free);
8309 /* Return true if T designates the implied `this' parameter. */
8311 bool
8312 is_this_parameter (tree t)
8314 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
8315 return false;
8316 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
8317 return true;
8320 /* We have an expression tree T that represents a call, either CALL_EXPR
8321 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8322 retrun the _DECL for that function. */
8324 static tree
8325 get_function_named_in_call (tree t)
8327 tree fun = NULL;
8328 switch (TREE_CODE (t))
8330 case CALL_EXPR:
8331 fun = CALL_EXPR_FN (t);
8332 break;
8334 case AGGR_INIT_EXPR:
8335 fun = AGGR_INIT_EXPR_FN (t);
8336 break;
8338 default:
8339 gcc_unreachable();
8340 break;
8342 if (TREE_CODE (fun) == ADDR_EXPR
8343 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8344 fun = TREE_OPERAND (fun, 0);
8345 return fun;
8348 /* We have an expression tree T that represents a call, either CALL_EXPR
8349 or AGGR_INIT_EXPR. Return the Nth argument. */
8351 static inline tree
8352 get_nth_callarg (tree t, int n)
8354 switch (TREE_CODE (t))
8356 case CALL_EXPR:
8357 return CALL_EXPR_ARG (t, n);
8359 case AGGR_INIT_EXPR:
8360 return AGGR_INIT_EXPR_ARG (t, n);
8362 default:
8363 gcc_unreachable ();
8364 return NULL;
8368 /* Look up the binding of the function parameter T in a constexpr
8369 function call context CALL. */
8371 static tree
8372 lookup_parameter_binding (const constexpr_call *call, tree t)
8374 tree b = purpose_member (t, call->bindings);
8375 return TREE_VALUE (b);
8378 /* Attempt to evaluate T which represents a call to a builtin function.
8379 We assume here that all builtin functions evaluate to scalar types
8380 represented by _CST nodes. */
8382 static tree
8383 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8384 bool allow_non_constant, bool addr,
8385 bool *non_constant_p, bool *overflow_p)
8387 const int nargs = call_expr_nargs (t);
8388 tree *args = (tree *) alloca (nargs * sizeof (tree));
8389 tree new_call;
8390 int i;
8391 for (i = 0; i < nargs; ++i)
8393 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8394 allow_non_constant, addr,
8395 non_constant_p, overflow_p);
8396 if (allow_non_constant && *non_constant_p)
8397 return t;
8399 if (*non_constant_p)
8400 return t;
8401 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8402 CALL_EXPR_FN (t), nargs, args);
8403 new_call = fold (new_call);
8404 VERIFY_CONSTANT (new_call);
8405 return new_call;
8408 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8409 the type of the value to match. */
8411 static tree
8412 adjust_temp_type (tree type, tree temp)
8414 if (TREE_TYPE (temp) == type)
8415 return temp;
8416 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8417 if (TREE_CODE (temp) == CONSTRUCTOR)
8418 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8419 gcc_assert (scalarish_type_p (type));
8420 return cp_fold_convert (type, temp);
8423 /* Subroutine of cxx_eval_call_expression.
8424 We are processing a call expression (either CALL_EXPR or
8425 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8426 all arguments and bind their values to correspondings
8427 parameters, making up the NEW_CALL context. */
8429 static void
8430 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8431 constexpr_call *new_call,
8432 bool allow_non_constant,
8433 bool *non_constant_p, bool *overflow_p)
8435 const int nargs = call_expr_nargs (t);
8436 tree fun = new_call->fundef->decl;
8437 tree parms = DECL_ARGUMENTS (fun);
8438 int i;
8439 for (i = 0; i < nargs; ++i)
8441 tree x, arg;
8442 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8443 /* For member function, the first argument is a pointer to the implied
8444 object. And for an object construction, don't bind `this' before
8445 it is fully constructed. */
8446 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8447 goto next;
8448 x = get_nth_callarg (t, i);
8449 if (parms && DECL_BY_REFERENCE (parms))
8451 /* cp_genericize made this a reference for argument passing, but
8452 we don't want to treat it like one for constexpr evaluation. */
8453 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8454 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8455 type = TREE_TYPE (type);
8456 x = convert_from_reference (x);
8458 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8459 TREE_CODE (type) == REFERENCE_TYPE,
8460 non_constant_p, overflow_p);
8461 /* Don't VERIFY_CONSTANT here. */
8462 if (*non_constant_p && allow_non_constant)
8463 return;
8464 /* Just discard ellipsis args after checking their constantitude. */
8465 if (!parms)
8466 continue;
8467 if (*non_constant_p)
8468 /* Don't try to adjust the type of non-constant args. */
8469 goto next;
8471 /* Make sure the binding has the same type as the parm. */
8472 if (TREE_CODE (type) != REFERENCE_TYPE)
8473 arg = adjust_temp_type (type, arg);
8474 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8475 next:
8476 parms = TREE_CHAIN (parms);
8480 /* Variables and functions to manage constexpr call expansion context.
8481 These do not need to be marked for PCH or GC. */
8483 /* FIXME remember and print actual constant arguments. */
8484 static vec<tree> call_stack = vNULL;
8485 static int call_stack_tick;
8486 static int last_cx_error_tick;
8488 static bool
8489 push_cx_call_context (tree call)
8491 ++call_stack_tick;
8492 if (!EXPR_HAS_LOCATION (call))
8493 SET_EXPR_LOCATION (call, input_location);
8494 call_stack.safe_push (call);
8495 if (call_stack.length () > (unsigned) max_constexpr_depth)
8496 return false;
8497 return true;
8500 static void
8501 pop_cx_call_context (void)
8503 ++call_stack_tick;
8504 call_stack.pop ();
8507 vec<tree>
8508 cx_error_context (void)
8510 vec<tree> r = vNULL;
8511 if (call_stack_tick != last_cx_error_tick
8512 && !call_stack.is_empty ())
8513 r = call_stack;
8514 last_cx_error_tick = call_stack_tick;
8515 return r;
8518 /* Subroutine of cxx_eval_constant_expression.
8519 Evaluate the call expression tree T in the context of OLD_CALL expression
8520 evaluation. */
8522 static tree
8523 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8524 bool allow_non_constant, bool addr,
8525 bool *non_constant_p, bool *overflow_p)
8527 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
8528 tree fun = get_function_named_in_call (t);
8529 tree result;
8530 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8531 constexpr_call **slot;
8532 constexpr_call *entry;
8533 bool depth_ok;
8535 if (TREE_CODE (fun) != FUNCTION_DECL)
8537 /* Might be a constexpr function pointer. */
8538 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8539 /*addr*/false, non_constant_p,
8540 overflow_p);
8541 STRIP_NOPS (fun);
8542 if (TREE_CODE (fun) == ADDR_EXPR)
8543 fun = TREE_OPERAND (fun, 0);
8545 if (TREE_CODE (fun) != FUNCTION_DECL)
8547 if (!allow_non_constant && !*non_constant_p)
8548 error_at (loc, "expression %qE does not designate a constexpr "
8549 "function", fun);
8550 *non_constant_p = true;
8551 return t;
8553 if (DECL_CLONED_FUNCTION_P (fun))
8554 fun = DECL_CLONED_FUNCTION (fun);
8555 if (is_builtin_fn (fun))
8556 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8557 addr, non_constant_p, overflow_p);
8558 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8560 if (!allow_non_constant)
8562 error_at (loc, "call to non-constexpr function %qD", fun);
8563 explain_invalid_constexpr_fn (fun);
8565 *non_constant_p = true;
8566 return t;
8569 /* Shortcut trivial constructor/op=. */
8570 if (trivial_fn_p (fun))
8572 if (call_expr_nargs (t) == 2)
8574 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8575 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8576 addr, non_constant_p, overflow_p);
8578 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8579 && AGGR_INIT_ZERO_FIRST (t))
8580 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8583 /* If in direct recursive call, optimize definition search. */
8584 if (old_call != NULL && old_call->fundef->decl == fun)
8585 new_call.fundef = old_call->fundef;
8586 else
8588 new_call.fundef = retrieve_constexpr_fundef (fun);
8589 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8591 if (!allow_non_constant)
8593 if (DECL_INITIAL (fun))
8595 /* The definition of fun was somehow unsuitable. */
8596 error_at (loc, "%qD called in a constant expression", fun);
8597 explain_invalid_constexpr_fn (fun);
8599 else
8600 error_at (loc, "%qD used before its definition", fun);
8602 *non_constant_p = true;
8603 return t;
8606 cxx_bind_parameters_in_call (old_call, t, &new_call,
8607 allow_non_constant, non_constant_p, overflow_p);
8608 if (*non_constant_p)
8609 return t;
8611 depth_ok = push_cx_call_context (t);
8613 new_call.hash
8614 = iterative_hash_template_arg (new_call.bindings,
8615 constexpr_fundef_hash (new_call.fundef));
8617 /* If we have seen this call before, we are done. */
8618 maybe_initialize_constexpr_call_table ();
8619 slot = (constexpr_call **)
8620 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8621 entry = *slot;
8622 if (entry == NULL)
8624 /* We need to keep a pointer to the entry, not just the slot, as the
8625 slot can move in the call to cxx_eval_builtin_function_call. */
8626 *slot = entry = ggc_alloc<constexpr_call> ();
8627 *entry = new_call;
8629 /* Calls which are in progress have their result set to NULL
8630 so that we can detect circular dependencies. */
8631 else if (entry->result == NULL)
8633 if (!allow_non_constant)
8634 error ("call has circular dependency");
8635 *non_constant_p = true;
8636 entry->result = result = error_mark_node;
8639 if (!depth_ok)
8641 if (!allow_non_constant)
8642 error ("constexpr evaluation depth exceeds maximum of %d (use "
8643 "-fconstexpr-depth= to increase the maximum)",
8644 max_constexpr_depth);
8645 *non_constant_p = true;
8646 entry->result = result = error_mark_node;
8648 else
8650 result = entry->result;
8651 if (!result || result == error_mark_node)
8652 result = (cxx_eval_constant_expression
8653 (&new_call, new_call.fundef->body,
8654 allow_non_constant, addr,
8655 non_constant_p, overflow_p));
8656 if (result == error_mark_node)
8657 *non_constant_p = true;
8658 if (*non_constant_p)
8659 entry->result = result = error_mark_node;
8660 else
8662 /* If this was a call to initialize an object, set the type of
8663 the CONSTRUCTOR to the type of that object. */
8664 if (DECL_CONSTRUCTOR_P (fun))
8666 tree ob_arg = get_nth_callarg (t, 0);
8667 STRIP_NOPS (ob_arg);
8668 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8669 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8670 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8671 result);
8673 entry->result = result;
8677 pop_cx_call_context ();
8678 return unshare_expr (result);
8681 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8683 bool
8684 reduced_constant_expression_p (tree t)
8686 switch (TREE_CODE (t))
8688 case PTRMEM_CST:
8689 /* Even if we can't lower this yet, it's constant. */
8690 return true;
8692 case CONSTRUCTOR:
8693 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
8694 tree elt; unsigned HOST_WIDE_INT idx;
8695 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
8696 if (!reduced_constant_expression_p (elt))
8697 return false;
8698 return true;
8700 default:
8701 /* FIXME are we calling this too much? */
8702 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8706 /* Some expressions may have constant operands but are not constant
8707 themselves, such as 1/0. Call this function (or rather, the macro
8708 following it) to check for that condition.
8710 We only call this in places that require an arithmetic constant, not in
8711 places where we might have a non-constant expression that can be a
8712 component of a constant expression, such as the address of a constexpr
8713 variable that might be dereferenced later. */
8715 static bool
8716 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8717 bool *overflow_p)
8719 if (!*non_constant_p && !reduced_constant_expression_p (t))
8721 if (!allow_non_constant)
8722 error ("%q+E is not a constant expression", t);
8723 *non_constant_p = true;
8725 if (TREE_OVERFLOW_P (t))
8727 if (!allow_non_constant)
8729 permerror (input_location, "overflow in constant expression");
8730 /* If we're being permissive (and are in an enforcing
8731 context), ignore the overflow. */
8732 if (flag_permissive)
8733 return *non_constant_p;
8735 *overflow_p = true;
8737 return *non_constant_p;
8740 /* Subroutine of cxx_eval_constant_expression.
8741 Attempt to reduce the unary expression tree T to a compile time value.
8742 If successful, return the value. Otherwise issue a diagnostic
8743 and return error_mark_node. */
8745 static tree
8746 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8747 bool allow_non_constant, bool addr,
8748 bool *non_constant_p, bool *overflow_p)
8750 tree r;
8751 tree orig_arg = TREE_OPERAND (t, 0);
8752 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8753 addr, non_constant_p, overflow_p);
8754 VERIFY_CONSTANT (arg);
8755 if (arg == orig_arg)
8756 return t;
8757 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8758 VERIFY_CONSTANT (r);
8759 return r;
8762 /* Subroutine of cxx_eval_constant_expression.
8763 Like cxx_eval_unary_expression, except for binary expressions. */
8765 static tree
8766 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8767 bool allow_non_constant, bool addr,
8768 bool *non_constant_p, bool *overflow_p)
8770 tree r;
8771 tree orig_lhs = TREE_OPERAND (t, 0);
8772 tree orig_rhs = TREE_OPERAND (t, 1);
8773 tree lhs, rhs;
8774 lhs = cxx_eval_constant_expression (call, orig_lhs,
8775 allow_non_constant, addr,
8776 non_constant_p, overflow_p);
8777 VERIFY_CONSTANT (lhs);
8778 rhs = cxx_eval_constant_expression (call, orig_rhs,
8779 allow_non_constant, addr,
8780 non_constant_p, overflow_p);
8781 VERIFY_CONSTANT (rhs);
8782 if (lhs == orig_lhs && rhs == orig_rhs)
8783 return t;
8784 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8785 VERIFY_CONSTANT (r);
8786 return r;
8789 /* Subroutine of cxx_eval_constant_expression.
8790 Attempt to evaluate condition expressions. Dead branches are not
8791 looked into. */
8793 static tree
8794 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8795 bool allow_non_constant, bool addr,
8796 bool *non_constant_p, bool *overflow_p)
8798 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8799 allow_non_constant, addr,
8800 non_constant_p, overflow_p);
8801 VERIFY_CONSTANT (val);
8802 /* Don't VERIFY_CONSTANT the other operands. */
8803 if (integer_zerop (val))
8804 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8805 allow_non_constant, addr,
8806 non_constant_p, overflow_p);
8807 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8808 allow_non_constant, addr,
8809 non_constant_p, overflow_p);
8812 /* Subroutine of cxx_eval_constant_expression.
8813 Attempt to reduce a reference to an array slot. */
8815 static tree
8816 cxx_eval_array_reference (const constexpr_call *call, tree t,
8817 bool allow_non_constant, bool addr,
8818 bool *non_constant_p, bool *overflow_p)
8820 tree oldary = TREE_OPERAND (t, 0);
8821 tree ary = cxx_eval_constant_expression (call, oldary,
8822 allow_non_constant, addr,
8823 non_constant_p, overflow_p);
8824 tree index, oldidx;
8825 HOST_WIDE_INT i;
8826 tree elem_type;
8827 unsigned len, elem_nchars = 1;
8828 if (*non_constant_p)
8829 return t;
8830 oldidx = TREE_OPERAND (t, 1);
8831 index = cxx_eval_constant_expression (call, oldidx,
8832 allow_non_constant, false,
8833 non_constant_p, overflow_p);
8834 VERIFY_CONSTANT (index);
8835 if (addr && ary == oldary && index == oldidx)
8836 return t;
8837 else if (addr)
8838 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8839 elem_type = TREE_TYPE (TREE_TYPE (ary));
8840 if (TREE_CODE (ary) == CONSTRUCTOR)
8841 len = CONSTRUCTOR_NELTS (ary);
8842 else if (TREE_CODE (ary) == STRING_CST)
8844 elem_nchars = (TYPE_PRECISION (elem_type)
8845 / TYPE_PRECISION (char_type_node));
8846 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8848 else
8850 /* We can't do anything with other tree codes, so use
8851 VERIFY_CONSTANT to complain and fail. */
8852 VERIFY_CONSTANT (ary);
8853 gcc_unreachable ();
8855 if (compare_tree_int (index, len) >= 0)
8857 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8859 /* If it's within the array bounds but doesn't have an explicit
8860 initializer, it's value-initialized. */
8861 tree val = build_value_init (elem_type, tf_warning_or_error);
8862 return cxx_eval_constant_expression (call, val,
8863 allow_non_constant, addr,
8864 non_constant_p, overflow_p);
8867 if (!allow_non_constant)
8868 error ("array subscript out of bound");
8869 *non_constant_p = true;
8870 return t;
8872 else if (tree_int_cst_lt (index, integer_zero_node))
8874 if (!allow_non_constant)
8875 error ("negative array subscript");
8876 *non_constant_p = true;
8877 return t;
8879 i = tree_to_shwi (index);
8880 if (TREE_CODE (ary) == CONSTRUCTOR)
8881 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8882 else if (elem_nchars == 1)
8883 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8884 TREE_STRING_POINTER (ary)[i]);
8885 else
8887 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8888 return native_interpret_expr (type, (const unsigned char *)
8889 TREE_STRING_POINTER (ary)
8890 + i * elem_nchars, elem_nchars);
8892 /* Don't VERIFY_CONSTANT here. */
8895 /* Subroutine of cxx_eval_constant_expression.
8896 Attempt to reduce a field access of a value of class type. */
8898 static tree
8899 cxx_eval_component_reference (const constexpr_call *call, tree t,
8900 bool allow_non_constant, bool addr,
8901 bool *non_constant_p, bool *overflow_p)
8903 unsigned HOST_WIDE_INT i;
8904 tree field;
8905 tree value;
8906 tree part = TREE_OPERAND (t, 1);
8907 tree orig_whole = TREE_OPERAND (t, 0);
8908 tree whole = cxx_eval_constant_expression (call, orig_whole,
8909 allow_non_constant, addr,
8910 non_constant_p, overflow_p);
8911 if (whole == orig_whole)
8912 return t;
8913 if (addr)
8914 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8915 whole, part, NULL_TREE);
8916 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8917 CONSTRUCTOR. */
8918 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8920 if (!allow_non_constant)
8921 error ("%qE is not a constant expression", orig_whole);
8922 *non_constant_p = true;
8924 if (DECL_MUTABLE_P (part))
8926 if (!allow_non_constant)
8927 error ("mutable %qD is not usable in a constant expression", part);
8928 *non_constant_p = true;
8930 if (*non_constant_p)
8931 return t;
8932 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8934 if (field == part)
8935 return value;
8937 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8938 && CONSTRUCTOR_NELTS (whole) > 0)
8940 /* DR 1188 says we don't have to deal with this. */
8941 if (!allow_non_constant)
8942 error ("accessing %qD member instead of initialized %qD member in "
8943 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8944 *non_constant_p = true;
8945 return t;
8948 /* If there's no explicit init for this field, it's value-initialized. */
8949 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8950 return cxx_eval_constant_expression (call, value,
8951 allow_non_constant, addr,
8952 non_constant_p, overflow_p);
8955 /* Subroutine of cxx_eval_constant_expression.
8956 Attempt to reduce a field access of a value of class type that is
8957 expressed as a BIT_FIELD_REF. */
8959 static tree
8960 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8961 bool allow_non_constant, bool addr,
8962 bool *non_constant_p, bool *overflow_p)
8964 tree orig_whole = TREE_OPERAND (t, 0);
8965 tree retval, fldval, utype, mask;
8966 bool fld_seen = false;
8967 HOST_WIDE_INT istart, isize;
8968 tree whole = cxx_eval_constant_expression (call, orig_whole,
8969 allow_non_constant, addr,
8970 non_constant_p, overflow_p);
8971 tree start, field, value;
8972 unsigned HOST_WIDE_INT i;
8974 if (whole == orig_whole)
8975 return t;
8976 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8977 CONSTRUCTOR. */
8978 if (!*non_constant_p
8979 && TREE_CODE (whole) != VECTOR_CST
8980 && TREE_CODE (whole) != CONSTRUCTOR)
8982 if (!allow_non_constant)
8983 error ("%qE is not a constant expression", orig_whole);
8984 *non_constant_p = true;
8986 if (*non_constant_p)
8987 return t;
8989 if (TREE_CODE (whole) == VECTOR_CST)
8990 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8991 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8993 start = TREE_OPERAND (t, 2);
8994 istart = tree_to_shwi (start);
8995 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8996 utype = TREE_TYPE (t);
8997 if (!TYPE_UNSIGNED (utype))
8998 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8999 retval = build_int_cst (utype, 0);
9000 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
9002 tree bitpos = bit_position (field);
9003 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
9004 return value;
9005 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
9006 && TREE_CODE (value) == INTEGER_CST
9007 && tree_fits_shwi_p (bitpos)
9008 && tree_fits_shwi_p (DECL_SIZE (field)))
9010 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
9011 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
9012 HOST_WIDE_INT shift;
9013 if (bit >= istart && bit + sz <= istart + isize)
9015 fldval = fold_convert (utype, value);
9016 mask = build_int_cst_type (utype, -1);
9017 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
9018 size_int (TYPE_PRECISION (utype) - sz));
9019 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
9020 size_int (TYPE_PRECISION (utype) - sz));
9021 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
9022 shift = bit - istart;
9023 if (BYTES_BIG_ENDIAN)
9024 shift = TYPE_PRECISION (utype) - shift - sz;
9025 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
9026 size_int (shift));
9027 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
9028 fld_seen = true;
9032 if (fld_seen)
9033 return fold_convert (TREE_TYPE (t), retval);
9034 gcc_unreachable ();
9035 return error_mark_node;
9038 /* Subroutine of cxx_eval_constant_expression.
9039 Evaluate a short-circuited logical expression T in the context
9040 of a given constexpr CALL. BAILOUT_VALUE is the value for
9041 early return. CONTINUE_VALUE is used here purely for
9042 sanity check purposes. */
9044 static tree
9045 cxx_eval_logical_expression (const constexpr_call *call, tree t,
9046 tree bailout_value, tree continue_value,
9047 bool allow_non_constant, bool addr,
9048 bool *non_constant_p, bool *overflow_p)
9050 tree r;
9051 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9052 allow_non_constant, addr,
9053 non_constant_p, overflow_p);
9054 VERIFY_CONSTANT (lhs);
9055 if (tree_int_cst_equal (lhs, bailout_value))
9056 return lhs;
9057 gcc_assert (tree_int_cst_equal (lhs, continue_value));
9058 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9059 allow_non_constant, addr, non_constant_p, overflow_p);
9060 VERIFY_CONSTANT (r);
9061 return r;
9064 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
9065 CONSTRUCTOR elements to initialize (part of) an object containing that
9066 field. Return a pointer to the constructor_elt corresponding to the
9067 initialization of the field. */
9069 static constructor_elt *
9070 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
9072 tree aggr = TREE_OPERAND (ref, 0);
9073 tree field = TREE_OPERAND (ref, 1);
9074 HOST_WIDE_INT i;
9075 constructor_elt *ce;
9077 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
9079 if (TREE_CODE (aggr) == COMPONENT_REF)
9081 constructor_elt *base_ce
9082 = base_field_constructor_elt (v, aggr);
9083 v = CONSTRUCTOR_ELTS (base_ce->value);
9086 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
9087 if (ce->index == field)
9088 return ce;
9090 gcc_unreachable ();
9091 return NULL;
9094 /* Subroutine of cxx_eval_constant_expression.
9095 The expression tree T denotes a C-style array or a C-style
9096 aggregate. Reduce it to a constant expression. */
9098 static tree
9099 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
9100 bool allow_non_constant, bool addr,
9101 bool *non_constant_p, bool *overflow_p)
9103 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
9104 vec<constructor_elt, va_gc> *n;
9105 vec_alloc (n, vec_safe_length (v));
9106 constructor_elt *ce;
9107 HOST_WIDE_INT i;
9108 bool changed = false;
9109 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
9110 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
9112 tree elt = cxx_eval_constant_expression (call, ce->value,
9113 allow_non_constant, addr,
9114 non_constant_p, overflow_p);
9115 /* Don't VERIFY_CONSTANT here. */
9116 if (allow_non_constant && *non_constant_p)
9117 goto fail;
9118 if (elt != ce->value)
9119 changed = true;
9120 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
9122 /* This is an initialization of a vfield inside a base
9123 subaggregate that we already initialized; push this
9124 initialization into the previous initialization. */
9125 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
9126 inner->value = elt;
9128 else if (ce->index
9129 && (TREE_CODE (ce->index) == NOP_EXPR
9130 || TREE_CODE (ce->index) == POINTER_PLUS_EXPR))
9132 /* This is an initializer for an empty base; now that we've
9133 checked that it's constant, we can ignore it. */
9134 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
9136 else
9137 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
9139 if (*non_constant_p || !changed)
9141 fail:
9142 vec_free (n);
9143 return t;
9145 t = build_constructor (TREE_TYPE (t), n);
9146 TREE_CONSTANT (t) = true;
9147 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
9148 t = fold (t);
9149 return t;
9152 /* Subroutine of cxx_eval_constant_expression.
9153 The expression tree T is a VEC_INIT_EXPR which denotes the desired
9154 initialization of a non-static data member of array type. Reduce it to a
9155 CONSTRUCTOR.
9157 Note that apart from value-initialization (when VALUE_INIT is true),
9158 this is only intended to support value-initialization and the
9159 initializations done by defaulted constructors for classes with
9160 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
9161 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
9162 for the copy/move constructor. */
9164 static tree
9165 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
9166 bool value_init, bool allow_non_constant, bool addr,
9167 bool *non_constant_p, bool *overflow_p)
9169 tree elttype = TREE_TYPE (atype);
9170 int max = tree_to_shwi (array_type_nelts (atype));
9171 vec<constructor_elt, va_gc> *n;
9172 vec_alloc (n, max + 1);
9173 bool pre_init = false;
9174 int i;
9176 /* For the default constructor, build up a call to the default
9177 constructor of the element type. We only need to handle class types
9178 here, as for a constructor to be constexpr, all members must be
9179 initialized, which for a defaulted default constructor means they must
9180 be of a class type with a constexpr default constructor. */
9181 if (TREE_CODE (elttype) == ARRAY_TYPE)
9182 /* We only do this at the lowest level. */;
9183 else if (value_init)
9185 init = build_value_init (elttype, tf_warning_or_error);
9186 init = cxx_eval_constant_expression
9187 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
9188 pre_init = true;
9190 else if (!init)
9192 vec<tree, va_gc> *argvec = make_tree_vector ();
9193 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9194 &argvec, elttype, LOOKUP_NORMAL,
9195 tf_warning_or_error);
9196 release_tree_vector (argvec);
9197 init = cxx_eval_constant_expression (call, init, allow_non_constant,
9198 addr, non_constant_p, overflow_p);
9199 pre_init = true;
9202 if (*non_constant_p && !allow_non_constant)
9203 goto fail;
9205 for (i = 0; i <= max; ++i)
9207 tree idx = build_int_cst (size_type_node, i);
9208 tree eltinit;
9209 if (TREE_CODE (elttype) == ARRAY_TYPE)
9211 /* A multidimensional array; recurse. */
9212 if (value_init || init == NULL_TREE)
9213 eltinit = NULL_TREE;
9214 else
9215 eltinit = cp_build_array_ref (input_location, init, idx,
9216 tf_warning_or_error);
9217 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
9218 allow_non_constant, addr,
9219 non_constant_p, overflow_p);
9221 else if (pre_init)
9223 /* Initializing an element using value or default initialization
9224 we just pre-built above. */
9225 if (i == 0)
9226 eltinit = init;
9227 else
9228 eltinit = unshare_expr (init);
9230 else
9232 /* Copying an element. */
9233 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9234 (atype, TREE_TYPE (init)));
9235 eltinit = cp_build_array_ref (input_location, init, idx,
9236 tf_warning_or_error);
9237 if (!real_lvalue_p (init))
9238 eltinit = move (eltinit);
9239 eltinit = force_rvalue (eltinit, tf_warning_or_error);
9240 eltinit = cxx_eval_constant_expression
9241 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
9243 if (*non_constant_p && !allow_non_constant)
9244 goto fail;
9245 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
9248 if (!*non_constant_p)
9250 init = build_constructor (atype, n);
9251 TREE_CONSTANT (init) = true;
9252 return init;
9255 fail:
9256 vec_free (n);
9257 return init;
9260 static tree
9261 cxx_eval_vec_init (const constexpr_call *call, tree t,
9262 bool allow_non_constant, bool addr,
9263 bool *non_constant_p, bool *overflow_p)
9265 tree atype = TREE_TYPE (t);
9266 tree init = VEC_INIT_EXPR_INIT (t);
9267 tree r = cxx_eval_vec_init_1 (call, atype, init,
9268 VEC_INIT_EXPR_VALUE_INIT (t),
9269 allow_non_constant, addr, non_constant_p, overflow_p);
9270 if (*non_constant_p)
9271 return t;
9272 else
9273 return r;
9276 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9277 match. We want to be less strict for simple *& folding; if we have a
9278 non-const temporary that we access through a const pointer, that should
9279 work. We handle this here rather than change fold_indirect_ref_1
9280 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9281 don't really make sense outside of constant expression evaluation. Also
9282 we want to allow folding to COMPONENT_REF, which could cause trouble
9283 with TBAA in fold_indirect_ref_1.
9285 Try to keep this function synced with fold_indirect_ref_1. */
9287 static tree
9288 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9290 tree sub, subtype;
9292 sub = op0;
9293 STRIP_NOPS (sub);
9294 subtype = TREE_TYPE (sub);
9295 if (!POINTER_TYPE_P (subtype))
9296 return NULL_TREE;
9298 if (TREE_CODE (sub) == ADDR_EXPR)
9300 tree op = TREE_OPERAND (sub, 0);
9301 tree optype = TREE_TYPE (op);
9303 /* *&CONST_DECL -> to the value of the const decl. */
9304 if (TREE_CODE (op) == CONST_DECL)
9305 return DECL_INITIAL (op);
9306 /* *&p => p; make sure to handle *&"str"[cst] here. */
9307 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9309 tree fop = fold_read_from_constant_string (op);
9310 if (fop)
9311 return fop;
9312 else
9313 return op;
9315 /* *(foo *)&fooarray => fooarray[0] */
9316 else if (TREE_CODE (optype) == ARRAY_TYPE
9317 && (same_type_ignoring_top_level_qualifiers_p
9318 (type, TREE_TYPE (optype))))
9320 tree type_domain = TYPE_DOMAIN (optype);
9321 tree min_val = size_zero_node;
9322 if (type_domain && TYPE_MIN_VALUE (type_domain))
9323 min_val = TYPE_MIN_VALUE (type_domain);
9324 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9325 NULL_TREE, NULL_TREE);
9327 /* *(foo *)&complexfoo => __real__ complexfoo */
9328 else if (TREE_CODE (optype) == COMPLEX_TYPE
9329 && (same_type_ignoring_top_level_qualifiers_p
9330 (type, TREE_TYPE (optype))))
9331 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9332 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9333 else if (TREE_CODE (optype) == VECTOR_TYPE
9334 && (same_type_ignoring_top_level_qualifiers_p
9335 (type, TREE_TYPE (optype))))
9337 tree part_width = TYPE_SIZE (type);
9338 tree index = bitsize_int (0);
9339 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9341 /* Also handle conversion to an empty base class, which
9342 is represented with a NOP_EXPR. */
9343 else if (is_empty_class (type)
9344 && CLASS_TYPE_P (optype)
9345 && DERIVED_FROM_P (type, optype))
9347 *empty_base = true;
9348 return op;
9350 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9351 else if (RECORD_OR_UNION_TYPE_P (optype))
9353 tree field = TYPE_FIELDS (optype);
9354 for (; field; field = DECL_CHAIN (field))
9355 if (TREE_CODE (field) == FIELD_DECL
9356 && integer_zerop (byte_position (field))
9357 && (same_type_ignoring_top_level_qualifiers_p
9358 (TREE_TYPE (field), type)))
9360 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9361 break;
9365 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9366 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9368 tree op00 = TREE_OPERAND (sub, 0);
9369 tree op01 = TREE_OPERAND (sub, 1);
9371 STRIP_NOPS (op00);
9372 if (TREE_CODE (op00) == ADDR_EXPR)
9374 tree op00type;
9375 op00 = TREE_OPERAND (op00, 0);
9376 op00type = TREE_TYPE (op00);
9378 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9379 if (TREE_CODE (op00type) == VECTOR_TYPE
9380 && (same_type_ignoring_top_level_qualifiers_p
9381 (type, TREE_TYPE (op00type))))
9383 HOST_WIDE_INT offset = tree_to_shwi (op01);
9384 tree part_width = TYPE_SIZE (type);
9385 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9386 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9387 tree index = bitsize_int (indexi);
9389 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
9390 return fold_build3_loc (loc,
9391 BIT_FIELD_REF, type, op00,
9392 part_width, index);
9395 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9396 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9397 && (same_type_ignoring_top_level_qualifiers_p
9398 (type, TREE_TYPE (op00type))))
9400 tree size = TYPE_SIZE_UNIT (type);
9401 if (tree_int_cst_equal (size, op01))
9402 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9404 /* ((foo *)&fooarray)[1] => fooarray[1] */
9405 else if (TREE_CODE (op00type) == ARRAY_TYPE
9406 && (same_type_ignoring_top_level_qualifiers_p
9407 (type, TREE_TYPE (op00type))))
9409 tree type_domain = TYPE_DOMAIN (op00type);
9410 tree min_val = size_zero_node;
9411 if (type_domain && TYPE_MIN_VALUE (type_domain))
9412 min_val = TYPE_MIN_VALUE (type_domain);
9413 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9414 TYPE_SIZE_UNIT (type));
9415 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9416 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9417 NULL_TREE, NULL_TREE);
9419 /* Also handle conversion to an empty base class, which
9420 is represented with a NOP_EXPR. */
9421 else if (is_empty_class (type)
9422 && CLASS_TYPE_P (op00type)
9423 && DERIVED_FROM_P (type, op00type))
9425 *empty_base = true;
9426 return op00;
9428 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9429 else if (RECORD_OR_UNION_TYPE_P (op00type))
9431 tree field = TYPE_FIELDS (op00type);
9432 for (; field; field = DECL_CHAIN (field))
9433 if (TREE_CODE (field) == FIELD_DECL
9434 && tree_int_cst_equal (byte_position (field), op01)
9435 && (same_type_ignoring_top_level_qualifiers_p
9436 (TREE_TYPE (field), type)))
9438 return fold_build3 (COMPONENT_REF, type, op00,
9439 field, NULL_TREE);
9440 break;
9445 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9446 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9447 && (same_type_ignoring_top_level_qualifiers_p
9448 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9450 tree type_domain;
9451 tree min_val = size_zero_node;
9452 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9453 if (newsub)
9454 sub = newsub;
9455 else
9456 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9457 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9458 if (type_domain && TYPE_MIN_VALUE (type_domain))
9459 min_val = TYPE_MIN_VALUE (type_domain);
9460 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9461 NULL_TREE);
9464 return NULL_TREE;
9467 static tree
9468 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9469 bool allow_non_constant, bool addr,
9470 bool *non_constant_p, bool *overflow_p)
9472 tree orig_op0 = TREE_OPERAND (t, 0);
9473 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9474 /*addr*/false, non_constant_p, overflow_p);
9475 bool empty_base = false;
9476 tree r;
9478 /* Don't VERIFY_CONSTANT here. */
9479 if (*non_constant_p)
9480 return t;
9482 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9483 &empty_base);
9485 if (r)
9486 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9487 addr, non_constant_p, overflow_p);
9488 else
9490 tree sub = op0;
9491 STRIP_NOPS (sub);
9492 if (TREE_CODE (sub) == ADDR_EXPR)
9494 /* We couldn't fold to a constant value. Make sure it's not
9495 something we should have been able to fold. */
9496 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9497 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9498 /* DR 1188 says we don't have to deal with this. */
9499 if (!allow_non_constant)
9500 error ("accessing value of %qE through a %qT glvalue in a "
9501 "constant expression", build_fold_indirect_ref (sub),
9502 TREE_TYPE (t));
9503 *non_constant_p = true;
9504 return t;
9508 /* If we're pulling out the value of an empty base, make sure
9509 that the whole object is constant and then return an empty
9510 CONSTRUCTOR. */
9511 if (empty_base)
9513 VERIFY_CONSTANT (r);
9514 r = build_constructor (TREE_TYPE (t), NULL);
9515 TREE_CONSTANT (r) = true;
9518 if (r == NULL_TREE)
9520 if (addr && op0 != orig_op0)
9521 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9522 if (!addr)
9523 VERIFY_CONSTANT (t);
9524 return t;
9526 return r;
9529 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9530 Shared between potential_constant_expression and
9531 cxx_eval_constant_expression. */
9533 static void
9534 non_const_var_error (tree r)
9536 tree type = TREE_TYPE (r);
9537 error ("the value of %qD is not usable in a constant "
9538 "expression", r);
9539 /* Avoid error cascade. */
9540 if (DECL_INITIAL (r) == error_mark_node)
9541 return;
9542 if (DECL_DECLARED_CONSTEXPR_P (r))
9543 inform (DECL_SOURCE_LOCATION (r),
9544 "%qD used in its own initializer", r);
9545 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9547 if (!CP_TYPE_CONST_P (type))
9548 inform (DECL_SOURCE_LOCATION (r),
9549 "%q#D is not const", r);
9550 else if (CP_TYPE_VOLATILE_P (type))
9551 inform (DECL_SOURCE_LOCATION (r),
9552 "%q#D is volatile", r);
9553 else if (!DECL_INITIAL (r)
9554 || !TREE_CONSTANT (DECL_INITIAL (r)))
9555 inform (DECL_SOURCE_LOCATION (r),
9556 "%qD was not initialized with a constant "
9557 "expression", r);
9558 else
9559 gcc_unreachable ();
9561 else
9563 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9564 inform (DECL_SOURCE_LOCATION (r),
9565 "%qD was not declared %<constexpr%>", r);
9566 else
9567 inform (DECL_SOURCE_LOCATION (r),
9568 "%qD does not have integral or enumeration type",
9573 /* Subroutine of cxx_eval_constant_expression.
9574 Like cxx_eval_unary_expression, except for trinary expressions. */
9576 static tree
9577 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9578 bool allow_non_constant, bool addr,
9579 bool *non_constant_p, bool *overflow_p)
9581 int i;
9582 tree args[3];
9583 tree val;
9585 for (i = 0; i < 3; i++)
9587 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9588 allow_non_constant, addr,
9589 non_constant_p, overflow_p);
9590 VERIFY_CONSTANT (args[i]);
9593 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9594 args[0], args[1], args[2]);
9595 if (val == NULL_TREE)
9596 return t;
9597 VERIFY_CONSTANT (val);
9598 return val;
9601 /* Attempt to reduce the expression T to a constant value.
9602 On failure, issue diagnostic and return error_mark_node. */
9603 /* FIXME unify with c_fully_fold */
9605 static tree
9606 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9607 bool allow_non_constant, bool addr,
9608 bool *non_constant_p, bool *overflow_p)
9610 tree r = t;
9612 if (t == error_mark_node)
9614 *non_constant_p = true;
9615 return t;
9617 if (CONSTANT_CLASS_P (t))
9619 if (TREE_CODE (t) == PTRMEM_CST)
9620 t = cplus_expand_constant (t);
9621 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9622 *overflow_p = true;
9623 return t;
9625 if (TREE_CODE (t) != NOP_EXPR
9626 && reduced_constant_expression_p (t))
9627 return fold (t);
9629 switch (TREE_CODE (t))
9631 case VAR_DECL:
9632 if (addr)
9633 return t;
9634 /* else fall through. */
9635 case CONST_DECL:
9636 r = integral_constant_value (t);
9637 if (TREE_CODE (r) == TARGET_EXPR
9638 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9639 r = TARGET_EXPR_INITIAL (r);
9640 if (DECL_P (r))
9642 if (!allow_non_constant)
9643 non_const_var_error (r);
9644 *non_constant_p = true;
9646 break;
9648 case FUNCTION_DECL:
9649 case TEMPLATE_DECL:
9650 case LABEL_DECL:
9651 return t;
9653 case PARM_DECL:
9654 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9656 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9658 if (!allow_non_constant)
9659 sorry ("use of the value of the object being constructed "
9660 "in a constant expression");
9661 *non_constant_p = true;
9663 else
9664 r = lookup_parameter_binding (call, t);
9666 else if (addr)
9667 /* Defer in case this is only used for its type. */;
9668 else
9670 if (!allow_non_constant)
9671 error ("%qE is not a constant expression", t);
9672 *non_constant_p = true;
9674 break;
9676 case CALL_EXPR:
9677 case AGGR_INIT_EXPR:
9678 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9679 non_constant_p, overflow_p);
9680 break;
9682 case TARGET_EXPR:
9683 if (!literal_type_p (TREE_TYPE (t)))
9685 if (!allow_non_constant)
9687 error ("temporary of non-literal type %qT in a "
9688 "constant expression", TREE_TYPE (t));
9689 explain_non_literal_class (TREE_TYPE (t));
9691 *non_constant_p = true;
9692 break;
9694 /* else fall through. */
9695 case INIT_EXPR:
9696 /* Pass false for 'addr' because these codes indicate
9697 initialization of a temporary. */
9698 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9699 allow_non_constant, false,
9700 non_constant_p, overflow_p);
9701 if (!*non_constant_p)
9702 /* Adjust the type of the result to the type of the temporary. */
9703 r = adjust_temp_type (TREE_TYPE (t), r);
9704 break;
9706 case SCOPE_REF:
9707 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9708 allow_non_constant, addr,
9709 non_constant_p, overflow_p);
9710 break;
9712 case RETURN_EXPR:
9713 case NON_LVALUE_EXPR:
9714 case TRY_CATCH_EXPR:
9715 case CLEANUP_POINT_EXPR:
9716 case MUST_NOT_THROW_EXPR:
9717 case SAVE_EXPR:
9718 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9719 allow_non_constant, addr,
9720 non_constant_p, overflow_p);
9721 break;
9723 /* These differ from cxx_eval_unary_expression in that this doesn't
9724 check for a constant operand or result; an address can be
9725 constant without its operand being, and vice versa. */
9726 case INDIRECT_REF:
9727 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9728 non_constant_p, overflow_p);
9729 break;
9731 case ADDR_EXPR:
9733 tree oldop = TREE_OPERAND (t, 0);
9734 tree op = cxx_eval_constant_expression (call, oldop,
9735 allow_non_constant,
9736 /*addr*/true,
9737 non_constant_p, overflow_p);
9738 /* Don't VERIFY_CONSTANT here. */
9739 if (*non_constant_p)
9740 return t;
9741 /* This function does more aggressive folding than fold itself. */
9742 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9743 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9744 return t;
9745 break;
9748 case REALPART_EXPR:
9749 case IMAGPART_EXPR:
9750 case CONJ_EXPR:
9751 case FIX_TRUNC_EXPR:
9752 case FLOAT_EXPR:
9753 case NEGATE_EXPR:
9754 case ABS_EXPR:
9755 case BIT_NOT_EXPR:
9756 case TRUTH_NOT_EXPR:
9757 case FIXED_CONVERT_EXPR:
9758 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9759 non_constant_p, overflow_p);
9760 break;
9762 case SIZEOF_EXPR:
9763 if (SIZEOF_EXPR_TYPE_P (t))
9764 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9765 SIZEOF_EXPR, false);
9766 else if (TYPE_P (TREE_OPERAND (t, 0)))
9767 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9768 false);
9769 else
9770 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9771 false);
9772 if (r == error_mark_node)
9773 r = size_one_node;
9774 VERIFY_CONSTANT (r);
9775 break;
9777 case COMPOUND_EXPR:
9779 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9780 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9781 introduced by build_call_a. */
9782 tree op0 = TREE_OPERAND (t, 0);
9783 tree op1 = TREE_OPERAND (t, 1);
9784 STRIP_NOPS (op1);
9785 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9786 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9787 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9788 addr, non_constant_p, overflow_p);
9789 else
9791 /* Check that the LHS is constant and then discard it. */
9792 cxx_eval_constant_expression (call, op0, allow_non_constant,
9793 false, non_constant_p, overflow_p);
9794 op1 = TREE_OPERAND (t, 1);
9795 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9796 addr, non_constant_p, overflow_p);
9799 break;
9801 case POINTER_PLUS_EXPR:
9802 case PLUS_EXPR:
9803 case MINUS_EXPR:
9804 case MULT_EXPR:
9805 case TRUNC_DIV_EXPR:
9806 case CEIL_DIV_EXPR:
9807 case FLOOR_DIV_EXPR:
9808 case ROUND_DIV_EXPR:
9809 case TRUNC_MOD_EXPR:
9810 case CEIL_MOD_EXPR:
9811 case ROUND_MOD_EXPR:
9812 case RDIV_EXPR:
9813 case EXACT_DIV_EXPR:
9814 case MIN_EXPR:
9815 case MAX_EXPR:
9816 case LSHIFT_EXPR:
9817 case RSHIFT_EXPR:
9818 case LROTATE_EXPR:
9819 case RROTATE_EXPR:
9820 case BIT_IOR_EXPR:
9821 case BIT_XOR_EXPR:
9822 case BIT_AND_EXPR:
9823 case TRUTH_XOR_EXPR:
9824 case LT_EXPR:
9825 case LE_EXPR:
9826 case GT_EXPR:
9827 case GE_EXPR:
9828 case EQ_EXPR:
9829 case NE_EXPR:
9830 case UNORDERED_EXPR:
9831 case ORDERED_EXPR:
9832 case UNLT_EXPR:
9833 case UNLE_EXPR:
9834 case UNGT_EXPR:
9835 case UNGE_EXPR:
9836 case UNEQ_EXPR:
9837 case LTGT_EXPR:
9838 case RANGE_EXPR:
9839 case COMPLEX_EXPR:
9840 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9841 non_constant_p, overflow_p);
9842 break;
9844 /* fold can introduce non-IF versions of these; still treat them as
9845 short-circuiting. */
9846 case TRUTH_AND_EXPR:
9847 case TRUTH_ANDIF_EXPR:
9848 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9849 boolean_true_node,
9850 allow_non_constant, addr,
9851 non_constant_p, overflow_p);
9852 break;
9854 case TRUTH_OR_EXPR:
9855 case TRUTH_ORIF_EXPR:
9856 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9857 boolean_false_node,
9858 allow_non_constant, addr,
9859 non_constant_p, overflow_p);
9860 break;
9862 case ARRAY_REF:
9863 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9864 non_constant_p, overflow_p);
9865 break;
9867 case COMPONENT_REF:
9868 if (is_overloaded_fn (t))
9870 /* We can only get here in checking mode via
9871 build_non_dependent_expr, because any expression that
9872 calls or takes the address of the function will have
9873 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9874 gcc_checking_assert (allow_non_constant || errorcount);
9875 *non_constant_p = true;
9876 return t;
9878 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9879 non_constant_p, overflow_p);
9880 break;
9882 case BIT_FIELD_REF:
9883 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9884 non_constant_p, overflow_p);
9885 break;
9887 case COND_EXPR:
9888 case VEC_COND_EXPR:
9889 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9890 non_constant_p, overflow_p);
9891 break;
9893 case CONSTRUCTOR:
9894 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9895 non_constant_p, overflow_p);
9896 break;
9898 case VEC_INIT_EXPR:
9899 /* We can get this in a defaulted constructor for a class with a
9900 non-static data member of array type. Either the initializer will
9901 be NULL, meaning default-initialization, or it will be an lvalue
9902 or xvalue of the same type, meaning direct-initialization from the
9903 corresponding member. */
9904 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9905 non_constant_p, overflow_p);
9906 break;
9908 case FMA_EXPR:
9909 case VEC_PERM_EXPR:
9910 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9911 non_constant_p, overflow_p);
9912 break;
9914 case CONVERT_EXPR:
9915 case VIEW_CONVERT_EXPR:
9916 case NOP_EXPR:
9918 tree oldop = TREE_OPERAND (t, 0);
9919 tree op = cxx_eval_constant_expression (call, oldop,
9920 allow_non_constant, addr,
9921 non_constant_p, overflow_p);
9922 if (*non_constant_p)
9923 return t;
9924 if (POINTER_TYPE_P (TREE_TYPE (t))
9925 && TREE_CODE (op) == INTEGER_CST
9926 && !integer_zerop (op))
9928 if (!allow_non_constant)
9929 error_at (EXPR_LOC_OR_LOC (t, input_location),
9930 "reinterpret_cast from integer to pointer");
9931 *non_constant_p = true;
9932 return t;
9934 if (op == oldop)
9935 /* We didn't fold at the top so we could check for ptr-int
9936 conversion. */
9937 return fold (t);
9938 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9939 /* Conversion of an out-of-range value has implementation-defined
9940 behavior; the language considers it different from arithmetic
9941 overflow, which is undefined. */
9942 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9943 TREE_OVERFLOW (r) = false;
9945 break;
9947 case EMPTY_CLASS_EXPR:
9948 /* This is good enough for a function argument that might not get
9949 used, and they can't do anything with it, so just return it. */
9950 return t;
9952 case LAMBDA_EXPR:
9953 case PREINCREMENT_EXPR:
9954 case POSTINCREMENT_EXPR:
9955 case PREDECREMENT_EXPR:
9956 case POSTDECREMENT_EXPR:
9957 case NEW_EXPR:
9958 case VEC_NEW_EXPR:
9959 case DELETE_EXPR:
9960 case VEC_DELETE_EXPR:
9961 case THROW_EXPR:
9962 case MODIFY_EXPR:
9963 case MODOP_EXPR:
9964 /* GCC internal stuff. */
9965 case VA_ARG_EXPR:
9966 case OBJ_TYPE_REF:
9967 case WITH_CLEANUP_EXPR:
9968 case STATEMENT_LIST:
9969 case BIND_EXPR:
9970 case NON_DEPENDENT_EXPR:
9971 case BASELINK:
9972 case EXPR_STMT:
9973 case OFFSET_REF:
9974 if (!allow_non_constant)
9975 error_at (EXPR_LOC_OR_LOC (t, input_location),
9976 "expression %qE is not a constant-expression", t);
9977 *non_constant_p = true;
9978 break;
9980 default:
9981 internal_error ("unexpected expression %qE of kind %s", t,
9982 get_tree_code_name (TREE_CODE (t)));
9983 *non_constant_p = true;
9984 break;
9987 if (r == error_mark_node)
9988 *non_constant_p = true;
9990 if (*non_constant_p)
9991 return t;
9992 else
9993 return r;
9996 static tree
9997 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9999 bool non_constant_p = false;
10000 bool overflow_p = false;
10001 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
10002 false, &non_constant_p, &overflow_p);
10004 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
10006 if (TREE_CODE (t) != CONSTRUCTOR
10007 && cp_has_mutable_p (TREE_TYPE (t)))
10009 /* We allow a mutable type if the original expression was a
10010 CONSTRUCTOR so that we can do aggregate initialization of
10011 constexpr variables. */
10012 if (!allow_non_constant)
10013 error ("%qT cannot be the type of a complete constant expression "
10014 "because it has mutable sub-objects", TREE_TYPE (t));
10015 non_constant_p = true;
10018 /* Technically we should check this for all subexpressions, but that
10019 runs into problems with our internal representation of pointer
10020 subtraction and the 5.19 rules are still in flux. */
10021 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
10022 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
10023 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
10025 if (!allow_non_constant)
10026 error ("conversion from pointer type %qT "
10027 "to arithmetic type %qT in a constant-expression",
10028 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
10029 non_constant_p = true;
10032 if (!non_constant_p && overflow_p)
10033 non_constant_p = true;
10035 if (non_constant_p && !allow_non_constant)
10036 return error_mark_node;
10037 else if (non_constant_p && TREE_CONSTANT (r))
10039 /* This isn't actually constant, so unset TREE_CONSTANT. */
10040 if (EXPR_P (r))
10041 r = copy_node (r);
10042 else if (TREE_CODE (r) == CONSTRUCTOR)
10043 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
10044 else
10045 r = build_nop (TREE_TYPE (r), r);
10046 TREE_CONSTANT (r) = false;
10048 else if (non_constant_p || r == t)
10049 return t;
10051 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
10053 if (TREE_CODE (t) == TARGET_EXPR
10054 && TARGET_EXPR_INITIAL (t) == r)
10055 return t;
10056 else
10058 r = get_target_expr (r);
10059 TREE_CONSTANT (r) = true;
10060 return r;
10063 else
10064 return r;
10067 /* Returns true if T is a valid subexpression of a constant expression,
10068 even if it isn't itself a constant expression. */
10070 bool
10071 is_sub_constant_expr (tree t)
10073 bool non_constant_p = false;
10074 bool overflow_p = false;
10075 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
10076 &overflow_p);
10077 return !non_constant_p && !overflow_p;
10080 /* If T represents a constant expression returns its reduced value.
10081 Otherwise return error_mark_node. If T is dependent, then
10082 return NULL. */
10084 tree
10085 cxx_constant_value (tree t)
10087 return cxx_eval_outermost_constant_expr (t, false);
10090 /* If T is a constant expression, returns its reduced value.
10091 Otherwise, if T does not have TREE_CONSTANT set, returns T.
10092 Otherwise, returns a version of T without TREE_CONSTANT. */
10094 tree
10095 maybe_constant_value (tree t)
10097 tree r;
10099 if (instantiation_dependent_expression_p (t)
10100 || type_unknown_p (t)
10101 || BRACE_ENCLOSED_INITIALIZER_P (t)
10102 || !potential_constant_expression (t))
10104 if (TREE_OVERFLOW_P (t))
10106 t = build_nop (TREE_TYPE (t), t);
10107 TREE_CONSTANT (t) = false;
10109 return t;
10112 r = cxx_eval_outermost_constant_expr (t, true);
10113 #ifdef ENABLE_CHECKING
10114 /* cp_tree_equal looks through NOPs, so allow them. */
10115 gcc_assert (r == t
10116 || CONVERT_EXPR_P (t)
10117 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10118 || !cp_tree_equal (r, t));
10119 #endif
10120 return r;
10123 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
10124 than wrapped in a TARGET_EXPR. */
10126 tree
10127 maybe_constant_init (tree t)
10129 if (TREE_CODE (t) == EXPR_STMT)
10130 t = TREE_OPERAND (t, 0);
10131 if (TREE_CODE (t) == CONVERT_EXPR
10132 && VOID_TYPE_P (TREE_TYPE (t)))
10133 t = TREE_OPERAND (t, 0);
10134 t = maybe_constant_value (t);
10135 if (TREE_CODE (t) == TARGET_EXPR)
10137 tree init = TARGET_EXPR_INITIAL (t);
10138 if (TREE_CODE (init) == CONSTRUCTOR)
10139 t = init;
10141 return t;
10144 #if 0
10145 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
10146 /* Return true if the object referred to by REF has automatic or thread
10147 local storage. */
10149 enum { ck_ok, ck_bad, ck_unknown };
10150 static int
10151 check_automatic_or_tls (tree ref)
10153 enum machine_mode mode;
10154 HOST_WIDE_INT bitsize, bitpos;
10155 tree offset;
10156 int volatilep = 0, unsignedp = 0;
10157 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
10158 &mode, &unsignedp, &volatilep, false);
10159 duration_kind dk;
10161 /* If there isn't a decl in the middle, we don't know the linkage here,
10162 and this isn't a constant expression anyway. */
10163 if (!DECL_P (decl))
10164 return ck_unknown;
10165 dk = decl_storage_duration (decl);
10166 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
10168 #endif
10170 /* Return true if T denotes a potentially constant expression. Issue
10171 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
10172 an lvalue-rvalue conversion is implied.
10174 C++0x [expr.const] used to say
10176 6 An expression is a potential constant expression if it is
10177 a constant expression where all occurrences of function
10178 parameters are replaced by arbitrary constant expressions
10179 of the appropriate type.
10181 2 A conditional expression is a constant expression unless it
10182 involves one of the following as a potentially evaluated
10183 subexpression (3.2), but subexpressions of logical AND (5.14),
10184 logical OR (5.15), and conditional (5.16) operations that are
10185 not evaluated are not considered. */
10187 static bool
10188 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
10190 enum { any = false, rval = true };
10191 int i;
10192 tree tmp;
10194 if (t == error_mark_node)
10195 return false;
10196 if (t == NULL_TREE)
10197 return true;
10198 if (TREE_THIS_VOLATILE (t))
10200 if (flags & tf_error)
10201 error ("expression %qE has side-effects", t);
10202 return false;
10204 if (CONSTANT_CLASS_P (t))
10205 return true;
10207 switch (TREE_CODE (t))
10209 case FUNCTION_DECL:
10210 case BASELINK:
10211 case TEMPLATE_DECL:
10212 case OVERLOAD:
10213 case TEMPLATE_ID_EXPR:
10214 case LABEL_DECL:
10215 case LABEL_EXPR:
10216 case CONST_DECL:
10217 case SIZEOF_EXPR:
10218 case ALIGNOF_EXPR:
10219 case OFFSETOF_EXPR:
10220 case NOEXCEPT_EXPR:
10221 case TEMPLATE_PARM_INDEX:
10222 case TRAIT_EXPR:
10223 case IDENTIFIER_NODE:
10224 case USERDEF_LITERAL:
10225 /* We can see a FIELD_DECL in a pointer-to-member expression. */
10226 case FIELD_DECL:
10227 case PARM_DECL:
10228 case USING_DECL:
10229 return true;
10231 case AGGR_INIT_EXPR:
10232 case CALL_EXPR:
10233 /* -- an invocation of a function other than a constexpr function
10234 or a constexpr constructor. */
10236 tree fun = get_function_named_in_call (t);
10237 const int nargs = call_expr_nargs (t);
10238 i = 0;
10240 if (is_overloaded_fn (fun))
10242 if (TREE_CODE (fun) == FUNCTION_DECL)
10244 if (builtin_valid_in_constant_expr_p (fun))
10245 return true;
10246 if (!DECL_DECLARED_CONSTEXPR_P (fun)
10247 /* Allow any built-in function; if the expansion
10248 isn't constant, we'll deal with that then. */
10249 && !is_builtin_fn (fun))
10251 if (flags & tf_error)
10253 error_at (EXPR_LOC_OR_LOC (t, input_location),
10254 "call to non-constexpr function %qD", fun);
10255 explain_invalid_constexpr_fn (fun);
10257 return false;
10259 /* A call to a non-static member function takes the address
10260 of the object as the first argument. But in a constant
10261 expression the address will be folded away, so look
10262 through it now. */
10263 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
10264 && !DECL_CONSTRUCTOR_P (fun))
10266 tree x = get_nth_callarg (t, 0);
10267 if (is_this_parameter (x))
10269 if (DECL_CONTEXT (x) == NULL_TREE
10270 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10272 if (flags & tf_error)
10273 sorry ("calling a member function of the "
10274 "object being constructed in a constant "
10275 "expression");
10276 return false;
10278 /* Otherwise OK. */;
10280 else if (!potential_constant_expression_1 (x, rval, flags))
10281 return false;
10282 i = 1;
10285 else
10287 if (!potential_constant_expression_1 (fun, true, flags))
10288 return false;
10289 fun = get_first_fn (fun);
10291 /* Skip initial arguments to base constructors. */
10292 if (DECL_BASE_CONSTRUCTOR_P (fun))
10293 i = num_artificial_parms_for (fun);
10294 fun = DECL_ORIGIN (fun);
10296 else
10298 if (potential_constant_expression_1 (fun, rval, flags))
10299 /* Might end up being a constant function pointer. */;
10300 else
10301 return false;
10303 for (; i < nargs; ++i)
10305 tree x = get_nth_callarg (t, i);
10306 if (!potential_constant_expression_1 (x, rval, flags))
10307 return false;
10309 return true;
10312 case NON_LVALUE_EXPR:
10313 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10314 -- an lvalue of integral type that refers to a non-volatile
10315 const variable or static data member initialized with
10316 constant expressions, or
10318 -- an lvalue of literal type that refers to non-volatile
10319 object defined with constexpr, or that refers to a
10320 sub-object of such an object; */
10321 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10323 case VAR_DECL:
10324 if (want_rval && !decl_constant_var_p (t)
10325 && !dependent_type_p (TREE_TYPE (t)))
10327 if (flags & tf_error)
10328 non_const_var_error (t);
10329 return false;
10331 return true;
10333 case NOP_EXPR:
10334 case CONVERT_EXPR:
10335 case VIEW_CONVERT_EXPR:
10336 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10337 may change to something more specific to type-punning (DR 1312). */
10339 tree from = TREE_OPERAND (t, 0);
10340 if (POINTER_TYPE_P (TREE_TYPE (t))
10341 && TREE_CODE (from) == INTEGER_CST
10342 && !integer_zerop (from))
10344 if (flags & tf_error)
10345 error_at (EXPR_LOC_OR_LOC (t, input_location),
10346 "reinterpret_cast from integer to pointer");
10347 return false;
10349 return (potential_constant_expression_1
10350 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10353 case ADDR_EXPR:
10354 /* -- a unary operator & that is applied to an lvalue that
10355 designates an object with thread or automatic storage
10356 duration; */
10357 t = TREE_OPERAND (t, 0);
10359 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
10360 /* A pointer-to-member constant. */
10361 return true;
10363 #if 0
10364 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10365 any checking here, as we might dereference the pointer later. If
10366 we remove this code, also remove check_automatic_or_tls. */
10367 i = check_automatic_or_tls (t);
10368 if (i == ck_ok)
10369 return true;
10370 if (i == ck_bad)
10372 if (flags & tf_error)
10373 error ("address-of an object %qE with thread local or "
10374 "automatic storage is not a constant expression", t);
10375 return false;
10377 #endif
10378 return potential_constant_expression_1 (t, any, flags);
10380 case COMPONENT_REF:
10381 case BIT_FIELD_REF:
10382 case ARROW_EXPR:
10383 case OFFSET_REF:
10384 /* -- a class member access unless its postfix-expression is
10385 of literal type or of pointer to literal type. */
10386 /* This test would be redundant, as it follows from the
10387 postfix-expression being a potential constant expression. */
10388 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10389 want_rval, flags);
10391 case EXPR_PACK_EXPANSION:
10392 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10393 want_rval, flags);
10395 case INDIRECT_REF:
10397 tree x = TREE_OPERAND (t, 0);
10398 STRIP_NOPS (x);
10399 if (is_this_parameter (x))
10401 if (DECL_CONTEXT (x)
10402 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10404 if (flags & tf_error)
10405 error ("use of %<this%> in a constant expression");
10406 return false;
10408 if (want_rval && DECL_CONTEXT (x)
10409 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10411 if (flags & tf_error)
10412 sorry ("use of the value of the object being constructed "
10413 "in a constant expression");
10414 return false;
10416 return true;
10418 return potential_constant_expression_1 (x, rval, flags);
10421 case LAMBDA_EXPR:
10422 case DYNAMIC_CAST_EXPR:
10423 case PSEUDO_DTOR_EXPR:
10424 case PREINCREMENT_EXPR:
10425 case POSTINCREMENT_EXPR:
10426 case PREDECREMENT_EXPR:
10427 case POSTDECREMENT_EXPR:
10428 case NEW_EXPR:
10429 case VEC_NEW_EXPR:
10430 case DELETE_EXPR:
10431 case VEC_DELETE_EXPR:
10432 case THROW_EXPR:
10433 case MODIFY_EXPR:
10434 case MODOP_EXPR:
10435 case OMP_ATOMIC:
10436 case OMP_ATOMIC_READ:
10437 case OMP_ATOMIC_CAPTURE_OLD:
10438 case OMP_ATOMIC_CAPTURE_NEW:
10439 /* GCC internal stuff. */
10440 case VA_ARG_EXPR:
10441 case OBJ_TYPE_REF:
10442 case WITH_CLEANUP_EXPR:
10443 case CLEANUP_POINT_EXPR:
10444 case MUST_NOT_THROW_EXPR:
10445 case TRY_CATCH_EXPR:
10446 case STATEMENT_LIST:
10447 /* Don't bother trying to define a subset of statement-expressions to
10448 be constant-expressions, at least for now. */
10449 case STMT_EXPR:
10450 case EXPR_STMT:
10451 case BIND_EXPR:
10452 case TRANSACTION_EXPR:
10453 case IF_STMT:
10454 case DO_STMT:
10455 case FOR_STMT:
10456 case WHILE_STMT:
10457 case DECL_EXPR:
10458 if (flags & tf_error)
10459 error ("expression %qE is not a constant-expression", t);
10460 return false;
10462 case TYPEID_EXPR:
10463 /* -- a typeid expression whose operand is of polymorphic
10464 class type; */
10466 tree e = TREE_OPERAND (t, 0);
10467 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10468 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10470 if (flags & tf_error)
10471 error ("typeid-expression is not a constant expression "
10472 "because %qE is of polymorphic type", e);
10473 return false;
10475 return true;
10478 case MINUS_EXPR:
10479 /* -- a subtraction where both operands are pointers. */
10480 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10481 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10483 if (flags & tf_error)
10484 error ("difference of two pointer expressions is not "
10485 "a constant expression");
10486 return false;
10488 want_rval = true;
10489 goto binary;
10491 case LT_EXPR:
10492 case LE_EXPR:
10493 case GT_EXPR:
10494 case GE_EXPR:
10495 case EQ_EXPR:
10496 case NE_EXPR:
10497 /* -- a relational or equality operator where at least
10498 one of the operands is a pointer. */
10499 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10500 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10502 if (flags & tf_error)
10503 error ("pointer comparison expression is not a "
10504 "constant expression");
10505 return false;
10507 want_rval = true;
10508 goto binary;
10510 case BIT_NOT_EXPR:
10511 /* A destructor. */
10512 if (TYPE_P (TREE_OPERAND (t, 0)))
10513 return true;
10514 /* else fall through. */
10516 case REALPART_EXPR:
10517 case IMAGPART_EXPR:
10518 case CONJ_EXPR:
10519 case SAVE_EXPR:
10520 case FIX_TRUNC_EXPR:
10521 case FLOAT_EXPR:
10522 case NEGATE_EXPR:
10523 case ABS_EXPR:
10524 case TRUTH_NOT_EXPR:
10525 case FIXED_CONVERT_EXPR:
10526 case UNARY_PLUS_EXPR:
10527 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10528 flags);
10530 case CAST_EXPR:
10531 case CONST_CAST_EXPR:
10532 case STATIC_CAST_EXPR:
10533 case REINTERPRET_CAST_EXPR:
10534 case IMPLICIT_CONV_EXPR:
10535 if (cxx_dialect < cxx11
10536 && !dependent_type_p (TREE_TYPE (t))
10537 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10538 /* In C++98, a conversion to non-integral type can't be part of a
10539 constant expression. */
10541 if (flags & tf_error)
10542 error ("cast to non-integral type %qT in a constant expression",
10543 TREE_TYPE (t));
10544 return false;
10547 return (potential_constant_expression_1
10548 (TREE_OPERAND (t, 0),
10549 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10551 case PAREN_EXPR:
10552 case NON_DEPENDENT_EXPR:
10553 /* For convenience. */
10554 case RETURN_EXPR:
10555 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10556 want_rval, flags);
10558 case SCOPE_REF:
10559 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10560 want_rval, flags);
10562 case TARGET_EXPR:
10563 if (!literal_type_p (TREE_TYPE (t)))
10565 if (flags & tf_error)
10567 error ("temporary of non-literal type %qT in a "
10568 "constant expression", TREE_TYPE (t));
10569 explain_non_literal_class (TREE_TYPE (t));
10571 return false;
10573 case INIT_EXPR:
10574 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10575 rval, flags);
10577 case CONSTRUCTOR:
10579 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10580 constructor_elt *ce;
10581 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10582 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10583 return false;
10584 return true;
10587 case TREE_LIST:
10589 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10590 || DECL_P (TREE_PURPOSE (t)));
10591 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10592 flags))
10593 return false;
10594 if (TREE_CHAIN (t) == NULL_TREE)
10595 return true;
10596 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10597 flags);
10600 case TRUNC_DIV_EXPR:
10601 case CEIL_DIV_EXPR:
10602 case FLOOR_DIV_EXPR:
10603 case ROUND_DIV_EXPR:
10604 case TRUNC_MOD_EXPR:
10605 case CEIL_MOD_EXPR:
10606 case ROUND_MOD_EXPR:
10608 tree denom = TREE_OPERAND (t, 1);
10609 if (!potential_constant_expression_1 (denom, rval, flags))
10610 return false;
10611 /* We can't call cxx_eval_outermost_constant_expr on an expression
10612 that hasn't been through fold_non_dependent_expr yet. */
10613 if (!processing_template_decl)
10614 denom = cxx_eval_outermost_constant_expr (denom, true);
10615 if (integer_zerop (denom))
10617 if (flags & tf_error)
10618 error ("division by zero is not a constant-expression");
10619 return false;
10621 else
10623 want_rval = true;
10624 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10625 want_rval, flags);
10629 case COMPOUND_EXPR:
10631 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10632 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10633 introduced by build_call_a. */
10634 tree op0 = TREE_OPERAND (t, 0);
10635 tree op1 = TREE_OPERAND (t, 1);
10636 STRIP_NOPS (op1);
10637 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10638 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10639 return potential_constant_expression_1 (op0, want_rval, flags);
10640 else
10641 goto binary;
10644 /* If the first operand is the non-short-circuit constant, look at
10645 the second operand; otherwise we only care about the first one for
10646 potentiality. */
10647 case TRUTH_AND_EXPR:
10648 case TRUTH_ANDIF_EXPR:
10649 tmp = boolean_true_node;
10650 goto truth;
10651 case TRUTH_OR_EXPR:
10652 case TRUTH_ORIF_EXPR:
10653 tmp = boolean_false_node;
10654 truth:
10656 tree op = TREE_OPERAND (t, 0);
10657 if (!potential_constant_expression_1 (op, rval, flags))
10658 return false;
10659 if (!processing_template_decl)
10660 op = cxx_eval_outermost_constant_expr (op, true);
10661 if (tree_int_cst_equal (op, tmp))
10662 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10663 else
10664 return true;
10667 case PLUS_EXPR:
10668 case MULT_EXPR:
10669 case POINTER_PLUS_EXPR:
10670 case RDIV_EXPR:
10671 case EXACT_DIV_EXPR:
10672 case MIN_EXPR:
10673 case MAX_EXPR:
10674 case LSHIFT_EXPR:
10675 case RSHIFT_EXPR:
10676 case LROTATE_EXPR:
10677 case RROTATE_EXPR:
10678 case BIT_IOR_EXPR:
10679 case BIT_XOR_EXPR:
10680 case BIT_AND_EXPR:
10681 case TRUTH_XOR_EXPR:
10682 case UNORDERED_EXPR:
10683 case ORDERED_EXPR:
10684 case UNLT_EXPR:
10685 case UNLE_EXPR:
10686 case UNGT_EXPR:
10687 case UNGE_EXPR:
10688 case UNEQ_EXPR:
10689 case LTGT_EXPR:
10690 case RANGE_EXPR:
10691 case COMPLEX_EXPR:
10692 want_rval = true;
10693 /* Fall through. */
10694 case ARRAY_REF:
10695 case ARRAY_RANGE_REF:
10696 case MEMBER_REF:
10697 case DOTSTAR_EXPR:
10698 binary:
10699 for (i = 0; i < 2; ++i)
10700 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10701 want_rval, flags))
10702 return false;
10703 return true;
10705 case CILK_SYNC_STMT:
10706 case CILK_SPAWN_STMT:
10707 case ARRAY_NOTATION_REF:
10708 return false;
10710 case FMA_EXPR:
10711 case VEC_PERM_EXPR:
10712 for (i = 0; i < 3; ++i)
10713 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10714 true, flags))
10715 return false;
10716 return true;
10718 case COND_EXPR:
10719 case VEC_COND_EXPR:
10720 /* If the condition is a known constant, we know which of the legs we
10721 care about; otherwise we only require that the condition and
10722 either of the legs be potentially constant. */
10723 tmp = TREE_OPERAND (t, 0);
10724 if (!potential_constant_expression_1 (tmp, rval, flags))
10725 return false;
10726 if (!processing_template_decl)
10727 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10728 if (integer_zerop (tmp))
10729 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10730 want_rval, flags);
10731 else if (TREE_CODE (tmp) == INTEGER_CST)
10732 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10733 want_rval, flags);
10734 for (i = 1; i < 3; ++i)
10735 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10736 want_rval, tf_none))
10737 return true;
10738 if (flags & tf_error)
10739 error ("expression %qE is not a constant-expression", t);
10740 return false;
10742 case VEC_INIT_EXPR:
10743 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10744 return true;
10745 if (flags & tf_error)
10747 error ("non-constant array initialization");
10748 diagnose_non_constexpr_vec_init (t);
10750 return false;
10752 default:
10753 if (objc_is_property_ref (t))
10754 return false;
10756 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10757 gcc_unreachable();
10758 return false;
10762 /* The main entry point to the above. */
10764 bool
10765 potential_constant_expression (tree t)
10767 return potential_constant_expression_1 (t, false, tf_none);
10770 /* As above, but require a constant rvalue. */
10772 bool
10773 potential_rvalue_constant_expression (tree t)
10775 return potential_constant_expression_1 (t, true, tf_none);
10778 /* Like above, but complain about non-constant expressions. */
10780 bool
10781 require_potential_constant_expression (tree t)
10783 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10786 /* Cross product of the above. */
10788 bool
10789 require_potential_rvalue_constant_expression (tree t)
10791 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10794 /* Insert the deduced return type for an auto function. */
10796 void
10797 apply_deduced_return_type (tree fco, tree return_type)
10799 tree result;
10801 if (return_type == error_mark_node)
10802 return;
10804 if (LAMBDA_FUNCTION_P (fco))
10806 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10807 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10810 if (DECL_CONV_FN_P (fco))
10811 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10813 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10815 result = DECL_RESULT (fco);
10816 if (result == NULL_TREE)
10817 return;
10818 if (TREE_TYPE (result) == return_type)
10819 return;
10821 /* We already have a DECL_RESULT from start_preparsed_function.
10822 Now we need to redo the work it and allocate_struct_function
10823 did to reflect the new type. */
10824 gcc_assert (current_function_decl == fco);
10825 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10826 TYPE_MAIN_VARIANT (return_type));
10827 DECL_ARTIFICIAL (result) = 1;
10828 DECL_IGNORED_P (result) = 1;
10829 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10830 result);
10832 DECL_RESULT (fco) = result;
10834 if (!processing_template_decl)
10836 if (!VOID_TYPE_P (TREE_TYPE (result)))
10837 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
10838 bool aggr = aggregate_value_p (result, fco);
10839 #ifdef PCC_STATIC_STRUCT_RETURN
10840 cfun->returns_pcc_struct = aggr;
10841 #endif
10842 cfun->returns_struct = aggr;
10847 /* DECL is a local variable or parameter from the surrounding scope of a
10848 lambda-expression. Returns the decltype for a use of the capture field
10849 for DECL even if it hasn't been captured yet. */
10851 static tree
10852 capture_decltype (tree decl)
10854 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10855 /* FIXME do lookup instead of list walk? */
10856 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10857 tree type;
10859 if (cap)
10860 type = TREE_TYPE (TREE_PURPOSE (cap));
10861 else
10862 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10864 case CPLD_NONE:
10865 error ("%qD is not captured", decl);
10866 return error_mark_node;
10868 case CPLD_COPY:
10869 type = TREE_TYPE (decl);
10870 if (TREE_CODE (type) == REFERENCE_TYPE
10871 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10872 type = TREE_TYPE (type);
10873 break;
10875 case CPLD_REFERENCE:
10876 type = TREE_TYPE (decl);
10877 if (TREE_CODE (type) != REFERENCE_TYPE)
10878 type = build_reference_type (TREE_TYPE (decl));
10879 break;
10881 default:
10882 gcc_unreachable ();
10885 if (TREE_CODE (type) != REFERENCE_TYPE)
10887 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10888 type = cp_build_qualified_type (type, (cp_type_quals (type)
10889 |TYPE_QUAL_CONST));
10890 type = build_reference_type (type);
10892 return type;
10895 #include "gt-cp-semantics.h"