1 /* C++-specific tree lowering bits; see also c-gimplify.c and tree-gimple.c.
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Contributed by Jason Merrill <jason@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
31 #include "tree-iterator.h"
34 #include "pointer-set.h"
37 /* Local declarations. */
39 enum bc_t { bc_break = 0, bc_continue = 1 };
41 /* Stack of labels which are targets for "break" or "continue",
42 linked through TREE_CHAIN. */
43 static tree bc_label[2];
45 /* Begin a scope which can be exited by a break or continue statement. BC
48 Just creates a label and pushes it into the current context. */
51 begin_bc_block (enum bc_t bc)
53 tree label = create_artificial_label ();
54 TREE_CHAIN (label) = bc_label[bc];
59 /* Finish a scope which can be exited by a break or continue statement.
60 LABEL was returned from the most recent call to begin_bc_block. BODY is
61 an expression for the contents of the scope.
63 If we saw a break (or continue) in the scope, append a LABEL_EXPR to
64 body. Otherwise, just forget the label. */
67 finish_bc_block (enum bc_t bc, tree label, gimple_seq body)
69 gcc_assert (label == bc_label[bc]);
71 if (TREE_USED (label))
73 gimple_seq_add_stmt (&body, gimple_build_label (label));
76 bc_label[bc] = TREE_CHAIN (label);
77 TREE_CHAIN (label) = NULL_TREE;
81 /* Get the LABEL_EXPR to represent a break or continue statement
82 in the current block scope. BC indicates which. */
85 get_bc_label (enum bc_t bc)
87 tree label = bc_label[bc];
89 if (label == NULL_TREE)
92 error ("break statement not within loop or switch");
94 error ("continue statement not within loop or switch");
99 /* Mark the label used for finish_bc_block. */
100 TREE_USED (label) = 1;
104 /* Genericize a TRY_BLOCK. */
107 genericize_try_block (tree *stmt_p)
109 tree body = TRY_STMTS (*stmt_p);
110 tree cleanup = TRY_HANDLERS (*stmt_p);
112 *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
115 /* Genericize a HANDLER by converting to a CATCH_EXPR. */
118 genericize_catch_block (tree *stmt_p)
120 tree type = HANDLER_TYPE (*stmt_p);
121 tree body = HANDLER_BODY (*stmt_p);
123 /* FIXME should the caught type go in TREE_TYPE? */
124 *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
127 /* A terser interface for building a representation of an exception
131 build_gimple_eh_filter_tree (tree body, tree allowed, tree failure)
135 /* FIXME should the allowed types go in TREE_TYPE? */
136 t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
137 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
139 t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
140 append_to_statement_list (body, &TREE_OPERAND (t, 0));
145 /* Genericize an EH_SPEC_BLOCK by converting it to a
146 TRY_CATCH_EXPR/EH_FILTER_EXPR pair. */
149 genericize_eh_spec_block (tree *stmt_p)
151 tree body = EH_SPEC_STMTS (*stmt_p);
152 tree allowed = EH_SPEC_RAISES (*stmt_p);
153 tree failure = build_call_n (call_unexpected_node, 1, build_exc_ptr ());
155 *stmt_p = build_gimple_eh_filter_tree (body, allowed, failure);
158 /* Genericize an IF_STMT by turning it into a COND_EXPR. */
161 genericize_if_stmt (tree *stmt_p)
163 tree stmt, cond, then_, else_;
164 location_t locus = EXPR_LOCATION (*stmt_p);
167 cond = IF_COND (stmt);
168 then_ = THEN_CLAUSE (stmt);
169 else_ = ELSE_CLAUSE (stmt);
172 then_ = build_empty_stmt ();
174 else_ = build_empty_stmt ();
176 if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
178 else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
181 stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
182 if (CAN_HAVE_LOCATION_P (stmt) && !EXPR_HAS_LOCATION (stmt))
183 SET_EXPR_LOCATION (stmt, locus);
187 /* Build a generic representation of one of the C loop forms. COND is the
188 loop condition or NULL_TREE. BODY is the (possibly compound) statement
189 controlled by the loop. INCR is the increment expression of a for-loop,
190 or NULL_TREE. COND_IS_FIRST indicates whether the condition is
191 evaluated before the loop body as in while and for loops, or after the
192 loop body as in do-while loops. */
195 gimplify_cp_loop (tree cond, tree body, tree incr, bool cond_is_first)
197 gimple top, entry, stmt;
198 gimple_seq stmt_list, body_seq, incr_seq, exit_seq;
199 tree cont_block, break_block;
200 location_t stmt_locus;
202 stmt_locus = input_location;
209 break_block = begin_bc_block (bc_break);
210 cont_block = begin_bc_block (bc_continue);
212 /* If condition is zero don't generate a loop construct. */
213 if (cond && integer_zerop (cond))
218 stmt = gimple_build_goto (get_bc_label (bc_break));
219 gimple_set_location (stmt, stmt_locus);
220 gimple_seq_add_stmt (&stmt_list, stmt);
225 /* If we use a LOOP_EXPR here, we have to feed the whole thing
226 back through the main gimplifier to lower it. Given that we
227 have to gimplify the loop body NOW so that we can resolve
228 break/continue stmts, seems easier to just expand to gotos. */
229 top = gimple_build_label (create_artificial_label ());
231 /* If we have an exit condition, then we build an IF with gotos either
232 out of the loop, or to the top of it. If there's no exit condition,
233 then we just build a jump back to the top. */
234 if (cond && !integer_nonzerop (cond))
236 if (cond != error_mark_node)
238 gimplify_expr (&cond, &exit_seq, NULL, is_gimple_val, fb_rvalue);
239 stmt = gimple_build_cond (NE_EXPR, cond,
240 build_int_cst (TREE_TYPE (cond), 0),
241 gimple_label_label (top),
242 get_bc_label (bc_break));
243 gimple_seq_add_stmt (&exit_seq, stmt);
250 entry = gimple_build_label (create_artificial_label ());
251 stmt = gimple_build_goto (gimple_label_label (entry));
254 stmt = gimple_build_goto (get_bc_label (bc_continue));
255 gimple_set_location (stmt, stmt_locus);
256 gimple_seq_add_stmt (&stmt_list, stmt);
261 stmt = gimple_build_goto (gimple_label_label (top));
262 gimple_seq_add_stmt (&exit_seq, stmt);
266 gimplify_stmt (&body, &body_seq);
267 gimplify_stmt (&incr, &incr_seq);
269 body_seq = finish_bc_block (bc_continue, cont_block, body_seq);
271 gimple_seq_add_stmt (&stmt_list, top);
272 gimple_seq_add_seq (&stmt_list, body_seq);
273 gimple_seq_add_seq (&stmt_list, incr_seq);
274 gimple_seq_add_stmt (&stmt_list, entry);
275 gimple_seq_add_seq (&stmt_list, exit_seq);
277 annotate_all_with_location (stmt_list, stmt_locus);
279 return finish_bc_block (bc_break, break_block, stmt_list);
282 /* Gimplify a FOR_STMT node. Move the stuff in the for-init-stmt into the
283 prequeue and hand off to gimplify_cp_loop. */
286 gimplify_for_stmt (tree *stmt_p, gimple_seq *pre_p)
290 if (FOR_INIT_STMT (stmt))
291 gimplify_and_add (FOR_INIT_STMT (stmt), pre_p);
293 gimple_seq_add_seq (pre_p,
294 gimplify_cp_loop (FOR_COND (stmt), FOR_BODY (stmt),
295 FOR_EXPR (stmt), 1));
299 /* Gimplify a WHILE_STMT node. */
302 gimplify_while_stmt (tree *stmt_p, gimple_seq *pre_p)
305 gimple_seq_add_seq (pre_p,
306 gimplify_cp_loop (WHILE_COND (stmt), WHILE_BODY (stmt),
311 /* Gimplify a DO_STMT node. */
314 gimplify_do_stmt (tree *stmt_p, gimple_seq *pre_p)
317 gimple_seq_add_seq (pre_p,
318 gimplify_cp_loop (DO_COND (stmt), DO_BODY (stmt),
323 /* Genericize a SWITCH_STMT by turning it into a SWITCH_EXPR. */
326 gimplify_switch_stmt (tree *stmt_p, gimple_seq *pre_p)
329 tree break_block, body, t;
330 location_t stmt_locus = input_location;
331 gimple_seq seq = NULL;
333 break_block = begin_bc_block (bc_break);
335 body = SWITCH_STMT_BODY (stmt);
337 body = build_empty_stmt ();
339 t = build3 (SWITCH_EXPR, SWITCH_STMT_TYPE (stmt),
340 SWITCH_STMT_COND (stmt), body, NULL_TREE);
341 SET_EXPR_LOCATION (t, stmt_locus);
342 gimplify_and_add (t, &seq);
344 seq = finish_bc_block (bc_break, break_block, seq);
345 gimple_seq_add_seq (pre_p, seq);
349 /* Hook into the middle of gimplifying an OMP_FOR node. This is required
350 in order to properly gimplify CONTINUE statements. Here we merely
351 manage the continue stack; the rest of the job is performed by the
352 regular gimplifier. */
354 static enum gimplify_status
355 cp_gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
357 tree for_stmt = *expr_p;
360 gimple_seq seq = NULL;
362 /* Protect ourselves from recursion. */
363 if (OMP_FOR_GIMPLIFYING_P (for_stmt))
365 OMP_FOR_GIMPLIFYING_P (for_stmt) = 1;
367 /* Note that while technically the continue label is enabled too soon
368 here, we should have already diagnosed invalid continues nested within
369 statement expressions within the INIT, COND, or INCR expressions. */
370 cont_block = begin_bc_block (bc_continue);
372 gimplify_and_add (for_stmt, &seq);
373 stmt = gimple_seq_last_stmt (seq);
374 if (gimple_code (stmt) == GIMPLE_OMP_FOR)
375 gimple_omp_set_body (stmt, finish_bc_block (bc_continue, cont_block,
376 gimple_omp_body (stmt)));
378 seq = finish_bc_block (bc_continue, cont_block, seq);
379 gimple_seq_add_seq (pre_p, seq);
381 OMP_FOR_GIMPLIFYING_P (for_stmt) = 0;
386 /* Gimplify an EXPR_STMT node. */
389 gimplify_expr_stmt (tree *stmt_p)
391 tree stmt = EXPR_STMT_EXPR (*stmt_p);
393 if (stmt == error_mark_node)
396 /* Gimplification of a statement expression will nullify the
397 statement if all its side effects are moved to *PRE_P and *POST_P.
399 In this case we will not want to emit the gimplified statement.
400 However, we may still want to emit a warning, so we do that before
402 if (stmt && warn_unused_value)
404 if (!TREE_SIDE_EFFECTS (stmt))
406 if (!IS_EMPTY_STMT (stmt)
407 && !VOID_TYPE_P (TREE_TYPE (stmt))
408 && !TREE_NO_WARNING (stmt))
409 warning (OPT_Wunused_value, "statement with no effect");
412 warn_if_unused_value (stmt, input_location);
415 if (stmt == NULL_TREE)
416 stmt = alloc_stmt_list ();
421 /* Gimplify initialization from an AGGR_INIT_EXPR. */
424 cp_gimplify_init_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
426 tree from = TREE_OPERAND (*expr_p, 1);
427 tree to = TREE_OPERAND (*expr_p, 0);
429 tree slot = NULL_TREE;
431 /* What about code that pulls out the temp and uses it elsewhere? I
432 think that such code never uses the TARGET_EXPR as an initializer. If
433 I'm wrong, we'll abort because the temp won't have any RTL. In that
434 case, I guess we'll need to replace references somehow. */
435 if (TREE_CODE (from) == TARGET_EXPR)
437 slot = TARGET_EXPR_SLOT (from);
438 from = TARGET_EXPR_INITIAL (from);
441 /* Look through any COMPOUND_EXPRs, since build_compound_expr pushes them
442 inside the TARGET_EXPR. */
445 tree sub = TREE_CODE (t) == COMPOUND_EXPR ? TREE_OPERAND (t, 0) : t;
447 /* If we are initializing from an AGGR_INIT_EXPR, drop the INIT_EXPR and
448 replace the slot operand with our target.
450 Should we add a target parm to gimplify_expr instead? No, as in this
451 case we want to replace the INIT_EXPR. */
452 if (TREE_CODE (sub) == AGGR_INIT_EXPR)
454 gimplify_expr (&to, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
455 AGGR_INIT_EXPR_SLOT (sub) = to;
458 /* The initialization is now a side-effect, so the container can
461 TREE_TYPE (from) = void_type_node;
467 t = TREE_OPERAND (t, 1);
472 /* Gimplify a MUST_NOT_THROW_EXPR. */
474 static enum gimplify_status
475 gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
478 tree temp = voidify_wrapper_expr (stmt, NULL);
479 tree body = TREE_OPERAND (stmt, 0);
481 stmt = build_gimple_eh_filter_tree (body, NULL_TREE,
482 build_call_n (terminate_node, 0));
484 gimplify_and_add (stmt, pre_p);
495 /* Do C++-specific gimplification. Args are as for gimplify_expr. */
498 cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
500 int saved_stmts_are_full_exprs_p = 0;
501 enum tree_code code = TREE_CODE (*expr_p);
502 enum gimplify_status ret;
504 VEC(gimple, heap) *bind_expr_stack = NULL;
506 if (STATEMENT_CODE_P (code))
508 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
509 current_stmt_tree ()->stmts_are_full_exprs_p
510 = STMT_IS_FULL_EXPR_P (*expr_p);
516 *expr_p = cplus_expand_constant (*expr_p);
521 simplify_aggr_init_expr (expr_p);
526 /* FIXME communicate throw type to back end, probably by moving
527 THROW_EXPR into ../tree.def. */
528 *expr_p = TREE_OPERAND (*expr_p, 0);
532 case MUST_NOT_THROW_EXPR:
533 ret = gimplify_must_not_throw_expr (expr_p, pre_p);
536 /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the
537 LHS of an assignment might also be involved in the RHS, as in bug
540 cp_gimplify_init_expr (expr_p, pre_p, post_p);
544 case EMPTY_CLASS_EXPR:
545 /* We create an empty CONSTRUCTOR with RECORD_TYPE. */
546 *expr_p = build_constructor (TREE_TYPE (*expr_p), NULL);
551 *expr_p = BASELINK_FUNCTIONS (*expr_p);
556 genericize_try_block (expr_p);
561 genericize_catch_block (expr_p);
566 genericize_eh_spec_block (expr_p);
571 /* Get the innermost inclosing GIMPLE_BIND that has a non NULL
572 BLOCK, and append an IMPORTED_DECL to its
573 BLOCK_VARS chained list. */
575 bind_expr_stack = gimple_bind_expr_stack ();
579 for (i = VEC_length (gimple, bind_expr_stack) - 1; i >= 0; i--)
580 if ((block = gimple_bind_block (VEC_index (gimple,
587 tree using_directive;
588 gcc_assert (TREE_OPERAND (*expr_p, 0));
590 using_directive = make_node (IMPORTED_DECL);
591 TREE_TYPE (using_directive) = void_type_node;
593 IMPORTED_DECL_ASSOCIATED_DECL (using_directive)
594 = TREE_OPERAND (*expr_p, 0);
595 TREE_CHAIN (using_directive) = BLOCK_VARS (block);
596 BLOCK_VARS (block) = using_directive;
598 /* The USING_STMT won't appear in GIMPLE. */
604 gimplify_for_stmt (expr_p, pre_p);
609 gimplify_while_stmt (expr_p, pre_p);
614 gimplify_do_stmt (expr_p, pre_p);
619 gimplify_switch_stmt (expr_p, pre_p);
624 ret = cp_gimplify_omp_for (expr_p, pre_p);
628 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_CONTINUE, NOT_TAKEN));
629 gimple_seq_add_stmt (pre_p, gimple_build_goto (get_bc_label (bc_continue)));
635 gimple_seq_add_stmt (pre_p, gimple_build_goto (get_bc_label (bc_break)));
641 gimplify_expr_stmt (expr_p);
645 case UNARY_PLUS_EXPR:
647 tree arg = TREE_OPERAND (*expr_p, 0);
648 tree type = TREE_TYPE (*expr_p);
649 *expr_p = (TREE_TYPE (arg) != type) ? fold_convert (type, arg)
656 ret = c_gimplify_expr (expr_p, pre_p, post_p);
660 /* Restore saved state. */
661 if (STATEMENT_CODE_P (code))
662 current_stmt_tree ()->stmts_are_full_exprs_p
663 = saved_stmts_are_full_exprs_p;
669 is_invisiref_parm (const_tree t)
671 return ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
672 && DECL_BY_REFERENCE (t));
675 /* Return true if the uid in both int tree maps are equal. */
678 cxx_int_tree_map_eq (const void *va, const void *vb)
680 const struct cxx_int_tree_map *a = (const struct cxx_int_tree_map *) va;
681 const struct cxx_int_tree_map *b = (const struct cxx_int_tree_map *) vb;
682 return (a->uid == b->uid);
685 /* Hash a UID in a cxx_int_tree_map. */
688 cxx_int_tree_map_hash (const void *item)
690 return ((const struct cxx_int_tree_map *)item)->uid;
693 /* Perform any pre-gimplification lowering of C++ front end trees to
697 cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
700 struct pointer_set_t *p_set = (struct pointer_set_t*) data;
702 if (is_invisiref_parm (stmt)
703 /* Don't dereference parms in a thunk, pass the references through. */
704 && !(DECL_THUNK_P (current_function_decl)
705 && TREE_CODE (stmt) == PARM_DECL))
707 *stmt_p = convert_from_reference (stmt);
712 /* Map block scope extern declarations to visible declarations with the
713 same name and type in outer scopes if any. */
714 if (cp_function_chain->extern_decl_map
715 && (TREE_CODE (stmt) == FUNCTION_DECL || TREE_CODE (stmt) == VAR_DECL)
716 && DECL_EXTERNAL (stmt))
718 struct cxx_int_tree_map *h, in;
719 in.uid = DECL_UID (stmt);
720 h = (struct cxx_int_tree_map *)
721 htab_find_with_hash (cp_function_chain->extern_decl_map,
731 /* Other than invisiref parms, don't walk the same tree twice. */
732 if (pointer_set_contains (p_set, stmt))
738 if (TREE_CODE (stmt) == ADDR_EXPR
739 && is_invisiref_parm (TREE_OPERAND (stmt, 0)))
741 *stmt_p = convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
744 else if (TREE_CODE (stmt) == RETURN_EXPR
745 && TREE_OPERAND (stmt, 0)
746 && is_invisiref_parm (TREE_OPERAND (stmt, 0)))
747 /* Don't dereference an invisiref RESULT_DECL inside a RETURN_EXPR. */
749 else if (TREE_CODE (stmt) == OMP_CLAUSE)
750 switch (OMP_CLAUSE_CODE (stmt))
752 case OMP_CLAUSE_LASTPRIVATE:
753 /* Don't dereference an invisiref in OpenMP clauses. */
754 if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
757 if (OMP_CLAUSE_LASTPRIVATE_STMT (stmt))
758 cp_walk_tree (&OMP_CLAUSE_LASTPRIVATE_STMT (stmt),
759 cp_genericize_r, p_set, NULL);
762 case OMP_CLAUSE_PRIVATE:
763 case OMP_CLAUSE_SHARED:
764 case OMP_CLAUSE_FIRSTPRIVATE:
765 case OMP_CLAUSE_COPYIN:
766 case OMP_CLAUSE_COPYPRIVATE:
767 /* Don't dereference an invisiref in OpenMP clauses. */
768 if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
771 case OMP_CLAUSE_REDUCTION:
772 gcc_assert (!is_invisiref_parm (OMP_CLAUSE_DECL (stmt)));
777 else if (IS_TYPE_OR_DECL_P (stmt))
780 /* Due to the way voidify_wrapper_expr is written, we don't get a chance
781 to lower this construct before scanning it, so we need to lower these
782 before doing anything else. */
783 else if (TREE_CODE (stmt) == CLEANUP_STMT)
784 *stmt_p = build2 (CLEANUP_EH_ONLY (stmt) ? TRY_CATCH_EXPR
788 CLEANUP_EXPR (stmt));
790 else if (TREE_CODE (stmt) == IF_STMT)
792 genericize_if_stmt (stmt_p);
793 /* *stmt_p has changed, tail recurse to handle it again. */
794 return cp_genericize_r (stmt_p, walk_subtrees, data);
797 /* COND_EXPR might have incompatible types in branches if one or both
798 arms are bitfields. Fix it up now. */
799 else if (TREE_CODE (stmt) == COND_EXPR)
802 = (TREE_OPERAND (stmt, 1)
803 ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 1))
806 = (TREE_OPERAND (stmt, 2)
807 ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 2))
810 && !useless_type_conversion_p (TREE_TYPE (stmt),
811 TREE_TYPE (TREE_OPERAND (stmt, 1))))
813 TREE_OPERAND (stmt, 1)
814 = fold_convert (type_left, TREE_OPERAND (stmt, 1));
815 gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
819 && !useless_type_conversion_p (TREE_TYPE (stmt),
820 TREE_TYPE (TREE_OPERAND (stmt, 2))))
822 TREE_OPERAND (stmt, 2)
823 = fold_convert (type_right, TREE_OPERAND (stmt, 2));
824 gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
829 pointer_set_insert (p_set, *stmt_p);
835 cp_genericize (tree fndecl)
838 struct pointer_set_t *p_set;
840 /* Fix up the types of parms passed by invisible reference. */
841 for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
842 if (TREE_ADDRESSABLE (TREE_TYPE (t)))
844 /* If a function's arguments are copied to create a thunk,
845 then DECL_BY_REFERENCE will be set -- but the type of the
846 argument will be a pointer type, so we will never get
848 gcc_assert (!DECL_BY_REFERENCE (t));
849 gcc_assert (DECL_ARG_TYPE (t) != TREE_TYPE (t));
850 TREE_TYPE (t) = DECL_ARG_TYPE (t);
851 DECL_BY_REFERENCE (t) = 1;
852 TREE_ADDRESSABLE (t) = 0;
856 /* Do the same for the return value. */
857 if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (fndecl))))
859 t = DECL_RESULT (fndecl);
860 TREE_TYPE (t) = build_reference_type (TREE_TYPE (t));
861 DECL_BY_REFERENCE (t) = 1;
862 TREE_ADDRESSABLE (t) = 0;
866 /* If we're a clone, the body is already GIMPLE. */
867 if (DECL_CLONED_FUNCTION_P (fndecl))
870 /* We do want to see every occurrence of the parms, so we can't just use
871 walk_tree's hash functionality. */
872 p_set = pointer_set_create ();
873 cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_genericize_r, p_set, NULL);
874 pointer_set_destroy (p_set);
876 /* Do everything else. */
877 c_genericize (fndecl);
879 gcc_assert (bc_label[bc_break] == NULL);
880 gcc_assert (bc_label[bc_continue] == NULL);
883 /* Build code to apply FN to each member of ARG1 and ARG2. FN may be
884 NULL if there is in fact nothing to do. ARG2 may be null if FN
885 actually only takes one argument. */
888 cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
890 tree defparm, parm, t;
898 nargs = list_length (DECL_ARGUMENTS (fn));
899 argarray = (tree *) alloca (nargs * sizeof (tree));
901 defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
903 defparm = TREE_CHAIN (defparm);
905 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
907 tree inner_type = TREE_TYPE (arg1);
908 tree start1, end1, p1;
909 tree start2 = NULL, p2 = NULL;
910 tree ret = NULL, lab;
916 inner_type = TREE_TYPE (inner_type);
917 start1 = build4 (ARRAY_REF, inner_type, start1,
918 size_zero_node, NULL, NULL);
920 start2 = build4 (ARRAY_REF, inner_type, start2,
921 size_zero_node, NULL, NULL);
923 while (TREE_CODE (inner_type) == ARRAY_TYPE);
924 start1 = build_fold_addr_expr (start1);
926 start2 = build_fold_addr_expr (start2);
928 end1 = TYPE_SIZE_UNIT (TREE_TYPE (arg1));
929 end1 = build2 (POINTER_PLUS_EXPR, TREE_TYPE (start1), start1, end1);
931 p1 = create_tmp_var (TREE_TYPE (start1), NULL);
932 t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, start1);
933 append_to_statement_list (t, &ret);
937 p2 = create_tmp_var (TREE_TYPE (start2), NULL);
938 t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, start2);
939 append_to_statement_list (t, &ret);
942 lab = create_artificial_label ();
943 t = build1 (LABEL_EXPR, void_type_node, lab);
944 append_to_statement_list (t, &ret);
949 /* Handle default arguments. */
950 for (parm = defparm; parm && parm != void_list_node;
951 parm = TREE_CHAIN (parm), i++)
952 argarray[i] = convert_default_arg (TREE_VALUE (parm),
953 TREE_PURPOSE (parm), fn, i);
954 t = build_call_a (fn, i, argarray);
955 t = fold_convert (void_type_node, t);
956 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
957 append_to_statement_list (t, &ret);
959 t = TYPE_SIZE_UNIT (inner_type);
960 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (p1), p1, t);
961 t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, t);
962 append_to_statement_list (t, &ret);
966 t = TYPE_SIZE_UNIT (inner_type);
967 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (p2), p2, t);
968 t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, t);
969 append_to_statement_list (t, &ret);
972 t = build2 (NE_EXPR, boolean_type_node, p1, end1);
973 t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&lab), NULL);
974 append_to_statement_list (t, &ret);
980 argarray[i++] = build_fold_addr_expr (arg1);
982 argarray[i++] = build_fold_addr_expr (arg2);
983 /* Handle default arguments. */
984 for (parm = defparm; parm && parm != void_list_node;
985 parm = TREE_CHAIN (parm), i++)
986 argarray[i] = convert_default_arg (TREE_VALUE (parm),
989 t = build_call_a (fn, i, argarray);
990 t = fold_convert (void_type_node, t);
991 return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
995 /* Return code to initialize DECL with its default constructor, or
996 NULL if there's nothing to do. */
999 cxx_omp_clause_default_ctor (tree clause, tree decl,
1000 tree outer ATTRIBUTE_UNUSED)
1002 tree info = CP_OMP_CLAUSE_INFO (clause);
1006 ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), decl, NULL);
1011 /* Return code to initialize DST with a copy constructor from SRC. */
1014 cxx_omp_clause_copy_ctor (tree clause, tree dst, tree src)
1016 tree info = CP_OMP_CLAUSE_INFO (clause);
1020 ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), dst, src);
1022 ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
1027 /* Similarly, except use an assignment operator instead. */
1030 cxx_omp_clause_assign_op (tree clause, tree dst, tree src)
1032 tree info = CP_OMP_CLAUSE_INFO (clause);
1036 ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 2), dst, src);
1038 ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
1043 /* Return code to destroy DECL. */
1046 cxx_omp_clause_dtor (tree clause, tree decl)
1048 tree info = CP_OMP_CLAUSE_INFO (clause);
1052 ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 1), decl, NULL);
1057 /* True if OpenMP should privatize what this DECL points to rather
1058 than the DECL itself. */
1061 cxx_omp_privatize_by_reference (const_tree decl)
1063 return is_invisiref_parm (decl);
1066 /* True if OpenMP sharing attribute of DECL is predetermined. */
1068 enum omp_clause_default_kind
1069 cxx_omp_predetermined_sharing (tree decl)
1073 /* Static data members are predetermined as shared. */
1074 if (TREE_STATIC (decl))
1076 tree ctx = CP_DECL_CONTEXT (decl);
1077 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
1078 return OMP_CLAUSE_DEFAULT_SHARED;
1081 type = TREE_TYPE (decl);
1082 if (TREE_CODE (type) == REFERENCE_TYPE)
1084 if (!is_invisiref_parm (decl))
1085 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1086 type = TREE_TYPE (type);
1088 if (TREE_CODE (decl) == RESULT_DECL && DECL_NAME (decl))
1090 /* NVR doesn't preserve const qualification of the
1092 tree outer = outer_curly_brace_block (current_function_decl);
1096 for (var = BLOCK_VARS (outer); var; var = TREE_CHAIN (var))
1097 if (DECL_NAME (decl) == DECL_NAME (var)
1098 && (TYPE_MAIN_VARIANT (type)
1099 == TYPE_MAIN_VARIANT (TREE_TYPE (var))))
1101 if (TYPE_READONLY (TREE_TYPE (var)))
1102 type = TREE_TYPE (var);
1108 if (type == error_mark_node)
1109 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1111 /* Variables with const-qualified type having no mutable member
1112 are predetermined shared. */
1113 if (TYPE_READONLY (type) && !cp_has_mutable_p (type))
1114 return OMP_CLAUSE_DEFAULT_SHARED;
1116 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1119 /* Finalize an implicitly determined clause. */
1122 cxx_omp_finish_clause (tree c)
1124 tree decl, inner_type;
1125 bool make_shared = false;
1127 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
1130 decl = OMP_CLAUSE_DECL (c);
1131 decl = require_complete_type (decl);
1132 inner_type = TREE_TYPE (decl);
1133 if (decl == error_mark_node)
1135 else if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1137 if (is_invisiref_parm (decl))
1138 inner_type = TREE_TYPE (inner_type);
1141 error ("%qE implicitly determined as %<firstprivate%> has reference type",
1147 /* We're interested in the base element, not arrays. */
1148 while (TREE_CODE (inner_type) == ARRAY_TYPE)
1149 inner_type = TREE_TYPE (inner_type);
1151 /* Check for special function availability by building a call to one.
1152 Save the results, because later we won't be in the right context
1153 for making these queries. */
1155 && CLASS_TYPE_P (inner_type)
1156 && cxx_omp_create_clause_info (c, inner_type, false, true, false))
1160 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_SHARED;