OSDN Git Service

PR c++/36254
[pf3gnuchains/gcc-fork.git] / gcc / cp / cp-gimplify.c
1 /* C++-specific tree lowering bits; see also c-gimplify.c and tree-gimple.c.
2
3    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5    Contributed by Jason Merrill <jason@redhat.com>
6
7 This file is part of GCC.
8
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
12 version.
13
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
17 for more details.
18
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/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "c-common.h"
30 #include "toplev.h"
31 #include "tree-iterator.h"
32 #include "gimple.h"
33 #include "hashtab.h"
34 #include "pointer-set.h"
35 #include "flags.h"
36
37 /* Local declarations.  */
38
39 enum bc_t { bc_break = 0, bc_continue = 1 };
40
41 /* Stack of labels which are targets for "break" or "continue",
42    linked through TREE_CHAIN.  */
43 static tree bc_label[2];
44
45 /* Begin a scope which can be exited by a break or continue statement.  BC
46    indicates which.
47
48    Just creates a label and pushes it into the current context.  */
49
50 static tree
51 begin_bc_block (enum bc_t bc)
52 {
53   tree label = create_artificial_label ();
54   TREE_CHAIN (label) = bc_label[bc];
55   bc_label[bc] = label;
56   return label;
57 }
58
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.
62
63    If we saw a break (or continue) in the scope, append a LABEL_EXPR to
64    body.  Otherwise, just forget the label.  */
65
66 static gimple_seq
67 finish_bc_block (enum bc_t bc, tree label, gimple_seq body)
68 {
69   gcc_assert (label == bc_label[bc]);
70
71   if (TREE_USED (label))
72     {
73       gimple_seq_add_stmt (&body, gimple_build_label (label));
74     }
75
76   bc_label[bc] = TREE_CHAIN (label);
77   TREE_CHAIN (label) = NULL_TREE;
78   return body;
79 }
80
81 /* Get the LABEL_EXPR to represent a break or continue statement
82    in the current block scope.  BC indicates which.  */
83
84 static tree
85 get_bc_label (enum bc_t bc)
86 {
87   tree label = bc_label[bc];
88
89   if (label == NULL_TREE)
90     {
91       if (bc == bc_break)
92         error ("break statement not within loop or switch");
93       else
94         error ("continue statement not within loop or switch");
95
96       return NULL_TREE;
97     }
98
99   /* Mark the label used for finish_bc_block.  */
100   TREE_USED (label) = 1;
101   return label;
102 }
103
104 /* Genericize a TRY_BLOCK.  */
105
106 static void
107 genericize_try_block (tree *stmt_p)
108 {
109   tree body = TRY_STMTS (*stmt_p);
110   tree cleanup = TRY_HANDLERS (*stmt_p);
111
112   *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
113 }
114
115 /* Genericize a HANDLER by converting to a CATCH_EXPR.  */
116
117 static void
118 genericize_catch_block (tree *stmt_p)
119 {
120   tree type = HANDLER_TYPE (*stmt_p);
121   tree body = HANDLER_BODY (*stmt_p);
122
123   /* FIXME should the caught type go in TREE_TYPE?  */
124   *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
125 }
126
127 /* A terser interface for building a representation of an exception
128    specification.  */
129
130 static tree
131 build_gimple_eh_filter_tree (tree body, tree allowed, tree failure)
132 {
133   tree t;
134
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));
138
139   t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
140   append_to_statement_list (body, &TREE_OPERAND (t, 0));
141
142   return t;
143 }
144
145 /* Genericize an EH_SPEC_BLOCK by converting it to a
146    TRY_CATCH_EXPR/EH_FILTER_EXPR pair.  */
147
148 static void
149 genericize_eh_spec_block (tree *stmt_p)
150 {
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 ());
154
155   *stmt_p = build_gimple_eh_filter_tree (body, allowed, failure);
156 }
157
158 /* Genericize an IF_STMT by turning it into a COND_EXPR.  */
159
160 static void
161 genericize_if_stmt (tree *stmt_p)
162 {
163   tree stmt, cond, then_, else_;
164   location_t locus = EXPR_LOCATION (*stmt_p);
165
166   stmt = *stmt_p;
167   cond = IF_COND (stmt);
168   then_ = THEN_CLAUSE (stmt);
169   else_ = ELSE_CLAUSE (stmt);
170
171   if (!then_)
172     then_ = build_empty_stmt ();
173   if (!else_)
174     else_ = build_empty_stmt ();
175
176   if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
177     stmt = then_;
178   else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
179     stmt = else_;
180   else
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);
184   *stmt_p = stmt;
185 }
186
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.  */
193
194 static gimple_seq
195 gimplify_cp_loop (tree cond, tree body, tree incr, bool cond_is_first)
196 {
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;
201
202   stmt_locus = input_location;
203   stmt_list = NULL;
204   body_seq = NULL;
205   incr_seq = NULL;
206   exit_seq = NULL;
207   entry = NULL;
208
209   break_block = begin_bc_block (bc_break);
210   cont_block = begin_bc_block (bc_continue);
211
212   /* If condition is zero don't generate a loop construct.  */
213   if (cond && integer_zerop (cond))
214     {
215       top = NULL;
216       if (cond_is_first)
217         {
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);
221         }
222     }
223   else
224     {
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 ());
230
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))
235         {
236           if (cond != error_mark_node)
237             { 
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);
244             }
245
246           if (cond_is_first)
247             {
248               if (incr)
249                 {
250                   entry = gimple_build_label (create_artificial_label ());
251                   stmt = gimple_build_goto (gimple_label_label (entry));
252                 }
253               else
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);
257             }
258         }
259       else
260         {
261           stmt = gimple_build_goto (gimple_label_label (top));
262           gimple_seq_add_stmt (&exit_seq, stmt);
263         }
264     }
265
266   gimplify_stmt (&body, &body_seq);
267   gimplify_stmt (&incr, &incr_seq);
268
269   body_seq = finish_bc_block (bc_continue, cont_block, body_seq);
270
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);
276
277   annotate_all_with_location (stmt_list, stmt_locus);
278
279   return finish_bc_block (bc_break, break_block, stmt_list);
280 }
281
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.  */
284
285 static void
286 gimplify_for_stmt (tree *stmt_p, gimple_seq *pre_p)
287 {
288   tree stmt = *stmt_p;
289
290   if (FOR_INIT_STMT (stmt))
291     gimplify_and_add (FOR_INIT_STMT (stmt), pre_p);
292
293   gimple_seq_add_seq (pre_p,
294                       gimplify_cp_loop (FOR_COND (stmt), FOR_BODY (stmt),
295                                         FOR_EXPR (stmt), 1));
296   *stmt_p = NULL_TREE;
297 }
298
299 /* Gimplify a WHILE_STMT node.  */
300
301 static void
302 gimplify_while_stmt (tree *stmt_p, gimple_seq *pre_p)
303 {
304   tree stmt = *stmt_p;
305   gimple_seq_add_seq (pre_p,
306                       gimplify_cp_loop (WHILE_COND (stmt), WHILE_BODY (stmt),
307                                         NULL_TREE, 1));
308   *stmt_p = NULL_TREE;
309 }
310
311 /* Gimplify a DO_STMT node.  */
312
313 static void
314 gimplify_do_stmt (tree *stmt_p, gimple_seq *pre_p)
315 {
316   tree stmt = *stmt_p;
317   gimple_seq_add_seq (pre_p,
318                       gimplify_cp_loop (DO_COND (stmt), DO_BODY (stmt),
319                                         NULL_TREE, 0));
320   *stmt_p = NULL_TREE;
321 }
322
323 /* Genericize a SWITCH_STMT by turning it into a SWITCH_EXPR.  */
324
325 static void
326 gimplify_switch_stmt (tree *stmt_p, gimple_seq *pre_p)
327 {
328   tree stmt = *stmt_p;
329   tree break_block, body, t;
330   location_t stmt_locus = input_location;
331   gimple_seq seq = NULL;
332
333   break_block = begin_bc_block (bc_break);
334
335   body = SWITCH_STMT_BODY (stmt);
336   if (!body)
337     body = build_empty_stmt ();
338
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);
343
344   seq = finish_bc_block (bc_break, break_block, seq);
345   gimple_seq_add_seq (pre_p, seq);
346   *stmt_p = NULL_TREE;
347 }
348
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.  */
353
354 static enum gimplify_status
355 cp_gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
356 {
357   tree for_stmt = *expr_p;
358   tree cont_block;
359   gimple stmt;
360   gimple_seq seq = NULL;
361
362   /* Protect ourselves from recursion.  */
363   if (OMP_FOR_GIMPLIFYING_P (for_stmt))
364     return GS_UNHANDLED;
365   OMP_FOR_GIMPLIFYING_P (for_stmt) = 1;
366
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);
371
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)));
377   else
378     seq = finish_bc_block (bc_continue, cont_block, seq);
379   gimple_seq_add_seq (pre_p, seq);
380
381   OMP_FOR_GIMPLIFYING_P (for_stmt) = 0;
382
383   return GS_ALL_DONE;
384 }
385
386 /*  Gimplify an EXPR_STMT node.  */
387
388 static void
389 gimplify_expr_stmt (tree *stmt_p)
390 {
391   tree stmt = EXPR_STMT_EXPR (*stmt_p);
392
393   if (stmt == error_mark_node)
394     stmt = NULL;
395
396   /* Gimplification of a statement expression will nullify the
397      statement if all its side effects are moved to *PRE_P and *POST_P.
398
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
401      gimplification.  */
402   if (stmt && warn_unused_value)
403     {
404       if (!TREE_SIDE_EFFECTS (stmt))
405         {
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");
410         }
411       else
412         warn_if_unused_value (stmt, input_location);
413     }
414
415   if (stmt == NULL_TREE)
416     stmt = alloc_stmt_list ();
417
418   *stmt_p = stmt;
419 }
420
421 /* Gimplify initialization from an AGGR_INIT_EXPR.  */
422
423 static void
424 cp_gimplify_init_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
425 {
426   tree from = TREE_OPERAND (*expr_p, 1);
427   tree to = TREE_OPERAND (*expr_p, 0);
428   tree t;
429   tree slot = NULL_TREE;
430
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)
436     {
437       slot = TARGET_EXPR_SLOT (from);
438       from = TARGET_EXPR_INITIAL (from);
439     }
440
441   /* Look through any COMPOUND_EXPRs, since build_compound_expr pushes them
442      inside the TARGET_EXPR.  */
443   for (t = from; t; )
444     {
445       tree sub = TREE_CODE (t) == COMPOUND_EXPR ? TREE_OPERAND (t, 0) : t;
446
447       /* If we are initializing from an AGGR_INIT_EXPR, drop the INIT_EXPR and
448          replace the slot operand with our target.
449
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)
453         {
454           gimplify_expr (&to, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
455           AGGR_INIT_EXPR_SLOT (sub) = to;
456           *expr_p = from;
457
458           /* The initialization is now a side-effect, so the container can
459              become void.  */
460           if (from != sub)
461             TREE_TYPE (from) = void_type_node;
462         }
463       else if (TREE_CODE (sub) == INIT_EXPR
464                && TREE_OPERAND (sub, 0) == slot)
465         {
466           /* An INIT_EXPR under TARGET_EXPR created by build_value_init,
467              will be followed by an AGGR_INIT_EXPR.  */
468           gimplify_expr (&to, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
469           TREE_OPERAND (sub, 0) = to;
470         }
471
472       if (t == sub)
473         break;
474       else
475         t = TREE_OPERAND (t, 1);
476     }
477
478 }
479
480 /* Gimplify a MUST_NOT_THROW_EXPR.  */
481
482 static enum gimplify_status
483 gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
484 {
485   tree stmt = *expr_p;
486   tree temp = voidify_wrapper_expr (stmt, NULL);
487   tree body = TREE_OPERAND (stmt, 0);
488
489   stmt = build_gimple_eh_filter_tree (body, NULL_TREE,
490                                       build_call_n (terminate_node, 0));
491
492   gimplify_and_add (stmt, pre_p);
493   if (temp)
494     {
495       *expr_p = temp;
496       return GS_OK;
497     }
498
499   *expr_p = NULL;
500   return GS_ALL_DONE;
501 }
502
503 /* Do C++-specific gimplification.  Args are as for gimplify_expr.  */
504
505 int
506 cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
507 {
508   int saved_stmts_are_full_exprs_p = 0;
509   enum tree_code code = TREE_CODE (*expr_p);
510   enum gimplify_status ret;
511   tree block = NULL;
512   VEC(gimple, heap) *bind_expr_stack = NULL;
513
514   if (STATEMENT_CODE_P (code))
515     {
516       saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
517       current_stmt_tree ()->stmts_are_full_exprs_p
518         = STMT_IS_FULL_EXPR_P (*expr_p);
519     }
520
521   switch (code)
522     {
523     case PTRMEM_CST:
524       *expr_p = cplus_expand_constant (*expr_p);
525       ret = GS_OK;
526       break;
527
528     case AGGR_INIT_EXPR:
529       simplify_aggr_init_expr (expr_p);
530       ret = GS_OK;
531       break;
532
533     case THROW_EXPR:
534       /* FIXME communicate throw type to back end, probably by moving
535          THROW_EXPR into ../tree.def.  */
536       *expr_p = TREE_OPERAND (*expr_p, 0);
537       ret = GS_OK;
538       break;
539
540     case MUST_NOT_THROW_EXPR:
541       ret = gimplify_must_not_throw_expr (expr_p, pre_p);
542       break;
543
544       /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the
545          LHS of an assignment might also be involved in the RHS, as in bug
546          25979.  */
547     case INIT_EXPR:
548       cp_gimplify_init_expr (expr_p, pre_p, post_p);
549       ret = GS_OK;
550       break;
551
552     case EMPTY_CLASS_EXPR:
553       /* We create an empty CONSTRUCTOR with RECORD_TYPE.  */
554       *expr_p = build_constructor (TREE_TYPE (*expr_p), NULL);
555       ret = GS_OK;
556       break;
557
558     case BASELINK:
559       *expr_p = BASELINK_FUNCTIONS (*expr_p);
560       ret = GS_OK;
561       break;
562
563     case TRY_BLOCK:
564       genericize_try_block (expr_p);
565       ret = GS_OK;
566       break;
567
568     case HANDLER:
569       genericize_catch_block (expr_p);
570       ret = GS_OK;
571       break;
572
573     case EH_SPEC_BLOCK:
574       genericize_eh_spec_block (expr_p);
575       ret = GS_OK;
576       break;
577
578     case USING_STMT:
579       /* Get the innermost inclosing GIMPLE_BIND that has a non NULL
580          BLOCK, and append an IMPORTED_DECL to its
581          BLOCK_VARS chained list.  */
582
583       bind_expr_stack = gimple_bind_expr_stack ();
584       if (bind_expr_stack)
585         {
586           int i;
587           for (i = VEC_length (gimple, bind_expr_stack) - 1; i >= 0; i--)
588             if ((block = gimple_bind_block (VEC_index (gimple,
589                                                        bind_expr_stack,
590                                                        i))))
591               break;
592         }
593       if (block)
594         {
595           tree using_directive;
596           gcc_assert (TREE_OPERAND (*expr_p,0)
597                       && NAMESPACE_DECL_CHECK (TREE_OPERAND (*expr_p, 0)));
598
599           using_directive = make_node (IMPORTED_DECL);
600           TREE_TYPE (using_directive) = void_type_node;
601
602           IMPORTED_DECL_ASSOCIATED_DECL (using_directive)
603             = TREE_OPERAND (*expr_p, 0);
604           DECL_NAME (using_directive)
605             = DECL_NAME (TREE_OPERAND (*expr_p, 0));
606           TREE_CHAIN (using_directive) = BLOCK_VARS (block);
607           BLOCK_VARS (block) = using_directive;
608         }
609       /* The USING_STMT won't appear in GIMPLE.  */
610       *expr_p = NULL;
611       ret = GS_ALL_DONE;
612       break;
613
614     case FOR_STMT:
615       gimplify_for_stmt (expr_p, pre_p);
616       ret = GS_OK;
617       break;
618
619     case WHILE_STMT:
620       gimplify_while_stmt (expr_p, pre_p);
621       ret = GS_OK;
622       break;
623
624     case DO_STMT:
625       gimplify_do_stmt (expr_p, pre_p);
626       ret = GS_OK;
627       break;
628
629     case SWITCH_STMT:
630       gimplify_switch_stmt (expr_p, pre_p);
631       ret = GS_OK;
632       break;
633
634     case OMP_FOR:
635       ret = cp_gimplify_omp_for (expr_p, pre_p);
636       break;
637
638     case CONTINUE_STMT:
639       gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_CONTINUE, NOT_TAKEN));
640       gimple_seq_add_stmt (pre_p, gimple_build_goto (get_bc_label (bc_continue)));
641       *expr_p = NULL_TREE;
642       ret = GS_ALL_DONE;
643       break;
644
645     case BREAK_STMT:
646       gimple_seq_add_stmt (pre_p, gimple_build_goto (get_bc_label (bc_break)));
647       *expr_p = NULL_TREE;
648       ret = GS_ALL_DONE;
649       break;
650
651     case EXPR_STMT:
652       gimplify_expr_stmt (expr_p);
653       ret = GS_OK;
654       break;
655
656     case UNARY_PLUS_EXPR:
657       {
658         tree arg = TREE_OPERAND (*expr_p, 0);
659         tree type = TREE_TYPE (*expr_p);
660         *expr_p = (TREE_TYPE (arg) != type) ? fold_convert (type, arg)
661                                             : arg;
662         ret = GS_OK;
663       }
664       break;
665
666     default:
667       ret = c_gimplify_expr (expr_p, pre_p, post_p);
668       break;
669     }
670
671   /* Restore saved state.  */
672   if (STATEMENT_CODE_P (code))
673     current_stmt_tree ()->stmts_are_full_exprs_p
674       = saved_stmts_are_full_exprs_p;
675
676   return ret;
677 }
678
679 static inline bool
680 is_invisiref_parm (const_tree t)
681 {
682   return ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
683           && DECL_BY_REFERENCE (t));
684 }
685
686 /* Return true if the uid in both int tree maps are equal.  */
687
688 int
689 cxx_int_tree_map_eq (const void *va, const void *vb)
690 {
691   const struct cxx_int_tree_map *a = (const struct cxx_int_tree_map *) va;
692   const struct cxx_int_tree_map *b = (const struct cxx_int_tree_map *) vb;
693   return (a->uid == b->uid);
694 }
695
696 /* Hash a UID in a cxx_int_tree_map.  */
697
698 unsigned int
699 cxx_int_tree_map_hash (const void *item)
700 {
701   return ((const struct cxx_int_tree_map *)item)->uid;
702 }
703
704 /* Perform any pre-gimplification lowering of C++ front end trees to
705    GENERIC.  */
706
707 static tree
708 cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
709 {
710   tree stmt = *stmt_p;
711   struct pointer_set_t *p_set = (struct pointer_set_t*) data;
712
713   if (is_invisiref_parm (stmt)
714       /* Don't dereference parms in a thunk, pass the references through. */
715       && !(DECL_THUNK_P (current_function_decl)
716            && TREE_CODE (stmt) == PARM_DECL))
717     {
718       *stmt_p = convert_from_reference (stmt);
719       *walk_subtrees = 0;
720       return NULL;
721     }
722
723   /* Map block scope extern declarations to visible declarations with the
724      same name and type in outer scopes if any.  */
725   if (cp_function_chain->extern_decl_map
726       && (TREE_CODE (stmt) == FUNCTION_DECL || TREE_CODE (stmt) == VAR_DECL)
727       && DECL_EXTERNAL (stmt))
728     {
729       struct cxx_int_tree_map *h, in;
730       in.uid = DECL_UID (stmt);
731       h = (struct cxx_int_tree_map *)
732           htab_find_with_hash (cp_function_chain->extern_decl_map,
733                                &in, in.uid);
734       if (h)
735         {
736           *stmt_p = h->to;
737           *walk_subtrees = 0;
738           return NULL;
739         }
740     }
741
742   /* Other than invisiref parms, don't walk the same tree twice.  */
743   if (pointer_set_contains (p_set, stmt))
744     {
745       *walk_subtrees = 0;
746       return NULL_TREE;
747     }
748
749   if (TREE_CODE (stmt) == ADDR_EXPR
750       && is_invisiref_parm (TREE_OPERAND (stmt, 0)))
751     {
752       *stmt_p = convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
753       *walk_subtrees = 0;
754     }
755   else if (TREE_CODE (stmt) == RETURN_EXPR
756            && TREE_OPERAND (stmt, 0)
757            && is_invisiref_parm (TREE_OPERAND (stmt, 0)))
758     /* Don't dereference an invisiref RESULT_DECL inside a RETURN_EXPR.  */
759     *walk_subtrees = 0;
760   else if (TREE_CODE (stmt) == OMP_CLAUSE)
761     switch (OMP_CLAUSE_CODE (stmt))
762       {
763       case OMP_CLAUSE_LASTPRIVATE:
764         /* Don't dereference an invisiref in OpenMP clauses.  */
765         if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
766           {
767             *walk_subtrees = 0;
768             if (OMP_CLAUSE_LASTPRIVATE_STMT (stmt))
769               cp_walk_tree (&OMP_CLAUSE_LASTPRIVATE_STMT (stmt),
770                             cp_genericize_r, p_set, NULL);
771           }
772         break;
773       case OMP_CLAUSE_PRIVATE:
774       case OMP_CLAUSE_SHARED:
775       case OMP_CLAUSE_FIRSTPRIVATE:
776       case OMP_CLAUSE_COPYIN:
777       case OMP_CLAUSE_COPYPRIVATE:
778         /* Don't dereference an invisiref in OpenMP clauses.  */
779         if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
780           *walk_subtrees = 0;
781         break;
782       case OMP_CLAUSE_REDUCTION:
783         gcc_assert (!is_invisiref_parm (OMP_CLAUSE_DECL (stmt)));
784         break;
785       default:
786         break;
787       }
788   else if (IS_TYPE_OR_DECL_P (stmt))
789     *walk_subtrees = 0;
790
791   /* Due to the way voidify_wrapper_expr is written, we don't get a chance
792      to lower this construct before scanning it, so we need to lower these
793      before doing anything else.  */
794   else if (TREE_CODE (stmt) == CLEANUP_STMT)
795     *stmt_p = build2 (CLEANUP_EH_ONLY (stmt) ? TRY_CATCH_EXPR
796                                              : TRY_FINALLY_EXPR,
797                       void_type_node,
798                       CLEANUP_BODY (stmt),
799                       CLEANUP_EXPR (stmt));
800
801   else if (TREE_CODE (stmt) == IF_STMT)
802     {
803       genericize_if_stmt (stmt_p);
804       /* *stmt_p has changed, tail recurse to handle it again.  */
805       return cp_genericize_r (stmt_p, walk_subtrees, data);
806     }
807
808   /* COND_EXPR might have incompatible types in branches if one or both
809      arms are bitfields.  Fix it up now.  */
810   else if (TREE_CODE (stmt) == COND_EXPR)
811     {
812       tree type_left
813         = (TREE_OPERAND (stmt, 1)
814            ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 1))
815            : NULL_TREE);
816       tree type_right
817         = (TREE_OPERAND (stmt, 2)
818            ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 2))
819            : NULL_TREE);
820       if (type_left
821           && !useless_type_conversion_p (TREE_TYPE (stmt),
822                                          TREE_TYPE (TREE_OPERAND (stmt, 1))))
823         {
824           TREE_OPERAND (stmt, 1)
825             = fold_convert (type_left, TREE_OPERAND (stmt, 1));
826           gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
827                                                  type_left));
828         }
829       if (type_right
830           && !useless_type_conversion_p (TREE_TYPE (stmt),
831                                          TREE_TYPE (TREE_OPERAND (stmt, 2))))
832         {
833           TREE_OPERAND (stmt, 2)
834             = fold_convert (type_right, TREE_OPERAND (stmt, 2));
835           gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
836                                                  type_right));
837         }
838     }
839
840   pointer_set_insert (p_set, *stmt_p);
841
842   return NULL;
843 }
844
845 void
846 cp_genericize (tree fndecl)
847 {
848   tree t;
849   struct pointer_set_t *p_set;
850
851   /* Fix up the types of parms passed by invisible reference.  */
852   for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
853     if (TREE_ADDRESSABLE (TREE_TYPE (t)))
854       {
855         /* If a function's arguments are copied to create a thunk,
856            then DECL_BY_REFERENCE will be set -- but the type of the
857            argument will be a pointer type, so we will never get
858            here.  */
859         gcc_assert (!DECL_BY_REFERENCE (t));
860         gcc_assert (DECL_ARG_TYPE (t) != TREE_TYPE (t));
861         TREE_TYPE (t) = DECL_ARG_TYPE (t);
862         DECL_BY_REFERENCE (t) = 1;
863         TREE_ADDRESSABLE (t) = 0;
864         relayout_decl (t);
865       }
866
867   /* Do the same for the return value.  */
868   if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (fndecl))))
869     {
870       t = DECL_RESULT (fndecl);
871       TREE_TYPE (t) = build_reference_type (TREE_TYPE (t));
872       DECL_BY_REFERENCE (t) = 1;
873       TREE_ADDRESSABLE (t) = 0;
874       relayout_decl (t);
875     }
876
877   /* If we're a clone, the body is already GIMPLE.  */
878   if (DECL_CLONED_FUNCTION_P (fndecl))
879     return;
880
881   /* We do want to see every occurrence of the parms, so we can't just use
882      walk_tree's hash functionality.  */
883   p_set = pointer_set_create ();
884   cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_genericize_r, p_set, NULL);
885   pointer_set_destroy (p_set);
886
887   /* Do everything else.  */
888   c_genericize (fndecl);
889
890   gcc_assert (bc_label[bc_break] == NULL);
891   gcc_assert (bc_label[bc_continue] == NULL);
892 }
893 \f
894 /* Build code to apply FN to each member of ARG1 and ARG2.  FN may be
895    NULL if there is in fact nothing to do.  ARG2 may be null if FN
896    actually only takes one argument.  */
897
898 static tree
899 cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
900 {
901   tree defparm, parm, t;
902   int i = 0;
903   int nargs;
904   tree *argarray;
905
906   if (fn == NULL)
907     return NULL;
908
909   nargs = list_length (DECL_ARGUMENTS (fn));
910   argarray = (tree *) alloca (nargs * sizeof (tree));
911
912   defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
913   if (arg2)
914     defparm = TREE_CHAIN (defparm);
915
916   if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
917     {
918       tree inner_type = TREE_TYPE (arg1);
919       tree start1, end1, p1;
920       tree start2 = NULL, p2 = NULL;
921       tree ret = NULL, lab;
922
923       start1 = arg1;
924       start2 = arg2;
925       do
926         {
927           inner_type = TREE_TYPE (inner_type);
928           start1 = build4 (ARRAY_REF, inner_type, start1,
929                            size_zero_node, NULL, NULL);
930           if (arg2)
931             start2 = build4 (ARRAY_REF, inner_type, start2,
932                              size_zero_node, NULL, NULL);
933         }
934       while (TREE_CODE (inner_type) == ARRAY_TYPE);
935       start1 = build_fold_addr_expr (start1);
936       if (arg2)
937         start2 = build_fold_addr_expr (start2);
938
939       end1 = TYPE_SIZE_UNIT (TREE_TYPE (arg1));
940       end1 = build2 (POINTER_PLUS_EXPR, TREE_TYPE (start1), start1, end1);
941
942       p1 = create_tmp_var (TREE_TYPE (start1), NULL);
943       t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, start1);
944       append_to_statement_list (t, &ret);
945
946       if (arg2)
947         {
948           p2 = create_tmp_var (TREE_TYPE (start2), NULL);
949           t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, start2);
950           append_to_statement_list (t, &ret);
951         }
952
953       lab = create_artificial_label ();
954       t = build1 (LABEL_EXPR, void_type_node, lab);
955       append_to_statement_list (t, &ret);
956
957       argarray[i++] = p1;
958       if (arg2)
959         argarray[i++] = p2;
960       /* Handle default arguments.  */
961       for (parm = defparm; parm && parm != void_list_node;
962            parm = TREE_CHAIN (parm), i++)
963         argarray[i] = convert_default_arg (TREE_VALUE (parm),
964                                            TREE_PURPOSE (parm), fn, i);
965       t = build_call_a (fn, i, argarray);
966       t = fold_convert (void_type_node, t);
967       t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
968       append_to_statement_list (t, &ret);
969
970       t = TYPE_SIZE_UNIT (inner_type);
971       t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (p1), p1, t);
972       t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, t);
973       append_to_statement_list (t, &ret);
974
975       if (arg2)
976         {
977           t = TYPE_SIZE_UNIT (inner_type);
978           t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (p2), p2, t);
979           t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, t);
980           append_to_statement_list (t, &ret);
981         }
982
983       t = build2 (NE_EXPR, boolean_type_node, p1, end1);
984       t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&lab), NULL);
985       append_to_statement_list (t, &ret);
986
987       return ret;
988     }
989   else
990     {
991       argarray[i++] = build_fold_addr_expr (arg1);
992       if (arg2)
993         argarray[i++] = build_fold_addr_expr (arg2);
994       /* Handle default arguments.  */
995       for (parm = defparm; parm && parm != void_list_node;
996            parm = TREE_CHAIN (parm), i++)
997         argarray[i] = convert_default_arg (TREE_VALUE (parm),
998                                            TREE_PURPOSE (parm),
999                                            fn, i);
1000       t = build_call_a (fn, i, argarray);
1001       t = fold_convert (void_type_node, t);
1002       return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
1003     }
1004 }
1005
1006 /* Return code to initialize DECL with its default constructor, or
1007    NULL if there's nothing to do.  */
1008
1009 tree
1010 cxx_omp_clause_default_ctor (tree clause, tree decl,
1011                              tree outer ATTRIBUTE_UNUSED)
1012 {
1013   tree info = CP_OMP_CLAUSE_INFO (clause);
1014   tree ret = NULL;
1015
1016   if (info)
1017     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), decl, NULL);
1018
1019   return ret;
1020 }
1021
1022 /* Return code to initialize DST with a copy constructor from SRC.  */
1023
1024 tree
1025 cxx_omp_clause_copy_ctor (tree clause, tree dst, tree src)
1026 {
1027   tree info = CP_OMP_CLAUSE_INFO (clause);
1028   tree ret = NULL;
1029
1030   if (info)
1031     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), dst, src);
1032   if (ret == NULL)
1033     ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
1034
1035   return ret;
1036 }
1037
1038 /* Similarly, except use an assignment operator instead.  */
1039
1040 tree
1041 cxx_omp_clause_assign_op (tree clause, tree dst, tree src)
1042 {
1043   tree info = CP_OMP_CLAUSE_INFO (clause);
1044   tree ret = NULL;
1045
1046   if (info)
1047     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 2), dst, src);
1048   if (ret == NULL)
1049     ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
1050
1051   return ret;
1052 }
1053
1054 /* Return code to destroy DECL.  */
1055
1056 tree
1057 cxx_omp_clause_dtor (tree clause, tree decl)
1058 {
1059   tree info = CP_OMP_CLAUSE_INFO (clause);
1060   tree ret = NULL;
1061
1062   if (info)
1063     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 1), decl, NULL);
1064
1065   return ret;
1066 }
1067
1068 /* True if OpenMP should privatize what this DECL points to rather
1069    than the DECL itself.  */
1070
1071 bool
1072 cxx_omp_privatize_by_reference (const_tree decl)
1073 {
1074   return is_invisiref_parm (decl);
1075 }
1076
1077 /* True if OpenMP sharing attribute of DECL is predetermined.  */
1078
1079 enum omp_clause_default_kind
1080 cxx_omp_predetermined_sharing (tree decl)
1081 {
1082   tree type;
1083
1084   /* Static data members are predetermined as shared.  */
1085   if (TREE_STATIC (decl))
1086     {
1087       tree ctx = CP_DECL_CONTEXT (decl);
1088       if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
1089         return OMP_CLAUSE_DEFAULT_SHARED;
1090     }
1091
1092   type = TREE_TYPE (decl);
1093   if (TREE_CODE (type) == REFERENCE_TYPE)
1094     {
1095       if (!is_invisiref_parm (decl))
1096         return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1097       type = TREE_TYPE (type);
1098
1099       if (TREE_CODE (decl) == RESULT_DECL && DECL_NAME (decl))
1100         {
1101           /* NVR doesn't preserve const qualification of the
1102              variable's type.  */
1103           tree outer = outer_curly_brace_block (current_function_decl);
1104           tree var;
1105
1106           if (outer)
1107             for (var = BLOCK_VARS (outer); var; var = TREE_CHAIN (var))
1108               if (DECL_NAME (decl) == DECL_NAME (var)
1109                   && (TYPE_MAIN_VARIANT (type)
1110                       == TYPE_MAIN_VARIANT (TREE_TYPE (var))))
1111                 {
1112                   if (TYPE_READONLY (TREE_TYPE (var)))
1113                     type = TREE_TYPE (var);
1114                   break;
1115                 }
1116         }
1117     }
1118
1119   if (type == error_mark_node)
1120     return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1121
1122   /* Variables with const-qualified type having no mutable member
1123      are predetermined shared.  */
1124   if (TYPE_READONLY (type) && !cp_has_mutable_p (type))
1125     return OMP_CLAUSE_DEFAULT_SHARED;
1126
1127   return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1128 }
1129
1130 /* Finalize an implicitly determined clause.  */
1131
1132 void
1133 cxx_omp_finish_clause (tree c)
1134 {
1135   tree decl, inner_type;
1136   bool make_shared = false;
1137
1138   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
1139     return;
1140
1141   decl = OMP_CLAUSE_DECL (c);
1142   decl = require_complete_type (decl);
1143   inner_type = TREE_TYPE (decl);
1144   if (decl == error_mark_node)
1145     make_shared = true;
1146   else if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1147     {
1148       if (is_invisiref_parm (decl))
1149         inner_type = TREE_TYPE (inner_type);
1150       else
1151         {
1152           error ("%qE implicitly determined as %<firstprivate%> has reference type",
1153                  decl);
1154           make_shared = true;
1155         }
1156     }
1157
1158   /* We're interested in the base element, not arrays.  */
1159   while (TREE_CODE (inner_type) == ARRAY_TYPE)
1160     inner_type = TREE_TYPE (inner_type);
1161
1162   /* Check for special function availability by building a call to one.
1163      Save the results, because later we won't be in the right context
1164      for making these queries.  */
1165   if (!make_shared
1166       && CLASS_TYPE_P (inner_type)
1167       && cxx_omp_create_clause_info (c, inner_type, false, true, false))
1168     make_shared = true;
1169
1170   if (make_shared)
1171     OMP_CLAUSE_CODE (c) = OMP_CLAUSE_SHARED;
1172 }