Merge from trunk:
[official-gcc.git] / main / gcc / cp / semantics.c
blobfaa115a7f8ea739960f8a5c08bfc15b2490cdb9d
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 "pointer-set.h"
48 #include "hash-table.h"
49 #include "gimplify.h"
50 #include "bitmap.h"
51 #include "omp-low.h"
52 #include "builtins.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 < cxx1y)
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 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2422 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2423 POSTDECREMENT_EXPR.) */
2425 tree
2426 finish_increment_expr (tree expr, enum tree_code code)
2428 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2431 /* Finish a use of `this'. Returns an expression for `this'. */
2433 tree
2434 finish_this_expr (void)
2436 tree result;
2438 if (current_class_ptr)
2440 tree type = TREE_TYPE (current_class_ref);
2442 /* In a lambda expression, 'this' refers to the captured 'this'. */
2443 if (LAMBDA_TYPE_P (type))
2444 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2445 else
2446 result = current_class_ptr;
2448 else if (current_function_decl
2449 && DECL_STATIC_FUNCTION_P (current_function_decl))
2451 error ("%<this%> is unavailable for static member functions");
2452 result = error_mark_node;
2454 else
2456 if (current_function_decl)
2457 error ("invalid use of %<this%> in non-member function");
2458 else
2459 error ("invalid use of %<this%> at top level");
2460 result = error_mark_node;
2463 /* The keyword 'this' is a prvalue expression. */
2464 result = rvalue (result);
2466 return result;
2469 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2470 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2471 the TYPE for the type given. If SCOPE is non-NULL, the expression
2472 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2474 tree
2475 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2476 location_t loc)
2478 if (object == error_mark_node || destructor == error_mark_node)
2479 return error_mark_node;
2481 gcc_assert (TYPE_P (destructor));
2483 if (!processing_template_decl)
2485 if (scope == error_mark_node)
2487 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2488 return error_mark_node;
2490 if (is_auto (destructor))
2491 destructor = TREE_TYPE (object);
2492 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2494 error_at (loc,
2495 "qualified type %qT does not match destructor name ~%qT",
2496 scope, destructor);
2497 return error_mark_node;
2501 /* [expr.pseudo] says both:
2503 The type designated by the pseudo-destructor-name shall be
2504 the same as the object type.
2506 and:
2508 The cv-unqualified versions of the object type and of the
2509 type designated by the pseudo-destructor-name shall be the
2510 same type.
2512 We implement the more generous second sentence, since that is
2513 what most other compilers do. */
2514 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2515 destructor))
2517 error_at (loc, "%qE is not of type %qT", object, destructor);
2518 return error_mark_node;
2522 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2523 scope, destructor);
2526 /* Finish an expression of the form CODE EXPR. */
2528 tree
2529 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2530 tsubst_flags_t complain)
2532 tree result = build_x_unary_op (loc, code, expr, complain);
2533 if ((complain & tf_warning)
2534 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2535 overflow_warning (input_location, result);
2537 return result;
2540 /* Finish a compound-literal expression. TYPE is the type to which
2541 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2543 tree
2544 finish_compound_literal (tree type, tree compound_literal,
2545 tsubst_flags_t complain)
2547 if (type == error_mark_node)
2548 return error_mark_node;
2550 if (TREE_CODE (type) == REFERENCE_TYPE)
2552 compound_literal
2553 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2554 complain);
2555 return cp_build_c_cast (type, compound_literal, complain);
2558 if (!TYPE_OBJ_P (type))
2560 if (complain & tf_error)
2561 error ("compound literal of non-object type %qT", type);
2562 return error_mark_node;
2565 if (processing_template_decl)
2567 TREE_TYPE (compound_literal) = type;
2568 /* Mark the expression as a compound literal. */
2569 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2570 return compound_literal;
2573 type = complete_type (type);
2575 if (TYPE_NON_AGGREGATE_CLASS (type))
2577 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2578 everywhere that deals with function arguments would be a pain, so
2579 just wrap it in a TREE_LIST. The parser set a flag so we know
2580 that it came from T{} rather than T({}). */
2581 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2582 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2583 return build_functional_cast (type, compound_literal, complain);
2586 if (TREE_CODE (type) == ARRAY_TYPE
2587 && check_array_initializer (NULL_TREE, type, compound_literal))
2588 return error_mark_node;
2589 compound_literal = reshape_init (type, compound_literal, complain);
2590 if (SCALAR_TYPE_P (type)
2591 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2592 && (complain & tf_warning_or_error))
2593 check_narrowing (type, compound_literal);
2594 if (TREE_CODE (type) == ARRAY_TYPE
2595 && TYPE_DOMAIN (type) == NULL_TREE)
2597 cp_complete_array_type_or_error (&type, compound_literal,
2598 false, complain);
2599 if (type == error_mark_node)
2600 return error_mark_node;
2602 compound_literal = digest_init (type, compound_literal, complain);
2603 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2604 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2605 /* Put static/constant array temporaries in static variables, but always
2606 represent class temporaries with TARGET_EXPR so we elide copies. */
2607 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2608 && TREE_CODE (type) == ARRAY_TYPE
2609 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2610 && initializer_constant_valid_p (compound_literal, type))
2612 tree decl = create_temporary_var (type);
2613 DECL_INITIAL (decl) = compound_literal;
2614 TREE_STATIC (decl) = 1;
2615 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2617 /* 5.19 says that a constant expression can include an
2618 lvalue-rvalue conversion applied to "a glvalue of literal type
2619 that refers to a non-volatile temporary object initialized
2620 with a constant expression". Rather than try to communicate
2621 that this VAR_DECL is a temporary, just mark it constexpr. */
2622 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2623 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2624 TREE_CONSTANT (decl) = true;
2626 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2627 decl = pushdecl_top_level (decl);
2628 DECL_NAME (decl) = make_anon_name ();
2629 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2631 /* Capture the current module info for statics. */
2632 if (L_IPO_COMP_MODE)
2633 varpool_node::get_create (decl);
2635 /* Make sure the destructor is callable. */
2636 tree clean = cxx_maybe_build_cleanup (decl, complain);
2637 if (clean == error_mark_node)
2638 return error_mark_node;
2640 return decl;
2642 else
2643 return get_target_expr_sfinae (compound_literal, complain);
2646 /* Return the declaration for the function-name variable indicated by
2647 ID. */
2649 tree
2650 finish_fname (tree id)
2652 tree decl;
2654 decl = fname_decl (input_location, C_RID_CODE (id), id);
2655 if (processing_template_decl && current_function_decl
2656 && decl != error_mark_node)
2657 decl = DECL_NAME (decl);
2658 return decl;
2661 /* Finish a translation unit. */
2663 void
2664 finish_translation_unit (void)
2666 /* In case there were missing closebraces,
2667 get us back to the global binding level. */
2668 pop_everything ();
2669 while (current_namespace != global_namespace)
2670 pop_namespace ();
2672 /* Do file scope __FUNCTION__ et al. */
2673 finish_fname_decls ();
2676 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2677 Returns the parameter. */
2679 tree
2680 finish_template_type_parm (tree aggr, tree identifier)
2682 if (aggr != class_type_node)
2684 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2685 aggr = class_type_node;
2688 return build_tree_list (aggr, identifier);
2691 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2692 Returns the parameter. */
2694 tree
2695 finish_template_template_parm (tree aggr, tree identifier)
2697 tree decl = build_decl (input_location,
2698 TYPE_DECL, identifier, NULL_TREE);
2699 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2700 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2701 DECL_TEMPLATE_RESULT (tmpl) = decl;
2702 DECL_ARTIFICIAL (decl) = 1;
2703 end_template_decl ();
2705 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2707 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2708 /*is_primary=*/true, /*is_partial=*/false,
2709 /*is_friend=*/0);
2711 return finish_template_type_parm (aggr, tmpl);
2714 /* ARGUMENT is the default-argument value for a template template
2715 parameter. If ARGUMENT is invalid, issue error messages and return
2716 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2718 tree
2719 check_template_template_default_arg (tree argument)
2721 if (TREE_CODE (argument) != TEMPLATE_DECL
2722 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2723 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2725 if (TREE_CODE (argument) == TYPE_DECL)
2726 error ("invalid use of type %qT as a default value for a template "
2727 "template-parameter", TREE_TYPE (argument));
2728 else
2729 error ("invalid default argument for a template template parameter");
2730 return error_mark_node;
2733 return argument;
2736 /* Begin a class definition, as indicated by T. */
2738 tree
2739 begin_class_definition (tree t)
2741 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2742 return error_mark_node;
2744 if (processing_template_parmlist)
2746 error ("definition of %q#T inside template parameter list", t);
2747 return error_mark_node;
2750 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2751 are passed the same as decimal scalar types. */
2752 if (TREE_CODE (t) == RECORD_TYPE
2753 && !processing_template_decl)
2755 tree ns = TYPE_CONTEXT (t);
2756 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2757 && DECL_CONTEXT (ns) == std_node
2758 && DECL_NAME (ns)
2759 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2761 const char *n = TYPE_NAME_STRING (t);
2762 if ((strcmp (n, "decimal32") == 0)
2763 || (strcmp (n, "decimal64") == 0)
2764 || (strcmp (n, "decimal128") == 0))
2765 TYPE_TRANSPARENT_AGGR (t) = 1;
2769 /* A non-implicit typename comes from code like:
2771 template <typename T> struct A {
2772 template <typename U> struct A<T>::B ...
2774 This is erroneous. */
2775 else if (TREE_CODE (t) == TYPENAME_TYPE)
2777 error ("invalid definition of qualified type %qT", t);
2778 t = error_mark_node;
2781 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2783 t = make_class_type (RECORD_TYPE);
2784 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2787 if (TYPE_BEING_DEFINED (t))
2789 t = make_class_type (TREE_CODE (t));
2790 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2792 maybe_process_partial_specialization (t);
2793 pushclass (t);
2794 TYPE_BEING_DEFINED (t) = 1;
2795 class_binding_level->defining_class_p = 1;
2797 if (flag_pack_struct)
2799 tree v;
2800 TYPE_PACKED (t) = 1;
2801 /* Even though the type is being defined for the first time
2802 here, there might have been a forward declaration, so there
2803 might be cv-qualified variants of T. */
2804 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2805 TYPE_PACKED (v) = 1;
2807 /* Reset the interface data, at the earliest possible
2808 moment, as it might have been set via a class foo;
2809 before. */
2810 if (! TYPE_ANONYMOUS_P (t))
2812 struct c_fileinfo *finfo = \
2813 get_fileinfo (LOCATION_FILE (input_location));
2814 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2815 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2816 (t, finfo->interface_unknown);
2818 reset_specialization();
2820 /* Make a declaration for this class in its own scope. */
2821 build_self_reference ();
2823 return t;
2826 /* Finish the member declaration given by DECL. */
2828 void
2829 finish_member_declaration (tree decl)
2831 if (decl == error_mark_node || decl == NULL_TREE)
2832 return;
2834 if (decl == void_type_node)
2835 /* The COMPONENT was a friend, not a member, and so there's
2836 nothing for us to do. */
2837 return;
2839 /* We should see only one DECL at a time. */
2840 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2842 /* Set up access control for DECL. */
2843 TREE_PRIVATE (decl)
2844 = (current_access_specifier == access_private_node);
2845 TREE_PROTECTED (decl)
2846 = (current_access_specifier == access_protected_node);
2847 if (TREE_CODE (decl) == TEMPLATE_DECL)
2849 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2850 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2853 /* Mark the DECL as a member of the current class, unless it's
2854 a member of an enumeration. */
2855 if (TREE_CODE (decl) != CONST_DECL)
2856 DECL_CONTEXT (decl) = current_class_type;
2858 /* Check for bare parameter packs in the member variable declaration. */
2859 if (TREE_CODE (decl) == FIELD_DECL)
2861 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2862 TREE_TYPE (decl) = error_mark_node;
2863 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2864 DECL_ATTRIBUTES (decl) = NULL_TREE;
2867 /* [dcl.link]
2869 A C language linkage is ignored for the names of class members
2870 and the member function type of class member functions. */
2871 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2872 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2874 /* Put functions on the TYPE_METHODS list and everything else on the
2875 TYPE_FIELDS list. Note that these are built up in reverse order.
2876 We reverse them (to obtain declaration order) in finish_struct. */
2877 if (DECL_DECLARES_FUNCTION_P (decl))
2879 /* We also need to add this function to the
2880 CLASSTYPE_METHOD_VEC. */
2881 if (add_method (current_class_type, decl, NULL_TREE))
2883 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2884 TYPE_METHODS (current_class_type) = decl;
2886 maybe_add_class_template_decl_list (current_class_type, decl,
2887 /*friend_p=*/0);
2890 /* Enter the DECL into the scope of the class, if the class
2891 isn't a closure (whose fields are supposed to be unnamed). */
2892 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2893 || pushdecl_class_level (decl))
2895 if (TREE_CODE (decl) == USING_DECL)
2897 /* For now, ignore class-scope USING_DECLS, so that
2898 debugging backends do not see them. */
2899 DECL_IGNORED_P (decl) = 1;
2902 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2903 go at the beginning. The reason is that lookup_field_1
2904 searches the list in order, and we want a field name to
2905 override a type name so that the "struct stat hack" will
2906 work. In particular:
2908 struct S { enum E { }; int E } s;
2909 s.E = 3;
2911 is valid. In addition, the FIELD_DECLs must be maintained in
2912 declaration order so that class layout works as expected.
2913 However, we don't need that order until class layout, so we
2914 save a little time by putting FIELD_DECLs on in reverse order
2915 here, and then reversing them in finish_struct_1. (We could
2916 also keep a pointer to the correct insertion points in the
2917 list.) */
2919 if (TREE_CODE (decl) == TYPE_DECL)
2920 TYPE_FIELDS (current_class_type)
2921 = chainon (TYPE_FIELDS (current_class_type), decl);
2922 else
2924 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2925 TYPE_FIELDS (current_class_type) = decl;
2928 maybe_add_class_template_decl_list (current_class_type, decl,
2929 /*friend_p=*/0);
2932 if (pch_file)
2933 note_decl_for_pch (decl);
2936 /* DECL has been declared while we are building a PCH file. Perform
2937 actions that we might normally undertake lazily, but which can be
2938 performed now so that they do not have to be performed in
2939 translation units which include the PCH file. */
2941 void
2942 note_decl_for_pch (tree decl)
2944 gcc_assert (pch_file);
2946 /* There's a good chance that we'll have to mangle names at some
2947 point, even if only for emission in debugging information. */
2948 if (VAR_OR_FUNCTION_DECL_P (decl)
2949 && !processing_template_decl)
2950 mangle_decl (decl);
2953 /* Finish processing a complete template declaration. The PARMS are
2954 the template parameters. */
2956 void
2957 finish_template_decl (tree parms)
2959 if (parms)
2960 end_template_decl ();
2961 else
2962 end_specialization ();
2965 /* Finish processing a template-id (which names a type) of the form
2966 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2967 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2968 the scope of template-id indicated. */
2970 tree
2971 finish_template_type (tree name, tree args, int entering_scope)
2973 tree type;
2975 type = lookup_template_class (name, args,
2976 NULL_TREE, NULL_TREE, entering_scope,
2977 tf_warning_or_error | tf_user);
2978 if (type == error_mark_node)
2979 return type;
2980 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2981 return TYPE_STUB_DECL (type);
2982 else
2983 return TYPE_NAME (type);
2986 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2987 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2988 BASE_CLASS, or NULL_TREE if an error occurred. The
2989 ACCESS_SPECIFIER is one of
2990 access_{default,public,protected_private}_node. For a virtual base
2991 we set TREE_TYPE. */
2993 tree
2994 finish_base_specifier (tree base, tree access, bool virtual_p)
2996 tree result;
2998 if (base == error_mark_node)
3000 error ("invalid base-class specification");
3001 result = NULL_TREE;
3003 else if (! MAYBE_CLASS_TYPE_P (base))
3005 error ("%qT is not a class type", base);
3006 result = NULL_TREE;
3008 else
3010 if (cp_type_quals (base) != 0)
3012 /* DR 484: Can a base-specifier name a cv-qualified
3013 class type? */
3014 base = TYPE_MAIN_VARIANT (base);
3016 result = build_tree_list (access, base);
3017 if (virtual_p)
3018 TREE_TYPE (result) = integer_type_node;
3021 return result;
3024 /* If FNS is a member function, a set of member functions, or a
3025 template-id referring to one or more member functions, return a
3026 BASELINK for FNS, incorporating the current access context.
3027 Otherwise, return FNS unchanged. */
3029 tree
3030 baselink_for_fns (tree fns)
3032 tree scope;
3033 tree cl;
3035 if (BASELINK_P (fns)
3036 || error_operand_p (fns))
3037 return fns;
3039 scope = ovl_scope (fns);
3040 if (!CLASS_TYPE_P (scope))
3041 return fns;
3043 cl = currently_open_derived_class (scope);
3044 if (!cl)
3045 cl = scope;
3046 cl = TYPE_BINFO (cl);
3047 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3050 /* Returns true iff DECL is a variable from a function outside
3051 the current one. */
3053 static bool
3054 outer_var_p (tree decl)
3056 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3057 && DECL_FUNCTION_SCOPE_P (decl)
3058 && (DECL_CONTEXT (decl) != current_function_decl
3059 || parsing_nsdmi ()));
3062 /* As above, but also checks that DECL is automatic. */
3064 static bool
3065 outer_automatic_var_p (tree decl)
3067 return (outer_var_p (decl)
3068 && !TREE_STATIC (decl));
3071 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3072 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3073 if non-NULL, is the type or namespace used to explicitly qualify
3074 ID_EXPRESSION. DECL is the entity to which that name has been
3075 resolved.
3077 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3078 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3079 be set to true if this expression isn't permitted in a
3080 constant-expression, but it is otherwise not set by this function.
3081 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3082 constant-expression, but a non-constant expression is also
3083 permissible.
3085 DONE is true if this expression is a complete postfix-expression;
3086 it is false if this expression is followed by '->', '[', '(', etc.
3087 ADDRESS_P is true iff this expression is the operand of '&'.
3088 TEMPLATE_P is true iff the qualified-id was of the form
3089 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3090 appears as a template argument.
3092 If an error occurs, and it is the kind of error that might cause
3093 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3094 is the caller's responsibility to issue the message. *ERROR_MSG
3095 will be a string with static storage duration, so the caller need
3096 not "free" it.
3098 Return an expression for the entity, after issuing appropriate
3099 diagnostics. This function is also responsible for transforming a
3100 reference to a non-static member into a COMPONENT_REF that makes
3101 the use of "this" explicit.
3103 Upon return, *IDK will be filled in appropriately. */
3104 tree
3105 finish_id_expression (tree id_expression,
3106 tree decl,
3107 tree scope,
3108 cp_id_kind *idk,
3109 bool integral_constant_expression_p,
3110 bool allow_non_integral_constant_expression_p,
3111 bool *non_integral_constant_expression_p,
3112 bool template_p,
3113 bool done,
3114 bool address_p,
3115 bool template_arg_p,
3116 const char **error_msg,
3117 location_t location)
3119 decl = strip_using_decl (decl);
3121 /* Initialize the output parameters. */
3122 *idk = CP_ID_KIND_NONE;
3123 *error_msg = NULL;
3125 if (id_expression == error_mark_node)
3126 return error_mark_node;
3127 /* If we have a template-id, then no further lookup is
3128 required. If the template-id was for a template-class, we
3129 will sometimes have a TYPE_DECL at this point. */
3130 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3131 || TREE_CODE (decl) == TYPE_DECL)
3133 /* Look up the name. */
3134 else
3136 if (decl == error_mark_node)
3138 /* Name lookup failed. */
3139 if (scope
3140 && (!TYPE_P (scope)
3141 || (!dependent_type_p (scope)
3142 && !(identifier_p (id_expression)
3143 && IDENTIFIER_TYPENAME_P (id_expression)
3144 && dependent_type_p (TREE_TYPE (id_expression))))))
3146 /* If the qualifying type is non-dependent (and the name
3147 does not name a conversion operator to a dependent
3148 type), issue an error. */
3149 qualified_name_lookup_error (scope, id_expression, decl, location);
3150 return error_mark_node;
3152 else if (!scope)
3154 /* It may be resolved via Koenig lookup. */
3155 *idk = CP_ID_KIND_UNQUALIFIED;
3156 return id_expression;
3158 else
3159 decl = id_expression;
3161 /* If DECL is a variable that would be out of scope under
3162 ANSI/ISO rules, but in scope in the ARM, name lookup
3163 will succeed. Issue a diagnostic here. */
3164 else
3165 decl = check_for_out_of_scope_variable (decl);
3167 /* Remember that the name was used in the definition of
3168 the current class so that we can check later to see if
3169 the meaning would have been different after the class
3170 was entirely defined. */
3171 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3172 maybe_note_name_used_in_class (id_expression, decl);
3174 /* Disallow uses of local variables from containing functions, except
3175 within lambda-expressions. */
3176 if (!outer_var_p (decl))
3177 /* OK */;
3178 else if (TREE_STATIC (decl)
3179 /* It's not a use (3.2) if we're in an unevaluated context. */
3180 || cp_unevaluated_operand)
3181 /* OK */;
3182 else
3184 tree context = DECL_CONTEXT (decl);
3185 tree containing_function = current_function_decl;
3186 tree lambda_stack = NULL_TREE;
3187 tree lambda_expr = NULL_TREE;
3188 tree initializer = convert_from_reference (decl);
3190 /* Mark it as used now even if the use is ill-formed. */
3191 mark_used (decl);
3193 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3194 support for an approach in which a reference to a local
3195 [constant] automatic variable in a nested class or lambda body
3196 would enter the expression as an rvalue, which would reduce
3197 the complexity of the problem"
3199 FIXME update for final resolution of core issue 696. */
3200 if (decl_maybe_constant_var_p (decl))
3202 if (processing_template_decl)
3203 /* In a template, the constant value may not be in a usable
3204 form, so wait until instantiation time. */
3205 return decl;
3206 else if (decl_constant_var_p (decl))
3207 return integral_constant_value (decl);
3210 if (parsing_nsdmi ())
3211 containing_function = NULL_TREE;
3212 /* If we are in a lambda function, we can move out until we hit
3213 1. the context,
3214 2. a non-lambda function, or
3215 3. a non-default capturing lambda function. */
3216 else while (context != containing_function
3217 && LAMBDA_FUNCTION_P (containing_function))
3219 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3220 (DECL_CONTEXT (containing_function));
3222 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3223 == CPLD_NONE)
3224 break;
3226 lambda_stack = tree_cons (NULL_TREE,
3227 lambda_expr,
3228 lambda_stack);
3230 containing_function
3231 = decl_function_context (containing_function);
3234 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3235 && DECL_ANON_UNION_VAR_P (decl))
3237 error ("cannot capture member %qD of anonymous union", decl);
3238 return error_mark_node;
3240 if (context == containing_function)
3242 decl = add_default_capture (lambda_stack,
3243 /*id=*/DECL_NAME (decl),
3244 initializer);
3246 else if (lambda_expr)
3248 error ("%qD is not captured", decl);
3249 return error_mark_node;
3251 else
3253 error (VAR_P (decl)
3254 ? G_("use of local variable with automatic storage from containing function")
3255 : G_("use of parameter from containing function"));
3256 inform (input_location, "%q+#D declared here", decl);
3257 return error_mark_node;
3261 /* Also disallow uses of function parameters outside the function
3262 body, except inside an unevaluated context (i.e. decltype). */
3263 if (TREE_CODE (decl) == PARM_DECL
3264 && DECL_CONTEXT (decl) == NULL_TREE
3265 && !cp_unevaluated_operand)
3267 *error_msg = "use of parameter outside function body";
3268 return error_mark_node;
3272 /* If we didn't find anything, or what we found was a type,
3273 then this wasn't really an id-expression. */
3274 if (TREE_CODE (decl) == TEMPLATE_DECL
3275 && !DECL_FUNCTION_TEMPLATE_P (decl))
3277 *error_msg = "missing template arguments";
3278 return error_mark_node;
3280 else if (TREE_CODE (decl) == TYPE_DECL
3281 || TREE_CODE (decl) == NAMESPACE_DECL)
3283 *error_msg = "expected primary-expression";
3284 return error_mark_node;
3287 /* If the name resolved to a template parameter, there is no
3288 need to look it up again later. */
3289 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3290 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3292 tree r;
3294 *idk = CP_ID_KIND_NONE;
3295 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3296 decl = TEMPLATE_PARM_DECL (decl);
3297 r = convert_from_reference (DECL_INITIAL (decl));
3299 if (integral_constant_expression_p
3300 && !dependent_type_p (TREE_TYPE (decl))
3301 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3303 if (!allow_non_integral_constant_expression_p)
3304 error ("template parameter %qD of type %qT is not allowed in "
3305 "an integral constant expression because it is not of "
3306 "integral or enumeration type", decl, TREE_TYPE (decl));
3307 *non_integral_constant_expression_p = true;
3309 return r;
3311 else
3313 bool dependent_p;
3315 /* If the declaration was explicitly qualified indicate
3316 that. The semantics of `A::f(3)' are different than
3317 `f(3)' if `f' is virtual. */
3318 *idk = (scope
3319 ? CP_ID_KIND_QUALIFIED
3320 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3321 ? CP_ID_KIND_TEMPLATE_ID
3322 : CP_ID_KIND_UNQUALIFIED));
3325 /* [temp.dep.expr]
3327 An id-expression is type-dependent if it contains an
3328 identifier that was declared with a dependent type.
3330 The standard is not very specific about an id-expression that
3331 names a set of overloaded functions. What if some of them
3332 have dependent types and some of them do not? Presumably,
3333 such a name should be treated as a dependent name. */
3334 /* Assume the name is not dependent. */
3335 dependent_p = false;
3336 if (!processing_template_decl)
3337 /* No names are dependent outside a template. */
3339 else if (TREE_CODE (decl) == CONST_DECL)
3340 /* We don't want to treat enumerators as dependent. */
3342 /* A template-id where the name of the template was not resolved
3343 is definitely dependent. */
3344 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3345 && (identifier_p (TREE_OPERAND (decl, 0))))
3346 dependent_p = true;
3347 /* For anything except an overloaded function, just check its
3348 type. */
3349 else if (!is_overloaded_fn (decl))
3350 dependent_p
3351 = dependent_type_p (TREE_TYPE (decl));
3352 /* For a set of overloaded functions, check each of the
3353 functions. */
3354 else
3356 tree fns = decl;
3358 if (BASELINK_P (fns))
3359 fns = BASELINK_FUNCTIONS (fns);
3361 /* For a template-id, check to see if the template
3362 arguments are dependent. */
3363 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3365 tree args = TREE_OPERAND (fns, 1);
3366 dependent_p = any_dependent_template_arguments_p (args);
3367 /* The functions are those referred to by the
3368 template-id. */
3369 fns = TREE_OPERAND (fns, 0);
3372 /* If there are no dependent template arguments, go through
3373 the overloaded functions. */
3374 while (fns && !dependent_p)
3376 tree fn = OVL_CURRENT (fns);
3378 /* Member functions of dependent classes are
3379 dependent. */
3380 if (TREE_CODE (fn) == FUNCTION_DECL
3381 && type_dependent_expression_p (fn))
3382 dependent_p = true;
3383 else if (TREE_CODE (fn) == TEMPLATE_DECL
3384 && dependent_template_p (fn))
3385 dependent_p = true;
3387 fns = OVL_NEXT (fns);
3391 /* If the name was dependent on a template parameter, we will
3392 resolve the name at instantiation time. */
3393 if (dependent_p)
3395 /* Create a SCOPE_REF for qualified names, if the scope is
3396 dependent. */
3397 if (scope)
3399 if (TYPE_P (scope))
3401 if (address_p && done)
3402 decl = finish_qualified_id_expr (scope, decl,
3403 done, address_p,
3404 template_p,
3405 template_arg_p,
3406 tf_warning_or_error);
3407 else
3409 tree type = NULL_TREE;
3410 if (DECL_P (decl) && !dependent_scope_p (scope))
3411 type = TREE_TYPE (decl);
3412 decl = build_qualified_name (type,
3413 scope,
3414 id_expression,
3415 template_p);
3418 if (TREE_TYPE (decl))
3419 decl = convert_from_reference (decl);
3420 return decl;
3422 /* A TEMPLATE_ID already contains all the information we
3423 need. */
3424 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3425 return id_expression;
3426 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3427 /* If we found a variable, then name lookup during the
3428 instantiation will always resolve to the same VAR_DECL
3429 (or an instantiation thereof). */
3430 if (VAR_P (decl)
3431 || TREE_CODE (decl) == PARM_DECL)
3433 mark_used (decl);
3434 return convert_from_reference (decl);
3436 /* The same is true for FIELD_DECL, but we also need to
3437 make sure that the syntax is correct. */
3438 else if (TREE_CODE (decl) == FIELD_DECL)
3440 /* Since SCOPE is NULL here, this is an unqualified name.
3441 Access checking has been performed during name lookup
3442 already. Turn off checking to avoid duplicate errors. */
3443 push_deferring_access_checks (dk_no_check);
3444 decl = finish_non_static_data_member
3445 (decl, NULL_TREE,
3446 /*qualifying_scope=*/NULL_TREE);
3447 pop_deferring_access_checks ();
3448 return decl;
3450 return id_expression;
3453 if (TREE_CODE (decl) == NAMESPACE_DECL)
3455 error ("use of namespace %qD as expression", decl);
3456 return error_mark_node;
3458 else if (DECL_CLASS_TEMPLATE_P (decl))
3460 error ("use of class template %qT as expression", decl);
3461 return error_mark_node;
3463 else if (TREE_CODE (decl) == TREE_LIST)
3465 /* Ambiguous reference to base members. */
3466 error ("request for member %qD is ambiguous in "
3467 "multiple inheritance lattice", id_expression);
3468 print_candidates (decl);
3469 return error_mark_node;
3472 /* Mark variable-like entities as used. Functions are similarly
3473 marked either below or after overload resolution. */
3474 if ((VAR_P (decl)
3475 || TREE_CODE (decl) == PARM_DECL
3476 || TREE_CODE (decl) == CONST_DECL
3477 || TREE_CODE (decl) == RESULT_DECL)
3478 && !mark_used (decl))
3479 return error_mark_node;
3481 /* Only certain kinds of names are allowed in constant
3482 expression. Template parameters have already
3483 been handled above. */
3484 if (! error_operand_p (decl)
3485 && integral_constant_expression_p
3486 && ! decl_constant_var_p (decl)
3487 && TREE_CODE (decl) != CONST_DECL
3488 && ! builtin_valid_in_constant_expr_p (decl))
3490 if (!allow_non_integral_constant_expression_p)
3492 error ("%qD cannot appear in a constant-expression", decl);
3493 return error_mark_node;
3495 *non_integral_constant_expression_p = true;
3498 tree wrap;
3499 if (VAR_P (decl)
3500 && !cp_unevaluated_operand
3501 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3502 && DECL_THREAD_LOCAL_P (decl)
3503 && (wrap = get_tls_wrapper_fn (decl)))
3505 /* Replace an evaluated use of the thread_local variable with
3506 a call to its wrapper. */
3507 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3509 else if (scope)
3511 decl = (adjust_result_of_qualified_name_lookup
3512 (decl, scope, current_nonlambda_class_type()));
3514 if (TREE_CODE (decl) == FUNCTION_DECL)
3515 mark_used (decl);
3517 if (TYPE_P (scope))
3518 decl = finish_qualified_id_expr (scope,
3519 decl,
3520 done,
3521 address_p,
3522 template_p,
3523 template_arg_p,
3524 tf_warning_or_error);
3525 else
3526 decl = convert_from_reference (decl);
3528 else if (TREE_CODE (decl) == FIELD_DECL)
3530 /* Since SCOPE is NULL here, this is an unqualified name.
3531 Access checking has been performed during name lookup
3532 already. Turn off checking to avoid duplicate errors. */
3533 push_deferring_access_checks (dk_no_check);
3534 decl = finish_non_static_data_member (decl, NULL_TREE,
3535 /*qualifying_scope=*/NULL_TREE);
3536 pop_deferring_access_checks ();
3538 else if (is_overloaded_fn (decl))
3540 tree first_fn;
3542 first_fn = get_first_fn (decl);
3543 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3544 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3546 if (!really_overloaded_fn (decl)
3547 && !mark_used (first_fn))
3548 return error_mark_node;
3550 if (!template_arg_p
3551 && TREE_CODE (first_fn) == FUNCTION_DECL
3552 && DECL_FUNCTION_MEMBER_P (first_fn)
3553 && !shared_member_p (decl))
3555 /* A set of member functions. */
3556 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3557 return finish_class_member_access_expr (decl, id_expression,
3558 /*template_p=*/false,
3559 tf_warning_or_error);
3562 decl = baselink_for_fns (decl);
3564 else
3566 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3567 && DECL_CLASS_SCOPE_P (decl))
3569 tree context = context_for_name_lookup (decl);
3570 if (context != current_class_type)
3572 tree path = currently_open_derived_class (context);
3573 perform_or_defer_access_check (TYPE_BINFO (path),
3574 decl, decl,
3575 tf_warning_or_error);
3579 decl = convert_from_reference (decl);
3583 /* Handle references (c++/56130). */
3584 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3585 if (TREE_DEPRECATED (t))
3586 warn_deprecated_use (t, NULL_TREE);
3588 return decl;
3591 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3592 use as a type-specifier. */
3594 tree
3595 finish_typeof (tree expr)
3597 tree type;
3599 if (type_dependent_expression_p (expr))
3601 type = cxx_make_type (TYPEOF_TYPE);
3602 TYPEOF_TYPE_EXPR (type) = expr;
3603 SET_TYPE_STRUCTURAL_EQUALITY (type);
3605 return type;
3608 expr = mark_type_use (expr);
3610 type = unlowered_expr_type (expr);
3612 if (!type || type == unknown_type_node)
3614 error ("type of %qE is unknown", expr);
3615 return error_mark_node;
3618 return type;
3621 /* Implement the __underlying_type keyword: Return the underlying
3622 type of TYPE, suitable for use as a type-specifier. */
3624 tree
3625 finish_underlying_type (tree type)
3627 tree underlying_type;
3629 if (processing_template_decl)
3631 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3632 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3633 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3635 return underlying_type;
3638 complete_type (type);
3640 if (TREE_CODE (type) != ENUMERAL_TYPE)
3642 error ("%qT is not an enumeration type", type);
3643 return error_mark_node;
3646 underlying_type = ENUM_UNDERLYING_TYPE (type);
3648 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3649 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3650 See finish_enum_value_list for details. */
3651 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3652 underlying_type
3653 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3654 TYPE_UNSIGNED (underlying_type));
3656 return underlying_type;
3659 /* Implement the __direct_bases keyword: Return the direct base classes
3660 of type */
3662 tree
3663 calculate_direct_bases (tree type)
3665 vec<tree, va_gc> *vector = make_tree_vector();
3666 tree bases_vec = NULL_TREE;
3667 vec<tree, va_gc> *base_binfos;
3668 tree binfo;
3669 unsigned i;
3671 complete_type (type);
3673 if (!NON_UNION_CLASS_TYPE_P (type))
3674 return make_tree_vec (0);
3676 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3678 /* Virtual bases are initialized first */
3679 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3681 if (BINFO_VIRTUAL_P (binfo))
3683 vec_safe_push (vector, binfo);
3687 /* Now non-virtuals */
3688 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3690 if (!BINFO_VIRTUAL_P (binfo))
3692 vec_safe_push (vector, binfo);
3697 bases_vec = make_tree_vec (vector->length ());
3699 for (i = 0; i < vector->length (); ++i)
3701 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3703 return bases_vec;
3706 /* Implement the __bases keyword: Return the base classes
3707 of type */
3709 /* Find morally non-virtual base classes by walking binfo hierarchy */
3710 /* Virtual base classes are handled separately in finish_bases */
3712 static tree
3713 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3715 /* Don't walk bases of virtual bases */
3716 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3719 static tree
3720 dfs_calculate_bases_post (tree binfo, void *data_)
3722 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3723 if (!BINFO_VIRTUAL_P (binfo))
3725 vec_safe_push (*data, BINFO_TYPE (binfo));
3727 return NULL_TREE;
3730 /* Calculates the morally non-virtual base classes of a class */
3731 static vec<tree, va_gc> *
3732 calculate_bases_helper (tree type)
3734 vec<tree, va_gc> *vector = make_tree_vector();
3736 /* Now add non-virtual base classes in order of construction */
3737 dfs_walk_all (TYPE_BINFO (type),
3738 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3739 return vector;
3742 tree
3743 calculate_bases (tree type)
3745 vec<tree, va_gc> *vector = make_tree_vector();
3746 tree bases_vec = NULL_TREE;
3747 unsigned i;
3748 vec<tree, va_gc> *vbases;
3749 vec<tree, va_gc> *nonvbases;
3750 tree binfo;
3752 complete_type (type);
3754 if (!NON_UNION_CLASS_TYPE_P (type))
3755 return make_tree_vec (0);
3757 /* First go through virtual base classes */
3758 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3759 vec_safe_iterate (vbases, i, &binfo); i++)
3761 vec<tree, va_gc> *vbase_bases;
3762 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3763 vec_safe_splice (vector, vbase_bases);
3764 release_tree_vector (vbase_bases);
3767 /* Now for the non-virtual bases */
3768 nonvbases = calculate_bases_helper (type);
3769 vec_safe_splice (vector, nonvbases);
3770 release_tree_vector (nonvbases);
3772 /* Last element is entire class, so don't copy */
3773 bases_vec = make_tree_vec (vector->length () - 1);
3775 for (i = 0; i < vector->length () - 1; ++i)
3777 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3779 release_tree_vector (vector);
3780 return bases_vec;
3783 tree
3784 finish_bases (tree type, bool direct)
3786 tree bases = NULL_TREE;
3788 if (!processing_template_decl)
3790 /* Parameter packs can only be used in templates */
3791 error ("Parameter pack __bases only valid in template declaration");
3792 return error_mark_node;
3795 bases = cxx_make_type (BASES);
3796 BASES_TYPE (bases) = type;
3797 BASES_DIRECT (bases) = direct;
3798 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3800 return bases;
3803 /* Perform C++-specific checks for __builtin_offsetof before calling
3804 fold_offsetof. */
3806 tree
3807 finish_offsetof (tree expr)
3809 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3811 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3812 TREE_OPERAND (expr, 2));
3813 return error_mark_node;
3815 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3816 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3817 || TREE_TYPE (expr) == unknown_type_node)
3819 if (INDIRECT_REF_P (expr))
3820 error ("second operand of %<offsetof%> is neither a single "
3821 "identifier nor a sequence of member accesses and "
3822 "array references");
3823 else
3825 if (TREE_CODE (expr) == COMPONENT_REF
3826 || TREE_CODE (expr) == COMPOUND_EXPR)
3827 expr = TREE_OPERAND (expr, 1);
3828 error ("cannot apply %<offsetof%> to member function %qD", expr);
3830 return error_mark_node;
3832 if (REFERENCE_REF_P (expr))
3833 expr = TREE_OPERAND (expr, 0);
3834 if (TREE_CODE (expr) == COMPONENT_REF)
3836 tree object = TREE_OPERAND (expr, 0);
3837 if (!complete_type_or_else (TREE_TYPE (object), object))
3838 return error_mark_node;
3840 return fold_offsetof (expr);
3843 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3844 function is broken out from the above for the benefit of the tree-ssa
3845 project. */
3847 void
3848 simplify_aggr_init_expr (tree *tp)
3850 tree aggr_init_expr = *tp;
3852 /* Form an appropriate CALL_EXPR. */
3853 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3854 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3855 tree type = TREE_TYPE (slot);
3857 tree call_expr;
3858 enum style_t { ctor, arg, pcc } style;
3860 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3861 style = ctor;
3862 #ifdef PCC_STATIC_STRUCT_RETURN
3863 else if (1)
3864 style = pcc;
3865 #endif
3866 else
3868 gcc_assert (TREE_ADDRESSABLE (type));
3869 style = arg;
3872 call_expr = build_call_array_loc (input_location,
3873 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3875 aggr_init_expr_nargs (aggr_init_expr),
3876 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3877 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3878 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3880 if (style == ctor)
3882 /* Replace the first argument to the ctor with the address of the
3883 slot. */
3884 cxx_mark_addressable (slot);
3885 CALL_EXPR_ARG (call_expr, 0) =
3886 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3888 else if (style == arg)
3890 /* Just mark it addressable here, and leave the rest to
3891 expand_call{,_inline}. */
3892 cxx_mark_addressable (slot);
3893 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3894 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3896 else if (style == pcc)
3898 /* If we're using the non-reentrant PCC calling convention, then we
3899 need to copy the returned value out of the static buffer into the
3900 SLOT. */
3901 push_deferring_access_checks (dk_no_check);
3902 call_expr = build_aggr_init (slot, call_expr,
3903 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3904 tf_warning_or_error);
3905 pop_deferring_access_checks ();
3906 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3909 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3911 tree init = build_zero_init (type, NULL_TREE,
3912 /*static_storage_p=*/false);
3913 init = build2 (INIT_EXPR, void_type_node, slot, init);
3914 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3915 init, call_expr);
3918 *tp = call_expr;
3921 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3923 void
3924 emit_associated_thunks (tree fn)
3926 /* When we use vcall offsets, we emit thunks with the virtual
3927 functions to which they thunk. The whole point of vcall offsets
3928 is so that you can know statically the entire set of thunks that
3929 will ever be needed for a given virtual function, thereby
3930 enabling you to output all the thunks with the function itself. */
3931 if (DECL_VIRTUAL_P (fn)
3932 /* Do not emit thunks for extern template instantiations. */
3933 && ! DECL_REALLY_EXTERN (fn))
3935 tree thunk;
3937 if (L_IPO_COMP_MODE)
3939 /* In LIPO mode, multiple copies of definitions for the same function
3940 may exist, but assembler hash table keeps only one copy which might
3941 have been deleted at this point. */
3942 struct cgraph_node *n = cgraph_node::get_create (fn);
3943 #ifdef FIXME_LIPO
3944 insert_to_assembler_name_hash ((symtab_node)n);
3945 #endif
3946 cgraph_link_node (n);
3949 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3951 if (!THUNK_ALIAS (thunk))
3953 use_thunk (thunk, /*emit_p=*/1);
3954 if (DECL_RESULT_THUNK_P (thunk))
3956 tree probe;
3958 for (probe = DECL_THUNKS (thunk);
3959 probe; probe = DECL_CHAIN (probe))
3960 use_thunk (probe, /*emit_p=*/1);
3963 else
3964 gcc_assert (!DECL_THUNKS (thunk));
3969 /* Returns true iff FUN is an instantiation of a constexpr function
3970 template or a defaulted constexpr function. */
3972 static inline bool
3973 is_instantiation_of_constexpr (tree fun)
3975 return ((DECL_TEMPLOID_INSTANTIATION (fun)
3976 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
3977 || (DECL_DEFAULTED_FN (fun)
3978 && DECL_DECLARED_CONSTEXPR_P (fun)));
3982 /* Generate RTL for FN. */
3984 bool
3985 expand_or_defer_fn_1 (tree fn)
3987 /* When the parser calls us after finishing the body of a template
3988 function, we don't really want to expand the body. */
3989 if (processing_template_decl)
3991 /* Normally, collection only occurs in rest_of_compilation. So,
3992 if we don't collect here, we never collect junk generated
3993 during the processing of templates until we hit a
3994 non-template function. It's not safe to do this inside a
3995 nested class, though, as the parser may have local state that
3996 is not a GC root. */
3997 if (!function_depth)
3998 ggc_collect ();
3999 return false;
4002 gcc_assert (DECL_SAVED_TREE (fn));
4004 /* We make a decision about linkage for these functions at the end
4005 of the compilation. Until that point, we do not want the back
4006 end to output them -- but we do want it to see the bodies of
4007 these functions so that it can inline them as appropriate. */
4008 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4010 if (DECL_INTERFACE_KNOWN (fn))
4011 /* We've already made a decision as to how this function will
4012 be handled. */;
4013 else if (!at_eof)
4014 tentative_decl_linkage (fn);
4015 else
4016 import_export_decl (fn);
4018 /* If the user wants us to keep all inline functions, then mark
4019 this function as needed so that finish_file will make sure to
4020 output it later. Similarly, all dllexport'd functions must
4021 be emitted; there may be callers in other DLLs. */
4022 if ((flag_keep_inline_functions
4023 && DECL_DECLARED_INLINE_P (fn)
4024 && !DECL_REALLY_EXTERN (fn))
4025 || (flag_keep_inline_dllexport
4026 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
4028 mark_needed (fn);
4029 DECL_EXTERNAL (fn) = 0;
4033 /* If this is a constructor or destructor body, we have to clone
4034 it. */
4035 if (maybe_clone_body (fn))
4037 /* We don't want to process FN again, so pretend we've written
4038 it out, even though we haven't. */
4039 TREE_ASM_WRITTEN (fn) = 1;
4040 /* If this is an instantiation of a constexpr function, keep
4041 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4042 if (!is_instantiation_of_constexpr (fn))
4043 DECL_SAVED_TREE (fn) = NULL_TREE;
4044 return false;
4047 /* There's no reason to do any of the work here if we're only doing
4048 semantic analysis; this code just generates RTL. */
4049 if (flag_syntax_only)
4050 return false;
4052 return true;
4055 void
4056 expand_or_defer_fn (tree fn)
4058 if (expand_or_defer_fn_1 (fn))
4060 function_depth++;
4062 /* Expand or defer, at the whim of the compilation unit manager. */
4063 cgraph_finalize_function (fn, function_depth > 1);
4064 emit_associated_thunks (fn);
4066 function_depth--;
4070 struct nrv_data
4072 nrv_data () : visited (37) {}
4074 tree var;
4075 tree result;
4076 hash_table<pointer_hash <tree_node> > visited;
4079 /* Helper function for walk_tree, used by finalize_nrv below. */
4081 static tree
4082 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4084 struct nrv_data *dp = (struct nrv_data *)data;
4085 tree_node **slot;
4087 /* No need to walk into types. There wouldn't be any need to walk into
4088 non-statements, except that we have to consider STMT_EXPRs. */
4089 if (TYPE_P (*tp))
4090 *walk_subtrees = 0;
4091 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4092 but differs from using NULL_TREE in that it indicates that we care
4093 about the value of the RESULT_DECL. */
4094 else if (TREE_CODE (*tp) == RETURN_EXPR)
4095 TREE_OPERAND (*tp, 0) = dp->result;
4096 /* Change all cleanups for the NRV to only run when an exception is
4097 thrown. */
4098 else if (TREE_CODE (*tp) == CLEANUP_STMT
4099 && CLEANUP_DECL (*tp) == dp->var)
4100 CLEANUP_EH_ONLY (*tp) = 1;
4101 /* Replace the DECL_EXPR for the NRV with an initialization of the
4102 RESULT_DECL, if needed. */
4103 else if (TREE_CODE (*tp) == DECL_EXPR
4104 && DECL_EXPR_DECL (*tp) == dp->var)
4106 tree init;
4107 if (DECL_INITIAL (dp->var)
4108 && DECL_INITIAL (dp->var) != error_mark_node)
4109 init = build2 (INIT_EXPR, void_type_node, dp->result,
4110 DECL_INITIAL (dp->var));
4111 else
4112 init = build_empty_stmt (EXPR_LOCATION (*tp));
4113 DECL_INITIAL (dp->var) = NULL_TREE;
4114 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4115 *tp = init;
4117 /* And replace all uses of the NRV with the RESULT_DECL. */
4118 else if (*tp == dp->var)
4119 *tp = dp->result;
4121 /* Avoid walking into the same tree more than once. Unfortunately, we
4122 can't just use walk_tree_without duplicates because it would only call
4123 us for the first occurrence of dp->var in the function body. */
4124 slot = dp->visited.find_slot (*tp, INSERT);
4125 if (*slot)
4126 *walk_subtrees = 0;
4127 else
4128 *slot = *tp;
4130 /* Keep iterating. */
4131 return NULL_TREE;
4134 /* Called from finish_function to implement the named return value
4135 optimization by overriding all the RETURN_EXPRs and pertinent
4136 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4137 RESULT_DECL for the function. */
4139 void
4140 finalize_nrv (tree *tp, tree var, tree result)
4142 struct nrv_data data;
4144 /* Copy name from VAR to RESULT. */
4145 DECL_NAME (result) = DECL_NAME (var);
4146 /* Don't forget that we take its address. */
4147 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4148 /* Finally set DECL_VALUE_EXPR to avoid assigning
4149 a stack slot at -O0 for the original var and debug info
4150 uses RESULT location for VAR. */
4151 SET_DECL_VALUE_EXPR (var, result);
4152 DECL_HAS_VALUE_EXPR_P (var) = 1;
4154 data.var = var;
4155 data.result = result;
4156 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4159 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4161 bool
4162 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4163 bool need_copy_ctor, bool need_copy_assignment,
4164 bool need_dtor)
4166 int save_errorcount = errorcount;
4167 tree info, t;
4169 /* Always allocate 3 elements for simplicity. These are the
4170 function decls for the ctor, dtor, and assignment op.
4171 This layout is known to the three lang hooks,
4172 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4173 and cxx_omp_clause_assign_op. */
4174 info = make_tree_vec (3);
4175 CP_OMP_CLAUSE_INFO (c) = info;
4177 if (need_default_ctor || need_copy_ctor)
4179 if (need_default_ctor)
4180 t = get_default_ctor (type);
4181 else
4182 t = get_copy_ctor (type, tf_warning_or_error);
4184 if (t && !trivial_fn_p (t))
4185 TREE_VEC_ELT (info, 0) = t;
4188 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4189 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4191 if (need_copy_assignment)
4193 t = get_copy_assign (type);
4195 if (t && !trivial_fn_p (t))
4196 TREE_VEC_ELT (info, 2) = t;
4199 return errorcount != save_errorcount;
4202 /* Helper function for handle_omp_array_sections. Called recursively
4203 to handle multiple array-section-subscripts. C is the clause,
4204 T current expression (initially OMP_CLAUSE_DECL), which is either
4205 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4206 expression if specified, TREE_VALUE length expression if specified,
4207 TREE_CHAIN is what it has been specified after, or some decl.
4208 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4209 set to true if any of the array-section-subscript could have length
4210 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4211 first array-section-subscript which is known not to have length
4212 of one. Given say:
4213 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4214 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4215 all are or may have length of 1, array-section-subscript [:2] is the
4216 first one knonwn not to have length 1. For array-section-subscript
4217 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4218 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4219 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4220 case though, as some lengths could be zero. */
4222 static tree
4223 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4224 bool &maybe_zero_len, unsigned int &first_non_one)
4226 tree ret, low_bound, length, type;
4227 if (TREE_CODE (t) != TREE_LIST)
4229 if (error_operand_p (t))
4230 return error_mark_node;
4231 if (type_dependent_expression_p (t))
4232 return NULL_TREE;
4233 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4235 if (processing_template_decl)
4236 return NULL_TREE;
4237 if (DECL_P (t))
4238 error_at (OMP_CLAUSE_LOCATION (c),
4239 "%qD is not a variable in %qs clause", t,
4240 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4241 else
4242 error_at (OMP_CLAUSE_LOCATION (c),
4243 "%qE is not a variable in %qs clause", t,
4244 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4245 return error_mark_node;
4247 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4248 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4250 error_at (OMP_CLAUSE_LOCATION (c),
4251 "%qD is threadprivate variable in %qs clause", t,
4252 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4253 return error_mark_node;
4255 t = convert_from_reference (t);
4256 return t;
4259 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4260 maybe_zero_len, first_non_one);
4261 if (ret == error_mark_node || ret == NULL_TREE)
4262 return ret;
4264 type = TREE_TYPE (ret);
4265 low_bound = TREE_PURPOSE (t);
4266 length = TREE_VALUE (t);
4267 if ((low_bound && type_dependent_expression_p (low_bound))
4268 || (length && type_dependent_expression_p (length)))
4269 return NULL_TREE;
4271 if (low_bound == error_mark_node || length == error_mark_node)
4272 return error_mark_node;
4274 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4276 error_at (OMP_CLAUSE_LOCATION (c),
4277 "low bound %qE of array section does not have integral type",
4278 low_bound);
4279 return error_mark_node;
4281 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4283 error_at (OMP_CLAUSE_LOCATION (c),
4284 "length %qE of array section does not have integral type",
4285 length);
4286 return error_mark_node;
4288 if (low_bound
4289 && TREE_CODE (low_bound) == INTEGER_CST
4290 && TYPE_PRECISION (TREE_TYPE (low_bound))
4291 > TYPE_PRECISION (sizetype))
4292 low_bound = fold_convert (sizetype, low_bound);
4293 if (length
4294 && TREE_CODE (length) == INTEGER_CST
4295 && TYPE_PRECISION (TREE_TYPE (length))
4296 > TYPE_PRECISION (sizetype))
4297 length = fold_convert (sizetype, length);
4298 if (low_bound == NULL_TREE)
4299 low_bound = integer_zero_node;
4301 if (length != NULL_TREE)
4303 if (!integer_nonzerop (length))
4304 maybe_zero_len = true;
4305 if (first_non_one == types.length ()
4306 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4307 first_non_one++;
4309 if (TREE_CODE (type) == ARRAY_TYPE)
4311 if (length == NULL_TREE
4312 && (TYPE_DOMAIN (type) == NULL_TREE
4313 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4315 error_at (OMP_CLAUSE_LOCATION (c),
4316 "for unknown bound array type length expression must "
4317 "be specified");
4318 return error_mark_node;
4320 if (TREE_CODE (low_bound) == INTEGER_CST
4321 && tree_int_cst_sgn (low_bound) == -1)
4323 error_at (OMP_CLAUSE_LOCATION (c),
4324 "negative low bound in array section in %qs clause",
4325 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4326 return error_mark_node;
4328 if (length != NULL_TREE
4329 && TREE_CODE (length) == INTEGER_CST
4330 && tree_int_cst_sgn (length) == -1)
4332 error_at (OMP_CLAUSE_LOCATION (c),
4333 "negative length in array section in %qs clause",
4334 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4335 return error_mark_node;
4337 if (TYPE_DOMAIN (type)
4338 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4339 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4340 == INTEGER_CST)
4342 tree size = size_binop (PLUS_EXPR,
4343 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4344 size_one_node);
4345 if (TREE_CODE (low_bound) == INTEGER_CST)
4347 if (tree_int_cst_lt (size, low_bound))
4349 error_at (OMP_CLAUSE_LOCATION (c),
4350 "low bound %qE above array section size "
4351 "in %qs clause", low_bound,
4352 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4353 return error_mark_node;
4355 if (tree_int_cst_equal (size, low_bound))
4356 maybe_zero_len = true;
4357 else if (length == NULL_TREE
4358 && first_non_one == types.length ()
4359 && tree_int_cst_equal
4360 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4361 low_bound))
4362 first_non_one++;
4364 else if (length == NULL_TREE)
4366 maybe_zero_len = true;
4367 if (first_non_one == types.length ())
4368 first_non_one++;
4370 if (length && TREE_CODE (length) == INTEGER_CST)
4372 if (tree_int_cst_lt (size, length))
4374 error_at (OMP_CLAUSE_LOCATION (c),
4375 "length %qE above array section size "
4376 "in %qs clause", length,
4377 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4378 return error_mark_node;
4380 if (TREE_CODE (low_bound) == INTEGER_CST)
4382 tree lbpluslen
4383 = size_binop (PLUS_EXPR,
4384 fold_convert (sizetype, low_bound),
4385 fold_convert (sizetype, length));
4386 if (TREE_CODE (lbpluslen) == INTEGER_CST
4387 && tree_int_cst_lt (size, lbpluslen))
4389 error_at (OMP_CLAUSE_LOCATION (c),
4390 "high bound %qE above array section size "
4391 "in %qs clause", lbpluslen,
4392 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4393 return error_mark_node;
4398 else if (length == NULL_TREE)
4400 maybe_zero_len = true;
4401 if (first_non_one == types.length ())
4402 first_non_one++;
4405 /* For [lb:] we will need to evaluate lb more than once. */
4406 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4408 tree lb = cp_save_expr (low_bound);
4409 if (lb != low_bound)
4411 TREE_PURPOSE (t) = lb;
4412 low_bound = lb;
4416 else if (TREE_CODE (type) == POINTER_TYPE)
4418 if (length == NULL_TREE)
4420 error_at (OMP_CLAUSE_LOCATION (c),
4421 "for pointer type length expression must be specified");
4422 return error_mark_node;
4424 /* If there is a pointer type anywhere but in the very first
4425 array-section-subscript, the array section can't be contiguous. */
4426 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4427 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4429 error_at (OMP_CLAUSE_LOCATION (c),
4430 "array section is not contiguous in %qs clause",
4431 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4432 return error_mark_node;
4435 else
4437 error_at (OMP_CLAUSE_LOCATION (c),
4438 "%qE does not have pointer or array type", ret);
4439 return error_mark_node;
4441 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4442 types.safe_push (TREE_TYPE (ret));
4443 /* We will need to evaluate lb more than once. */
4444 tree lb = cp_save_expr (low_bound);
4445 if (lb != low_bound)
4447 TREE_PURPOSE (t) = lb;
4448 low_bound = lb;
4450 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4451 return ret;
4454 /* Handle array sections for clause C. */
4456 static bool
4457 handle_omp_array_sections (tree c)
4459 bool maybe_zero_len = false;
4460 unsigned int first_non_one = 0;
4461 auto_vec<tree> types;
4462 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4463 maybe_zero_len, first_non_one);
4464 if (first == error_mark_node)
4465 return true;
4466 if (first == NULL_TREE)
4467 return false;
4468 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4470 tree t = OMP_CLAUSE_DECL (c);
4471 tree tem = NULL_TREE;
4472 if (processing_template_decl)
4473 return false;
4474 /* Need to evaluate side effects in the length expressions
4475 if any. */
4476 while (TREE_CODE (t) == TREE_LIST)
4478 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4480 if (tem == NULL_TREE)
4481 tem = TREE_VALUE (t);
4482 else
4483 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4484 TREE_VALUE (t), tem);
4486 t = TREE_CHAIN (t);
4488 if (tem)
4489 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4490 OMP_CLAUSE_DECL (c) = first;
4492 else
4494 unsigned int num = types.length (), i;
4495 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4496 tree condition = NULL_TREE;
4498 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4499 maybe_zero_len = true;
4500 if (processing_template_decl && maybe_zero_len)
4501 return false;
4503 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4504 t = TREE_CHAIN (t))
4506 tree low_bound = TREE_PURPOSE (t);
4507 tree length = TREE_VALUE (t);
4509 i--;
4510 if (low_bound
4511 && TREE_CODE (low_bound) == INTEGER_CST
4512 && TYPE_PRECISION (TREE_TYPE (low_bound))
4513 > TYPE_PRECISION (sizetype))
4514 low_bound = fold_convert (sizetype, low_bound);
4515 if (length
4516 && TREE_CODE (length) == INTEGER_CST
4517 && TYPE_PRECISION (TREE_TYPE (length))
4518 > TYPE_PRECISION (sizetype))
4519 length = fold_convert (sizetype, length);
4520 if (low_bound == NULL_TREE)
4521 low_bound = integer_zero_node;
4522 if (!maybe_zero_len && i > first_non_one)
4524 if (integer_nonzerop (low_bound))
4525 goto do_warn_noncontiguous;
4526 if (length != NULL_TREE
4527 && TREE_CODE (length) == INTEGER_CST
4528 && TYPE_DOMAIN (types[i])
4529 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4530 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4531 == INTEGER_CST)
4533 tree size;
4534 size = size_binop (PLUS_EXPR,
4535 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4536 size_one_node);
4537 if (!tree_int_cst_equal (length, size))
4539 do_warn_noncontiguous:
4540 error_at (OMP_CLAUSE_LOCATION (c),
4541 "array section is not contiguous in %qs "
4542 "clause",
4543 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4544 return true;
4547 if (!processing_template_decl
4548 && length != NULL_TREE
4549 && TREE_SIDE_EFFECTS (length))
4551 if (side_effects == NULL_TREE)
4552 side_effects = length;
4553 else
4554 side_effects = build2 (COMPOUND_EXPR,
4555 TREE_TYPE (side_effects),
4556 length, side_effects);
4559 else if (processing_template_decl)
4560 continue;
4561 else
4563 tree l;
4565 if (i > first_non_one && length && integer_nonzerop (length))
4566 continue;
4567 if (length)
4568 l = fold_convert (sizetype, length);
4569 else
4571 l = size_binop (PLUS_EXPR,
4572 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4573 size_one_node);
4574 l = size_binop (MINUS_EXPR, l,
4575 fold_convert (sizetype, low_bound));
4577 if (i > first_non_one)
4579 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4580 size_zero_node);
4581 if (condition == NULL_TREE)
4582 condition = l;
4583 else
4584 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4585 l, condition);
4587 else if (size == NULL_TREE)
4589 size = size_in_bytes (TREE_TYPE (types[i]));
4590 size = size_binop (MULT_EXPR, size, l);
4591 if (condition)
4592 size = fold_build3 (COND_EXPR, sizetype, condition,
4593 size, size_zero_node);
4595 else
4596 size = size_binop (MULT_EXPR, size, l);
4599 if (!processing_template_decl)
4601 if (side_effects)
4602 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4603 OMP_CLAUSE_DECL (c) = first;
4604 OMP_CLAUSE_SIZE (c) = size;
4605 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4606 return false;
4607 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4608 OMP_CLAUSE_MAP);
4609 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4610 if (!cxx_mark_addressable (t))
4611 return false;
4612 OMP_CLAUSE_DECL (c2) = t;
4613 t = build_fold_addr_expr (first);
4614 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4615 ptrdiff_type_node, t);
4616 tree ptr = OMP_CLAUSE_DECL (c2);
4617 ptr = convert_from_reference (ptr);
4618 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4619 ptr = build_fold_addr_expr (ptr);
4620 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4621 ptrdiff_type_node, t,
4622 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4623 ptrdiff_type_node, ptr));
4624 OMP_CLAUSE_SIZE (c2) = t;
4625 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4626 OMP_CLAUSE_CHAIN (c) = c2;
4627 ptr = OMP_CLAUSE_DECL (c2);
4628 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4629 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4631 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4632 OMP_CLAUSE_MAP);
4633 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4634 OMP_CLAUSE_DECL (c3) = ptr;
4635 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4636 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4637 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4638 OMP_CLAUSE_CHAIN (c2) = c3;
4642 return false;
4645 /* Return identifier to look up for omp declare reduction. */
4647 tree
4648 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4650 const char *p = NULL;
4651 const char *m = NULL;
4652 switch (reduction_code)
4654 case PLUS_EXPR:
4655 case MULT_EXPR:
4656 case MINUS_EXPR:
4657 case BIT_AND_EXPR:
4658 case BIT_XOR_EXPR:
4659 case BIT_IOR_EXPR:
4660 case TRUTH_ANDIF_EXPR:
4661 case TRUTH_ORIF_EXPR:
4662 reduction_id = ansi_opname (reduction_code);
4663 break;
4664 case MIN_EXPR:
4665 p = "min";
4666 break;
4667 case MAX_EXPR:
4668 p = "max";
4669 break;
4670 default:
4671 break;
4674 if (p == NULL)
4676 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4677 return error_mark_node;
4678 p = IDENTIFIER_POINTER (reduction_id);
4681 if (type != NULL_TREE)
4682 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4684 const char prefix[] = "omp declare reduction ";
4685 size_t lenp = sizeof (prefix);
4686 if (strncmp (p, prefix, lenp - 1) == 0)
4687 lenp = 1;
4688 size_t len = strlen (p);
4689 size_t lenm = m ? strlen (m) + 1 : 0;
4690 char *name = XALLOCAVEC (char, lenp + len + lenm);
4691 if (lenp > 1)
4692 memcpy (name, prefix, lenp - 1);
4693 memcpy (name + lenp - 1, p, len + 1);
4694 if (m)
4696 name[lenp + len - 1] = '~';
4697 memcpy (name + lenp + len, m, lenm);
4699 return get_identifier (name);
4702 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4703 FUNCTION_DECL or NULL_TREE if not found. */
4705 static tree
4706 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4707 vec<tree> *ambiguousp)
4709 tree orig_id = id;
4710 tree baselink = NULL_TREE;
4711 if (identifier_p (id))
4713 cp_id_kind idk;
4714 bool nonint_cst_expression_p;
4715 const char *error_msg;
4716 id = omp_reduction_id (ERROR_MARK, id, type);
4717 tree decl = lookup_name (id);
4718 if (decl == NULL_TREE)
4719 decl = error_mark_node;
4720 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4721 &nonint_cst_expression_p, false, true, false,
4722 false, &error_msg, loc);
4723 if (idk == CP_ID_KIND_UNQUALIFIED
4724 && identifier_p (id))
4726 vec<tree, va_gc> *args = NULL;
4727 vec_safe_push (args, build_reference_type (type));
4728 id = perform_koenig_lookup (id, args, tf_none);
4731 else if (TREE_CODE (id) == SCOPE_REF)
4732 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4733 omp_reduction_id (ERROR_MARK,
4734 TREE_OPERAND (id, 1),
4735 type),
4736 false, false);
4737 tree fns = id;
4738 if (id && is_overloaded_fn (id))
4739 id = get_fns (id);
4740 for (; id; id = OVL_NEXT (id))
4742 tree fndecl = OVL_CURRENT (id);
4743 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4745 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4746 if (same_type_p (TREE_TYPE (argtype), type))
4747 break;
4750 if (id && BASELINK_P (fns))
4752 if (baselinkp)
4753 *baselinkp = fns;
4754 else
4755 baselink = fns;
4757 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4759 vec<tree> ambiguous = vNULL;
4760 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4761 unsigned int ix;
4762 if (ambiguousp == NULL)
4763 ambiguousp = &ambiguous;
4764 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4766 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4767 baselinkp ? baselinkp : &baselink,
4768 ambiguousp);
4769 if (id == NULL_TREE)
4770 continue;
4771 if (!ambiguousp->is_empty ())
4772 ambiguousp->safe_push (id);
4773 else if (ret != NULL_TREE)
4775 ambiguousp->safe_push (ret);
4776 ambiguousp->safe_push (id);
4777 ret = NULL_TREE;
4779 else
4780 ret = id;
4782 if (ambiguousp != &ambiguous)
4783 return ret;
4784 if (!ambiguous.is_empty ())
4786 const char *str = _("candidates are:");
4787 unsigned int idx;
4788 tree udr;
4789 error_at (loc, "user defined reduction lookup is ambiguous");
4790 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4792 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4793 if (idx == 0)
4794 str = get_spaces (str);
4796 ambiguous.release ();
4797 ret = error_mark_node;
4798 baselink = NULL_TREE;
4800 id = ret;
4802 if (id && baselink)
4803 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4804 id, id, tf_warning_or_error);
4805 return id;
4808 /* Helper function for cp_parser_omp_declare_reduction_exprs
4809 and tsubst_omp_udr.
4810 Remove CLEANUP_STMT for data (omp_priv variable).
4811 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4812 DECL_EXPR. */
4814 tree
4815 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4817 if (TYPE_P (*tp))
4818 *walk_subtrees = 0;
4819 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4820 *tp = CLEANUP_BODY (*tp);
4821 else if (TREE_CODE (*tp) == DECL_EXPR)
4823 tree decl = DECL_EXPR_DECL (*tp);
4824 if (!processing_template_decl
4825 && decl == (tree) data
4826 && DECL_INITIAL (decl)
4827 && DECL_INITIAL (decl) != error_mark_node)
4829 tree list = NULL_TREE;
4830 append_to_statement_list_force (*tp, &list);
4831 tree init_expr = build2 (INIT_EXPR, void_type_node,
4832 decl, DECL_INITIAL (decl));
4833 DECL_INITIAL (decl) = NULL_TREE;
4834 append_to_statement_list_force (init_expr, &list);
4835 *tp = list;
4838 return NULL_TREE;
4841 /* Data passed from cp_check_omp_declare_reduction to
4842 cp_check_omp_declare_reduction_r. */
4844 struct cp_check_omp_declare_reduction_data
4846 location_t loc;
4847 tree stmts[7];
4848 bool combiner_p;
4851 /* Helper function for cp_check_omp_declare_reduction, called via
4852 cp_walk_tree. */
4854 static tree
4855 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4857 struct cp_check_omp_declare_reduction_data *udr_data
4858 = (struct cp_check_omp_declare_reduction_data *) data;
4859 if (SSA_VAR_P (*tp)
4860 && !DECL_ARTIFICIAL (*tp)
4861 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4862 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4864 location_t loc = udr_data->loc;
4865 if (udr_data->combiner_p)
4866 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4867 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4868 *tp);
4869 else
4870 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4871 "to variable %qD which is not %<omp_priv%> nor "
4872 "%<omp_orig%>",
4873 *tp);
4874 return *tp;
4876 return NULL_TREE;
4879 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4881 void
4882 cp_check_omp_declare_reduction (tree udr)
4884 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4885 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4886 type = TREE_TYPE (type);
4887 int i;
4888 location_t loc = DECL_SOURCE_LOCATION (udr);
4890 if (type == error_mark_node)
4891 return;
4892 if (ARITHMETIC_TYPE_P (type))
4894 static enum tree_code predef_codes[]
4895 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4896 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4897 for (i = 0; i < 8; i++)
4899 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4900 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4901 const char *n2 = IDENTIFIER_POINTER (id);
4902 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4903 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4904 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4905 break;
4908 if (i == 8
4909 && TREE_CODE (type) != COMPLEX_EXPR)
4911 const char prefix_minmax[] = "omp declare reduction m";
4912 size_t prefix_size = sizeof (prefix_minmax) - 1;
4913 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4914 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4915 prefix_minmax, prefix_size) == 0
4916 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4917 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4918 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4919 i = 0;
4921 if (i < 8)
4923 error_at (loc, "predeclared arithmetic type %qT in "
4924 "%<#pragma omp declare reduction%>", type);
4925 return;
4928 else if (TREE_CODE (type) == FUNCTION_TYPE
4929 || TREE_CODE (type) == METHOD_TYPE
4930 || TREE_CODE (type) == ARRAY_TYPE)
4932 error_at (loc, "function or array type %qT in "
4933 "%<#pragma omp declare reduction%>", type);
4934 return;
4936 else if (TREE_CODE (type) == REFERENCE_TYPE)
4938 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4939 type);
4940 return;
4942 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4944 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4945 "%<#pragma omp declare reduction%>", type);
4946 return;
4949 tree body = DECL_SAVED_TREE (udr);
4950 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4951 return;
4953 tree_stmt_iterator tsi;
4954 struct cp_check_omp_declare_reduction_data data;
4955 memset (data.stmts, 0, sizeof data.stmts);
4956 for (i = 0, tsi = tsi_start (body);
4957 i < 7 && !tsi_end_p (tsi);
4958 i++, tsi_next (&tsi))
4959 data.stmts[i] = tsi_stmt (tsi);
4960 data.loc = loc;
4961 gcc_assert (tsi_end_p (tsi));
4962 if (i >= 3)
4964 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4965 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4966 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4967 return;
4968 data.combiner_p = true;
4969 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4970 &data, NULL))
4971 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4973 if (i >= 6)
4975 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4976 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4977 data.combiner_p = false;
4978 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4979 &data, NULL)
4980 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4981 cp_check_omp_declare_reduction_r, &data, NULL))
4982 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4983 if (i == 7)
4984 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4988 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4989 an inline call. But, remap
4990 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4991 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4993 static tree
4994 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
4995 tree decl, tree placeholder)
4997 copy_body_data id;
4998 hash_map<tree, tree> decl_map;
5000 decl_map.put (omp_decl1, placeholder);
5001 decl_map.put (omp_decl2, decl);
5002 memset (&id, 0, sizeof (id));
5003 id.src_fn = DECL_CONTEXT (omp_decl1);
5004 id.dst_fn = current_function_decl;
5005 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5006 id.decl_map = &decl_map;
5008 id.copy_decl = copy_decl_no_change;
5009 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5010 id.transform_new_cfg = true;
5011 id.transform_return_to_modify = false;
5012 id.transform_lang_insert_block = NULL;
5013 id.eh_lp_nr = 0;
5014 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5015 return stmt;
5018 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5019 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5021 static tree
5022 find_omp_placeholder_r (tree *tp, int *, void *data)
5024 if (*tp == (tree) data)
5025 return *tp;
5026 return NULL_TREE;
5029 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5030 Return true if there is some error and the clause should be removed. */
5032 static bool
5033 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5035 tree t = OMP_CLAUSE_DECL (c);
5036 bool predefined = false;
5037 tree type = TREE_TYPE (t);
5038 if (TREE_CODE (type) == REFERENCE_TYPE)
5039 type = TREE_TYPE (type);
5040 if (type == error_mark_node)
5041 return true;
5042 else if (ARITHMETIC_TYPE_P (type))
5043 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5045 case PLUS_EXPR:
5046 case MULT_EXPR:
5047 case MINUS_EXPR:
5048 predefined = true;
5049 break;
5050 case MIN_EXPR:
5051 case MAX_EXPR:
5052 if (TREE_CODE (type) == COMPLEX_TYPE)
5053 break;
5054 predefined = true;
5055 break;
5056 case BIT_AND_EXPR:
5057 case BIT_IOR_EXPR:
5058 case BIT_XOR_EXPR:
5059 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5060 break;
5061 predefined = true;
5062 break;
5063 case TRUTH_ANDIF_EXPR:
5064 case TRUTH_ORIF_EXPR:
5065 if (FLOAT_TYPE_P (type))
5066 break;
5067 predefined = true;
5068 break;
5069 default:
5070 break;
5072 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5074 error ("%qE has invalid type for %<reduction%>", t);
5075 return true;
5077 else if (!processing_template_decl)
5079 t = require_complete_type (t);
5080 if (t == error_mark_node)
5081 return true;
5082 OMP_CLAUSE_DECL (c) = t;
5085 if (predefined)
5087 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5088 return false;
5090 else if (processing_template_decl)
5091 return false;
5093 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5095 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5096 if (TREE_CODE (type) == REFERENCE_TYPE)
5097 type = TREE_TYPE (type);
5098 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5099 if (id == NULL_TREE)
5100 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5101 NULL_TREE, NULL_TREE);
5102 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5103 if (id)
5105 if (id == error_mark_node)
5106 return true;
5107 id = OVL_CURRENT (id);
5108 mark_used (id);
5109 tree body = DECL_SAVED_TREE (id);
5110 if (TREE_CODE (body) == STATEMENT_LIST)
5112 tree_stmt_iterator tsi;
5113 tree placeholder = NULL_TREE;
5114 int i;
5115 tree stmts[7];
5116 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5117 atype = TREE_TYPE (atype);
5118 bool need_static_cast = !same_type_p (type, atype);
5119 memset (stmts, 0, sizeof stmts);
5120 for (i = 0, tsi = tsi_start (body);
5121 i < 7 && !tsi_end_p (tsi);
5122 i++, tsi_next (&tsi))
5123 stmts[i] = tsi_stmt (tsi);
5124 gcc_assert (tsi_end_p (tsi));
5126 if (i >= 3)
5128 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5129 && TREE_CODE (stmts[1]) == DECL_EXPR);
5130 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5131 DECL_ARTIFICIAL (placeholder) = 1;
5132 DECL_IGNORED_P (placeholder) = 1;
5133 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5134 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5135 cxx_mark_addressable (placeholder);
5136 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5137 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5138 != REFERENCE_TYPE)
5139 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5140 tree omp_out = placeholder;
5141 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5142 if (need_static_cast)
5144 tree rtype = build_reference_type (atype);
5145 omp_out = build_static_cast (rtype, omp_out,
5146 tf_warning_or_error);
5147 omp_in = build_static_cast (rtype, omp_in,
5148 tf_warning_or_error);
5149 if (omp_out == error_mark_node || omp_in == error_mark_node)
5150 return true;
5151 omp_out = convert_from_reference (omp_out);
5152 omp_in = convert_from_reference (omp_in);
5154 OMP_CLAUSE_REDUCTION_MERGE (c)
5155 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5156 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5158 if (i >= 6)
5160 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5161 && TREE_CODE (stmts[4]) == DECL_EXPR);
5162 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5163 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5164 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5165 cxx_mark_addressable (placeholder);
5166 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5167 tree omp_orig = placeholder;
5168 if (need_static_cast)
5170 if (i == 7)
5172 error_at (OMP_CLAUSE_LOCATION (c),
5173 "user defined reduction with constructor "
5174 "initializer for base class %qT", atype);
5175 return true;
5177 tree rtype = build_reference_type (atype);
5178 omp_priv = build_static_cast (rtype, omp_priv,
5179 tf_warning_or_error);
5180 omp_orig = build_static_cast (rtype, omp_orig,
5181 tf_warning_or_error);
5182 if (omp_priv == error_mark_node
5183 || omp_orig == error_mark_node)
5184 return true;
5185 omp_priv = convert_from_reference (omp_priv);
5186 omp_orig = convert_from_reference (omp_orig);
5188 if (i == 6)
5189 *need_default_ctor = true;
5190 OMP_CLAUSE_REDUCTION_INIT (c)
5191 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5192 DECL_EXPR_DECL (stmts[3]),
5193 omp_priv, omp_orig);
5194 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5195 find_omp_placeholder_r, placeholder, NULL))
5196 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5198 else if (i >= 3)
5200 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5201 *need_default_ctor = true;
5202 else
5204 tree init;
5205 tree v = convert_from_reference (t);
5206 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5207 init = build_constructor (TREE_TYPE (v), NULL);
5208 else
5209 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5210 OMP_CLAUSE_REDUCTION_INIT (c)
5211 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5216 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5217 *need_dtor = true;
5218 else
5220 error ("user defined reduction not found for %qD", t);
5221 return true;
5223 return false;
5226 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5227 Remove any elements from the list that are invalid. */
5229 tree
5230 finish_omp_clauses (tree clauses)
5232 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5233 bitmap_head aligned_head;
5234 tree c, t, *pc;
5235 bool branch_seen = false;
5236 bool copyprivate_seen = false;
5238 bitmap_obstack_initialize (NULL);
5239 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5240 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5241 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5242 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5244 for (pc = &clauses, c = clauses; c ; c = *pc)
5246 bool remove = false;
5248 switch (OMP_CLAUSE_CODE (c))
5250 case OMP_CLAUSE_SHARED:
5251 goto check_dup_generic;
5252 case OMP_CLAUSE_PRIVATE:
5253 goto check_dup_generic;
5254 case OMP_CLAUSE_REDUCTION:
5255 goto check_dup_generic;
5256 case OMP_CLAUSE_COPYPRIVATE:
5257 copyprivate_seen = true;
5258 goto check_dup_generic;
5259 case OMP_CLAUSE_COPYIN:
5260 goto check_dup_generic;
5261 case OMP_CLAUSE_LINEAR:
5262 t = OMP_CLAUSE_DECL (c);
5263 if (!type_dependent_expression_p (t)
5264 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5265 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5267 error ("linear clause applied to non-integral non-pointer "
5268 "variable with %qT type", TREE_TYPE (t));
5269 remove = true;
5270 break;
5272 t = OMP_CLAUSE_LINEAR_STEP (c);
5273 if (t == NULL_TREE)
5274 t = integer_one_node;
5275 if (t == error_mark_node)
5277 remove = true;
5278 break;
5280 else if (!type_dependent_expression_p (t)
5281 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5283 error ("linear step expression must be integral");
5284 remove = true;
5285 break;
5287 else
5289 t = mark_rvalue_use (t);
5290 if (!processing_template_decl)
5292 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5293 t = maybe_constant_value (t);
5294 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5295 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5296 == POINTER_TYPE)
5298 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5299 OMP_CLAUSE_DECL (c), t);
5300 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5301 MINUS_EXPR, sizetype, t,
5302 OMP_CLAUSE_DECL (c));
5303 if (t == error_mark_node)
5305 remove = true;
5306 break;
5309 else
5310 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5312 OMP_CLAUSE_LINEAR_STEP (c) = t;
5314 goto check_dup_generic;
5315 check_dup_generic:
5316 t = OMP_CLAUSE_DECL (c);
5317 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5319 if (processing_template_decl)
5320 break;
5321 if (DECL_P (t))
5322 error ("%qD is not a variable in clause %qs", t,
5323 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5324 else
5325 error ("%qE is not a variable in clause %qs", t,
5326 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5327 remove = true;
5329 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5330 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5331 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5333 error ("%qD appears more than once in data clauses", t);
5334 remove = true;
5336 else
5337 bitmap_set_bit (&generic_head, DECL_UID (t));
5338 break;
5340 case OMP_CLAUSE_FIRSTPRIVATE:
5341 t = OMP_CLAUSE_DECL (c);
5342 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5344 if (processing_template_decl)
5345 break;
5346 if (DECL_P (t))
5347 error ("%qD is not a variable in clause %<firstprivate%>", t);
5348 else
5349 error ("%qE is not a variable in clause %<firstprivate%>", t);
5350 remove = true;
5352 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5353 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5355 error ("%qD appears more than once in data clauses", t);
5356 remove = true;
5358 else
5359 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5360 break;
5362 case OMP_CLAUSE_LASTPRIVATE:
5363 t = OMP_CLAUSE_DECL (c);
5364 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5366 if (processing_template_decl)
5367 break;
5368 if (DECL_P (t))
5369 error ("%qD is not a variable in clause %<lastprivate%>", t);
5370 else
5371 error ("%qE is not a variable in clause %<lastprivate%>", t);
5372 remove = true;
5374 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5375 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5377 error ("%qD appears more than once in data clauses", t);
5378 remove = true;
5380 else
5381 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5382 break;
5384 case OMP_CLAUSE_IF:
5385 t = OMP_CLAUSE_IF_EXPR (c);
5386 t = maybe_convert_cond (t);
5387 if (t == error_mark_node)
5388 remove = true;
5389 else if (!processing_template_decl)
5390 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5391 OMP_CLAUSE_IF_EXPR (c) = t;
5392 break;
5394 case OMP_CLAUSE_FINAL:
5395 t = OMP_CLAUSE_FINAL_EXPR (c);
5396 t = maybe_convert_cond (t);
5397 if (t == error_mark_node)
5398 remove = true;
5399 else if (!processing_template_decl)
5400 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5401 OMP_CLAUSE_FINAL_EXPR (c) = t;
5402 break;
5404 case OMP_CLAUSE_NUM_THREADS:
5405 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5406 if (t == error_mark_node)
5407 remove = true;
5408 else if (!type_dependent_expression_p (t)
5409 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5411 error ("num_threads expression must be integral");
5412 remove = true;
5414 else
5416 t = mark_rvalue_use (t);
5417 if (!processing_template_decl)
5418 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5419 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5421 break;
5423 case OMP_CLAUSE_SCHEDULE:
5424 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5425 if (t == NULL)
5427 else if (t == error_mark_node)
5428 remove = true;
5429 else if (!type_dependent_expression_p (t)
5430 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5432 error ("schedule chunk size expression must be integral");
5433 remove = true;
5435 else
5437 t = mark_rvalue_use (t);
5438 if (!processing_template_decl)
5439 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5440 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5442 break;
5444 case OMP_CLAUSE_SIMDLEN:
5445 case OMP_CLAUSE_SAFELEN:
5446 t = OMP_CLAUSE_OPERAND (c, 0);
5447 if (t == error_mark_node)
5448 remove = true;
5449 else if (!type_dependent_expression_p (t)
5450 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5452 error ("%qs length expression must be integral",
5453 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5454 remove = true;
5456 else
5458 t = mark_rvalue_use (t);
5459 t = maybe_constant_value (t);
5460 if (!processing_template_decl)
5462 if (TREE_CODE (t) != INTEGER_CST
5463 || tree_int_cst_sgn (t) != 1)
5465 error ("%qs length expression must be positive constant"
5466 " integer expression",
5467 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5468 remove = true;
5471 OMP_CLAUSE_OPERAND (c, 0) = t;
5473 break;
5475 case OMP_CLAUSE_NUM_TEAMS:
5476 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5477 if (t == error_mark_node)
5478 remove = true;
5479 else if (!type_dependent_expression_p (t)
5480 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5482 error ("%<num_teams%> expression must be integral");
5483 remove = true;
5485 else
5487 t = mark_rvalue_use (t);
5488 if (!processing_template_decl)
5489 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5490 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5492 break;
5494 case OMP_CLAUSE_THREAD_LIMIT:
5495 t = OMP_CLAUSE_THREAD_LIMIT_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 ("%<thread_limit%> 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_THREAD_LIMIT_EXPR (c) = t;
5511 break;
5513 case OMP_CLAUSE_DEVICE:
5514 t = OMP_CLAUSE_DEVICE_ID (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 ("%<device%> id 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_DEVICE_ID (c) = t;
5530 break;
5532 case OMP_CLAUSE_DIST_SCHEDULE:
5533 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5534 if (t == NULL)
5536 else if (t == error_mark_node)
5537 remove = true;
5538 else if (!type_dependent_expression_p (t)
5539 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5541 error ("%<dist_schedule%> chunk size expression must be "
5542 "integral");
5543 remove = true;
5545 else
5547 t = mark_rvalue_use (t);
5548 if (!processing_template_decl)
5549 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5550 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5552 break;
5554 case OMP_CLAUSE_ALIGNED:
5555 t = OMP_CLAUSE_DECL (c);
5556 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5558 if (processing_template_decl)
5559 break;
5560 if (DECL_P (t))
5561 error ("%qD is not a variable in %<aligned%> clause", t);
5562 else
5563 error ("%qE is not a variable in %<aligned%> clause", t);
5564 remove = true;
5566 else if (!type_dependent_expression_p (t)
5567 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5568 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5569 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5570 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5571 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5572 != ARRAY_TYPE))))
5574 error_at (OMP_CLAUSE_LOCATION (c),
5575 "%qE in %<aligned%> clause is neither a pointer nor "
5576 "an array nor a reference to pointer or array", t);
5577 remove = true;
5579 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5581 error ("%qD appears more than once in %<aligned%> clauses", t);
5582 remove = true;
5584 else
5585 bitmap_set_bit (&aligned_head, DECL_UID (t));
5586 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5587 if (t == error_mark_node)
5588 remove = true;
5589 else if (t == NULL_TREE)
5590 break;
5591 else if (!type_dependent_expression_p (t)
5592 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5594 error ("%<aligned%> clause alignment expression must "
5595 "be integral");
5596 remove = true;
5598 else
5600 t = mark_rvalue_use (t);
5601 t = maybe_constant_value (t);
5602 if (!processing_template_decl)
5604 if (TREE_CODE (t) != INTEGER_CST
5605 || tree_int_cst_sgn (t) != 1)
5607 error ("%<aligned%> clause alignment expression must be "
5608 "positive constant integer expression");
5609 remove = true;
5612 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5614 break;
5616 case OMP_CLAUSE_DEPEND:
5617 t = OMP_CLAUSE_DECL (c);
5618 if (TREE_CODE (t) == TREE_LIST)
5620 if (handle_omp_array_sections (c))
5621 remove = true;
5622 break;
5624 if (t == error_mark_node)
5625 remove = true;
5626 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5628 if (processing_template_decl)
5629 break;
5630 if (DECL_P (t))
5631 error ("%qD is not a variable in %<depend%> clause", t);
5632 else
5633 error ("%qE is not a variable in %<depend%> clause", t);
5634 remove = true;
5636 else if (!processing_template_decl
5637 && !cxx_mark_addressable (t))
5638 remove = true;
5639 break;
5641 case OMP_CLAUSE_MAP:
5642 case OMP_CLAUSE_TO:
5643 case OMP_CLAUSE_FROM:
5644 t = OMP_CLAUSE_DECL (c);
5645 if (TREE_CODE (t) == TREE_LIST)
5647 if (handle_omp_array_sections (c))
5648 remove = true;
5649 else
5651 t = OMP_CLAUSE_DECL (c);
5652 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5654 error_at (OMP_CLAUSE_LOCATION (c),
5655 "array section does not have mappable type "
5656 "in %qs clause",
5657 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5658 remove = true;
5661 break;
5663 if (t == error_mark_node)
5664 remove = true;
5665 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5667 if (processing_template_decl)
5668 break;
5669 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5670 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5671 break;
5672 if (DECL_P (t))
5673 error ("%qD is not a variable in %qs clause", t,
5674 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5675 else
5676 error ("%qE is not a variable in %qs clause", t,
5677 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5678 remove = true;
5680 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5682 error ("%qD is threadprivate variable in %qs clause", t,
5683 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5684 remove = true;
5686 else if (!processing_template_decl
5687 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5688 && !cxx_mark_addressable (t))
5689 remove = true;
5690 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5691 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5692 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5693 == REFERENCE_TYPE)
5694 ? TREE_TYPE (TREE_TYPE (t))
5695 : TREE_TYPE (t)))
5697 error_at (OMP_CLAUSE_LOCATION (c),
5698 "%qD does not have a mappable type in %qs clause", t,
5699 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5700 remove = true;
5702 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5704 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5705 error ("%qD appears more than once in motion clauses", t);
5706 else
5707 error ("%qD appears more than once in map clauses", t);
5708 remove = true;
5710 else
5711 bitmap_set_bit (&generic_head, DECL_UID (t));
5712 break;
5714 case OMP_CLAUSE_UNIFORM:
5715 t = OMP_CLAUSE_DECL (c);
5716 if (TREE_CODE (t) != PARM_DECL)
5718 if (processing_template_decl)
5719 break;
5720 if (DECL_P (t))
5721 error ("%qD is not an argument in %<uniform%> clause", t);
5722 else
5723 error ("%qE is not an argument in %<uniform%> clause", t);
5724 remove = true;
5725 break;
5727 goto check_dup_generic;
5729 case OMP_CLAUSE_NOWAIT:
5730 case OMP_CLAUSE_ORDERED:
5731 case OMP_CLAUSE_DEFAULT:
5732 case OMP_CLAUSE_UNTIED:
5733 case OMP_CLAUSE_COLLAPSE:
5734 case OMP_CLAUSE_MERGEABLE:
5735 case OMP_CLAUSE_PARALLEL:
5736 case OMP_CLAUSE_FOR:
5737 case OMP_CLAUSE_SECTIONS:
5738 case OMP_CLAUSE_TASKGROUP:
5739 case OMP_CLAUSE_PROC_BIND:
5740 break;
5742 case OMP_CLAUSE_INBRANCH:
5743 case OMP_CLAUSE_NOTINBRANCH:
5744 if (branch_seen)
5746 error ("%<inbranch%> clause is incompatible with "
5747 "%<notinbranch%>");
5748 remove = true;
5750 branch_seen = true;
5751 break;
5753 default:
5754 gcc_unreachable ();
5757 if (remove)
5758 *pc = OMP_CLAUSE_CHAIN (c);
5759 else
5760 pc = &OMP_CLAUSE_CHAIN (c);
5763 for (pc = &clauses, c = clauses; c ; c = *pc)
5765 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5766 bool remove = false;
5767 bool need_complete_non_reference = false;
5768 bool need_default_ctor = false;
5769 bool need_copy_ctor = false;
5770 bool need_copy_assignment = false;
5771 bool need_implicitly_determined = false;
5772 bool need_dtor = false;
5773 tree type, inner_type;
5775 switch (c_kind)
5777 case OMP_CLAUSE_SHARED:
5778 need_implicitly_determined = true;
5779 break;
5780 case OMP_CLAUSE_PRIVATE:
5781 need_complete_non_reference = true;
5782 need_default_ctor = true;
5783 need_dtor = true;
5784 need_implicitly_determined = true;
5785 break;
5786 case OMP_CLAUSE_FIRSTPRIVATE:
5787 need_complete_non_reference = true;
5788 need_copy_ctor = true;
5789 need_dtor = true;
5790 need_implicitly_determined = true;
5791 break;
5792 case OMP_CLAUSE_LASTPRIVATE:
5793 need_complete_non_reference = true;
5794 need_copy_assignment = true;
5795 need_implicitly_determined = true;
5796 break;
5797 case OMP_CLAUSE_REDUCTION:
5798 need_implicitly_determined = true;
5799 break;
5800 case OMP_CLAUSE_COPYPRIVATE:
5801 need_copy_assignment = true;
5802 break;
5803 case OMP_CLAUSE_COPYIN:
5804 need_copy_assignment = true;
5805 break;
5806 case OMP_CLAUSE_NOWAIT:
5807 if (copyprivate_seen)
5809 error_at (OMP_CLAUSE_LOCATION (c),
5810 "%<nowait%> clause must not be used together "
5811 "with %<copyprivate%>");
5812 *pc = OMP_CLAUSE_CHAIN (c);
5813 continue;
5815 /* FALLTHRU */
5816 default:
5817 pc = &OMP_CLAUSE_CHAIN (c);
5818 continue;
5821 t = OMP_CLAUSE_DECL (c);
5822 if (processing_template_decl
5823 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5825 pc = &OMP_CLAUSE_CHAIN (c);
5826 continue;
5829 switch (c_kind)
5831 case OMP_CLAUSE_LASTPRIVATE:
5832 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5834 need_default_ctor = true;
5835 need_dtor = true;
5837 break;
5839 case OMP_CLAUSE_REDUCTION:
5840 if (finish_omp_reduction_clause (c, &need_default_ctor,
5841 &need_dtor))
5842 remove = true;
5843 else
5844 t = OMP_CLAUSE_DECL (c);
5845 break;
5847 case OMP_CLAUSE_COPYIN:
5848 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5850 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5851 remove = true;
5853 break;
5855 default:
5856 break;
5859 if (need_complete_non_reference || need_copy_assignment)
5861 t = require_complete_type (t);
5862 if (t == error_mark_node)
5863 remove = true;
5864 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5865 && need_complete_non_reference)
5867 error ("%qE has reference type for %qs", t,
5868 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5869 remove = true;
5872 if (need_implicitly_determined)
5874 const char *share_name = NULL;
5876 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5877 share_name = "threadprivate";
5878 else switch (cxx_omp_predetermined_sharing (t))
5880 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5881 break;
5882 case OMP_CLAUSE_DEFAULT_SHARED:
5883 /* const vars may be specified in firstprivate clause. */
5884 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5885 && cxx_omp_const_qual_no_mutable (t))
5886 break;
5887 share_name = "shared";
5888 break;
5889 case OMP_CLAUSE_DEFAULT_PRIVATE:
5890 share_name = "private";
5891 break;
5892 default:
5893 gcc_unreachable ();
5895 if (share_name)
5897 error ("%qE is predetermined %qs for %qs",
5898 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5899 remove = true;
5903 /* We're interested in the base element, not arrays. */
5904 inner_type = type = TREE_TYPE (t);
5905 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5906 inner_type = TREE_TYPE (inner_type);
5908 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5909 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5910 inner_type = TREE_TYPE (inner_type);
5912 /* Check for special function availability by building a call to one.
5913 Save the results, because later we won't be in the right context
5914 for making these queries. */
5915 if (CLASS_TYPE_P (inner_type)
5916 && COMPLETE_TYPE_P (inner_type)
5917 && (need_default_ctor || need_copy_ctor
5918 || need_copy_assignment || need_dtor)
5919 && !type_dependent_expression_p (t)
5920 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5921 need_copy_ctor, need_copy_assignment,
5922 need_dtor))
5923 remove = true;
5925 if (remove)
5926 *pc = OMP_CLAUSE_CHAIN (c);
5927 else
5928 pc = &OMP_CLAUSE_CHAIN (c);
5931 bitmap_obstack_release (NULL);
5932 return clauses;
5935 /* For all variables in the tree_list VARS, mark them as thread local. */
5937 void
5938 finish_omp_threadprivate (tree vars)
5940 tree t;
5942 /* Mark every variable in VARS to be assigned thread local storage. */
5943 for (t = vars; t; t = TREE_CHAIN (t))
5945 tree v = TREE_PURPOSE (t);
5947 if (error_operand_p (v))
5949 else if (!VAR_P (v))
5950 error ("%<threadprivate%> %qD is not file, namespace "
5951 "or block scope variable", v);
5952 /* If V had already been marked threadprivate, it doesn't matter
5953 whether it had been used prior to this point. */
5954 else if (TREE_USED (v)
5955 && (DECL_LANG_SPECIFIC (v) == NULL
5956 || !CP_DECL_THREADPRIVATE_P (v)))
5957 error ("%qE declared %<threadprivate%> after first use", v);
5958 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
5959 error ("automatic variable %qE cannot be %<threadprivate%>", v);
5960 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
5961 error ("%<threadprivate%> %qE has incomplete type", v);
5962 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
5963 && CP_DECL_CONTEXT (v) != current_class_type)
5964 error ("%<threadprivate%> %qE directive not "
5965 "in %qT definition", v, CP_DECL_CONTEXT (v));
5966 else
5968 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5969 if (DECL_LANG_SPECIFIC (v) == NULL)
5971 retrofit_lang_decl (v);
5973 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5974 after the allocation of the lang_decl structure. */
5975 if (DECL_DISCRIMINATOR_P (v))
5976 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
5979 if (! DECL_THREAD_LOCAL_P (v))
5981 set_decl_tls_model (v, decl_default_tls_model (v));
5982 /* If rtl has been already set for this var, call
5983 make_decl_rtl once again, so that encode_section_info
5984 has a chance to look at the new decl flags. */
5985 if (DECL_RTL_SET_P (v))
5986 make_decl_rtl (v);
5988 CP_DECL_THREADPRIVATE_P (v) = 1;
5993 /* Build an OpenMP structured block. */
5995 tree
5996 begin_omp_structured_block (void)
5998 return do_pushlevel (sk_omp);
6001 tree
6002 finish_omp_structured_block (tree block)
6004 return do_poplevel (block);
6007 /* Similarly, except force the retention of the BLOCK. */
6009 tree
6010 begin_omp_parallel (void)
6012 keep_next_level (true);
6013 return begin_omp_structured_block ();
6016 tree
6017 finish_omp_parallel (tree clauses, tree body)
6019 tree stmt;
6021 body = finish_omp_structured_block (body);
6023 stmt = make_node (OMP_PARALLEL);
6024 TREE_TYPE (stmt) = void_type_node;
6025 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6026 OMP_PARALLEL_BODY (stmt) = body;
6028 return add_stmt (stmt);
6031 tree
6032 begin_omp_task (void)
6034 keep_next_level (true);
6035 return begin_omp_structured_block ();
6038 tree
6039 finish_omp_task (tree clauses, tree body)
6041 tree stmt;
6043 body = finish_omp_structured_block (body);
6045 stmt = make_node (OMP_TASK);
6046 TREE_TYPE (stmt) = void_type_node;
6047 OMP_TASK_CLAUSES (stmt) = clauses;
6048 OMP_TASK_BODY (stmt) = body;
6050 return add_stmt (stmt);
6053 /* Helper function for finish_omp_for. Convert Ith random access iterator
6054 into integral iterator. Return FALSE if successful. */
6056 static bool
6057 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6058 tree condv, tree incrv, tree *body,
6059 tree *pre_body, tree clauses)
6061 tree diff, iter_init, iter_incr = NULL, last;
6062 tree incr_var = NULL, orig_pre_body, orig_body, c;
6063 tree decl = TREE_VEC_ELT (declv, i);
6064 tree init = TREE_VEC_ELT (initv, i);
6065 tree cond = TREE_VEC_ELT (condv, i);
6066 tree incr = TREE_VEC_ELT (incrv, i);
6067 tree iter = decl;
6068 location_t elocus = locus;
6070 if (init && EXPR_HAS_LOCATION (init))
6071 elocus = EXPR_LOCATION (init);
6073 switch (TREE_CODE (cond))
6075 case GT_EXPR:
6076 case GE_EXPR:
6077 case LT_EXPR:
6078 case LE_EXPR:
6079 if (TREE_OPERAND (cond, 1) == iter)
6080 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6081 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6082 if (TREE_OPERAND (cond, 0) != iter)
6083 cond = error_mark_node;
6084 else
6086 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6087 TREE_CODE (cond),
6088 iter, ERROR_MARK,
6089 TREE_OPERAND (cond, 1), ERROR_MARK,
6090 NULL, tf_warning_or_error);
6091 if (error_operand_p (tem))
6092 return true;
6094 break;
6095 default:
6096 cond = error_mark_node;
6097 break;
6099 if (cond == error_mark_node)
6101 error_at (elocus, "invalid controlling predicate");
6102 return true;
6104 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6105 ERROR_MARK, iter, ERROR_MARK, NULL,
6106 tf_warning_or_error);
6107 if (error_operand_p (diff))
6108 return true;
6109 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6111 error_at (elocus, "difference between %qE and %qD does not have integer type",
6112 TREE_OPERAND (cond, 1), iter);
6113 return true;
6116 switch (TREE_CODE (incr))
6118 case PREINCREMENT_EXPR:
6119 case PREDECREMENT_EXPR:
6120 case POSTINCREMENT_EXPR:
6121 case POSTDECREMENT_EXPR:
6122 if (TREE_OPERAND (incr, 0) != iter)
6124 incr = error_mark_node;
6125 break;
6127 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6128 TREE_CODE (incr), iter,
6129 tf_warning_or_error);
6130 if (error_operand_p (iter_incr))
6131 return true;
6132 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6133 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6134 incr = integer_one_node;
6135 else
6136 incr = integer_minus_one_node;
6137 break;
6138 case MODIFY_EXPR:
6139 if (TREE_OPERAND (incr, 0) != iter)
6140 incr = error_mark_node;
6141 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6142 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6144 tree rhs = TREE_OPERAND (incr, 1);
6145 if (TREE_OPERAND (rhs, 0) == iter)
6147 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6148 != INTEGER_TYPE)
6149 incr = error_mark_node;
6150 else
6152 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6153 iter, TREE_CODE (rhs),
6154 TREE_OPERAND (rhs, 1),
6155 tf_warning_or_error);
6156 if (error_operand_p (iter_incr))
6157 return true;
6158 incr = TREE_OPERAND (rhs, 1);
6159 incr = cp_convert (TREE_TYPE (diff), incr,
6160 tf_warning_or_error);
6161 if (TREE_CODE (rhs) == MINUS_EXPR)
6163 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6164 incr = fold_if_not_in_template (incr);
6166 if (TREE_CODE (incr) != INTEGER_CST
6167 && (TREE_CODE (incr) != NOP_EXPR
6168 || (TREE_CODE (TREE_OPERAND (incr, 0))
6169 != INTEGER_CST)))
6170 iter_incr = NULL;
6173 else if (TREE_OPERAND (rhs, 1) == iter)
6175 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6176 || TREE_CODE (rhs) != PLUS_EXPR)
6177 incr = error_mark_node;
6178 else
6180 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6181 PLUS_EXPR,
6182 TREE_OPERAND (rhs, 0),
6183 ERROR_MARK, iter,
6184 ERROR_MARK, NULL,
6185 tf_warning_or_error);
6186 if (error_operand_p (iter_incr))
6187 return true;
6188 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6189 iter, NOP_EXPR,
6190 iter_incr,
6191 tf_warning_or_error);
6192 if (error_operand_p (iter_incr))
6193 return true;
6194 incr = TREE_OPERAND (rhs, 0);
6195 iter_incr = NULL;
6198 else
6199 incr = error_mark_node;
6201 else
6202 incr = error_mark_node;
6203 break;
6204 default:
6205 incr = error_mark_node;
6206 break;
6209 if (incr == error_mark_node)
6211 error_at (elocus, "invalid increment expression");
6212 return true;
6215 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6216 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6217 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6218 && OMP_CLAUSE_DECL (c) == iter)
6219 break;
6221 decl = create_temporary_var (TREE_TYPE (diff));
6222 pushdecl (decl);
6223 add_decl_expr (decl);
6224 last = create_temporary_var (TREE_TYPE (diff));
6225 pushdecl (last);
6226 add_decl_expr (last);
6227 if (c && iter_incr == NULL)
6229 incr_var = create_temporary_var (TREE_TYPE (diff));
6230 pushdecl (incr_var);
6231 add_decl_expr (incr_var);
6233 gcc_assert (stmts_are_full_exprs_p ());
6235 orig_pre_body = *pre_body;
6236 *pre_body = push_stmt_list ();
6237 if (orig_pre_body)
6238 add_stmt (orig_pre_body);
6239 if (init != NULL)
6240 finish_expr_stmt (build_x_modify_expr (elocus,
6241 iter, NOP_EXPR, init,
6242 tf_warning_or_error));
6243 init = build_int_cst (TREE_TYPE (diff), 0);
6244 if (c && iter_incr == NULL)
6246 finish_expr_stmt (build_x_modify_expr (elocus,
6247 incr_var, NOP_EXPR,
6248 incr, tf_warning_or_error));
6249 incr = incr_var;
6250 iter_incr = build_x_modify_expr (elocus,
6251 iter, PLUS_EXPR, incr,
6252 tf_warning_or_error);
6254 finish_expr_stmt (build_x_modify_expr (elocus,
6255 last, NOP_EXPR, init,
6256 tf_warning_or_error));
6257 *pre_body = pop_stmt_list (*pre_body);
6259 cond = cp_build_binary_op (elocus,
6260 TREE_CODE (cond), decl, diff,
6261 tf_warning_or_error);
6262 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6263 elocus, incr, NULL_TREE);
6265 orig_body = *body;
6266 *body = push_stmt_list ();
6267 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6268 iter_init = build_x_modify_expr (elocus,
6269 iter, PLUS_EXPR, iter_init,
6270 tf_warning_or_error);
6271 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6272 finish_expr_stmt (iter_init);
6273 finish_expr_stmt (build_x_modify_expr (elocus,
6274 last, NOP_EXPR, decl,
6275 tf_warning_or_error));
6276 add_stmt (orig_body);
6277 *body = pop_stmt_list (*body);
6279 if (c)
6281 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6282 finish_expr_stmt (iter_incr);
6283 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6284 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6287 TREE_VEC_ELT (declv, i) = decl;
6288 TREE_VEC_ELT (initv, i) = init;
6289 TREE_VEC_ELT (condv, i) = cond;
6290 TREE_VEC_ELT (incrv, i) = incr;
6292 return false;
6295 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6296 are directly for their associated operands in the statement. DECL
6297 and INIT are a combo; if DECL is NULL then INIT ought to be a
6298 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6299 optional statements that need to go before the loop into its
6300 sk_omp scope. */
6302 tree
6303 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6304 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6306 tree omp_for = NULL, orig_incr = NULL;
6307 tree decl, init, cond, incr;
6308 location_t elocus;
6309 int i;
6311 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6312 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6313 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6314 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6316 decl = TREE_VEC_ELT (declv, i);
6317 init = TREE_VEC_ELT (initv, i);
6318 cond = TREE_VEC_ELT (condv, i);
6319 incr = TREE_VEC_ELT (incrv, i);
6320 elocus = locus;
6322 if (decl == NULL)
6324 if (init != NULL)
6325 switch (TREE_CODE (init))
6327 case MODIFY_EXPR:
6328 decl = TREE_OPERAND (init, 0);
6329 init = TREE_OPERAND (init, 1);
6330 break;
6331 case MODOP_EXPR:
6332 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6334 decl = TREE_OPERAND (init, 0);
6335 init = TREE_OPERAND (init, 2);
6337 break;
6338 default:
6339 break;
6342 if (decl == NULL)
6344 error_at (locus,
6345 "expected iteration declaration or initialization");
6346 return NULL;
6350 if (init && EXPR_HAS_LOCATION (init))
6351 elocus = EXPR_LOCATION (init);
6353 if (cond == NULL)
6355 error_at (elocus, "missing controlling predicate");
6356 return NULL;
6359 if (incr == NULL)
6361 error_at (elocus, "missing increment expression");
6362 return NULL;
6365 TREE_VEC_ELT (declv, i) = decl;
6366 TREE_VEC_ELT (initv, i) = init;
6369 if (dependent_omp_for_p (declv, initv, condv, incrv))
6371 tree stmt;
6373 stmt = make_node (code);
6375 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6377 /* This is really just a place-holder. We'll be decomposing this
6378 again and going through the cp_build_modify_expr path below when
6379 we instantiate the thing. */
6380 TREE_VEC_ELT (initv, i)
6381 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6382 TREE_VEC_ELT (initv, i));
6385 TREE_TYPE (stmt) = void_type_node;
6386 OMP_FOR_INIT (stmt) = initv;
6387 OMP_FOR_COND (stmt) = condv;
6388 OMP_FOR_INCR (stmt) = incrv;
6389 OMP_FOR_BODY (stmt) = body;
6390 OMP_FOR_PRE_BODY (stmt) = pre_body;
6391 OMP_FOR_CLAUSES (stmt) = clauses;
6393 SET_EXPR_LOCATION (stmt, locus);
6394 return add_stmt (stmt);
6397 if (processing_template_decl)
6398 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6400 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6402 decl = TREE_VEC_ELT (declv, i);
6403 init = TREE_VEC_ELT (initv, i);
6404 cond = TREE_VEC_ELT (condv, i);
6405 incr = TREE_VEC_ELT (incrv, i);
6406 if (orig_incr)
6407 TREE_VEC_ELT (orig_incr, i) = incr;
6408 elocus = locus;
6410 if (init && EXPR_HAS_LOCATION (init))
6411 elocus = EXPR_LOCATION (init);
6413 if (!DECL_P (decl))
6415 error_at (elocus, "expected iteration declaration or initialization");
6416 return NULL;
6419 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6421 if (orig_incr)
6422 TREE_VEC_ELT (orig_incr, i) = incr;
6423 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6424 TREE_CODE (TREE_OPERAND (incr, 1)),
6425 TREE_OPERAND (incr, 2),
6426 tf_warning_or_error);
6429 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6431 if (code == OMP_SIMD)
6433 error_at (elocus, "%<#pragma omp simd%> used with class "
6434 "iteration variable %qE", decl);
6435 return NULL;
6437 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6438 incrv, &body, &pre_body, clauses))
6439 return NULL;
6440 continue;
6443 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6444 && !TYPE_PTR_P (TREE_TYPE (decl)))
6446 error_at (elocus, "invalid type for iteration variable %qE", decl);
6447 return NULL;
6450 if (!processing_template_decl)
6452 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6453 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6455 else
6456 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6457 if (cond
6458 && TREE_SIDE_EFFECTS (cond)
6459 && COMPARISON_CLASS_P (cond)
6460 && !processing_template_decl)
6462 tree t = TREE_OPERAND (cond, 0);
6463 if (TREE_SIDE_EFFECTS (t)
6464 && t != decl
6465 && (TREE_CODE (t) != NOP_EXPR
6466 || TREE_OPERAND (t, 0) != decl))
6467 TREE_OPERAND (cond, 0)
6468 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6470 t = TREE_OPERAND (cond, 1);
6471 if (TREE_SIDE_EFFECTS (t)
6472 && t != decl
6473 && (TREE_CODE (t) != NOP_EXPR
6474 || TREE_OPERAND (t, 0) != decl))
6475 TREE_OPERAND (cond, 1)
6476 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6478 if (decl == error_mark_node || init == error_mark_node)
6479 return NULL;
6481 TREE_VEC_ELT (declv, i) = decl;
6482 TREE_VEC_ELT (initv, i) = init;
6483 TREE_VEC_ELT (condv, i) = cond;
6484 TREE_VEC_ELT (incrv, i) = incr;
6485 i++;
6488 if (IS_EMPTY_STMT (pre_body))
6489 pre_body = NULL;
6491 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6492 body, pre_body);
6494 if (omp_for == NULL)
6495 return NULL;
6497 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6499 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6500 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6502 if (TREE_CODE (incr) != MODIFY_EXPR)
6503 continue;
6505 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6506 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6507 && !processing_template_decl)
6509 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6510 if (TREE_SIDE_EFFECTS (t)
6511 && t != decl
6512 && (TREE_CODE (t) != NOP_EXPR
6513 || TREE_OPERAND (t, 0) != decl))
6514 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6515 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6517 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6518 if (TREE_SIDE_EFFECTS (t)
6519 && t != decl
6520 && (TREE_CODE (t) != NOP_EXPR
6521 || TREE_OPERAND (t, 0) != decl))
6522 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6523 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6526 if (orig_incr)
6527 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6529 if (omp_for != NULL)
6530 OMP_FOR_CLAUSES (omp_for) = clauses;
6531 return omp_for;
6534 void
6535 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6536 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6538 tree orig_lhs;
6539 tree orig_rhs;
6540 tree orig_v;
6541 tree orig_lhs1;
6542 tree orig_rhs1;
6543 bool dependent_p;
6544 tree stmt;
6546 orig_lhs = lhs;
6547 orig_rhs = rhs;
6548 orig_v = v;
6549 orig_lhs1 = lhs1;
6550 orig_rhs1 = rhs1;
6551 dependent_p = false;
6552 stmt = NULL_TREE;
6554 /* Even in a template, we can detect invalid uses of the atomic
6555 pragma if neither LHS nor RHS is type-dependent. */
6556 if (processing_template_decl)
6558 dependent_p = (type_dependent_expression_p (lhs)
6559 || (rhs && type_dependent_expression_p (rhs))
6560 || (v && type_dependent_expression_p (v))
6561 || (lhs1 && type_dependent_expression_p (lhs1))
6562 || (rhs1 && type_dependent_expression_p (rhs1)));
6563 if (!dependent_p)
6565 lhs = build_non_dependent_expr (lhs);
6566 if (rhs)
6567 rhs = build_non_dependent_expr (rhs);
6568 if (v)
6569 v = build_non_dependent_expr (v);
6570 if (lhs1)
6571 lhs1 = build_non_dependent_expr (lhs1);
6572 if (rhs1)
6573 rhs1 = build_non_dependent_expr (rhs1);
6576 if (!dependent_p)
6578 bool swapped = false;
6579 if (rhs1 && cp_tree_equal (lhs, rhs))
6581 tree tem = rhs;
6582 rhs = rhs1;
6583 rhs1 = tem;
6584 swapped = !commutative_tree_code (opcode);
6586 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6588 if (code == OMP_ATOMIC)
6589 error ("%<#pragma omp atomic update%> uses two different "
6590 "expressions for memory");
6591 else
6592 error ("%<#pragma omp atomic capture%> uses two different "
6593 "expressions for memory");
6594 return;
6596 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6598 if (code == OMP_ATOMIC)
6599 error ("%<#pragma omp atomic update%> uses two different "
6600 "expressions for memory");
6601 else
6602 error ("%<#pragma omp atomic capture%> uses two different "
6603 "expressions for memory");
6604 return;
6606 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6607 v, lhs1, rhs1, swapped, seq_cst);
6608 if (stmt == error_mark_node)
6609 return;
6611 if (processing_template_decl)
6613 if (code == OMP_ATOMIC_READ)
6615 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6616 OMP_ATOMIC_READ, orig_lhs);
6617 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6618 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6620 else
6622 if (opcode == NOP_EXPR)
6623 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6624 else
6625 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6626 if (orig_rhs1)
6627 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6628 COMPOUND_EXPR, orig_rhs1, stmt);
6629 if (code != OMP_ATOMIC)
6631 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6632 code, orig_lhs1, stmt);
6633 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6634 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6637 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6638 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6640 finish_expr_stmt (stmt);
6643 void
6644 finish_omp_barrier (void)
6646 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6647 vec<tree, va_gc> *vec = make_tree_vector ();
6648 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6649 release_tree_vector (vec);
6650 finish_expr_stmt (stmt);
6653 void
6654 finish_omp_flush (void)
6656 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6657 vec<tree, va_gc> *vec = make_tree_vector ();
6658 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6659 release_tree_vector (vec);
6660 finish_expr_stmt (stmt);
6663 void
6664 finish_omp_taskwait (void)
6666 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6667 vec<tree, va_gc> *vec = make_tree_vector ();
6668 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6669 release_tree_vector (vec);
6670 finish_expr_stmt (stmt);
6673 void
6674 finish_omp_taskyield (void)
6676 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6677 vec<tree, va_gc> *vec = make_tree_vector ();
6678 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6679 release_tree_vector (vec);
6680 finish_expr_stmt (stmt);
6683 void
6684 finish_omp_cancel (tree clauses)
6686 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6687 int mask = 0;
6688 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6689 mask = 1;
6690 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6691 mask = 2;
6692 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6693 mask = 4;
6694 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6695 mask = 8;
6696 else
6698 error ("%<#pragma omp cancel must specify one of "
6699 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6700 return;
6702 vec<tree, va_gc> *vec = make_tree_vector ();
6703 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6704 if (ifc != NULL_TREE)
6706 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6707 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6708 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6709 build_zero_cst (type));
6711 else
6712 ifc = boolean_true_node;
6713 vec->quick_push (build_int_cst (integer_type_node, mask));
6714 vec->quick_push (ifc);
6715 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6716 release_tree_vector (vec);
6717 finish_expr_stmt (stmt);
6720 void
6721 finish_omp_cancellation_point (tree clauses)
6723 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6724 int mask = 0;
6725 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6726 mask = 1;
6727 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6728 mask = 2;
6729 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6730 mask = 4;
6731 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6732 mask = 8;
6733 else
6735 error ("%<#pragma omp cancellation point must specify one of "
6736 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6737 return;
6739 vec<tree, va_gc> *vec
6740 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6741 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6742 release_tree_vector (vec);
6743 finish_expr_stmt (stmt);
6746 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6747 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6748 should create an extra compound stmt. */
6750 tree
6751 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6753 tree r;
6755 if (pcompound)
6756 *pcompound = begin_compound_stmt (0);
6758 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6760 /* Only add the statement to the function if support enabled. */
6761 if (flag_tm)
6762 add_stmt (r);
6763 else
6764 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6765 ? G_("%<__transaction_relaxed%> without "
6766 "transactional memory support enabled")
6767 : G_("%<__transaction_atomic%> without "
6768 "transactional memory support enabled")));
6770 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6771 TREE_SIDE_EFFECTS (r) = 1;
6772 return r;
6775 /* End a __transaction_atomic or __transaction_relaxed statement.
6776 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6777 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6778 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6780 void
6781 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6783 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6784 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6785 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6786 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6788 /* noexcept specifications are not allowed for function transactions. */
6789 gcc_assert (!(noex && compound_stmt));
6790 if (noex)
6792 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6793 noex);
6794 /* This may not be true when the STATEMENT_LIST is empty. */
6795 if (EXPR_P (body))
6796 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6797 TREE_SIDE_EFFECTS (body) = 1;
6798 TRANSACTION_EXPR_BODY (stmt) = body;
6801 if (compound_stmt)
6802 finish_compound_stmt (compound_stmt);
6805 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6806 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6807 condition. */
6809 tree
6810 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6812 tree ret;
6813 if (noex)
6815 expr = build_must_not_throw_expr (expr, noex);
6816 if (EXPR_P (expr))
6817 SET_EXPR_LOCATION (expr, loc);
6818 TREE_SIDE_EFFECTS (expr) = 1;
6820 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6821 if (flags & TM_STMT_ATTR_RELAXED)
6822 TRANSACTION_EXPR_RELAXED (ret) = 1;
6823 TREE_SIDE_EFFECTS (ret) = 1;
6824 SET_EXPR_LOCATION (ret, loc);
6825 return ret;
6828 void
6829 init_cp_semantics (void)
6833 /* Build a STATIC_ASSERT for a static assertion with the condition
6834 CONDITION and the message text MESSAGE. LOCATION is the location
6835 of the static assertion in the source code. When MEMBER_P, this
6836 static assertion is a member of a class. */
6837 void
6838 finish_static_assert (tree condition, tree message, location_t location,
6839 bool member_p)
6841 if (message == NULL_TREE
6842 || message == error_mark_node
6843 || condition == NULL_TREE
6844 || condition == error_mark_node)
6845 return;
6847 if (check_for_bare_parameter_packs (condition))
6848 condition = error_mark_node;
6850 if (type_dependent_expression_p (condition)
6851 || value_dependent_expression_p (condition))
6853 /* We're in a template; build a STATIC_ASSERT and put it in
6854 the right place. */
6855 tree assertion;
6857 assertion = make_node (STATIC_ASSERT);
6858 STATIC_ASSERT_CONDITION (assertion) = condition;
6859 STATIC_ASSERT_MESSAGE (assertion) = message;
6860 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
6862 if (member_p)
6863 maybe_add_class_template_decl_list (current_class_type,
6864 assertion,
6865 /*friend_p=*/0);
6866 else
6867 add_stmt (assertion);
6869 return;
6872 /* Fold the expression and convert it to a boolean value. */
6873 condition = fold_non_dependent_expr (condition);
6874 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
6875 condition = maybe_constant_value (condition);
6877 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
6878 /* Do nothing; the condition is satisfied. */
6880 else
6882 location_t saved_loc = input_location;
6884 input_location = location;
6885 if (TREE_CODE (condition) == INTEGER_CST
6886 && integer_zerop (condition))
6887 /* Report the error. */
6888 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
6889 else if (condition && condition != error_mark_node)
6891 error ("non-constant condition for static assertion");
6892 if (require_potential_rvalue_constant_expression (condition))
6893 cxx_constant_value (condition);
6895 input_location = saved_loc;
6899 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6900 suitable for use as a type-specifier.
6902 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6903 id-expression or a class member access, FALSE when it was parsed as
6904 a full expression. */
6906 tree
6907 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
6908 tsubst_flags_t complain)
6910 tree type = NULL_TREE;
6912 if (!expr || error_operand_p (expr))
6913 return error_mark_node;
6915 if (TYPE_P (expr)
6916 || TREE_CODE (expr) == TYPE_DECL
6917 || (TREE_CODE (expr) == BIT_NOT_EXPR
6918 && TYPE_P (TREE_OPERAND (expr, 0))))
6920 if (complain & tf_error)
6921 error ("argument to decltype must be an expression");
6922 return error_mark_node;
6925 /* Depending on the resolution of DR 1172, we may later need to distinguish
6926 instantiation-dependent but not type-dependent expressions so that, say,
6927 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6928 if (instantiation_dependent_expression_p (expr))
6930 type = cxx_make_type (DECLTYPE_TYPE);
6931 DECLTYPE_TYPE_EXPR (type) = expr;
6932 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
6933 = id_expression_or_member_access_p;
6934 SET_TYPE_STRUCTURAL_EQUALITY (type);
6936 return type;
6939 /* The type denoted by decltype(e) is defined as follows: */
6941 expr = resolve_nondeduced_context (expr);
6943 if (invalid_nonstatic_memfn_p (expr, complain))
6944 return error_mark_node;
6946 if (type_unknown_p (expr))
6948 if (complain & tf_error)
6949 error ("decltype cannot resolve address of overloaded function");
6950 return error_mark_node;
6953 /* To get the size of a static data member declared as an array of
6954 unknown bound, we need to instantiate it. */
6955 if (VAR_P (expr)
6956 && VAR_HAD_UNKNOWN_BOUND (expr)
6957 && DECL_TEMPLATE_INSTANTIATION (expr))
6958 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
6960 if (id_expression_or_member_access_p)
6962 /* If e is an id-expression or a class member access (5.2.5
6963 [expr.ref]), decltype(e) is defined as the type of the entity
6964 named by e. If there is no such entity, or e names a set of
6965 overloaded functions, the program is ill-formed. */
6966 if (identifier_p (expr))
6967 expr = lookup_name (expr);
6969 if (INDIRECT_REF_P (expr))
6970 /* This can happen when the expression is, e.g., "a.b". Just
6971 look at the underlying operand. */
6972 expr = TREE_OPERAND (expr, 0);
6974 if (TREE_CODE (expr) == OFFSET_REF
6975 || TREE_CODE (expr) == MEMBER_REF
6976 || TREE_CODE (expr) == SCOPE_REF)
6977 /* We're only interested in the field itself. If it is a
6978 BASELINK, we will need to see through it in the next
6979 step. */
6980 expr = TREE_OPERAND (expr, 1);
6982 if (BASELINK_P (expr))
6983 /* See through BASELINK nodes to the underlying function. */
6984 expr = BASELINK_FUNCTIONS (expr);
6986 switch (TREE_CODE (expr))
6988 case FIELD_DECL:
6989 if (DECL_BIT_FIELD_TYPE (expr))
6991 type = DECL_BIT_FIELD_TYPE (expr);
6992 break;
6994 /* Fall through for fields that aren't bitfields. */
6996 case FUNCTION_DECL:
6997 case VAR_DECL:
6998 case CONST_DECL:
6999 case PARM_DECL:
7000 case RESULT_DECL:
7001 case TEMPLATE_PARM_INDEX:
7002 expr = mark_type_use (expr);
7003 type = TREE_TYPE (expr);
7004 break;
7006 case ERROR_MARK:
7007 type = error_mark_node;
7008 break;
7010 case COMPONENT_REF:
7011 case COMPOUND_EXPR:
7012 mark_type_use (expr);
7013 type = is_bitfield_expr_with_lowered_type (expr);
7014 if (!type)
7015 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7016 break;
7018 case BIT_FIELD_REF:
7019 gcc_unreachable ();
7021 case INTEGER_CST:
7022 case PTRMEM_CST:
7023 /* We can get here when the id-expression refers to an
7024 enumerator or non-type template parameter. */
7025 type = TREE_TYPE (expr);
7026 break;
7028 default:
7029 /* Handle instantiated template non-type arguments. */
7030 type = TREE_TYPE (expr);
7031 break;
7034 else
7036 /* Within a lambda-expression:
7038 Every occurrence of decltype((x)) where x is a possibly
7039 parenthesized id-expression that names an entity of
7040 automatic storage duration is treated as if x were
7041 transformed into an access to a corresponding data member
7042 of the closure type that would have been declared if x
7043 were a use of the denoted entity. */
7044 if (outer_automatic_var_p (expr)
7045 && current_function_decl
7046 && LAMBDA_FUNCTION_P (current_function_decl))
7047 type = capture_decltype (expr);
7048 else if (error_operand_p (expr))
7049 type = error_mark_node;
7050 else if (expr == current_class_ptr)
7051 /* If the expression is just "this", we want the
7052 cv-unqualified pointer for the "this" type. */
7053 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7054 else
7056 /* Otherwise, where T is the type of e, if e is an lvalue,
7057 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7058 cp_lvalue_kind clk = lvalue_kind (expr);
7059 type = unlowered_expr_type (expr);
7060 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7062 /* For vector types, pick a non-opaque variant. */
7063 if (TREE_CODE (type) == VECTOR_TYPE)
7064 type = strip_typedefs (type);
7066 if (clk != clk_none && !(clk & clk_class))
7067 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7071 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
7072 && (flag_iso || warn_vla > 0))
7074 if (complain & tf_warning_or_error)
7075 pedwarn (input_location, OPT_Wvla,
7076 "taking decltype of array of runtime bound");
7077 else
7078 return error_mark_node;
7081 return type;
7084 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7085 __has_nothrow_copy, depending on assign_p. */
7087 static bool
7088 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7090 tree fns;
7092 if (assign_p)
7094 int ix;
7095 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7096 if (ix < 0)
7097 return false;
7098 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7100 else if (TYPE_HAS_COPY_CTOR (type))
7102 /* If construction of the copy constructor was postponed, create
7103 it now. */
7104 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7105 lazily_declare_fn (sfk_copy_constructor, type);
7106 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7107 lazily_declare_fn (sfk_move_constructor, type);
7108 fns = CLASSTYPE_CONSTRUCTORS (type);
7110 else
7111 return false;
7113 for (; fns; fns = OVL_NEXT (fns))
7115 tree fn = OVL_CURRENT (fns);
7117 if (assign_p)
7119 if (copy_fn_p (fn) == 0)
7120 continue;
7122 else if (copy_fn_p (fn) <= 0)
7123 continue;
7125 maybe_instantiate_noexcept (fn);
7126 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7127 return false;
7130 return true;
7133 /* Actually evaluates the trait. */
7135 static bool
7136 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7138 enum tree_code type_code1;
7139 tree t;
7141 type_code1 = TREE_CODE (type1);
7143 switch (kind)
7145 case CPTK_HAS_NOTHROW_ASSIGN:
7146 type1 = strip_array_types (type1);
7147 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7148 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7149 || (CLASS_TYPE_P (type1)
7150 && classtype_has_nothrow_assign_or_copy_p (type1,
7151 true))));
7153 case CPTK_HAS_TRIVIAL_ASSIGN:
7154 /* ??? The standard seems to be missing the "or array of such a class
7155 type" wording for this trait. */
7156 type1 = strip_array_types (type1);
7157 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7158 && (trivial_type_p (type1)
7159 || (CLASS_TYPE_P (type1)
7160 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7162 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7163 type1 = strip_array_types (type1);
7164 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7165 || (CLASS_TYPE_P (type1)
7166 && (t = locate_ctor (type1))
7167 && (maybe_instantiate_noexcept (t),
7168 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7170 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7171 type1 = strip_array_types (type1);
7172 return (trivial_type_p (type1)
7173 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7175 case CPTK_HAS_NOTHROW_COPY:
7176 type1 = strip_array_types (type1);
7177 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7178 || (CLASS_TYPE_P (type1)
7179 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7181 case CPTK_HAS_TRIVIAL_COPY:
7182 /* ??? The standard seems to be missing the "or array of such a class
7183 type" wording for this trait. */
7184 type1 = strip_array_types (type1);
7185 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7186 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7188 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7189 type1 = strip_array_types (type1);
7190 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7191 || (CLASS_TYPE_P (type1)
7192 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7194 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7195 return type_has_virtual_destructor (type1);
7197 case CPTK_IS_ABSTRACT:
7198 return (ABSTRACT_CLASS_TYPE_P (type1));
7200 case CPTK_IS_BASE_OF:
7201 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7202 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7203 || DERIVED_FROM_P (type1, type2)));
7205 case CPTK_IS_CLASS:
7206 return (NON_UNION_CLASS_TYPE_P (type1));
7208 case CPTK_IS_CONVERTIBLE_TO:
7209 /* TODO */
7210 return false;
7212 case CPTK_IS_EMPTY:
7213 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7215 case CPTK_IS_ENUM:
7216 return (type_code1 == ENUMERAL_TYPE);
7218 case CPTK_IS_FINAL:
7219 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7221 case CPTK_IS_LITERAL_TYPE:
7222 return (literal_type_p (type1));
7224 case CPTK_IS_POD:
7225 return (pod_type_p (type1));
7227 case CPTK_IS_POLYMORPHIC:
7228 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7230 case CPTK_IS_STD_LAYOUT:
7231 return (std_layout_type_p (type1));
7233 case CPTK_IS_TRIVIAL:
7234 return (trivial_type_p (type1));
7236 case CPTK_IS_UNION:
7237 return (type_code1 == UNION_TYPE);
7239 default:
7240 gcc_unreachable ();
7241 return false;
7245 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7246 void, or a complete type, returns it, otherwise NULL_TREE. */
7248 static tree
7249 check_trait_type (tree type)
7251 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7252 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7253 return type;
7255 if (VOID_TYPE_P (type))
7256 return type;
7258 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7261 /* Process a trait expression. */
7263 tree
7264 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7266 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7267 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7268 || kind == CPTK_HAS_NOTHROW_COPY
7269 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7270 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7271 || kind == CPTK_HAS_TRIVIAL_COPY
7272 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7273 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7274 || kind == CPTK_IS_ABSTRACT
7275 || kind == CPTK_IS_BASE_OF
7276 || kind == CPTK_IS_CLASS
7277 || kind == CPTK_IS_CONVERTIBLE_TO
7278 || kind == CPTK_IS_EMPTY
7279 || kind == CPTK_IS_ENUM
7280 || kind == CPTK_IS_FINAL
7281 || kind == CPTK_IS_LITERAL_TYPE
7282 || kind == CPTK_IS_POD
7283 || kind == CPTK_IS_POLYMORPHIC
7284 || kind == CPTK_IS_STD_LAYOUT
7285 || kind == CPTK_IS_TRIVIAL
7286 || kind == CPTK_IS_UNION);
7288 if (kind == CPTK_IS_CONVERTIBLE_TO)
7290 sorry ("__is_convertible_to");
7291 return error_mark_node;
7294 if (type1 == error_mark_node
7295 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
7296 && type2 == error_mark_node))
7297 return error_mark_node;
7299 if (processing_template_decl)
7301 tree trait_expr = make_node (TRAIT_EXPR);
7302 TREE_TYPE (trait_expr) = boolean_type_node;
7303 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7304 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7305 TRAIT_EXPR_KIND (trait_expr) = kind;
7306 return trait_expr;
7309 switch (kind)
7311 case CPTK_HAS_NOTHROW_ASSIGN:
7312 case CPTK_HAS_TRIVIAL_ASSIGN:
7313 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7314 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7315 case CPTK_HAS_NOTHROW_COPY:
7316 case CPTK_HAS_TRIVIAL_COPY:
7317 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7318 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7319 case CPTK_IS_ABSTRACT:
7320 case CPTK_IS_EMPTY:
7321 case CPTK_IS_FINAL:
7322 case CPTK_IS_LITERAL_TYPE:
7323 case CPTK_IS_POD:
7324 case CPTK_IS_POLYMORPHIC:
7325 case CPTK_IS_STD_LAYOUT:
7326 case CPTK_IS_TRIVIAL:
7327 if (!check_trait_type (type1))
7328 return error_mark_node;
7329 break;
7331 case CPTK_IS_BASE_OF:
7332 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7333 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7334 && !complete_type_or_else (type2, NULL_TREE))
7335 /* We already issued an error. */
7336 return error_mark_node;
7337 break;
7339 case CPTK_IS_CLASS:
7340 case CPTK_IS_ENUM:
7341 case CPTK_IS_UNION:
7342 break;
7344 case CPTK_IS_CONVERTIBLE_TO:
7345 default:
7346 gcc_unreachable ();
7349 return (trait_expr_value (kind, type1, type2)
7350 ? boolean_true_node : boolean_false_node);
7353 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7354 which is ignored for C++. */
7356 void
7357 set_float_const_decimal64 (void)
7361 void
7362 clear_float_const_decimal64 (void)
7366 bool
7367 float_const_decimal64_p (void)
7369 return 0;
7373 /* Return true if T is a literal type. */
7375 bool
7376 literal_type_p (tree t)
7378 if (SCALAR_TYPE_P (t)
7379 || TREE_CODE (t) == VECTOR_TYPE
7380 || TREE_CODE (t) == REFERENCE_TYPE)
7381 return true;
7382 if (CLASS_TYPE_P (t))
7384 t = complete_type (t);
7385 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7386 return CLASSTYPE_LITERAL_P (t);
7388 if (TREE_CODE (t) == ARRAY_TYPE)
7389 return literal_type_p (strip_array_types (t));
7390 return false;
7393 /* If DECL is a variable declared `constexpr', require its type
7394 be literal. Return the DECL if OK, otherwise NULL. */
7396 tree
7397 ensure_literal_type_for_constexpr_object (tree decl)
7399 tree type = TREE_TYPE (decl);
7400 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7401 && !processing_template_decl)
7403 tree stype = strip_array_types (type);
7404 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
7405 /* Don't complain here, we'll complain about incompleteness
7406 when we try to initialize the variable. */;
7407 else if (!literal_type_p (type))
7409 error ("the type %qT of constexpr variable %qD is not literal",
7410 type, decl);
7411 explain_non_literal_class (type);
7412 return NULL;
7415 return decl;
7418 /* Representation of entries in the constexpr function definition table. */
7420 typedef struct GTY(()) constexpr_fundef {
7421 tree decl;
7422 tree body;
7423 } constexpr_fundef;
7425 /* This table holds all constexpr function definitions seen in
7426 the current translation unit. */
7428 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7430 /* Utility function used for managing the constexpr function table.
7431 Return true if the entries pointed to by P and Q are for the
7432 same constexpr function. */
7434 static inline int
7435 constexpr_fundef_equal (const void *p, const void *q)
7437 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7438 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7439 return lhs->decl == rhs->decl;
7442 /* Utility function used for managing the constexpr function table.
7443 Return a hash value for the entry pointed to by Q. */
7445 static inline hashval_t
7446 constexpr_fundef_hash (const void *p)
7448 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7449 return DECL_UID (fundef->decl);
7452 /* Return a previously saved definition of function FUN. */
7454 static constexpr_fundef *
7455 retrieve_constexpr_fundef (tree fun)
7457 constexpr_fundef fundef = { NULL, NULL };
7458 if (constexpr_fundef_table == NULL)
7459 return NULL;
7461 fundef.decl = fun;
7462 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7465 /* Check whether the parameter and return types of FUN are valid for a
7466 constexpr function, and complain if COMPLAIN. */
7468 static bool
7469 is_valid_constexpr_fn (tree fun, bool complain)
7471 bool ret = true;
7473 if (DECL_INHERITED_CTOR_BASE (fun)
7474 && TREE_CODE (fun) == TEMPLATE_DECL)
7476 ret = false;
7477 if (complain)
7478 error ("inherited constructor %qD is not constexpr",
7479 get_inherited_ctor (fun));
7481 else
7483 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
7484 parm != NULL_TREE; parm = TREE_CHAIN (parm))
7485 if (!literal_type_p (TREE_TYPE (parm)))
7487 ret = false;
7488 if (complain)
7490 error ("invalid type for parameter %d of constexpr "
7491 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7492 explain_non_literal_class (TREE_TYPE (parm));
7497 if (!DECL_CONSTRUCTOR_P (fun))
7499 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7500 if (!literal_type_p (rettype))
7502 ret = false;
7503 if (complain)
7505 error ("invalid return type %qT of constexpr function %q+D",
7506 rettype, fun);
7507 explain_non_literal_class (rettype);
7511 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7512 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7514 ret = false;
7515 if (complain)
7517 error ("enclosing class of constexpr non-static member "
7518 "function %q+#D is not a literal type", fun);
7519 explain_non_literal_class (DECL_CONTEXT (fun));
7523 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7525 ret = false;
7526 if (complain)
7527 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7530 return ret;
7533 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7534 for a member of an anonymous aggregate, INIT is the initializer for that
7535 member, and VEC_OUTER is the vector of constructor elements for the class
7536 whose constructor we are processing. Add the initializer to the vector
7537 and return true to indicate success. */
7539 static bool
7540 build_anon_member_initialization (tree member, tree init,
7541 vec<constructor_elt, va_gc> **vec_outer)
7543 /* MEMBER presents the relevant fields from the inside out, but we need
7544 to build up the initializer from the outside in so that we can reuse
7545 previously built CONSTRUCTORs if this is, say, the second field in an
7546 anonymous struct. So we use a vec as a stack. */
7547 auto_vec<tree, 2> fields;
7550 fields.safe_push (TREE_OPERAND (member, 1));
7551 member = TREE_OPERAND (member, 0);
7553 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
7554 && TREE_CODE (member) == COMPONENT_REF);
7556 /* VEC has the constructor elements vector for the context of FIELD.
7557 If FIELD is an anonymous aggregate, we will push inside it. */
7558 vec<constructor_elt, va_gc> **vec = vec_outer;
7559 tree field;
7560 while (field = fields.pop(),
7561 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7563 tree ctor;
7564 /* If there is already an outer constructor entry for the anonymous
7565 aggregate FIELD, use it; otherwise, insert one. */
7566 if (vec_safe_is_empty (*vec)
7567 || (*vec)->last().index != field)
7569 ctor = build_constructor (TREE_TYPE (field), NULL);
7570 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7572 else
7573 ctor = (*vec)->last().value;
7574 vec = &CONSTRUCTOR_ELTS (ctor);
7577 /* Now we're at the innermost field, the one that isn't an anonymous
7578 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7579 gcc_assert (fields.is_empty());
7580 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7582 return true;
7585 /* Subroutine of build_constexpr_constructor_member_initializers.
7586 The expression tree T represents a data member initialization
7587 in a (constexpr) constructor definition. Build a pairing of
7588 the data member with its initializer, and prepend that pair
7589 to the existing initialization pair INITS. */
7591 static bool
7592 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7594 tree member, init;
7595 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7596 t = TREE_OPERAND (t, 0);
7597 if (TREE_CODE (t) == EXPR_STMT)
7598 t = TREE_OPERAND (t, 0);
7599 if (t == error_mark_node)
7600 return false;
7601 if (TREE_CODE (t) == STATEMENT_LIST)
7603 tree_stmt_iterator i;
7604 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7606 if (! build_data_member_initialization (tsi_stmt (i), vec))
7607 return false;
7609 return true;
7611 if (TREE_CODE (t) == CLEANUP_STMT)
7613 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7614 but we can in a constexpr constructor for a non-literal class. Just
7615 ignore it; either all the initialization will be constant, in which
7616 case the cleanup can't run, or it can't be constexpr.
7617 Still recurse into CLEANUP_BODY. */
7618 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7620 if (TREE_CODE (t) == CONVERT_EXPR)
7621 t = TREE_OPERAND (t, 0);
7622 if (TREE_CODE (t) == INIT_EXPR
7623 || TREE_CODE (t) == MODIFY_EXPR)
7625 member = TREE_OPERAND (t, 0);
7626 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7628 else if (TREE_CODE (t) == CALL_EXPR)
7630 member = CALL_EXPR_ARG (t, 0);
7631 /* We don't use build_cplus_new here because it complains about
7632 abstract bases. Leaving the call unwrapped means that it has the
7633 wrong type, but cxx_eval_constant_expression doesn't care. */
7634 init = break_out_target_exprs (t);
7636 else if (TREE_CODE (t) == DECL_EXPR)
7637 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7638 return true;
7639 else
7640 gcc_unreachable ();
7641 if (INDIRECT_REF_P (member))
7642 member = TREE_OPERAND (member, 0);
7643 if (TREE_CODE (member) == NOP_EXPR)
7645 tree op = member;
7646 STRIP_NOPS (op);
7647 if (TREE_CODE (op) == ADDR_EXPR)
7649 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7650 (TREE_TYPE (TREE_TYPE (op)),
7651 TREE_TYPE (TREE_TYPE (member))));
7652 /* Initializing a cv-qualified member; we need to look through
7653 the const_cast. */
7654 member = op;
7656 else if (op == current_class_ptr
7657 && (same_type_ignoring_top_level_qualifiers_p
7658 (TREE_TYPE (TREE_TYPE (member)),
7659 current_class_type)))
7660 /* Delegating constructor. */
7661 member = op;
7662 else
7664 /* This is an initializer for an empty base; keep it for now so
7665 we can check it in cxx_eval_bare_aggregate. */
7666 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7669 if (TREE_CODE (member) == ADDR_EXPR)
7670 member = TREE_OPERAND (member, 0);
7671 if (TREE_CODE (member) == COMPONENT_REF)
7673 tree aggr = TREE_OPERAND (member, 0);
7674 if (TREE_CODE (aggr) != COMPONENT_REF)
7675 /* Normal member initialization. */
7676 member = TREE_OPERAND (member, 1);
7677 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7678 /* Initializing a member of an anonymous union. */
7679 return build_anon_member_initialization (member, init, vec);
7680 else
7681 /* We're initializing a vtable pointer in a base. Leave it as
7682 COMPONENT_REF so we remember the path to get to the vfield. */
7683 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7686 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7687 return true;
7690 /* Make sure that there are no statements after LAST in the constructor
7691 body represented by LIST. */
7693 bool
7694 check_constexpr_ctor_body (tree last, tree list)
7696 bool ok = true;
7697 if (TREE_CODE (list) == STATEMENT_LIST)
7699 tree_stmt_iterator i = tsi_last (list);
7700 for (; !tsi_end_p (i); tsi_prev (&i))
7702 tree t = tsi_stmt (i);
7703 if (t == last)
7704 break;
7705 if (TREE_CODE (t) == BIND_EXPR)
7707 if (BIND_EXPR_VARS (t))
7709 ok = false;
7710 break;
7712 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7713 return false;
7714 else
7715 continue;
7717 /* We currently allow typedefs and static_assert.
7718 FIXME allow them in the standard, too. */
7719 if (TREE_CODE (t) != STATIC_ASSERT)
7721 ok = false;
7722 break;
7726 else if (list != last
7727 && TREE_CODE (list) != STATIC_ASSERT)
7728 ok = false;
7729 if (!ok)
7731 error ("constexpr constructor does not have empty body");
7732 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7734 return ok;
7737 /* V is a vector of constructor elements built up for the base and member
7738 initializers of a constructor for TYPE. They need to be in increasing
7739 offset order, which they might not be yet if TYPE has a primary base
7740 which is not first in the base-clause or a vptr and at least one base
7741 all of which are non-primary. */
7743 static vec<constructor_elt, va_gc> *
7744 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7746 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7747 tree field_type;
7748 unsigned i;
7749 constructor_elt *ce;
7751 if (pri)
7752 field_type = BINFO_TYPE (pri);
7753 else if (TYPE_CONTAINS_VPTR_P (type))
7754 field_type = vtbl_ptr_type_node;
7755 else
7756 return v;
7758 /* Find the element for the primary base or vptr and move it to the
7759 beginning of the vec. */
7760 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7761 if (TREE_TYPE (ce->index) == field_type)
7762 break;
7764 if (i > 0 && i < vec_safe_length (v))
7766 vec<constructor_elt, va_gc> &vref = *v;
7767 constructor_elt elt = vref[i];
7768 for (; i > 0; --i)
7769 vref[i] = vref[i-1];
7770 vref[0] = elt;
7773 return v;
7776 /* Build compile-time evalable representations of member-initializer list
7777 for a constexpr constructor. */
7779 static tree
7780 build_constexpr_constructor_member_initializers (tree type, tree body)
7782 vec<constructor_elt, va_gc> *vec = NULL;
7783 bool ok = true;
7784 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7785 || TREE_CODE (body) == EH_SPEC_BLOCK)
7786 body = TREE_OPERAND (body, 0);
7787 if (TREE_CODE (body) == STATEMENT_LIST)
7788 body = STATEMENT_LIST_HEAD (body)->stmt;
7789 body = BIND_EXPR_BODY (body);
7790 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7792 body = TREE_OPERAND (body, 0);
7793 if (TREE_CODE (body) == EXPR_STMT)
7794 body = TREE_OPERAND (body, 0);
7795 if (TREE_CODE (body) == INIT_EXPR
7796 && (same_type_ignoring_top_level_qualifiers_p
7797 (TREE_TYPE (TREE_OPERAND (body, 0)),
7798 current_class_type)))
7800 /* Trivial copy. */
7801 return TREE_OPERAND (body, 1);
7803 ok = build_data_member_initialization (body, &vec);
7805 else if (TREE_CODE (body) == STATEMENT_LIST)
7807 tree_stmt_iterator i;
7808 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7810 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7811 if (!ok)
7812 break;
7815 else if (TREE_CODE (body) == TRY_BLOCK)
7817 error ("body of %<constexpr%> constructor cannot be "
7818 "a function-try-block");
7819 return error_mark_node;
7821 else if (EXPR_P (body))
7822 ok = build_data_member_initialization (body, &vec);
7823 else
7824 gcc_assert (errorcount > 0);
7825 if (ok)
7827 if (vec_safe_length (vec) > 0)
7829 /* In a delegating constructor, return the target. */
7830 constructor_elt *ce = &(*vec)[0];
7831 if (ce->index == current_class_ptr)
7833 body = ce->value;
7834 vec_free (vec);
7835 return body;
7838 vec = sort_constexpr_mem_initializers (type, vec);
7839 return build_constructor (type, vec);
7841 else
7842 return error_mark_node;
7845 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7846 declared to be constexpr, or a sub-statement thereof. Returns the
7847 return value if suitable, error_mark_node for a statement not allowed in
7848 a constexpr function, or NULL_TREE if no return value was found. */
7850 static tree
7851 constexpr_fn_retval (tree body)
7853 switch (TREE_CODE (body))
7855 case STATEMENT_LIST:
7857 tree_stmt_iterator i;
7858 tree expr = NULL_TREE;
7859 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7861 tree s = constexpr_fn_retval (tsi_stmt (i));
7862 if (s == error_mark_node)
7863 return error_mark_node;
7864 else if (s == NULL_TREE)
7865 /* Keep iterating. */;
7866 else if (expr)
7867 /* Multiple return statements. */
7868 return error_mark_node;
7869 else
7870 expr = s;
7872 return expr;
7875 case RETURN_EXPR:
7876 return break_out_target_exprs (TREE_OPERAND (body, 0));
7878 case DECL_EXPR:
7879 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
7880 return NULL_TREE;
7881 return error_mark_node;
7883 case CLEANUP_POINT_EXPR:
7884 return constexpr_fn_retval (TREE_OPERAND (body, 0));
7886 case USING_STMT:
7887 return NULL_TREE;
7889 default:
7890 return error_mark_node;
7894 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7895 FUN; do the necessary transformations to turn it into a single expression
7896 that we can store in the hash table. */
7898 static tree
7899 massage_constexpr_body (tree fun, tree body)
7901 if (DECL_CONSTRUCTOR_P (fun))
7902 body = build_constexpr_constructor_member_initializers
7903 (DECL_CONTEXT (fun), body);
7904 else
7906 if (TREE_CODE (body) == EH_SPEC_BLOCK)
7907 body = EH_SPEC_STMTS (body);
7908 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
7909 body = TREE_OPERAND (body, 0);
7910 if (TREE_CODE (body) == BIND_EXPR)
7911 body = BIND_EXPR_BODY (body);
7912 body = constexpr_fn_retval (body);
7914 return body;
7917 /* FUN is a constexpr constructor with massaged body BODY. Return true
7918 if some bases/fields are uninitialized, and complain if COMPLAIN. */
7920 static bool
7921 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
7923 bool bad;
7924 tree field;
7925 unsigned i, nelts;
7926 tree ctype;
7928 if (TREE_CODE (body) != CONSTRUCTOR)
7929 return false;
7931 nelts = CONSTRUCTOR_NELTS (body);
7932 ctype = DECL_CONTEXT (fun);
7933 field = TYPE_FIELDS (ctype);
7935 if (TREE_CODE (ctype) == UNION_TYPE)
7937 if (nelts == 0 && next_initializable_field (field))
7939 if (complain)
7940 error ("%<constexpr%> constructor for union %qT must "
7941 "initialize exactly one non-static data member", ctype);
7942 return true;
7944 return false;
7947 bad = false;
7948 for (i = 0; i <= nelts; ++i)
7950 tree index;
7951 if (i == nelts)
7952 index = NULL_TREE;
7953 else
7955 index = CONSTRUCTOR_ELT (body, i)->index;
7956 /* Skip base and vtable inits. */
7957 if (TREE_CODE (index) != FIELD_DECL
7958 || DECL_ARTIFICIAL (index))
7959 continue;
7961 for (; field != index; field = DECL_CHAIN (field))
7963 tree ftype;
7964 if (TREE_CODE (field) != FIELD_DECL
7965 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7966 || DECL_ARTIFICIAL (field))
7967 continue;
7968 ftype = strip_array_types (TREE_TYPE (field));
7969 if (type_has_constexpr_default_constructor (ftype))
7971 /* It's OK to skip a member with a trivial constexpr ctor.
7972 A constexpr ctor that isn't trivial should have been
7973 added in by now. */
7974 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
7975 || errorcount != 0);
7976 continue;
7978 if (!complain)
7979 return true;
7980 error ("uninitialized member %qD in %<constexpr%> constructor",
7981 field);
7982 bad = true;
7984 if (field == NULL_TREE)
7985 break;
7986 field = DECL_CHAIN (field);
7989 return bad;
7992 /* Clear constexpr hash table */
7994 void
7995 cp_clear_constexpr_hashtable (void)
7997 /* htab_delete (constexpr_fundef_table); */
7998 constexpr_fundef_table = NULL;
8001 /* We are processing the definition of the constexpr function FUN.
8002 Check that its BODY fulfills the propriate requirements and
8003 enter it in the constexpr function definition table.
8004 For constructor BODY is actually the TREE_LIST of the
8005 member-initializer list. */
8007 tree
8008 register_constexpr_fundef (tree fun, tree body)
8010 constexpr_fundef entry;
8011 constexpr_fundef **slot;
8013 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
8014 return NULL;
8016 body = massage_constexpr_body (fun, body);
8017 if (body == NULL_TREE || body == error_mark_node)
8019 if (!DECL_CONSTRUCTOR_P (fun))
8020 error ("body of constexpr function %qD not a return-statement", fun);
8021 return NULL;
8024 if (!potential_rvalue_constant_expression (body))
8026 if (!DECL_GENERATED_P (fun))
8027 require_potential_rvalue_constant_expression (body);
8028 return NULL;
8031 if (DECL_CONSTRUCTOR_P (fun)
8032 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
8033 return NULL;
8035 /* Create the constexpr function table if necessary. */
8036 if (constexpr_fundef_table == NULL)
8037 constexpr_fundef_table = htab_create_ggc (101,
8038 constexpr_fundef_hash,
8039 constexpr_fundef_equal,
8040 ggc_free);
8041 entry.decl = fun;
8042 entry.body = body;
8043 slot = (constexpr_fundef **)
8044 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
8046 gcc_assert (*slot == NULL);
8047 *slot = ggc_alloc<constexpr_fundef> ();
8048 **slot = entry;
8050 return fun;
8053 /* FUN is a non-constexpr function called in a context that requires a
8054 constant expression. If it comes from a constexpr template, explain why
8055 the instantiation isn't constexpr. */
8057 void
8058 explain_invalid_constexpr_fn (tree fun)
8060 static hash_set<tree> *diagnosed;
8061 tree body;
8062 location_t save_loc;
8063 /* Only diagnose defaulted functions or instantiations. */
8064 if (!DECL_DEFAULTED_FN (fun)
8065 && !is_instantiation_of_constexpr (fun))
8066 return;
8067 if (diagnosed == NULL)
8068 diagnosed = new hash_set<tree>;
8069 if (diagnosed->add (fun))
8070 /* Already explained. */
8071 return;
8073 save_loc = input_location;
8074 input_location = DECL_SOURCE_LOCATION (fun);
8075 inform (0, "%q+D is not usable as a constexpr function because:", fun);
8076 /* First check the declaration. */
8077 if (is_valid_constexpr_fn (fun, true))
8079 /* Then if it's OK, the body. */
8080 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8081 explain_implicit_non_constexpr (fun);
8082 else
8084 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
8085 require_potential_rvalue_constant_expression (body);
8086 if (DECL_CONSTRUCTOR_P (fun))
8087 cx_check_missing_mem_inits (fun, body, true);
8090 input_location = save_loc;
8093 /* Objects of this type represent calls to constexpr functions
8094 along with the bindings of parameters to their arguments, for
8095 the purpose of compile time evaluation. */
8097 typedef struct GTY(()) constexpr_call {
8098 /* Description of the constexpr function definition. */
8099 constexpr_fundef *fundef;
8100 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
8101 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
8102 Note: This arrangement is made to accommodate the use of
8103 iterative_hash_template_arg (see pt.c). If you change this
8104 representation, also change the hash calculation in
8105 cxx_eval_call_expression. */
8106 tree bindings;
8107 /* Result of the call.
8108 NULL means the call is being evaluated.
8109 error_mark_node means that the evaluation was erroneous;
8110 otherwise, the actuall value of the call. */
8111 tree result;
8112 /* The hash of this call; we remember it here to avoid having to
8113 recalculate it when expanding the hash table. */
8114 hashval_t hash;
8115 } constexpr_call;
8117 /* A table of all constexpr calls that have been evaluated by the
8118 compiler in this translation unit. */
8120 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8122 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8123 bool, bool, bool *, bool *);
8125 /* Compute a hash value for a constexpr call representation. */
8127 static hashval_t
8128 constexpr_call_hash (const void *p)
8130 const constexpr_call *info = (const constexpr_call *) p;
8131 return info->hash;
8134 /* Return 1 if the objects pointed to by P and Q represent calls
8135 to the same constexpr function with the same arguments.
8136 Otherwise, return 0. */
8138 static int
8139 constexpr_call_equal (const void *p, const void *q)
8141 const constexpr_call *lhs = (const constexpr_call *) p;
8142 const constexpr_call *rhs = (const constexpr_call *) q;
8143 tree lhs_bindings;
8144 tree rhs_bindings;
8145 if (lhs == rhs)
8146 return 1;
8147 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8148 return 0;
8149 lhs_bindings = lhs->bindings;
8150 rhs_bindings = rhs->bindings;
8151 while (lhs_bindings != NULL && rhs_bindings != NULL)
8153 tree lhs_arg = TREE_VALUE (lhs_bindings);
8154 tree rhs_arg = TREE_VALUE (rhs_bindings);
8155 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8156 if (!cp_tree_equal (lhs_arg, rhs_arg))
8157 return 0;
8158 lhs_bindings = TREE_CHAIN (lhs_bindings);
8159 rhs_bindings = TREE_CHAIN (rhs_bindings);
8161 return lhs_bindings == rhs_bindings;
8164 /* Initialize the constexpr call table, if needed. */
8166 static void
8167 maybe_initialize_constexpr_call_table (void)
8169 if (constexpr_call_table == NULL)
8170 constexpr_call_table = htab_create_ggc (101,
8171 constexpr_call_hash,
8172 constexpr_call_equal,
8173 ggc_free);
8176 /* Return true if T designates the implied `this' parameter. */
8178 bool
8179 is_this_parameter (tree t)
8181 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
8182 return false;
8183 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
8184 return true;
8187 /* We have an expression tree T that represents a call, either CALL_EXPR
8188 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8189 retrun the _DECL for that function. */
8191 static tree
8192 get_function_named_in_call (tree t)
8194 tree fun = NULL;
8195 switch (TREE_CODE (t))
8197 case CALL_EXPR:
8198 fun = CALL_EXPR_FN (t);
8199 break;
8201 case AGGR_INIT_EXPR:
8202 fun = AGGR_INIT_EXPR_FN (t);
8203 break;
8205 default:
8206 gcc_unreachable();
8207 break;
8209 if (TREE_CODE (fun) == ADDR_EXPR
8210 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8211 fun = TREE_OPERAND (fun, 0);
8212 return fun;
8215 /* We have an expression tree T that represents a call, either CALL_EXPR
8216 or AGGR_INIT_EXPR. Return the Nth argument. */
8218 static inline tree
8219 get_nth_callarg (tree t, int n)
8221 switch (TREE_CODE (t))
8223 case CALL_EXPR:
8224 return CALL_EXPR_ARG (t, n);
8226 case AGGR_INIT_EXPR:
8227 return AGGR_INIT_EXPR_ARG (t, n);
8229 default:
8230 gcc_unreachable ();
8231 return NULL;
8235 /* Look up the binding of the function parameter T in a constexpr
8236 function call context CALL. */
8238 static tree
8239 lookup_parameter_binding (const constexpr_call *call, tree t)
8241 tree b = purpose_member (t, call->bindings);
8242 return TREE_VALUE (b);
8245 /* Attempt to evaluate T which represents a call to a builtin function.
8246 We assume here that all builtin functions evaluate to scalar types
8247 represented by _CST nodes. */
8249 static tree
8250 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8251 bool allow_non_constant, bool addr,
8252 bool *non_constant_p, bool *overflow_p)
8254 const int nargs = call_expr_nargs (t);
8255 tree *args = (tree *) alloca (nargs * sizeof (tree));
8256 tree new_call;
8257 int i;
8258 for (i = 0; i < nargs; ++i)
8260 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8261 allow_non_constant, addr,
8262 non_constant_p, overflow_p);
8263 if (allow_non_constant && *non_constant_p)
8264 return t;
8266 if (*non_constant_p)
8267 return t;
8268 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8269 CALL_EXPR_FN (t), nargs, args);
8270 new_call = fold (new_call);
8271 VERIFY_CONSTANT (new_call);
8272 return new_call;
8275 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8276 the type of the value to match. */
8278 static tree
8279 adjust_temp_type (tree type, tree temp)
8281 if (TREE_TYPE (temp) == type)
8282 return temp;
8283 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8284 if (TREE_CODE (temp) == CONSTRUCTOR)
8285 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8286 gcc_assert (scalarish_type_p (type));
8287 return cp_fold_convert (type, temp);
8290 /* Subroutine of cxx_eval_call_expression.
8291 We are processing a call expression (either CALL_EXPR or
8292 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8293 all arguments and bind their values to correspondings
8294 parameters, making up the NEW_CALL context. */
8296 static void
8297 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8298 constexpr_call *new_call,
8299 bool allow_non_constant,
8300 bool *non_constant_p, bool *overflow_p)
8302 const int nargs = call_expr_nargs (t);
8303 tree fun = new_call->fundef->decl;
8304 tree parms = DECL_ARGUMENTS (fun);
8305 int i;
8306 for (i = 0; i < nargs; ++i)
8308 tree x, arg;
8309 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8310 /* For member function, the first argument is a pointer to the implied
8311 object. And for an object construction, don't bind `this' before
8312 it is fully constructed. */
8313 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8314 goto next;
8315 x = get_nth_callarg (t, i);
8316 if (parms && DECL_BY_REFERENCE (parms))
8318 /* cp_genericize made this a reference for argument passing, but
8319 we don't want to treat it like one for constexpr evaluation. */
8320 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8321 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8322 type = TREE_TYPE (type);
8323 x = convert_from_reference (x);
8325 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8326 TREE_CODE (type) == REFERENCE_TYPE,
8327 non_constant_p, overflow_p);
8328 /* Don't VERIFY_CONSTANT here. */
8329 if (*non_constant_p && allow_non_constant)
8330 return;
8331 /* Just discard ellipsis args after checking their constantitude. */
8332 if (!parms)
8333 continue;
8334 if (*non_constant_p)
8335 /* Don't try to adjust the type of non-constant args. */
8336 goto next;
8338 /* Make sure the binding has the same type as the parm. */
8339 if (TREE_CODE (type) != REFERENCE_TYPE)
8340 arg = adjust_temp_type (type, arg);
8341 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8342 next:
8343 parms = TREE_CHAIN (parms);
8347 /* Variables and functions to manage constexpr call expansion context.
8348 These do not need to be marked for PCH or GC. */
8350 /* FIXME remember and print actual constant arguments. */
8351 static vec<tree> call_stack = vNULL;
8352 static int call_stack_tick;
8353 static int last_cx_error_tick;
8355 static bool
8356 push_cx_call_context (tree call)
8358 ++call_stack_tick;
8359 if (!EXPR_HAS_LOCATION (call))
8360 SET_EXPR_LOCATION (call, input_location);
8361 call_stack.safe_push (call);
8362 if (call_stack.length () > (unsigned) max_constexpr_depth)
8363 return false;
8364 return true;
8367 static void
8368 pop_cx_call_context (void)
8370 ++call_stack_tick;
8371 call_stack.pop ();
8374 vec<tree>
8375 cx_error_context (void)
8377 vec<tree> r = vNULL;
8378 if (call_stack_tick != last_cx_error_tick
8379 && !call_stack.is_empty ())
8380 r = call_stack;
8381 last_cx_error_tick = call_stack_tick;
8382 return r;
8385 /* Subroutine of cxx_eval_constant_expression.
8386 Evaluate the call expression tree T in the context of OLD_CALL expression
8387 evaluation. */
8389 static tree
8390 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8391 bool allow_non_constant, bool addr,
8392 bool *non_constant_p, bool *overflow_p)
8394 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
8395 tree fun = get_function_named_in_call (t);
8396 tree result;
8397 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8398 constexpr_call **slot;
8399 constexpr_call *entry;
8400 bool depth_ok;
8402 if (TREE_CODE (fun) != FUNCTION_DECL)
8404 /* Might be a constexpr function pointer. */
8405 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8406 /*addr*/false, non_constant_p, overflow_p);
8407 if (TREE_CODE (fun) == ADDR_EXPR)
8408 fun = TREE_OPERAND (fun, 0);
8410 if (TREE_CODE (fun) != FUNCTION_DECL)
8412 if (!allow_non_constant && !*non_constant_p)
8413 error_at (loc, "expression %qE does not designate a constexpr "
8414 "function", fun);
8415 *non_constant_p = true;
8416 return t;
8418 if (DECL_CLONED_FUNCTION_P (fun))
8419 fun = DECL_CLONED_FUNCTION (fun);
8420 if (is_builtin_fn (fun))
8421 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8422 addr, non_constant_p, overflow_p);
8423 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8425 if (!allow_non_constant)
8427 error_at (loc, "call to non-constexpr function %qD", fun);
8428 explain_invalid_constexpr_fn (fun);
8430 *non_constant_p = true;
8431 return t;
8434 /* Shortcut trivial constructor/op=. */
8435 if (trivial_fn_p (fun))
8437 if (call_expr_nargs (t) == 2)
8439 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8440 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8441 addr, non_constant_p, overflow_p);
8443 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8444 && AGGR_INIT_ZERO_FIRST (t))
8445 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8448 /* If in direct recursive call, optimize definition search. */
8449 if (old_call != NULL && old_call->fundef->decl == fun)
8450 new_call.fundef = old_call->fundef;
8451 else
8453 new_call.fundef = retrieve_constexpr_fundef (fun);
8454 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8456 if (!allow_non_constant)
8458 if (DECL_INITIAL (fun))
8460 /* The definition of fun was somehow unsuitable. */
8461 error_at (loc, "%qD called in a constant expression", fun);
8462 explain_invalid_constexpr_fn (fun);
8464 else
8465 error_at (loc, "%qD used before its definition", fun);
8467 *non_constant_p = true;
8468 return t;
8471 cxx_bind_parameters_in_call (old_call, t, &new_call,
8472 allow_non_constant, non_constant_p, overflow_p);
8473 if (*non_constant_p)
8474 return t;
8476 depth_ok = push_cx_call_context (t);
8478 new_call.hash
8479 = iterative_hash_template_arg (new_call.bindings,
8480 constexpr_fundef_hash (new_call.fundef));
8482 /* If we have seen this call before, we are done. */
8483 maybe_initialize_constexpr_call_table ();
8484 slot = (constexpr_call **)
8485 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8486 entry = *slot;
8487 if (entry == NULL)
8489 /* We need to keep a pointer to the entry, not just the slot, as the
8490 slot can move in the call to cxx_eval_builtin_function_call. */
8491 *slot = entry = ggc_alloc<constexpr_call> ();
8492 *entry = new_call;
8494 /* Calls which are in progress have their result set to NULL
8495 so that we can detect circular dependencies. */
8496 else if (entry->result == NULL)
8498 if (!allow_non_constant)
8499 error ("call has circular dependency");
8500 *non_constant_p = true;
8501 entry->result = result = error_mark_node;
8504 if (!depth_ok)
8506 if (!allow_non_constant)
8507 error ("constexpr evaluation depth exceeds maximum of %d (use "
8508 "-fconstexpr-depth= to increase the maximum)",
8509 max_constexpr_depth);
8510 *non_constant_p = true;
8511 entry->result = result = error_mark_node;
8513 else
8515 result = entry->result;
8516 if (!result || result == error_mark_node)
8517 result = (cxx_eval_constant_expression
8518 (&new_call, new_call.fundef->body,
8519 allow_non_constant, addr,
8520 non_constant_p, overflow_p));
8521 if (result == error_mark_node)
8522 *non_constant_p = true;
8523 if (*non_constant_p)
8524 entry->result = result = error_mark_node;
8525 else
8527 /* If this was a call to initialize an object, set the type of
8528 the CONSTRUCTOR to the type of that object. */
8529 if (DECL_CONSTRUCTOR_P (fun))
8531 tree ob_arg = get_nth_callarg (t, 0);
8532 STRIP_NOPS (ob_arg);
8533 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8534 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8535 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8536 result);
8538 entry->result = result;
8542 pop_cx_call_context ();
8543 return unshare_expr (result);
8546 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8548 bool
8549 reduced_constant_expression_p (tree t)
8551 switch (TREE_CODE (t))
8553 case PTRMEM_CST:
8554 /* Even if we can't lower this yet, it's constant. */
8555 return true;
8557 case CONSTRUCTOR:
8558 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
8559 tree elt; unsigned HOST_WIDE_INT idx;
8560 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
8561 if (!reduced_constant_expression_p (elt))
8562 return false;
8563 return true;
8565 default:
8566 /* FIXME are we calling this too much? */
8567 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8571 /* Some expressions may have constant operands but are not constant
8572 themselves, such as 1/0. Call this function (or rather, the macro
8573 following it) to check for that condition.
8575 We only call this in places that require an arithmetic constant, not in
8576 places where we might have a non-constant expression that can be a
8577 component of a constant expression, such as the address of a constexpr
8578 variable that might be dereferenced later. */
8580 static bool
8581 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8582 bool *overflow_p)
8584 if (!*non_constant_p && !reduced_constant_expression_p (t))
8586 if (!allow_non_constant)
8587 error ("%q+E is not a constant expression", t);
8588 *non_constant_p = true;
8590 if (TREE_OVERFLOW_P (t))
8592 if (!allow_non_constant)
8594 permerror (input_location, "overflow in constant expression");
8595 /* If we're being permissive (and are in an enforcing
8596 context), ignore the overflow. */
8597 if (flag_permissive)
8598 return *non_constant_p;
8600 *overflow_p = true;
8602 return *non_constant_p;
8605 /* Subroutine of cxx_eval_constant_expression.
8606 Attempt to reduce the unary expression tree T to a compile time value.
8607 If successful, return the value. Otherwise issue a diagnostic
8608 and return error_mark_node. */
8610 static tree
8611 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8612 bool allow_non_constant, bool addr,
8613 bool *non_constant_p, bool *overflow_p)
8615 tree r;
8616 tree orig_arg = TREE_OPERAND (t, 0);
8617 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8618 addr, non_constant_p, overflow_p);
8619 VERIFY_CONSTANT (arg);
8620 if (arg == orig_arg)
8621 return t;
8622 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8623 VERIFY_CONSTANT (r);
8624 return r;
8627 /* Subroutine of cxx_eval_constant_expression.
8628 Like cxx_eval_unary_expression, except for binary expressions. */
8630 static tree
8631 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8632 bool allow_non_constant, bool addr,
8633 bool *non_constant_p, bool *overflow_p)
8635 tree r;
8636 tree orig_lhs = TREE_OPERAND (t, 0);
8637 tree orig_rhs = TREE_OPERAND (t, 1);
8638 tree lhs, rhs;
8639 lhs = cxx_eval_constant_expression (call, orig_lhs,
8640 allow_non_constant, addr,
8641 non_constant_p, overflow_p);
8642 VERIFY_CONSTANT (lhs);
8643 rhs = cxx_eval_constant_expression (call, orig_rhs,
8644 allow_non_constant, addr,
8645 non_constant_p, overflow_p);
8646 VERIFY_CONSTANT (rhs);
8647 if (lhs == orig_lhs && rhs == orig_rhs)
8648 return t;
8649 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8650 VERIFY_CONSTANT (r);
8651 return r;
8654 /* Subroutine of cxx_eval_constant_expression.
8655 Attempt to evaluate condition expressions. Dead branches are not
8656 looked into. */
8658 static tree
8659 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8660 bool allow_non_constant, bool addr,
8661 bool *non_constant_p, bool *overflow_p)
8663 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8664 allow_non_constant, addr,
8665 non_constant_p, overflow_p);
8666 VERIFY_CONSTANT (val);
8667 /* Don't VERIFY_CONSTANT the other operands. */
8668 if (integer_zerop (val))
8669 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8670 allow_non_constant, addr,
8671 non_constant_p, overflow_p);
8672 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8673 allow_non_constant, addr,
8674 non_constant_p, overflow_p);
8677 /* Subroutine of cxx_eval_constant_expression.
8678 Attempt to reduce a reference to an array slot. */
8680 static tree
8681 cxx_eval_array_reference (const constexpr_call *call, tree t,
8682 bool allow_non_constant, bool addr,
8683 bool *non_constant_p, bool *overflow_p)
8685 tree oldary = TREE_OPERAND (t, 0);
8686 tree ary = cxx_eval_constant_expression (call, oldary,
8687 allow_non_constant, addr,
8688 non_constant_p, overflow_p);
8689 tree index, oldidx;
8690 HOST_WIDE_INT i;
8691 tree elem_type;
8692 unsigned len, elem_nchars = 1;
8693 if (*non_constant_p)
8694 return t;
8695 oldidx = TREE_OPERAND (t, 1);
8696 index = cxx_eval_constant_expression (call, oldidx,
8697 allow_non_constant, false,
8698 non_constant_p, overflow_p);
8699 VERIFY_CONSTANT (index);
8700 if (addr && ary == oldary && index == oldidx)
8701 return t;
8702 else if (addr)
8703 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8704 elem_type = TREE_TYPE (TREE_TYPE (ary));
8705 if (TREE_CODE (ary) == CONSTRUCTOR)
8706 len = CONSTRUCTOR_NELTS (ary);
8707 else if (TREE_CODE (ary) == STRING_CST)
8709 elem_nchars = (TYPE_PRECISION (elem_type)
8710 / TYPE_PRECISION (char_type_node));
8711 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8713 else
8715 /* We can't do anything with other tree codes, so use
8716 VERIFY_CONSTANT to complain and fail. */
8717 VERIFY_CONSTANT (ary);
8718 gcc_unreachable ();
8720 if (compare_tree_int (index, len) >= 0)
8722 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8724 /* If it's within the array bounds but doesn't have an explicit
8725 initializer, it's value-initialized. */
8726 tree val = build_value_init (elem_type, tf_warning_or_error);
8727 return cxx_eval_constant_expression (call, val,
8728 allow_non_constant, addr,
8729 non_constant_p, overflow_p);
8732 if (!allow_non_constant)
8733 error ("array subscript out of bound");
8734 *non_constant_p = true;
8735 return t;
8737 else if (tree_int_cst_lt (index, integer_zero_node))
8739 if (!allow_non_constant)
8740 error ("negative array subscript");
8741 *non_constant_p = true;
8742 return t;
8744 i = tree_to_shwi (index);
8745 if (TREE_CODE (ary) == CONSTRUCTOR)
8746 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8747 else if (elem_nchars == 1)
8748 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8749 TREE_STRING_POINTER (ary)[i]);
8750 else
8752 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8753 return native_interpret_expr (type, (const unsigned char *)
8754 TREE_STRING_POINTER (ary)
8755 + i * elem_nchars, elem_nchars);
8757 /* Don't VERIFY_CONSTANT here. */
8760 /* Subroutine of cxx_eval_constant_expression.
8761 Attempt to reduce a field access of a value of class type. */
8763 static tree
8764 cxx_eval_component_reference (const constexpr_call *call, tree t,
8765 bool allow_non_constant, bool addr,
8766 bool *non_constant_p, bool *overflow_p)
8768 unsigned HOST_WIDE_INT i;
8769 tree field;
8770 tree value;
8771 tree part = TREE_OPERAND (t, 1);
8772 tree orig_whole = TREE_OPERAND (t, 0);
8773 tree whole = cxx_eval_constant_expression (call, orig_whole,
8774 allow_non_constant, addr,
8775 non_constant_p, overflow_p);
8776 if (whole == orig_whole)
8777 return t;
8778 if (addr)
8779 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8780 whole, part, NULL_TREE);
8781 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8782 CONSTRUCTOR. */
8783 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8785 if (!allow_non_constant)
8786 error ("%qE is not a constant expression", orig_whole);
8787 *non_constant_p = true;
8789 if (DECL_MUTABLE_P (part))
8791 if (!allow_non_constant)
8792 error ("mutable %qD is not usable in a constant expression", part);
8793 *non_constant_p = true;
8795 if (*non_constant_p)
8796 return t;
8797 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8799 if (field == part)
8800 return value;
8802 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8803 && CONSTRUCTOR_NELTS (whole) > 0)
8805 /* DR 1188 says we don't have to deal with this. */
8806 if (!allow_non_constant)
8807 error ("accessing %qD member instead of initialized %qD member in "
8808 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8809 *non_constant_p = true;
8810 return t;
8813 /* If there's no explicit init for this field, it's value-initialized. */
8814 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8815 return cxx_eval_constant_expression (call, value,
8816 allow_non_constant, addr,
8817 non_constant_p, overflow_p);
8820 /* Subroutine of cxx_eval_constant_expression.
8821 Attempt to reduce a field access of a value of class type that is
8822 expressed as a BIT_FIELD_REF. */
8824 static tree
8825 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8826 bool allow_non_constant, bool addr,
8827 bool *non_constant_p, bool *overflow_p)
8829 tree orig_whole = TREE_OPERAND (t, 0);
8830 tree retval, fldval, utype, mask;
8831 bool fld_seen = false;
8832 HOST_WIDE_INT istart, isize;
8833 tree whole = cxx_eval_constant_expression (call, orig_whole,
8834 allow_non_constant, addr,
8835 non_constant_p, overflow_p);
8836 tree start, field, value;
8837 unsigned HOST_WIDE_INT i;
8839 if (whole == orig_whole)
8840 return t;
8841 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8842 CONSTRUCTOR. */
8843 if (!*non_constant_p
8844 && TREE_CODE (whole) != VECTOR_CST
8845 && TREE_CODE (whole) != CONSTRUCTOR)
8847 if (!allow_non_constant)
8848 error ("%qE is not a constant expression", orig_whole);
8849 *non_constant_p = true;
8851 if (*non_constant_p)
8852 return t;
8854 if (TREE_CODE (whole) == VECTOR_CST)
8855 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8856 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8858 start = TREE_OPERAND (t, 2);
8859 istart = tree_to_shwi (start);
8860 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8861 utype = TREE_TYPE (t);
8862 if (!TYPE_UNSIGNED (utype))
8863 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8864 retval = build_int_cst (utype, 0);
8865 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8867 tree bitpos = bit_position (field);
8868 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
8869 return value;
8870 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
8871 && TREE_CODE (value) == INTEGER_CST
8872 && tree_fits_shwi_p (bitpos)
8873 && tree_fits_shwi_p (DECL_SIZE (field)))
8875 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
8876 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
8877 HOST_WIDE_INT shift;
8878 if (bit >= istart && bit + sz <= istart + isize)
8880 fldval = fold_convert (utype, value);
8881 mask = build_int_cst_type (utype, -1);
8882 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
8883 size_int (TYPE_PRECISION (utype) - sz));
8884 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
8885 size_int (TYPE_PRECISION (utype) - sz));
8886 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
8887 shift = bit - istart;
8888 if (BYTES_BIG_ENDIAN)
8889 shift = TYPE_PRECISION (utype) - shift - sz;
8890 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
8891 size_int (shift));
8892 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
8893 fld_seen = true;
8897 if (fld_seen)
8898 return fold_convert (TREE_TYPE (t), retval);
8899 gcc_unreachable ();
8900 return error_mark_node;
8903 /* Subroutine of cxx_eval_constant_expression.
8904 Evaluate a short-circuited logical expression T in the context
8905 of a given constexpr CALL. BAILOUT_VALUE is the value for
8906 early return. CONTINUE_VALUE is used here purely for
8907 sanity check purposes. */
8909 static tree
8910 cxx_eval_logical_expression (const constexpr_call *call, tree t,
8911 tree bailout_value, tree continue_value,
8912 bool allow_non_constant, bool addr,
8913 bool *non_constant_p, bool *overflow_p)
8915 tree r;
8916 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8917 allow_non_constant, addr,
8918 non_constant_p, overflow_p);
8919 VERIFY_CONSTANT (lhs);
8920 if (tree_int_cst_equal (lhs, bailout_value))
8921 return lhs;
8922 gcc_assert (tree_int_cst_equal (lhs, continue_value));
8923 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8924 allow_non_constant, addr, non_constant_p, overflow_p);
8925 VERIFY_CONSTANT (r);
8926 return r;
8929 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8930 CONSTRUCTOR elements to initialize (part of) an object containing that
8931 field. Return a pointer to the constructor_elt corresponding to the
8932 initialization of the field. */
8934 static constructor_elt *
8935 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
8937 tree aggr = TREE_OPERAND (ref, 0);
8938 tree field = TREE_OPERAND (ref, 1);
8939 HOST_WIDE_INT i;
8940 constructor_elt *ce;
8942 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
8944 if (TREE_CODE (aggr) == COMPONENT_REF)
8946 constructor_elt *base_ce
8947 = base_field_constructor_elt (v, aggr);
8948 v = CONSTRUCTOR_ELTS (base_ce->value);
8951 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8952 if (ce->index == field)
8953 return ce;
8955 gcc_unreachable ();
8956 return NULL;
8959 /* Subroutine of cxx_eval_constant_expression.
8960 The expression tree T denotes a C-style array or a C-style
8961 aggregate. Reduce it to a constant expression. */
8963 static tree
8964 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
8965 bool allow_non_constant, bool addr,
8966 bool *non_constant_p, bool *overflow_p)
8968 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8969 vec<constructor_elt, va_gc> *n;
8970 vec_alloc (n, vec_safe_length (v));
8971 constructor_elt *ce;
8972 HOST_WIDE_INT i;
8973 bool changed = false;
8974 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8975 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8977 tree elt = cxx_eval_constant_expression (call, ce->value,
8978 allow_non_constant, addr,
8979 non_constant_p, overflow_p);
8980 /* Don't VERIFY_CONSTANT here. */
8981 if (allow_non_constant && *non_constant_p)
8982 goto fail;
8983 if (elt != ce->value)
8984 changed = true;
8985 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
8987 /* This is an initialization of a vfield inside a base
8988 subaggregate that we already initialized; push this
8989 initialization into the previous initialization. */
8990 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
8991 inner->value = elt;
8993 else if (ce->index && TREE_CODE (ce->index) == NOP_EXPR)
8995 /* This is an initializer for an empty base; now that we've
8996 checked that it's constant, we can ignore it. */
8997 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
8999 else
9000 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
9002 if (*non_constant_p || !changed)
9004 fail:
9005 vec_free (n);
9006 return t;
9008 t = build_constructor (TREE_TYPE (t), n);
9009 TREE_CONSTANT (t) = true;
9010 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
9011 t = fold (t);
9012 return t;
9015 /* Subroutine of cxx_eval_constant_expression.
9016 The expression tree T is a VEC_INIT_EXPR which denotes the desired
9017 initialization of a non-static data member of array type. Reduce it to a
9018 CONSTRUCTOR.
9020 Note that apart from value-initialization (when VALUE_INIT is true),
9021 this is only intended to support value-initialization and the
9022 initializations done by defaulted constructors for classes with
9023 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
9024 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
9025 for the copy/move constructor. */
9027 static tree
9028 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
9029 bool value_init, bool allow_non_constant, bool addr,
9030 bool *non_constant_p, bool *overflow_p)
9032 tree elttype = TREE_TYPE (atype);
9033 int max = tree_to_shwi (array_type_nelts (atype));
9034 vec<constructor_elt, va_gc> *n;
9035 vec_alloc (n, max + 1);
9036 bool pre_init = false;
9037 int i;
9039 /* For the default constructor, build up a call to the default
9040 constructor of the element type. We only need to handle class types
9041 here, as for a constructor to be constexpr, all members must be
9042 initialized, which for a defaulted default constructor means they must
9043 be of a class type with a constexpr default constructor. */
9044 if (TREE_CODE (elttype) == ARRAY_TYPE)
9045 /* We only do this at the lowest level. */;
9046 else if (value_init)
9048 init = build_value_init (elttype, tf_warning_or_error);
9049 init = cxx_eval_constant_expression
9050 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
9051 pre_init = true;
9053 else if (!init)
9055 vec<tree, va_gc> *argvec = make_tree_vector ();
9056 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9057 &argvec, elttype, LOOKUP_NORMAL,
9058 tf_warning_or_error);
9059 release_tree_vector (argvec);
9060 init = cxx_eval_constant_expression (call, init, allow_non_constant,
9061 addr, non_constant_p, overflow_p);
9062 pre_init = true;
9065 if (*non_constant_p && !allow_non_constant)
9066 goto fail;
9068 for (i = 0; i <= max; ++i)
9070 tree idx = build_int_cst (size_type_node, i);
9071 tree eltinit;
9072 if (TREE_CODE (elttype) == ARRAY_TYPE)
9074 /* A multidimensional array; recurse. */
9075 if (value_init || init == NULL_TREE)
9076 eltinit = NULL_TREE;
9077 else
9078 eltinit = cp_build_array_ref (input_location, init, idx,
9079 tf_warning_or_error);
9080 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
9081 allow_non_constant, addr,
9082 non_constant_p, overflow_p);
9084 else if (pre_init)
9086 /* Initializing an element using value or default initialization
9087 we just pre-built above. */
9088 if (i == 0)
9089 eltinit = init;
9090 else
9091 eltinit = unshare_expr (init);
9093 else
9095 /* Copying an element. */
9096 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9097 (atype, TREE_TYPE (init)));
9098 eltinit = cp_build_array_ref (input_location, init, idx,
9099 tf_warning_or_error);
9100 if (!real_lvalue_p (init))
9101 eltinit = move (eltinit);
9102 eltinit = force_rvalue (eltinit, tf_warning_or_error);
9103 eltinit = cxx_eval_constant_expression
9104 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
9106 if (*non_constant_p && !allow_non_constant)
9107 goto fail;
9108 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
9111 if (!*non_constant_p)
9113 init = build_constructor (atype, n);
9114 TREE_CONSTANT (init) = true;
9115 return init;
9118 fail:
9119 vec_free (n);
9120 return init;
9123 static tree
9124 cxx_eval_vec_init (const constexpr_call *call, tree t,
9125 bool allow_non_constant, bool addr,
9126 bool *non_constant_p, bool *overflow_p)
9128 tree atype = TREE_TYPE (t);
9129 tree init = VEC_INIT_EXPR_INIT (t);
9130 tree r = cxx_eval_vec_init_1 (call, atype, init,
9131 VEC_INIT_EXPR_VALUE_INIT (t),
9132 allow_non_constant, addr, non_constant_p, overflow_p);
9133 if (*non_constant_p)
9134 return t;
9135 else
9136 return r;
9139 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9140 match. We want to be less strict for simple *& folding; if we have a
9141 non-const temporary that we access through a const pointer, that should
9142 work. We handle this here rather than change fold_indirect_ref_1
9143 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9144 don't really make sense outside of constant expression evaluation. Also
9145 we want to allow folding to COMPONENT_REF, which could cause trouble
9146 with TBAA in fold_indirect_ref_1.
9148 Try to keep this function synced with fold_indirect_ref_1. */
9150 static tree
9151 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9153 tree sub, subtype;
9155 sub = op0;
9156 STRIP_NOPS (sub);
9157 subtype = TREE_TYPE (sub);
9158 if (!POINTER_TYPE_P (subtype))
9159 return NULL_TREE;
9161 if (TREE_CODE (sub) == ADDR_EXPR)
9163 tree op = TREE_OPERAND (sub, 0);
9164 tree optype = TREE_TYPE (op);
9166 /* *&CONST_DECL -> to the value of the const decl. */
9167 if (TREE_CODE (op) == CONST_DECL)
9168 return DECL_INITIAL (op);
9169 /* *&p => p; make sure to handle *&"str"[cst] here. */
9170 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9172 tree fop = fold_read_from_constant_string (op);
9173 if (fop)
9174 return fop;
9175 else
9176 return op;
9178 /* *(foo *)&fooarray => fooarray[0] */
9179 else if (TREE_CODE (optype) == ARRAY_TYPE
9180 && (same_type_ignoring_top_level_qualifiers_p
9181 (type, TREE_TYPE (optype))))
9183 tree type_domain = TYPE_DOMAIN (optype);
9184 tree min_val = size_zero_node;
9185 if (type_domain && TYPE_MIN_VALUE (type_domain))
9186 min_val = TYPE_MIN_VALUE (type_domain);
9187 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9188 NULL_TREE, NULL_TREE);
9190 /* *(foo *)&complexfoo => __real__ complexfoo */
9191 else if (TREE_CODE (optype) == COMPLEX_TYPE
9192 && (same_type_ignoring_top_level_qualifiers_p
9193 (type, TREE_TYPE (optype))))
9194 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9195 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9196 else if (TREE_CODE (optype) == VECTOR_TYPE
9197 && (same_type_ignoring_top_level_qualifiers_p
9198 (type, TREE_TYPE (optype))))
9200 tree part_width = TYPE_SIZE (type);
9201 tree index = bitsize_int (0);
9202 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9204 /* Also handle conversion to an empty base class, which
9205 is represented with a NOP_EXPR. */
9206 else if (is_empty_class (type)
9207 && CLASS_TYPE_P (optype)
9208 && DERIVED_FROM_P (type, optype))
9210 *empty_base = true;
9211 return op;
9213 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9214 else if (RECORD_OR_UNION_TYPE_P (optype))
9216 tree field = TYPE_FIELDS (optype);
9217 for (; field; field = DECL_CHAIN (field))
9218 if (TREE_CODE (field) == FIELD_DECL
9219 && integer_zerop (byte_position (field))
9220 && (same_type_ignoring_top_level_qualifiers_p
9221 (TREE_TYPE (field), type)))
9223 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9224 break;
9228 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9229 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9231 tree op00 = TREE_OPERAND (sub, 0);
9232 tree op01 = TREE_OPERAND (sub, 1);
9234 STRIP_NOPS (op00);
9235 if (TREE_CODE (op00) == ADDR_EXPR)
9237 tree op00type;
9238 op00 = TREE_OPERAND (op00, 0);
9239 op00type = TREE_TYPE (op00);
9241 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9242 if (TREE_CODE (op00type) == VECTOR_TYPE
9243 && (same_type_ignoring_top_level_qualifiers_p
9244 (type, TREE_TYPE (op00type))))
9246 HOST_WIDE_INT offset = tree_to_shwi (op01);
9247 tree part_width = TYPE_SIZE (type);
9248 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9249 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9250 tree index = bitsize_int (indexi);
9252 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
9253 return fold_build3_loc (loc,
9254 BIT_FIELD_REF, type, op00,
9255 part_width, index);
9258 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9259 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9260 && (same_type_ignoring_top_level_qualifiers_p
9261 (type, TREE_TYPE (op00type))))
9263 tree size = TYPE_SIZE_UNIT (type);
9264 if (tree_int_cst_equal (size, op01))
9265 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9267 /* ((foo *)&fooarray)[1] => fooarray[1] */
9268 else if (TREE_CODE (op00type) == ARRAY_TYPE
9269 && (same_type_ignoring_top_level_qualifiers_p
9270 (type, TREE_TYPE (op00type))))
9272 tree type_domain = TYPE_DOMAIN (op00type);
9273 tree min_val = size_zero_node;
9274 if (type_domain && TYPE_MIN_VALUE (type_domain))
9275 min_val = TYPE_MIN_VALUE (type_domain);
9276 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9277 TYPE_SIZE_UNIT (type));
9278 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9279 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9280 NULL_TREE, NULL_TREE);
9282 /* Also handle conversion to an empty base class, which
9283 is represented with a NOP_EXPR. */
9284 else if (is_empty_class (type)
9285 && CLASS_TYPE_P (op00type)
9286 && DERIVED_FROM_P (type, op00type))
9288 *empty_base = true;
9289 return op00;
9291 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9292 else if (RECORD_OR_UNION_TYPE_P (op00type))
9294 tree field = TYPE_FIELDS (op00type);
9295 for (; field; field = DECL_CHAIN (field))
9296 if (TREE_CODE (field) == FIELD_DECL
9297 && tree_int_cst_equal (byte_position (field), op01)
9298 && (same_type_ignoring_top_level_qualifiers_p
9299 (TREE_TYPE (field), type)))
9301 return fold_build3 (COMPONENT_REF, type, op00,
9302 field, NULL_TREE);
9303 break;
9308 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9309 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9310 && (same_type_ignoring_top_level_qualifiers_p
9311 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9313 tree type_domain;
9314 tree min_val = size_zero_node;
9315 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9316 if (newsub)
9317 sub = newsub;
9318 else
9319 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9320 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9321 if (type_domain && TYPE_MIN_VALUE (type_domain))
9322 min_val = TYPE_MIN_VALUE (type_domain);
9323 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9324 NULL_TREE);
9327 return NULL_TREE;
9330 static tree
9331 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9332 bool allow_non_constant, bool addr,
9333 bool *non_constant_p, bool *overflow_p)
9335 tree orig_op0 = TREE_OPERAND (t, 0);
9336 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9337 /*addr*/false, non_constant_p, overflow_p);
9338 bool empty_base = false;
9339 tree r;
9341 /* Don't VERIFY_CONSTANT here. */
9342 if (*non_constant_p)
9343 return t;
9345 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9346 &empty_base);
9348 if (r)
9349 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9350 addr, non_constant_p, overflow_p);
9351 else
9353 tree sub = op0;
9354 STRIP_NOPS (sub);
9355 if (TREE_CODE (sub) == ADDR_EXPR)
9357 /* We couldn't fold to a constant value. Make sure it's not
9358 something we should have been able to fold. */
9359 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9360 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9361 /* DR 1188 says we don't have to deal with this. */
9362 if (!allow_non_constant)
9363 error ("accessing value of %qE through a %qT glvalue in a "
9364 "constant expression", build_fold_indirect_ref (sub),
9365 TREE_TYPE (t));
9366 *non_constant_p = true;
9367 return t;
9371 /* If we're pulling out the value of an empty base, make sure
9372 that the whole object is constant and then return an empty
9373 CONSTRUCTOR. */
9374 if (empty_base)
9376 VERIFY_CONSTANT (r);
9377 r = build_constructor (TREE_TYPE (t), NULL);
9378 TREE_CONSTANT (r) = true;
9381 if (r == NULL_TREE)
9383 if (addr && op0 != orig_op0)
9384 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9385 if (!addr)
9386 VERIFY_CONSTANT (t);
9387 return t;
9389 return r;
9392 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9393 Shared between potential_constant_expression and
9394 cxx_eval_constant_expression. */
9396 static void
9397 non_const_var_error (tree r)
9399 tree type = TREE_TYPE (r);
9400 error ("the value of %qD is not usable in a constant "
9401 "expression", r);
9402 /* Avoid error cascade. */
9403 if (DECL_INITIAL (r) == error_mark_node)
9404 return;
9405 if (DECL_DECLARED_CONSTEXPR_P (r))
9406 inform (DECL_SOURCE_LOCATION (r),
9407 "%qD used in its own initializer", r);
9408 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9410 if (!CP_TYPE_CONST_P (type))
9411 inform (DECL_SOURCE_LOCATION (r),
9412 "%q#D is not const", r);
9413 else if (CP_TYPE_VOLATILE_P (type))
9414 inform (DECL_SOURCE_LOCATION (r),
9415 "%q#D is volatile", r);
9416 else if (!DECL_INITIAL (r)
9417 || !TREE_CONSTANT (DECL_INITIAL (r)))
9418 inform (DECL_SOURCE_LOCATION (r),
9419 "%qD was not initialized with a constant "
9420 "expression", r);
9421 else
9422 gcc_unreachable ();
9424 else
9426 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9427 inform (DECL_SOURCE_LOCATION (r),
9428 "%qD was not declared %<constexpr%>", r);
9429 else
9430 inform (DECL_SOURCE_LOCATION (r),
9431 "%qD does not have integral or enumeration type",
9436 /* Subroutine of cxx_eval_constant_expression.
9437 Like cxx_eval_unary_expression, except for trinary expressions. */
9439 static tree
9440 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9441 bool allow_non_constant, bool addr,
9442 bool *non_constant_p, bool *overflow_p)
9444 int i;
9445 tree args[3];
9446 tree val;
9448 for (i = 0; i < 3; i++)
9450 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9451 allow_non_constant, addr,
9452 non_constant_p, overflow_p);
9453 VERIFY_CONSTANT (args[i]);
9456 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9457 args[0], args[1], args[2]);
9458 if (val == NULL_TREE)
9459 return t;
9460 VERIFY_CONSTANT (val);
9461 return val;
9464 /* Attempt to reduce the expression T to a constant value.
9465 On failure, issue diagnostic and return error_mark_node. */
9466 /* FIXME unify with c_fully_fold */
9468 static tree
9469 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9470 bool allow_non_constant, bool addr,
9471 bool *non_constant_p, bool *overflow_p)
9473 tree r = t;
9475 if (t == error_mark_node)
9477 *non_constant_p = true;
9478 return t;
9480 if (CONSTANT_CLASS_P (t))
9482 if (TREE_CODE (t) == PTRMEM_CST)
9483 t = cplus_expand_constant (t);
9484 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9485 *overflow_p = true;
9486 return t;
9488 if (TREE_CODE (t) != NOP_EXPR
9489 && reduced_constant_expression_p (t))
9490 return fold (t);
9492 switch (TREE_CODE (t))
9494 case VAR_DECL:
9495 if (addr)
9496 return t;
9497 /* else fall through. */
9498 case CONST_DECL:
9499 r = integral_constant_value (t);
9500 if (TREE_CODE (r) == TARGET_EXPR
9501 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9502 r = TARGET_EXPR_INITIAL (r);
9503 if (DECL_P (r))
9505 if (!allow_non_constant)
9506 non_const_var_error (r);
9507 *non_constant_p = true;
9509 break;
9511 case FUNCTION_DECL:
9512 case TEMPLATE_DECL:
9513 case LABEL_DECL:
9514 return t;
9516 case PARM_DECL:
9517 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9519 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9521 if (!allow_non_constant)
9522 sorry ("use of the value of the object being constructed "
9523 "in a constant expression");
9524 *non_constant_p = true;
9526 else
9527 r = lookup_parameter_binding (call, t);
9529 else if (addr)
9530 /* Defer in case this is only used for its type. */;
9531 else
9533 if (!allow_non_constant)
9534 error ("%qE is not a constant expression", t);
9535 *non_constant_p = true;
9537 break;
9539 case CALL_EXPR:
9540 case AGGR_INIT_EXPR:
9541 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9542 non_constant_p, overflow_p);
9543 break;
9545 case TARGET_EXPR:
9546 if (!literal_type_p (TREE_TYPE (t)))
9548 if (!allow_non_constant)
9550 error ("temporary of non-literal type %qT in a "
9551 "constant expression", TREE_TYPE (t));
9552 explain_non_literal_class (TREE_TYPE (t));
9554 *non_constant_p = true;
9555 break;
9557 /* else fall through. */
9558 case INIT_EXPR:
9559 /* Pass false for 'addr' because these codes indicate
9560 initialization of a temporary. */
9561 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9562 allow_non_constant, false,
9563 non_constant_p, overflow_p);
9564 if (!*non_constant_p)
9565 /* Adjust the type of the result to the type of the temporary. */
9566 r = adjust_temp_type (TREE_TYPE (t), r);
9567 break;
9569 case SCOPE_REF:
9570 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9571 allow_non_constant, addr,
9572 non_constant_p, overflow_p);
9573 break;
9575 case RETURN_EXPR:
9576 case NON_LVALUE_EXPR:
9577 case TRY_CATCH_EXPR:
9578 case CLEANUP_POINT_EXPR:
9579 case MUST_NOT_THROW_EXPR:
9580 case SAVE_EXPR:
9581 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9582 allow_non_constant, addr,
9583 non_constant_p, overflow_p);
9584 break;
9586 /* These differ from cxx_eval_unary_expression in that this doesn't
9587 check for a constant operand or result; an address can be
9588 constant without its operand being, and vice versa. */
9589 case INDIRECT_REF:
9590 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9591 non_constant_p, overflow_p);
9592 break;
9594 case ADDR_EXPR:
9596 tree oldop = TREE_OPERAND (t, 0);
9597 tree op = cxx_eval_constant_expression (call, oldop,
9598 allow_non_constant,
9599 /*addr*/true,
9600 non_constant_p, overflow_p);
9601 /* Don't VERIFY_CONSTANT here. */
9602 if (*non_constant_p)
9603 return t;
9604 /* This function does more aggressive folding than fold itself. */
9605 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9606 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9607 return t;
9608 break;
9611 case REALPART_EXPR:
9612 case IMAGPART_EXPR:
9613 case CONJ_EXPR:
9614 case FIX_TRUNC_EXPR:
9615 case FLOAT_EXPR:
9616 case NEGATE_EXPR:
9617 case ABS_EXPR:
9618 case BIT_NOT_EXPR:
9619 case TRUTH_NOT_EXPR:
9620 case FIXED_CONVERT_EXPR:
9621 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9622 non_constant_p, overflow_p);
9623 break;
9625 case SIZEOF_EXPR:
9626 if (SIZEOF_EXPR_TYPE_P (t))
9627 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9628 SIZEOF_EXPR, false);
9629 else if (TYPE_P (TREE_OPERAND (t, 0)))
9630 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9631 false);
9632 else
9633 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9634 false);
9635 if (r == error_mark_node)
9636 r = size_one_node;
9637 VERIFY_CONSTANT (r);
9638 break;
9640 case COMPOUND_EXPR:
9642 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9643 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9644 introduced by build_call_a. */
9645 tree op0 = TREE_OPERAND (t, 0);
9646 tree op1 = TREE_OPERAND (t, 1);
9647 STRIP_NOPS (op1);
9648 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9649 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9650 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9651 addr, non_constant_p, overflow_p);
9652 else
9654 /* Check that the LHS is constant and then discard it. */
9655 cxx_eval_constant_expression (call, op0, allow_non_constant,
9656 false, non_constant_p, overflow_p);
9657 op1 = TREE_OPERAND (t, 1);
9658 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9659 addr, non_constant_p, overflow_p);
9662 break;
9664 case POINTER_PLUS_EXPR:
9665 case PLUS_EXPR:
9666 case MINUS_EXPR:
9667 case MULT_EXPR:
9668 case TRUNC_DIV_EXPR:
9669 case CEIL_DIV_EXPR:
9670 case FLOOR_DIV_EXPR:
9671 case ROUND_DIV_EXPR:
9672 case TRUNC_MOD_EXPR:
9673 case CEIL_MOD_EXPR:
9674 case ROUND_MOD_EXPR:
9675 case RDIV_EXPR:
9676 case EXACT_DIV_EXPR:
9677 case MIN_EXPR:
9678 case MAX_EXPR:
9679 case LSHIFT_EXPR:
9680 case RSHIFT_EXPR:
9681 case LROTATE_EXPR:
9682 case RROTATE_EXPR:
9683 case BIT_IOR_EXPR:
9684 case BIT_XOR_EXPR:
9685 case BIT_AND_EXPR:
9686 case TRUTH_XOR_EXPR:
9687 case LT_EXPR:
9688 case LE_EXPR:
9689 case GT_EXPR:
9690 case GE_EXPR:
9691 case EQ_EXPR:
9692 case NE_EXPR:
9693 case UNORDERED_EXPR:
9694 case ORDERED_EXPR:
9695 case UNLT_EXPR:
9696 case UNLE_EXPR:
9697 case UNGT_EXPR:
9698 case UNGE_EXPR:
9699 case UNEQ_EXPR:
9700 case LTGT_EXPR:
9701 case RANGE_EXPR:
9702 case COMPLEX_EXPR:
9703 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9704 non_constant_p, overflow_p);
9705 break;
9707 /* fold can introduce non-IF versions of these; still treat them as
9708 short-circuiting. */
9709 case TRUTH_AND_EXPR:
9710 case TRUTH_ANDIF_EXPR:
9711 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9712 boolean_true_node,
9713 allow_non_constant, addr,
9714 non_constant_p, overflow_p);
9715 break;
9717 case TRUTH_OR_EXPR:
9718 case TRUTH_ORIF_EXPR:
9719 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9720 boolean_false_node,
9721 allow_non_constant, addr,
9722 non_constant_p, overflow_p);
9723 break;
9725 case ARRAY_REF:
9726 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9727 non_constant_p, overflow_p);
9728 break;
9730 case COMPONENT_REF:
9731 if (is_overloaded_fn (t))
9733 /* We can only get here in checking mode via
9734 build_non_dependent_expr, because any expression that
9735 calls or takes the address of the function will have
9736 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9737 gcc_checking_assert (allow_non_constant || errorcount);
9738 *non_constant_p = true;
9739 return t;
9741 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9742 non_constant_p, overflow_p);
9743 break;
9745 case BIT_FIELD_REF:
9746 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9747 non_constant_p, overflow_p);
9748 break;
9750 case COND_EXPR:
9751 case VEC_COND_EXPR:
9752 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9753 non_constant_p, overflow_p);
9754 break;
9756 case CONSTRUCTOR:
9757 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9758 non_constant_p, overflow_p);
9759 break;
9761 case VEC_INIT_EXPR:
9762 /* We can get this in a defaulted constructor for a class with a
9763 non-static data member of array type. Either the initializer will
9764 be NULL, meaning default-initialization, or it will be an lvalue
9765 or xvalue of the same type, meaning direct-initialization from the
9766 corresponding member. */
9767 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9768 non_constant_p, overflow_p);
9769 break;
9771 case FMA_EXPR:
9772 case VEC_PERM_EXPR:
9773 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9774 non_constant_p, overflow_p);
9775 break;
9777 case CONVERT_EXPR:
9778 case VIEW_CONVERT_EXPR:
9779 case NOP_EXPR:
9781 tree oldop = TREE_OPERAND (t, 0);
9782 tree op = cxx_eval_constant_expression (call, oldop,
9783 allow_non_constant, addr,
9784 non_constant_p, overflow_p);
9785 if (*non_constant_p)
9786 return t;
9787 if (POINTER_TYPE_P (TREE_TYPE (t))
9788 && TREE_CODE (op) == INTEGER_CST
9789 && !integer_zerop (op))
9791 if (!allow_non_constant)
9792 error_at (EXPR_LOC_OR_LOC (t, input_location),
9793 "reinterpret_cast from integer to pointer");
9794 *non_constant_p = true;
9795 return t;
9797 if (op == oldop)
9798 /* We didn't fold at the top so we could check for ptr-int
9799 conversion. */
9800 return fold (t);
9801 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9802 /* Conversion of an out-of-range value has implementation-defined
9803 behavior; the language considers it different from arithmetic
9804 overflow, which is undefined. */
9805 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9806 TREE_OVERFLOW (r) = false;
9808 break;
9810 case EMPTY_CLASS_EXPR:
9811 /* This is good enough for a function argument that might not get
9812 used, and they can't do anything with it, so just return it. */
9813 return t;
9815 case LAMBDA_EXPR:
9816 case PREINCREMENT_EXPR:
9817 case POSTINCREMENT_EXPR:
9818 case PREDECREMENT_EXPR:
9819 case POSTDECREMENT_EXPR:
9820 case NEW_EXPR:
9821 case VEC_NEW_EXPR:
9822 case DELETE_EXPR:
9823 case VEC_DELETE_EXPR:
9824 case THROW_EXPR:
9825 case MODIFY_EXPR:
9826 case MODOP_EXPR:
9827 /* GCC internal stuff. */
9828 case VA_ARG_EXPR:
9829 case OBJ_TYPE_REF:
9830 case WITH_CLEANUP_EXPR:
9831 case STATEMENT_LIST:
9832 case BIND_EXPR:
9833 case NON_DEPENDENT_EXPR:
9834 case BASELINK:
9835 case EXPR_STMT:
9836 case OFFSET_REF:
9837 if (!allow_non_constant)
9838 error_at (EXPR_LOC_OR_LOC (t, input_location),
9839 "expression %qE is not a constant-expression", t);
9840 *non_constant_p = true;
9841 break;
9843 default:
9844 internal_error ("unexpected expression %qE of kind %s", t,
9845 get_tree_code_name (TREE_CODE (t)));
9846 *non_constant_p = true;
9847 break;
9850 if (r == error_mark_node)
9851 *non_constant_p = true;
9853 if (*non_constant_p)
9854 return t;
9855 else
9856 return r;
9859 static tree
9860 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9862 bool non_constant_p = false;
9863 bool overflow_p = false;
9864 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
9865 false, &non_constant_p, &overflow_p);
9867 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9869 if (TREE_CODE (t) != CONSTRUCTOR
9870 && cp_has_mutable_p (TREE_TYPE (t)))
9872 /* We allow a mutable type if the original expression was a
9873 CONSTRUCTOR so that we can do aggregate initialization of
9874 constexpr variables. */
9875 if (!allow_non_constant)
9876 error ("%qT cannot be the type of a complete constant expression "
9877 "because it has mutable sub-objects", TREE_TYPE (t));
9878 non_constant_p = true;
9881 /* Technically we should check this for all subexpressions, but that
9882 runs into problems with our internal representation of pointer
9883 subtraction and the 5.19 rules are still in flux. */
9884 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
9885 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
9886 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
9888 if (!allow_non_constant)
9889 error ("conversion from pointer type %qT "
9890 "to arithmetic type %qT in a constant-expression",
9891 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
9892 non_constant_p = true;
9895 if (!non_constant_p && overflow_p)
9896 non_constant_p = true;
9898 if (non_constant_p && !allow_non_constant)
9899 return error_mark_node;
9900 else if (non_constant_p && TREE_CONSTANT (r))
9902 /* This isn't actually constant, so unset TREE_CONSTANT. */
9903 if (EXPR_P (r))
9904 r = copy_node (r);
9905 else if (TREE_CODE (r) == CONSTRUCTOR)
9906 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
9907 else
9908 r = build_nop (TREE_TYPE (r), r);
9909 TREE_CONSTANT (r) = false;
9911 else if (non_constant_p || r == t)
9912 return t;
9914 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9916 if (TREE_CODE (t) == TARGET_EXPR
9917 && TARGET_EXPR_INITIAL (t) == r)
9918 return t;
9919 else
9921 r = get_target_expr (r);
9922 TREE_CONSTANT (r) = true;
9923 return r;
9926 else
9927 return r;
9930 /* Returns true if T is a valid subexpression of a constant expression,
9931 even if it isn't itself a constant expression. */
9933 bool
9934 is_sub_constant_expr (tree t)
9936 bool non_constant_p = false;
9937 bool overflow_p = false;
9938 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
9939 &overflow_p);
9940 return !non_constant_p && !overflow_p;
9943 /* If T represents a constant expression returns its reduced value.
9944 Otherwise return error_mark_node. If T is dependent, then
9945 return NULL. */
9947 tree
9948 cxx_constant_value (tree t)
9950 return cxx_eval_outermost_constant_expr (t, false);
9953 /* If T is a constant expression, returns its reduced value.
9954 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9955 Otherwise, returns a version of T without TREE_CONSTANT. */
9957 tree
9958 maybe_constant_value (tree t)
9960 tree r;
9962 if (instantiation_dependent_expression_p (t)
9963 || type_unknown_p (t)
9964 || BRACE_ENCLOSED_INITIALIZER_P (t)
9965 || !potential_constant_expression (t))
9967 if (TREE_OVERFLOW_P (t))
9969 t = build_nop (TREE_TYPE (t), t);
9970 TREE_CONSTANT (t) = false;
9972 return t;
9975 r = cxx_eval_outermost_constant_expr (t, true);
9976 #ifdef ENABLE_CHECKING
9977 /* cp_tree_equal looks through NOPs, so allow them. */
9978 gcc_assert (r == t
9979 || CONVERT_EXPR_P (t)
9980 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9981 || !cp_tree_equal (r, t));
9982 #endif
9983 return r;
9986 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9987 than wrapped in a TARGET_EXPR. */
9989 tree
9990 maybe_constant_init (tree t)
9992 t = maybe_constant_value (t);
9993 if (TREE_CODE (t) == TARGET_EXPR)
9995 tree init = TARGET_EXPR_INITIAL (t);
9996 if (TREE_CODE (init) == CONSTRUCTOR)
9997 t = init;
9999 return t;
10002 #if 0
10003 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
10004 /* Return true if the object referred to by REF has automatic or thread
10005 local storage. */
10007 enum { ck_ok, ck_bad, ck_unknown };
10008 static int
10009 check_automatic_or_tls (tree ref)
10011 enum machine_mode mode;
10012 HOST_WIDE_INT bitsize, bitpos;
10013 tree offset;
10014 int volatilep = 0, unsignedp = 0;
10015 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
10016 &mode, &unsignedp, &volatilep, false);
10017 duration_kind dk;
10019 /* If there isn't a decl in the middle, we don't know the linkage here,
10020 and this isn't a constant expression anyway. */
10021 if (!DECL_P (decl))
10022 return ck_unknown;
10023 dk = decl_storage_duration (decl);
10024 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
10026 #endif
10028 /* Return true if T denotes a potentially constant expression. Issue
10029 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
10030 an lvalue-rvalue conversion is implied.
10032 C++0x [expr.const] used to say
10034 6 An expression is a potential constant expression if it is
10035 a constant expression where all occurrences of function
10036 parameters are replaced by arbitrary constant expressions
10037 of the appropriate type.
10039 2 A conditional expression is a constant expression unless it
10040 involves one of the following as a potentially evaluated
10041 subexpression (3.2), but subexpressions of logical AND (5.14),
10042 logical OR (5.15), and conditional (5.16) operations that are
10043 not evaluated are not considered. */
10045 static bool
10046 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
10048 enum { any = false, rval = true };
10049 int i;
10050 tree tmp;
10052 if (t == error_mark_node)
10053 return false;
10054 if (t == NULL_TREE)
10055 return true;
10056 if (TREE_THIS_VOLATILE (t))
10058 if (flags & tf_error)
10059 error ("expression %qE has side-effects", t);
10060 return false;
10062 if (CONSTANT_CLASS_P (t))
10063 return true;
10065 switch (TREE_CODE (t))
10067 case FUNCTION_DECL:
10068 case BASELINK:
10069 case TEMPLATE_DECL:
10070 case OVERLOAD:
10071 case TEMPLATE_ID_EXPR:
10072 case LABEL_DECL:
10073 case LABEL_EXPR:
10074 case CONST_DECL:
10075 case SIZEOF_EXPR:
10076 case ALIGNOF_EXPR:
10077 case OFFSETOF_EXPR:
10078 case NOEXCEPT_EXPR:
10079 case TEMPLATE_PARM_INDEX:
10080 case TRAIT_EXPR:
10081 case IDENTIFIER_NODE:
10082 case USERDEF_LITERAL:
10083 /* We can see a FIELD_DECL in a pointer-to-member expression. */
10084 case FIELD_DECL:
10085 case PARM_DECL:
10086 case USING_DECL:
10087 return true;
10089 case AGGR_INIT_EXPR:
10090 case CALL_EXPR:
10091 /* -- an invocation of a function other than a constexpr function
10092 or a constexpr constructor. */
10094 tree fun = get_function_named_in_call (t);
10095 const int nargs = call_expr_nargs (t);
10096 i = 0;
10098 if (is_overloaded_fn (fun))
10100 if (TREE_CODE (fun) == FUNCTION_DECL)
10102 if (builtin_valid_in_constant_expr_p (fun))
10103 return true;
10104 if (!DECL_DECLARED_CONSTEXPR_P (fun)
10105 /* Allow any built-in function; if the expansion
10106 isn't constant, we'll deal with that then. */
10107 && !is_builtin_fn (fun))
10109 if (flags & tf_error)
10111 error_at (EXPR_LOC_OR_LOC (t, input_location),
10112 "call to non-constexpr function %qD", fun);
10113 explain_invalid_constexpr_fn (fun);
10115 return false;
10117 /* A call to a non-static member function takes the address
10118 of the object as the first argument. But in a constant
10119 expression the address will be folded away, so look
10120 through it now. */
10121 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
10122 && !DECL_CONSTRUCTOR_P (fun))
10124 tree x = get_nth_callarg (t, 0);
10125 if (is_this_parameter (x))
10127 if (DECL_CONTEXT (x) == NULL_TREE
10128 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10130 if (flags & tf_error)
10131 sorry ("calling a member function of the "
10132 "object being constructed in a constant "
10133 "expression");
10134 return false;
10136 /* Otherwise OK. */;
10138 else if (!potential_constant_expression_1 (x, rval, flags))
10139 return false;
10140 i = 1;
10143 else
10145 if (!potential_constant_expression_1 (fun, true, flags))
10146 return false;
10147 fun = get_first_fn (fun);
10149 /* Skip initial arguments to base constructors. */
10150 if (DECL_BASE_CONSTRUCTOR_P (fun))
10151 i = num_artificial_parms_for (fun);
10152 fun = DECL_ORIGIN (fun);
10154 else
10156 if (potential_constant_expression_1 (fun, rval, flags))
10157 /* Might end up being a constant function pointer. */;
10158 else
10159 return false;
10161 for (; i < nargs; ++i)
10163 tree x = get_nth_callarg (t, i);
10164 if (!potential_constant_expression_1 (x, rval, flags))
10165 return false;
10167 return true;
10170 case NON_LVALUE_EXPR:
10171 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10172 -- an lvalue of integral type that refers to a non-volatile
10173 const variable or static data member initialized with
10174 constant expressions, or
10176 -- an lvalue of literal type that refers to non-volatile
10177 object defined with constexpr, or that refers to a
10178 sub-object of such an object; */
10179 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10181 case VAR_DECL:
10182 if (want_rval && !decl_constant_var_p (t)
10183 && !dependent_type_p (TREE_TYPE (t)))
10185 if (flags & tf_error)
10186 non_const_var_error (t);
10187 return false;
10189 return true;
10191 case NOP_EXPR:
10192 case CONVERT_EXPR:
10193 case VIEW_CONVERT_EXPR:
10194 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10195 may change to something more specific to type-punning (DR 1312). */
10197 tree from = TREE_OPERAND (t, 0);
10198 if (POINTER_TYPE_P (TREE_TYPE (t))
10199 && TREE_CODE (from) == INTEGER_CST
10200 && !integer_zerop (from))
10202 if (flags & tf_error)
10203 error_at (EXPR_LOC_OR_LOC (t, input_location),
10204 "reinterpret_cast from integer to pointer");
10205 return false;
10207 return (potential_constant_expression_1
10208 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10211 case ADDR_EXPR:
10212 /* -- a unary operator & that is applied to an lvalue that
10213 designates an object with thread or automatic storage
10214 duration; */
10215 t = TREE_OPERAND (t, 0);
10216 #if 0
10217 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10218 any checking here, as we might dereference the pointer later. If
10219 we remove this code, also remove check_automatic_or_tls. */
10220 i = check_automatic_or_tls (t);
10221 if (i == ck_ok)
10222 return true;
10223 if (i == ck_bad)
10225 if (flags & tf_error)
10226 error ("address-of an object %qE with thread local or "
10227 "automatic storage is not a constant expression", t);
10228 return false;
10230 #endif
10231 return potential_constant_expression_1 (t, any, flags);
10233 case COMPONENT_REF:
10234 case BIT_FIELD_REF:
10235 case ARROW_EXPR:
10236 case OFFSET_REF:
10237 /* -- a class member access unless its postfix-expression is
10238 of literal type or of pointer to literal type. */
10239 /* This test would be redundant, as it follows from the
10240 postfix-expression being a potential constant expression. */
10241 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10242 want_rval, flags);
10244 case EXPR_PACK_EXPANSION:
10245 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10246 want_rval, flags);
10248 case INDIRECT_REF:
10250 tree x = TREE_OPERAND (t, 0);
10251 STRIP_NOPS (x);
10252 if (is_this_parameter (x))
10254 if (DECL_CONTEXT (x)
10255 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10257 if (flags & tf_error)
10258 error ("use of %<this%> in a constant expression");
10259 return false;
10261 if (want_rval && DECL_CONTEXT (x)
10262 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10264 if (flags & tf_error)
10265 sorry ("use of the value of the object being constructed "
10266 "in a constant expression");
10267 return false;
10269 return true;
10271 return potential_constant_expression_1 (x, rval, flags);
10274 case LAMBDA_EXPR:
10275 case DYNAMIC_CAST_EXPR:
10276 case PSEUDO_DTOR_EXPR:
10277 case PREINCREMENT_EXPR:
10278 case POSTINCREMENT_EXPR:
10279 case PREDECREMENT_EXPR:
10280 case POSTDECREMENT_EXPR:
10281 case NEW_EXPR:
10282 case VEC_NEW_EXPR:
10283 case DELETE_EXPR:
10284 case VEC_DELETE_EXPR:
10285 case THROW_EXPR:
10286 case MODIFY_EXPR:
10287 case MODOP_EXPR:
10288 case OMP_ATOMIC:
10289 case OMP_ATOMIC_READ:
10290 case OMP_ATOMIC_CAPTURE_OLD:
10291 case OMP_ATOMIC_CAPTURE_NEW:
10292 /* GCC internal stuff. */
10293 case VA_ARG_EXPR:
10294 case OBJ_TYPE_REF:
10295 case WITH_CLEANUP_EXPR:
10296 case CLEANUP_POINT_EXPR:
10297 case MUST_NOT_THROW_EXPR:
10298 case TRY_CATCH_EXPR:
10299 case STATEMENT_LIST:
10300 /* Don't bother trying to define a subset of statement-expressions to
10301 be constant-expressions, at least for now. */
10302 case STMT_EXPR:
10303 case EXPR_STMT:
10304 case BIND_EXPR:
10305 case TRANSACTION_EXPR:
10306 case IF_STMT:
10307 case DO_STMT:
10308 case FOR_STMT:
10309 case WHILE_STMT:
10310 case DECL_EXPR:
10311 if (flags & tf_error)
10312 error ("expression %qE is not a constant-expression", t);
10313 return false;
10315 case TYPEID_EXPR:
10316 /* -- a typeid expression whose operand is of polymorphic
10317 class type; */
10319 tree e = TREE_OPERAND (t, 0);
10320 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10321 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10323 if (flags & tf_error)
10324 error ("typeid-expression is not a constant expression "
10325 "because %qE is of polymorphic type", e);
10326 return false;
10328 return true;
10331 case MINUS_EXPR:
10332 /* -- a subtraction where both operands are pointers. */
10333 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10334 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10336 if (flags & tf_error)
10337 error ("difference of two pointer expressions is not "
10338 "a constant expression");
10339 return false;
10341 want_rval = true;
10342 goto binary;
10344 case LT_EXPR:
10345 case LE_EXPR:
10346 case GT_EXPR:
10347 case GE_EXPR:
10348 case EQ_EXPR:
10349 case NE_EXPR:
10350 /* -- a relational or equality operator where at least
10351 one of the operands is a pointer. */
10352 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10353 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10355 if (flags & tf_error)
10356 error ("pointer comparison expression is not a "
10357 "constant expression");
10358 return false;
10360 want_rval = true;
10361 goto binary;
10363 case BIT_NOT_EXPR:
10364 /* A destructor. */
10365 if (TYPE_P (TREE_OPERAND (t, 0)))
10366 return true;
10367 /* else fall through. */
10369 case REALPART_EXPR:
10370 case IMAGPART_EXPR:
10371 case CONJ_EXPR:
10372 case SAVE_EXPR:
10373 case FIX_TRUNC_EXPR:
10374 case FLOAT_EXPR:
10375 case NEGATE_EXPR:
10376 case ABS_EXPR:
10377 case TRUTH_NOT_EXPR:
10378 case FIXED_CONVERT_EXPR:
10379 case UNARY_PLUS_EXPR:
10380 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10381 flags);
10383 case CAST_EXPR:
10384 case CONST_CAST_EXPR:
10385 case STATIC_CAST_EXPR:
10386 case REINTERPRET_CAST_EXPR:
10387 case IMPLICIT_CONV_EXPR:
10388 if (cxx_dialect < cxx11
10389 && !dependent_type_p (TREE_TYPE (t))
10390 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10391 /* In C++98, a conversion to non-integral type can't be part of a
10392 constant expression. */
10394 if (flags & tf_error)
10395 error ("cast to non-integral type %qT in a constant expression",
10396 TREE_TYPE (t));
10397 return false;
10400 return (potential_constant_expression_1
10401 (TREE_OPERAND (t, 0),
10402 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10404 case PAREN_EXPR:
10405 case NON_DEPENDENT_EXPR:
10406 /* For convenience. */
10407 case RETURN_EXPR:
10408 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10409 want_rval, flags);
10411 case SCOPE_REF:
10412 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10413 want_rval, flags);
10415 case TARGET_EXPR:
10416 if (!literal_type_p (TREE_TYPE (t)))
10418 if (flags & tf_error)
10420 error ("temporary of non-literal type %qT in a "
10421 "constant expression", TREE_TYPE (t));
10422 explain_non_literal_class (TREE_TYPE (t));
10424 return false;
10426 case INIT_EXPR:
10427 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10428 rval, flags);
10430 case CONSTRUCTOR:
10432 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10433 constructor_elt *ce;
10434 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10435 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10436 return false;
10437 return true;
10440 case TREE_LIST:
10442 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10443 || DECL_P (TREE_PURPOSE (t)));
10444 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10445 flags))
10446 return false;
10447 if (TREE_CHAIN (t) == NULL_TREE)
10448 return true;
10449 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10450 flags);
10453 case TRUNC_DIV_EXPR:
10454 case CEIL_DIV_EXPR:
10455 case FLOOR_DIV_EXPR:
10456 case ROUND_DIV_EXPR:
10457 case TRUNC_MOD_EXPR:
10458 case CEIL_MOD_EXPR:
10459 case ROUND_MOD_EXPR:
10461 tree denom = TREE_OPERAND (t, 1);
10462 if (!potential_constant_expression_1 (denom, rval, flags))
10463 return false;
10464 /* We can't call cxx_eval_outermost_constant_expr on an expression
10465 that hasn't been through fold_non_dependent_expr yet. */
10466 if (!processing_template_decl)
10467 denom = cxx_eval_outermost_constant_expr (denom, true);
10468 if (integer_zerop (denom))
10470 if (flags & tf_error)
10471 error ("division by zero is not a constant-expression");
10472 return false;
10474 else
10476 want_rval = true;
10477 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10478 want_rval, flags);
10482 case COMPOUND_EXPR:
10484 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10485 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10486 introduced by build_call_a. */
10487 tree op0 = TREE_OPERAND (t, 0);
10488 tree op1 = TREE_OPERAND (t, 1);
10489 STRIP_NOPS (op1);
10490 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10491 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10492 return potential_constant_expression_1 (op0, want_rval, flags);
10493 else
10494 goto binary;
10497 /* If the first operand is the non-short-circuit constant, look at
10498 the second operand; otherwise we only care about the first one for
10499 potentiality. */
10500 case TRUTH_AND_EXPR:
10501 case TRUTH_ANDIF_EXPR:
10502 tmp = boolean_true_node;
10503 goto truth;
10504 case TRUTH_OR_EXPR:
10505 case TRUTH_ORIF_EXPR:
10506 tmp = boolean_false_node;
10507 truth:
10509 tree op = TREE_OPERAND (t, 0);
10510 if (!potential_constant_expression_1 (op, rval, flags))
10511 return false;
10512 if (!processing_template_decl)
10513 op = cxx_eval_outermost_constant_expr (op, true);
10514 if (tree_int_cst_equal (op, tmp))
10515 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10516 else
10517 return true;
10520 case PLUS_EXPR:
10521 case MULT_EXPR:
10522 case POINTER_PLUS_EXPR:
10523 case RDIV_EXPR:
10524 case EXACT_DIV_EXPR:
10525 case MIN_EXPR:
10526 case MAX_EXPR:
10527 case LSHIFT_EXPR:
10528 case RSHIFT_EXPR:
10529 case LROTATE_EXPR:
10530 case RROTATE_EXPR:
10531 case BIT_IOR_EXPR:
10532 case BIT_XOR_EXPR:
10533 case BIT_AND_EXPR:
10534 case TRUTH_XOR_EXPR:
10535 case UNORDERED_EXPR:
10536 case ORDERED_EXPR:
10537 case UNLT_EXPR:
10538 case UNLE_EXPR:
10539 case UNGT_EXPR:
10540 case UNGE_EXPR:
10541 case UNEQ_EXPR:
10542 case LTGT_EXPR:
10543 case RANGE_EXPR:
10544 case COMPLEX_EXPR:
10545 want_rval = true;
10546 /* Fall through. */
10547 case ARRAY_REF:
10548 case ARRAY_RANGE_REF:
10549 case MEMBER_REF:
10550 case DOTSTAR_EXPR:
10551 binary:
10552 for (i = 0; i < 2; ++i)
10553 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10554 want_rval, flags))
10555 return false;
10556 return true;
10558 case CILK_SYNC_STMT:
10559 case CILK_SPAWN_STMT:
10560 case ARRAY_NOTATION_REF:
10561 return false;
10563 case FMA_EXPR:
10564 case VEC_PERM_EXPR:
10565 for (i = 0; i < 3; ++i)
10566 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10567 true, flags))
10568 return false;
10569 return true;
10571 case COND_EXPR:
10572 case VEC_COND_EXPR:
10573 /* If the condition is a known constant, we know which of the legs we
10574 care about; otherwise we only require that the condition and
10575 either of the legs be potentially constant. */
10576 tmp = TREE_OPERAND (t, 0);
10577 if (!potential_constant_expression_1 (tmp, rval, flags))
10578 return false;
10579 if (!processing_template_decl)
10580 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10581 if (integer_zerop (tmp))
10582 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10583 want_rval, flags);
10584 else if (TREE_CODE (tmp) == INTEGER_CST)
10585 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10586 want_rval, flags);
10587 for (i = 1; i < 3; ++i)
10588 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10589 want_rval, tf_none))
10590 return true;
10591 if (flags & tf_error)
10592 error ("expression %qE is not a constant-expression", t);
10593 return false;
10595 case VEC_INIT_EXPR:
10596 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10597 return true;
10598 if (flags & tf_error)
10600 error ("non-constant array initialization");
10601 diagnose_non_constexpr_vec_init (t);
10603 return false;
10605 default:
10606 if (objc_is_property_ref (t))
10607 return false;
10609 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10610 gcc_unreachable();
10611 return false;
10615 /* The main entry point to the above. */
10617 bool
10618 potential_constant_expression (tree t)
10620 return potential_constant_expression_1 (t, false, tf_none);
10623 /* As above, but require a constant rvalue. */
10625 bool
10626 potential_rvalue_constant_expression (tree t)
10628 return potential_constant_expression_1 (t, true, tf_none);
10631 /* Like above, but complain about non-constant expressions. */
10633 bool
10634 require_potential_constant_expression (tree t)
10636 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10639 /* Cross product of the above. */
10641 bool
10642 require_potential_rvalue_constant_expression (tree t)
10644 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10647 /* Insert the deduced return type for an auto function. */
10649 void
10650 apply_deduced_return_type (tree fco, tree return_type)
10652 tree result;
10654 if (return_type == error_mark_node)
10655 return;
10657 if (LAMBDA_FUNCTION_P (fco))
10659 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10660 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10663 if (DECL_CONV_FN_P (fco))
10664 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10666 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10668 result = DECL_RESULT (fco);
10669 if (result == NULL_TREE)
10670 return;
10671 if (TREE_TYPE (result) == return_type)
10672 return;
10674 /* We already have a DECL_RESULT from start_preparsed_function.
10675 Now we need to redo the work it and allocate_struct_function
10676 did to reflect the new type. */
10677 gcc_assert (current_function_decl == fco);
10678 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10679 TYPE_MAIN_VARIANT (return_type));
10680 DECL_ARTIFICIAL (result) = 1;
10681 DECL_IGNORED_P (result) = 1;
10682 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10683 result);
10685 DECL_RESULT (fco) = result;
10687 if (!processing_template_decl)
10689 if (!VOID_TYPE_P (TREE_TYPE (result)))
10690 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
10691 bool aggr = aggregate_value_p (result, fco);
10692 #ifdef PCC_STATIC_STRUCT_RETURN
10693 cfun->returns_pcc_struct = aggr;
10694 #endif
10695 cfun->returns_struct = aggr;
10700 /* DECL is a local variable or parameter from the surrounding scope of a
10701 lambda-expression. Returns the decltype for a use of the capture field
10702 for DECL even if it hasn't been captured yet. */
10704 static tree
10705 capture_decltype (tree decl)
10707 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10708 /* FIXME do lookup instead of list walk? */
10709 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10710 tree type;
10712 if (cap)
10713 type = TREE_TYPE (TREE_PURPOSE (cap));
10714 else
10715 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10717 case CPLD_NONE:
10718 error ("%qD is not captured", decl);
10719 return error_mark_node;
10721 case CPLD_COPY:
10722 type = TREE_TYPE (decl);
10723 if (TREE_CODE (type) == REFERENCE_TYPE
10724 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10725 type = TREE_TYPE (type);
10726 break;
10728 case CPLD_REFERENCE:
10729 type = TREE_TYPE (decl);
10730 if (TREE_CODE (type) != REFERENCE_TYPE)
10731 type = build_reference_type (TREE_TYPE (decl));
10732 break;
10734 default:
10735 gcc_unreachable ();
10738 if (TREE_CODE (type) != REFERENCE_TYPE)
10740 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10741 type = cp_build_qualified_type (type, (cp_type_quals (type)
10742 |TYPE_QUAL_CONST));
10743 type = build_reference_type (type);
10745 return type;
10748 #include "gt-cp-semantics.h"