OSDN Git Service

* cp-tree.h (commonparms): Remove prototype.
[pf3gnuchains/gcc-fork.git] / gcc / cp / except.c
1 /* Handle exceptional things in C++.
2    Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004  Free Software Foundation, Inc.
4    Contributed by Michael Tiemann <tiemann@cygnus.com>
5    Rewritten by Mike Stump <mrs@cygnus.com>, based upon an
6    initial re-implementation courtesy Tad Hunt.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.  */
24
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "rtl.h"
32 #include "expr.h"
33 #include "libfuncs.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "except.h"
38 #include "toplev.h"
39 #include "tree-inline.h"
40 #include "tree-iterator.h"
41
42 static void push_eh_cleanup (tree);
43 static tree prepare_eh_type (tree);
44 static tree build_eh_type_type (tree);
45 static tree do_begin_catch (void);
46 static int dtor_nothrow (tree);
47 static tree do_end_catch (tree);
48 static bool decl_is_java_type (tree decl, int err);
49 static void initialize_handler_parm (tree, tree);
50 static tree do_allocate_exception (tree);
51 static tree wrap_cleanups_r (tree *, int *, void *);
52 static int complete_ptr_ref_or_void_ptr_p (tree, tree);
53 static bool is_admissible_throw_operand (tree);
54 static int can_convert_eh (tree, tree);
55 static tree cp_protect_cleanup_actions (void);
56
57 /* Sets up all the global eh stuff that needs to be initialized at the
58    start of compilation.  */
59
60 void
61 init_exception_processing (void)
62 {
63   tree tmp;
64
65   /* void std::terminate (); */
66   push_namespace (std_identifier);
67   tmp = build_function_type (void_type_node, void_list_node);
68   terminate_node = build_cp_library_fn_ptr ("terminate", tmp);
69   TREE_THIS_VOLATILE (terminate_node) = 1;
70   TREE_NOTHROW (terminate_node) = 1;
71   pop_namespace ();
72
73   /* void __cxa_call_unexpected(void *); */
74   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
75   tmp = build_function_type (void_type_node, tmp);
76   call_unexpected_node
77     = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
78
79   eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
80                                              ? "__gxx_personality_sj0"
81                                              : "__gxx_personality_v0");
82
83   lang_eh_runtime_type = build_eh_type_type;
84   lang_protect_cleanup_actions = &cp_protect_cleanup_actions;
85 }
86
87 /* Returns an expression to be executed if an unhandled exception is
88    propagated out of a cleanup region.  */
89
90 static tree
91 cp_protect_cleanup_actions (void)
92 {
93   /* [except.terminate]
94
95      When the destruction of an object during stack unwinding exits
96      using an exception ... void terminate(); is called.  */
97   return build_call (terminate_node, NULL_TREE);
98 }     
99
100 static tree
101 prepare_eh_type (tree type)
102 {
103   if (type == NULL_TREE)
104     return type;
105   if (type == error_mark_node)
106     return error_mark_node;
107
108   /* peel back references, so they match.  */
109   type = non_reference (type);
110
111   /* Peel off cv qualifiers.  */
112   type = TYPE_MAIN_VARIANT (type);
113
114   return type;
115 }
116
117 /* Return the type info for TYPE as used by EH machinery.  */
118 tree
119 eh_type_info (tree type)
120 {
121   tree exp;
122
123   if (type == NULL_TREE || type == error_mark_node)
124     return type;
125
126   if (decl_is_java_type (type, 0))
127     exp = build_java_class_ref (TREE_TYPE (type));
128   else
129     exp = get_tinfo_decl (type);
130
131   return exp;
132 }
133
134 /* Build the address of a typeinfo decl for use in the runtime
135    matching field of the exception model.  */
136
137 static tree
138 build_eh_type_type (tree type)
139 {
140   tree exp = eh_type_info (type);
141
142   if (!exp)
143     return NULL;
144
145   mark_used (exp);
146
147   return convert (ptr_type_node, build_address (exp));
148 }
149
150 tree
151 build_exc_ptr (void)
152 {
153   return build0 (EXC_PTR_EXPR, ptr_type_node);
154 }
155
156 /* Build up a call to __cxa_begin_catch, to tell the runtime that the
157    exception has been handled.  */
158
159 static tree
160 do_begin_catch (void)
161 {
162   tree fn;
163
164   fn = get_identifier ("__cxa_begin_catch");
165   if (!get_global_value_if_present (fn, &fn))
166     {
167       /* Declare void* __cxa_begin_catch (void *).  */
168       tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
169       fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp));
170     }
171
172   return build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
173                                              NULL_TREE));
174 }
175
176 /* Returns nonzero if cleaning up an exception of type TYPE (which can be
177    NULL_TREE for a ... handler) will not throw an exception.  */
178
179 static int
180 dtor_nothrow (tree type)
181 {
182   if (type == NULL_TREE)
183     return 0;
184
185   if (! TYPE_HAS_DESTRUCTOR (type))
186     return 1;
187
188   return TREE_NOTHROW (CLASSTYPE_DESTRUCTORS (type));
189 }
190
191 /* Build up a call to __cxa_end_catch, to destroy the exception object
192    for the current catch block if no others are currently using it.  */
193
194 static tree
195 do_end_catch (tree type)
196 {
197   tree fn, cleanup;
198
199   fn = get_identifier ("__cxa_end_catch");
200   if (!get_global_value_if_present (fn, &fn))
201     {
202       /* Declare void __cxa_end_catch ().  */
203       fn = push_void_library_fn (fn, void_list_node);
204       /* This can throw if the destructor for the exception throws.  */
205       TREE_NOTHROW (fn) = 0;
206     }
207
208   cleanup = build_function_call (fn, NULL_TREE);
209   TREE_NOTHROW (cleanup) = dtor_nothrow (type);
210
211   return cleanup;
212 }
213
214 /* This routine creates the cleanup for the current exception.  */
215
216 static void
217 push_eh_cleanup (tree type)
218 {
219   finish_decl_cleanup (NULL_TREE, do_end_catch (type));
220 }
221
222 /* Return nonzero value if DECL is a Java type suitable for catch or
223    throw.  */
224
225 static bool
226 decl_is_java_type (tree decl, int err)
227 {
228   bool r = (TREE_CODE (decl) == POINTER_TYPE
229             && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
230             && TYPE_FOR_JAVA (TREE_TYPE (decl)));
231
232   if (err)
233     {
234       if (TREE_CODE (decl) == REFERENCE_TYPE
235           && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
236           && TYPE_FOR_JAVA (TREE_TYPE (decl)))
237         {
238           /* Can't throw a reference.  */
239           error ("type %qT is disallowed in Java %<throw%> or %<catch%>",
240                  decl);
241         }
242
243       if (r)
244         {
245           tree jthrow_node
246             = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jthrowable"));
247
248           if (jthrow_node == NULL_TREE)
249             fatal_error
250               ("call to Java %<catch%> or %<throw%> with %<jthrowable%> undefined");
251
252           jthrow_node = TREE_TYPE (TREE_TYPE (jthrow_node));
253
254           if (! DERIVED_FROM_P (jthrow_node, TREE_TYPE (decl)))
255             {
256               /* Thrown object must be a Throwable.  */
257               error ("type %qT is not derived from %<java::lang::Throwable%>",
258                      TREE_TYPE (decl));
259             }
260         }
261     }
262
263   return r;
264 }
265
266 /* Select the personality routine to be used for exception handling,
267    or issue an error if we need two different ones in the same
268    translation unit.
269    ??? At present eh_personality_libfunc is set to
270    __gxx_personality_(sj|v)0 in init_exception_processing - should it
271    be done here instead?  */
272 void
273 choose_personality_routine (enum languages lang)
274 {
275   static enum {
276     chose_none,
277     chose_cpp,
278     chose_java,
279     gave_error
280   } state;
281
282   switch (state)
283     {
284     case gave_error:
285       return;
286
287     case chose_cpp:
288       if (lang != lang_cplusplus)
289         goto give_error;
290       return;
291
292     case chose_java:
293       if (lang != lang_java)
294         goto give_error;
295       return;
296
297     case chose_none:
298       ; /* Proceed to language selection.  */
299     }
300
301   switch (lang)
302     {
303     case lang_cplusplus:
304       state = chose_cpp;
305       break;
306
307     case lang_java:
308       state = chose_java;
309       eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
310                                                  ? "__gcj_personality_sj0"
311                                                  : "__gcj_personality_v0");
312       break;
313
314     default:
315       gcc_unreachable ();
316     }
317   return;
318
319  give_error:
320   error ("mixing C++ and Java catches in a single translation unit");
321   state = gave_error;
322 }
323
324 /* Initialize the catch parameter DECL.  */
325
326 static void 
327 initialize_handler_parm (tree decl, tree exp)
328 {
329   tree init;
330   tree init_type;
331
332   /* Make sure we mark the catch param as used, otherwise we'll get a
333      warning about an unused ((anonymous)).  */
334   TREE_USED (decl) = 1;
335
336   /* Figure out the type that the initializer is.  Pointers are returned
337      adjusted by value from __cxa_begin_catch.  Others are returned by 
338      reference.  */
339   init_type = TREE_TYPE (decl);
340   if (!POINTER_TYPE_P (init_type))
341     init_type = build_reference_type (init_type);
342
343   choose_personality_routine (decl_is_java_type (init_type, 0)
344                               ? lang_java : lang_cplusplus);
345
346   /* Since pointers are passed by value, initialize a reference to
347      pointer catch parm with the address of the temporary.  */
348   if (TREE_CODE (init_type) == REFERENCE_TYPE
349       && TYPE_PTR_P (TREE_TYPE (init_type)))
350     exp = build_unary_op (ADDR_EXPR, exp, 1);
351
352   exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
353
354   init = convert_from_reference (exp);
355
356   /* If the constructor for the catch parm exits via an exception, we
357      must call terminate.  See eh23.C.  */
358   if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
359     {
360       /* Generate the copy constructor call directly so we can wrap it.
361          See also expand_default_init.  */
362       init = ocp_convert (TREE_TYPE (decl), init,
363                           CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
364       init = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (init), init);
365     }
366
367   /* Let `cp_finish_decl' know that this initializer is ok.  */
368   DECL_INITIAL (decl) = error_mark_node;
369   decl = pushdecl (decl);
370
371   start_decl_1 (decl);
372   cp_finish_decl (decl, init, NULL_TREE,
373                   LOOKUP_ONLYCONVERTING|DIRECT_BIND);
374 }
375
376 /* Call this to start a catch block.  DECL is the catch parameter.  */
377
378 tree
379 expand_start_catch_block (tree decl)
380 {
381   tree exp = NULL_TREE;
382   tree type;
383   bool is_java;
384
385   if (! doing_eh (1))
386     return NULL_TREE;
387
388   /* Make sure this declaration is reasonable.  */
389   if (decl && !complete_ptr_ref_or_void_ptr_p (TREE_TYPE (decl), NULL_TREE))
390     decl = NULL_TREE;
391
392   if (decl)
393     type = prepare_eh_type (TREE_TYPE (decl));
394   else
395     type = NULL_TREE;
396
397   is_java = false;
398   if (decl)
399     {
400       tree init;
401
402       if (decl_is_java_type (type, 1))
403         {
404           /* Java only passes object via pointer and doesn't require
405              adjusting.  The java object is immediately before the
406              generic exception header.  */
407           init = build_exc_ptr ();
408           init = build1 (NOP_EXPR, build_pointer_type (type), init);
409           init = build2 (MINUS_EXPR, TREE_TYPE (init), init,
410                          TYPE_SIZE_UNIT (TREE_TYPE (init)));
411           init = build_indirect_ref (init, NULL);
412           is_java = true;
413         }
414       else
415         {
416           /* C++ requires that we call __cxa_begin_catch to get the
417              pointer to the actual object.  */
418           init = do_begin_catch ();
419         }
420           
421       exp = create_temporary_var (ptr_type_node);
422       DECL_REGISTER (exp) = 1;
423       cp_finish_decl (exp, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
424       finish_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
425     }
426   else
427     finish_expr_stmt (do_begin_catch ());
428
429   /* C++ requires that we call __cxa_end_catch at the end of
430      processing the exception.  */
431   if (! is_java)
432     push_eh_cleanup (type);
433
434   if (decl)
435     initialize_handler_parm (decl, exp);
436
437   return type;
438 }
439
440
441 /* Call this to end a catch block.  Its responsible for emitting the
442    code to handle jumping back to the correct place, and for emitting
443    the label to jump to if this catch block didn't match.  */
444
445 void
446 expand_end_catch_block (void)
447 {
448   if (! doing_eh (1))
449     return;
450
451   /* The exception being handled is rethrown if control reaches the end of
452      a handler of the function-try-block of a constructor or destructor.  */
453   if (in_function_try_handler
454       && (DECL_CONSTRUCTOR_P (current_function_decl)
455           || DECL_DESTRUCTOR_P (current_function_decl)))
456     finish_expr_stmt (build_throw (NULL_TREE));
457 }
458
459 tree
460 begin_eh_spec_block (void)
461 {
462   tree r = build_stmt (EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
463   add_stmt (r);
464   EH_SPEC_STMTS (r) = push_stmt_list ();
465   return r;
466 }
467
468 void
469 finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
470 {
471   tree raises;
472
473   EH_SPEC_STMTS (eh_spec_block) = pop_stmt_list (EH_SPEC_STMTS (eh_spec_block));
474
475   /* Strip cv quals, etc, from the specification types.  */
476   for (raises = NULL_TREE;
477        raw_raises && TREE_VALUE (raw_raises);
478        raw_raises = TREE_CHAIN (raw_raises))
479     {
480       tree type = prepare_eh_type (TREE_VALUE (raw_raises));
481       tree tinfo = eh_type_info (type);
482
483       mark_used (tinfo);
484       raises = tree_cons (NULL_TREE, type, raises);
485     }
486
487   EH_SPEC_RAISES (eh_spec_block) = raises;
488 }
489
490 /* Return a pointer to a buffer for an exception object of type TYPE.  */
491
492 static tree
493 do_allocate_exception (tree type)
494 {
495   tree fn;
496
497   fn = get_identifier ("__cxa_allocate_exception");
498   if (!get_global_value_if_present (fn, &fn))
499     {
500       /* Declare void *__cxa_allocate_exception(size_t).  */
501       tree tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
502       fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp));
503     }
504   
505   return build_function_call (fn, tree_cons (NULL_TREE, size_in_bytes (type),
506                                              NULL_TREE));
507 }
508
509 /* Call __cxa_free_exception from a cleanup.  This is never invoked
510    directly, but see the comment for stabilize_throw_expr.  */
511
512 static tree
513 do_free_exception (tree ptr)
514 {
515   tree fn;
516
517   fn = get_identifier ("__cxa_free_exception");
518   if (!get_global_value_if_present (fn, &fn))
519     {
520       /* Declare void __cxa_free_exception (void *).  */
521       fn = push_void_library_fn (fn, tree_cons (NULL_TREE, ptr_type_node,
522                                                 void_list_node));
523     }
524
525   return build_function_call (fn, tree_cons (NULL_TREE, ptr, NULL_TREE));
526 }
527
528 /* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
529    Called from build_throw via walk_tree_without_duplicates.  */
530
531 static tree
532 wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
533                  void *data ATTRIBUTE_UNUSED)
534 {
535   tree exp = *tp;
536   tree cleanup;
537
538   /* Don't walk into types.  */
539   if (TYPE_P (exp))
540     {
541       *walk_subtrees = 0;
542       return NULL_TREE;
543     }
544   if (TREE_CODE (exp) != TARGET_EXPR)
545     return NULL_TREE;
546
547   cleanup = TARGET_EXPR_CLEANUP (exp);
548   if (cleanup)
549     {
550       cleanup = build1 (MUST_NOT_THROW_EXPR, void_type_node, cleanup);
551       TARGET_EXPR_CLEANUP (exp) = cleanup;
552     }
553
554   /* Keep iterating.  */
555   return NULL_TREE;
556 }
557
558 /* Build a throw expression.  */
559
560 tree
561 build_throw (tree exp)
562 {
563   tree fn;
564
565   if (exp == error_mark_node)
566     return exp;
567
568   if (processing_template_decl)
569     {
570       current_function_returns_abnormally = 1;
571       return build_min (THROW_EXPR, void_type_node, exp);
572     }
573
574   if (exp == null_node)
575     warning ("throwing NULL, which has integral, not pointer type");
576   
577   if (exp != NULL_TREE)
578     {
579       if (!is_admissible_throw_operand (exp))
580         return error_mark_node;
581     }
582
583   if (! doing_eh (1))
584     return error_mark_node;
585
586   if (exp && decl_is_java_type (TREE_TYPE (exp), 1))
587     {
588       tree fn = get_identifier ("_Jv_Throw");
589       if (!get_global_value_if_present (fn, &fn))
590         {
591           /* Declare void _Jv_Throw (void *).  */
592           tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
593           tmp = build_function_type (ptr_type_node, tmp);
594           fn = push_throw_library_fn (fn, tmp);
595         }
596       else if (really_overloaded_fn (fn))
597         {
598           error ("%qD should never be overloaded", fn);
599           return error_mark_node;
600         }
601       fn = OVL_CURRENT (fn);
602       exp = build_function_call (fn, tree_cons (NULL_TREE, exp, NULL_TREE));
603     }
604   else if (exp)
605     {
606       tree throw_type;
607       tree cleanup;
608       tree object, ptr;
609       tree tmp;
610       tree temp_expr, allocate_expr;
611       bool elided;
612
613       /* The CLEANUP_TYPE is the internal type of a destructor.  */
614       if (!cleanup_type)
615         {
616           tmp = void_list_node;
617           tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
618           tmp = build_function_type (void_type_node, tmp);
619           cleanup_type = build_pointer_type (tmp);
620         }
621       
622       fn = get_identifier ("__cxa_throw");
623       if (!get_global_value_if_present (fn, &fn))
624         {
625           /* Declare void __cxa_throw (void*, void*, void (*)(void*)).  */
626           /* ??? Second argument is supposed to be "std::type_info*".  */
627           tmp = void_list_node;
628           tmp = tree_cons (NULL_TREE, cleanup_type, tmp);
629           tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
630           tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
631           tmp = build_function_type (void_type_node, tmp);
632           fn = push_throw_library_fn (fn, tmp);
633         }
634       
635       /* throw expression */
636       /* First, decay it.  */
637       exp = decay_conversion (exp);
638
639       /* OK, this is kind of wacky.  The standard says that we call
640          terminate when the exception handling mechanism, after
641          completing evaluation of the expression to be thrown but
642          before the exception is caught (_except.throw_), calls a
643          user function that exits via an uncaught exception.
644
645          So we have to protect the actual initialization of the
646          exception object with terminate(), but evaluate the
647          expression first.  Since there could be temps in the
648          expression, we need to handle that, too.  We also expand
649          the call to __cxa_allocate_exception first (which doesn't
650          matter, since it can't throw).  */
651
652       /* Allocate the space for the exception.  */
653       allocate_expr = do_allocate_exception (TREE_TYPE (exp));
654       allocate_expr = get_target_expr (allocate_expr);
655       ptr = TARGET_EXPR_SLOT (allocate_expr);
656       object = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (exp)), ptr);
657       object = build_indirect_ref (object, NULL);
658
659       elided = (TREE_CODE (exp) == TARGET_EXPR);
660
661       /* And initialize the exception object.  */
662       exp = build_init (object, exp, LOOKUP_ONLYCONVERTING);
663       if (exp == error_mark_node)
664         {
665           error ("  in thrown expression");
666           return error_mark_node;
667         }
668
669       /* Pre-evaluate the thrown expression first, since if we allocated
670          the space first we would have to deal with cleaning it up if
671          evaluating this expression throws.
672
673          The case where EXP the initializer is a call to a constructor or a
674          function returning a class is a bit of a grey area in the
675          standard; it's unclear whether or not it should be allowed to
676          throw.  We used to say no, as that allowed us to optimize this
677          case without worrying about deallocating the exception object if
678          it does.  But that conflicted with expectations (PR 13944) and the
679          EDG compiler; now we wrap the initialization in a TRY_CATCH_EXPR
680          to call do_free_exception rather than in a MUST_NOT_THROW_EXPR,
681          for this case only.
682
683          Note that we don't check the return value from stabilize_init
684          because it will only return false in cases where elided is true,
685          and therefore we don't need to work around the failure to
686          preevaluate.  */
687       temp_expr = NULL_TREE;
688       stabilize_init (exp, &temp_expr);
689
690       if (elided)
691         exp = build2 (TRY_CATCH_EXPR, void_type_node, exp,
692                       do_free_exception (ptr));
693       else
694         exp = build1 (MUST_NOT_THROW_EXPR, void_type_node, exp);
695
696       /* Prepend the allocation.  */
697       exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
698       if (temp_expr)
699         {
700           /* Prepend the calculation of the throw expression.  Also, force
701              any cleanups from the expression to be evaluated here so that
702              we don't have to do them during unwinding.  But first wrap
703              them in MUST_NOT_THROW_EXPR, since they are run after the
704              exception object is initialized.  */
705           walk_tree_without_duplicates (&temp_expr, wrap_cleanups_r, 0);
706           exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), temp_expr, exp);
707           exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
708         }
709
710       throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
711
712       if (TYPE_HAS_DESTRUCTOR (TREE_TYPE (object)))
713         {
714           cleanup = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
715                                      complete_dtor_identifier, 0);
716           cleanup = BASELINK_FUNCTIONS (cleanup);
717           mark_used (cleanup);
718           cxx_mark_addressable (cleanup);
719           /* Pretend it's a normal function.  */
720           cleanup = build1 (ADDR_EXPR, cleanup_type, cleanup);
721         }
722       else
723         cleanup = build_int_cst (cleanup_type, 0);
724         
725       tmp = tree_cons (NULL_TREE, cleanup, NULL_TREE);
726       tmp = tree_cons (NULL_TREE, throw_type, tmp);
727       tmp = tree_cons (NULL_TREE, ptr, tmp);
728       /* ??? Indicate that this function call throws throw_type.  */
729       tmp = build_function_call (fn, tmp);
730
731       /* Tack on the initialization stuff.  */
732       exp = build2 (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp);
733     }
734   else
735     {
736       /* Rethrow current exception.  */
737
738       tree fn = get_identifier ("__cxa_rethrow");
739       if (!get_global_value_if_present (fn, &fn))
740         {
741           /* Declare void __cxa_rethrow (void).  */
742           fn = push_throw_library_fn
743             (fn, build_function_type (void_type_node, void_list_node));
744         }
745
746       /* ??? Indicate that this function call allows exceptions of the type
747          of the enclosing catch block (if known).  */    
748       exp = build_function_call (fn, NULL_TREE);
749     }
750
751   exp = build1 (THROW_EXPR, void_type_node, exp);
752
753   return exp;
754 }
755
756 /* Make sure TYPE is complete, pointer to complete, reference to
757    complete, or pointer to cv void. Issue diagnostic on failure.
758    Return the zero on failure and nonzero on success. FROM can be
759    the expr or decl from whence TYPE came, if available.  */
760
761 static int
762 complete_ptr_ref_or_void_ptr_p (tree type, tree from)
763 {
764   int is_ptr;
765   
766   /* Check complete.  */
767   type = complete_type_or_else (type, from);
768   if (!type)
769     return 0;
770   
771   /* Or a pointer or ref to one, or cv void *.  */
772   is_ptr = TREE_CODE (type) == POINTER_TYPE;
773   if (is_ptr || TREE_CODE (type) == REFERENCE_TYPE)
774     {
775       tree core = TREE_TYPE (type);
776   
777       if (is_ptr && VOID_TYPE_P (core))
778         /* OK */;
779       else if (!complete_type_or_else (core, from))
780         return 0;
781     }
782   return 1;
783 }
784
785 /* Return truth-value if EXPRESSION is admissible in throw-expression,
786    i.e. if it is not of incomplete type or a pointer/reference to such
787    a type or of an abstract class type.  */
788
789 static bool
790 is_admissible_throw_operand (tree expr)
791 {
792   tree type = TREE_TYPE (expr);
793
794   /* 15.1/4 [...] The type of the throw-expression shall not be an
795             incomplete type, or a pointer or a reference to an incomplete
796             type, other than void*, const void*, volatile void*, or
797             const volatile void*.  Except for these restriction and the
798             restrictions on type matching mentioned in 15.3, the operand
799             of throw is treated exactly as a function argument in a call
800             (5.2.2) or the operand of a return statement.  */
801   if (!complete_ptr_ref_or_void_ptr_p (type, expr))
802     return false;
803
804   /* 10.4/3 An abstract class shall not be used as a parameter type,
805             as a function return type or as type of an explicit
806             conversion.  */
807   else if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
808     {
809       error ("expression %qE of abstract class type %qT cannot "
810              "be used in throw-expression", expr, type);
811       return false;
812     }
813
814   return true;
815 }
816
817 /* Returns nonzero if FN is a declaration of a standard C library
818    function which is known not to throw.
819
820    [lib.res.on.exception.handling]: None of the functions from the
821    Standard C library shall report an error by throwing an
822    exception, unless it calls a program-supplied function that
823    throws an exception.  */
824
825 #include "cfns.h"
826
827 int
828 nothrow_libfn_p (tree fn)
829 {
830   tree id;
831
832   if (TREE_PUBLIC (fn)
833       && DECL_EXTERNAL (fn)
834       && DECL_NAMESPACE_SCOPE_P (fn)
835       && DECL_EXTERN_C_P (fn))
836     /* OK */;
837   else
838     /* Can't be a C library function.  */
839     return 0;
840
841   /* Being a C library function, DECL_ASSEMBLER_NAME == DECL_NAME
842      unless the system headers are playing rename tricks, and if
843      they are, we don't want to be confused by them.  */
844   id = DECL_NAME (fn);
845   return !!libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
846 }
847
848 /* Returns nonzero if an exception of type FROM will be caught by a
849    handler for type TO, as per [except.handle].  */
850
851 static int
852 can_convert_eh (tree to, tree from)
853 {
854   to = non_reference (to);
855   from = non_reference (from);
856
857   if (TREE_CODE (to) == POINTER_TYPE && TREE_CODE (from) == POINTER_TYPE)
858     {
859       to = TREE_TYPE (to);
860       from = TREE_TYPE (from);
861
862       if (! at_least_as_qualified_p (to, from))
863         return 0;
864
865       if (TREE_CODE (to) == VOID_TYPE)
866         return 1;
867
868       /* Else fall through.  */
869     }
870
871   if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
872       && PUBLICLY_UNIQUELY_DERIVED_P (to, from))
873     return 1;
874
875   return 0;
876 }
877
878 /* Check whether any of the handlers in I are shadowed by another handler
879    accepting TYPE.  Note that the shadowing may not be complete; even if
880    an exception of type B would be caught by a handler for A, there could
881    be a derived class C for which A is an ambiguous base but B is not, so
882    the handler for B would catch an exception of type C.  */
883
884 static void
885 check_handlers_1 (tree master, tree_stmt_iterator i)
886 {
887   tree type = TREE_TYPE (master);
888
889   for (; !tsi_end_p (i); tsi_next (&i))
890     {
891       tree handler = tsi_stmt (i);
892       if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
893         {
894           warning ("%Hexception of type %qT will be caught",
895                    EXPR_LOCUS (handler), TREE_TYPE (handler));
896           warning ("%H   by earlier handler for %qT",
897                    EXPR_LOCUS (master), type);
898           break;
899         }
900     }
901 }
902
903 /* Given a STATEMENT_LIST of HANDLERs, make sure that they're OK.  */
904
905 void
906 check_handlers (tree handlers)
907 {
908   tree_stmt_iterator i;
909
910   /* If we don't have a STATEMENT_LIST, then we've just got one
911      handler, and thus nothing to warn about.  */
912   if (TREE_CODE (handlers) != STATEMENT_LIST)
913     return;
914
915   i = tsi_start (handlers);
916   if (!tsi_end_p (i))
917     while (1)
918       {
919         tree handler = tsi_stmt (i);
920         tsi_next (&i);
921
922         /* No more handlers; nothing to shadow.  */
923         if (tsi_end_p (i))
924           break;
925         if (TREE_TYPE (handler) == NULL_TREE)
926           pedwarn ("%H%<...%> handler must be the last handler for"
927                    " its try block", EXPR_LOCUS (handler));
928         else
929           check_handlers_1 (handler, i);
930       }
931 }