OSDN Git Service

2005-10-28 Andrew Pinski <pinskia@physics.uc.edu>
[pf3gnuchains/gcc-fork.git] / gcc / cp / except.c
index eb8cf0e..be7208e 100644 (file)
 /* Handle exceptional things in C++.
-   Copyright (C) 1989, 92-97, 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
+   2000, 2001, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
    Contributed by Michael Tiemann <tiemann@cygnus.com>
    Rewritten by Mike Stump <mrs@cygnus.com>, based upon an
    initial re-implementation courtesy Tad Hunt.
 
-This file is part of GNU CC.
+This file is part of GCC.
 
-GNU CC is free software; you can redistribute it and/or modify
+GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
-GNU CC is distributed in the hope that it will be useful,
+GCC is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING.  If not, write to
+the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
 #include "tree.h"
 #include "rtl.h"
+#include "expr.h"
+#include "libfuncs.h"
 #include "cp-tree.h"
 #include "flags.h"
-#include "obstack.h"
-#include "expr.h"
 #include "output.h"
 #include "except.h"
-#include "defaults.h"
 #include "toplev.h"
-#include "eh-common.h"
-
-static void push_eh_cleanup PROTO((void));
-static tree build_eh_type_type PROTO((tree));
-static tree call_eh_info PROTO((void));
-static void push_eh_info PROTO((void));
-static tree get_eh_info PROTO((void));
-static tree get_eh_value PROTO((void));
-#if 0
-static tree get_eh_type PROTO((void));
-static tree get_eh_caught PROTO((void));
-static tree get_eh_handlers PROTO((void));
-#endif
-static tree do_pop_exception PROTO((void));
-static tree build_eh_type_type_ref PROTO((tree));
-static tree build_terminate_handler PROTO((void));
-static tree alloc_eh_object PROTO((tree));
-static int complete_ptr_ref_or_void_ptr_p PROTO((tree, tree));
-static void initialize_handler_parm PROTO((tree));
-static tree expand_throw PROTO((tree));
-
-#if 0
-/* This is the startup, and finish stuff per exception table.  */
-
-/* XXX - Tad: exception handling section */
-#ifndef EXCEPT_SECTION_ASM_OP
-#define EXCEPT_SECTION_ASM_OP  "section\t.gcc_except_table,\"a\",@progbits"
-#endif
-
-#ifdef EXCEPT_SECTION_ASM_OP
-
- /* on machines which support it, the exception table lives in another section,
-       but it needs a label so we can reference it...  This sets up that
-    label! */
-asm (EXCEPT_SECTION_ASM_OP);
-exception_table __EXCEPTION_TABLE__[1] = { (void*)0, (void*)0, (void*)0 };
-asm (TEXT_SECTION_ASM_OP);
-
-#endif /* EXCEPT_SECTION_ASM_OP */
-
-#ifdef EXCEPT_SECTION_ASM_OP
-
- /* we need to know where the end of the exception table is... so this
-    is how we do it! */
-
-asm (EXCEPT_SECTION_ASM_OP);
-exception_table __EXCEPTION_END__[1] = { (void*)-1, (void*)-1, (void*)-1 };
-asm (TEXT_SECTION_ASM_OP);
-
-#endif /* EXCEPT_SECTION_ASM_OP */
-
-#endif
-
-#include "decl.h"
-#include "insn-flags.h"
-#include "obstack.h"
-
-/* ======================================================================
-   Briefly the algorithm works like this:
-
-     When a constructor or start of a try block is encountered,
-     push_eh_entry (&eh_stack) is called.  Push_eh_entry () creates a
-     new entry in the unwind protection stack and returns a label to
-     output to start the protection for that block.
-
-     When a destructor or end try block is encountered, pop_eh_entry
-     (&eh_stack) is called.  Pop_eh_entry () returns the eh_entry it
-     created when push_eh_entry () was called.  The eh_entry structure
-     contains three things at this point.  The start protect label,
-     the end protect label, and the exception handler label.  The end
-     protect label should be output before the call to the destructor
-     (if any). If it was a destructor, then its parse tree is stored
-     in the finalization variable in the eh_entry structure.  Otherwise
-     the finalization variable is set to NULL to reflect the fact that
-     it is the end of a try block.  Next, this modified eh_entry node
-     is enqueued in the finalizations queue by calling
-     enqueue_eh_entry (&queue,entry).
-
-       +---------------------------------------------------------------+
-       |XXX: Will need modification to deal with partially             |
-       |                       constructed arrays of objects           |
-       |                                                               |
-       |       Basically, this consists of keeping track of how many   |
-       |       of the objects have been constructed already (this      |
-       |       should be in a register though, so that shouldn't be a  |
-       |       problem.                                                |
-       +---------------------------------------------------------------+
-
-     When a catch block is encountered, there is a lot of work to be
-     done.
-
-     Since we don't want to generate the catch block inline with the
-     regular flow of the function, we need to have some way of doing
-     so.  Luckily, we can use sequences to defer the catch sections.
-     When the start of a catch block is encountered, we start the
-     sequence.  After the catch block is generated, we end the
-     sequence.
-
-     Next we must insure that when the catch block is executed, all
-     finalizations for the matching try block have been completed.  If
-     any of those finalizations throw an exception, we must call
-     terminate according to the ARM (section r.15.6.1).  What this
-     means is that we need to dequeue and emit finalizations for each
-     entry in the eh_queue until we get to an entry with a NULL
-     finalization field.  For any of the finalization entries, if it
-     is not a call to terminate (), we must protect it by giving it
-     another start label, end label, and exception handler label,
-     setting its finalization tree to be a call to terminate (), and
-     enqueue'ing this new eh_entry to be output at an outer level.
-     Finally, after all that is done, we can get around to outputting
-     the catch block which basically wraps all the "catch (...) {...}"
-     statements in a big if/then/else construct that matches the
-     correct block to call.
-     
-     ===================================================================== */
-
-/* ====================================================================== */
-
-/* sets up all the global eh stuff that needs to be initialized at the
+#include "tree-inline.h"
+#include "tree-iterator.h"
+#include "target.h"
+
+static void push_eh_cleanup (tree);
+static tree prepare_eh_type (tree);
+static tree build_eh_type_type (tree);
+static tree do_begin_catch (void);
+static int dtor_nothrow (tree);
+static tree do_end_catch (tree);
+static bool decl_is_java_type (tree decl, int err);
+static void initialize_handler_parm (tree, tree);
+static tree do_allocate_exception (tree);
+static tree wrap_cleanups_r (tree *, int *, void *);
+static int complete_ptr_ref_or_void_ptr_p (tree, tree);
+static bool is_admissible_throw_operand (tree);
+static int can_convert_eh (tree, tree);
+static tree cp_protect_cleanup_actions (void);
+
+/* Sets up all the global eh stuff that needs to be initialized at the
    start of compilation.  */
 
 void
-init_exception_processing ()
+init_exception_processing (void)
 {
-  /* void vtype () */
-  tree vtype = build_function_type (void_type_node, void_list_node);
-  
-  if (flag_honor_std)
-    push_namespace (get_identifier ("std"));
-  terminate_node = auto_function (get_identifier ("terminate"), vtype);
-  TREE_THIS_VOLATILE (terminate_node) = 1;
-  if (flag_honor_std)
-    pop_namespace ();
+  tree tmp;
 
-  set_exception_lang_code (EH_LANG_C_plus_plus);
-  set_exception_version_code (1);
+  /* void std::terminate (); */
+  push_namespace (std_identifier);
+  tmp = build_function_type (void_type_node, void_list_node);
+  terminate_node = build_cp_library_fn_ptr ("terminate", tmp);
+  TREE_THIS_VOLATILE (terminate_node) = 1;
+  TREE_NOTHROW (terminate_node) = 1;
+  pop_namespace ();
+
+  /* void __cxa_call_unexpected(void *); */
+  tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
+  tmp = build_function_type (void_type_node, tmp);
+  call_unexpected_node
+    = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
+
+  eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
+                                            ? "__gxx_personality_sj0"
+                                            : "__gxx_personality_v0");
+  if (targetm.arm_eabi_unwinder)
+    unwind_resume_libfunc = init_one_libfunc ("__cxa_end_cleanup");
+  else
+    default_init_unwind_resume_libfunc ();
 
-  /* If we use setjmp/longjmp EH, arrange for all cleanup actions to
-     be protected with __terminate.  */
-  protect_cleanup_actions_with_terminate = 1;
+  lang_eh_runtime_type = build_eh_type_type;
+  lang_protect_cleanup_actions = &cp_protect_cleanup_actions;
 }
 
-/* Retrieve a pointer to the cp_eh_info node for the current exception.  */
+/* Returns an expression to be executed if an unhandled exception is
+   propagated out of a cleanup region.  */
 
 static tree
-call_eh_info ()
+cp_protect_cleanup_actions (void)
 {
-  tree fn;
+  /* [except.terminate]
 
-  fn = get_identifier ("__start_cp_handler");
-  if (IDENTIFIER_GLOBAL_VALUE (fn))
-    fn = IDENTIFIER_GLOBAL_VALUE (fn);
-  else
-    {
-      tree t1, t, fields[7];
-
-      /* Declare cp_eh_info * __start_cp_handler (void),
-        as defined in exception.cc. */
-
-      /* struct cp_eh_info.  This must match exception.cc.  Note that this
-        type is not pushed anywhere.  */
-      t1= make_lang_type (RECORD_TYPE);
-      fields[0] = build_lang_decl (FIELD_DECL, 
-                    get_identifier ("handler_label"), ptr_type_node);
-      fields[1] = build_lang_decl (FIELD_DECL, 
-                    get_identifier ("dynamic_handler_chain"), ptr_type_node);
-      fields[2] = build_lang_decl (FIELD_DECL, 
-                    get_identifier ("info"), ptr_type_node);
-      fields[3] = build_lang_decl (FIELD_DECL, 
-                    get_identifier ("table_index"), ptr_type_node);
-      /* N.B.: The fourth field LEN is expected to be
-        the number of fields - 1, not the total number of fields.  */
-      finish_builtin_type (t1, "eh_context", fields, 3, ptr_type_node);
-      t1 = build_pointer_type (t1);
-
-      t1= make_lang_type (RECORD_TYPE);
-      fields[0] = build_lang_decl (FIELD_DECL, 
-                    get_identifier ("match_function"), ptr_type_node);
-      fields[1] = build_lang_decl (FIELD_DECL, 
-                    get_identifier ("language"), short_integer_type_node);
-      fields[2] = build_lang_decl (FIELD_DECL, 
-                    get_identifier ("version"), short_integer_type_node);
-      /* N.B.: The fourth field LEN is expected to be
-        the number of fields - 1, not the total number of fields.  */
-      finish_builtin_type (t1, "__eh_info", fields, 2, ptr_type_node);
-      t = make_lang_type (RECORD_TYPE);
-      fields[0] = build_lang_decl (FIELD_DECL, 
-                                  get_identifier ("eh_info"), t1);
-      fields[1] = build_lang_decl (FIELD_DECL, get_identifier ("value"),
-                                  ptr_type_node);
-      fields[2] = build_lang_decl (FIELD_DECL, get_identifier ("type"),
-                                  ptr_type_node);
-      fields[3] = build_lang_decl
-       (FIELD_DECL, get_identifier ("cleanup"),
-        build_pointer_type (build_function_type
-                            (ptr_type_node, tree_cons
-                             (NULL_TREE, ptr_type_node, void_list_node))));
-      fields[4] = build_lang_decl (FIELD_DECL, get_identifier ("caught"),
-                                  boolean_type_node);
-      fields[5] = build_lang_decl (FIELD_DECL, get_identifier ("next"),
-                                  build_pointer_type (t));
-      fields[6] = build_lang_decl
-       (FIELD_DECL, get_identifier ("handlers"), long_integer_type_node);
-      /* N.B.: The fourth field LEN is expected to be
-        the number of fields - 1, not the total number of fields.  */
-      finish_builtin_type (t, "cp_eh_info", fields, 6, ptr_type_node);
-      t = build_pointer_type (t);
-
-      /* And now the function.  */
-      fn = build_lang_decl (FUNCTION_DECL, fn,
-                           build_function_type (t, void_list_node));
-      DECL_EXTERNAL (fn) = 1;
-      TREE_PUBLIC (fn) = 1;
-      DECL_ARTIFICIAL (fn) = 1;
-      pushdecl_top_level (fn);
-      make_function_rtl (fn);
-    }
-  mark_used (fn);
-  return build_function_call (fn, NULL_TREE);
+     When the destruction of an object during stack unwinding exits
+     using an exception ... void terminate(); is called.  */
+  return build_call (terminate_node, NULL_TREE);
 }
 
-/* Retrieve a pointer to the cp_eh_info node for the current exception
-   and save it in the current binding level.  */
-
-static void
-push_eh_info ()
+static tree
+prepare_eh_type (tree type)
 {
-  tree decl, fn = call_eh_info ();
-
-  /* Remember the pointer to the current exception info; it won't change
-     during this catch block.  */
-  decl = build_decl (VAR_DECL, get_identifier ("__exception_info"),
-                    TREE_TYPE (fn));
-  DECL_ARTIFICIAL (decl) = 1;
-  DECL_INITIAL (decl) = fn;
-  decl = pushdecl (decl);
-  cp_finish_decl (decl, fn, NULL_TREE, 0);
-}
+  if (type == NULL_TREE)
+    return type;
+  if (type == error_mark_node)
+    return error_mark_node;
+
+  /* peel back references, so they match.  */
+  type = non_reference (type);
 
-/* Returns a reference to the cp_eh_info node for the current exception.  */
+  /* Peel off cv qualifiers.  */
+  type = TYPE_MAIN_VARIANT (type);
 
-static tree
-get_eh_info ()
-{
-  /* Look for the pointer pushed in push_eh_info.  */
-  tree t = lookup_name (get_identifier ("__exception_info"), 0);
-  return build_indirect_ref (t, NULL_PTR);
+  return type;
 }
 
-/* Returns a reference to the current exception object.  */
-
-static tree
-get_eh_value ()
+/* Return the type info for TYPE as used by EH machinery.  */
+tree
+eh_type_info (tree type)
 {
-  return build_component_ref (get_eh_info (), get_identifier ("value"),
-                             NULL_TREE, 0);
-}
+  tree exp;
 
-/* Returns a reference to the current exception type.  */
+  if (type == NULL_TREE || type == error_mark_node)
+    return type;
 
-#if 0
-static tree
-get_eh_type ()
-{
-  return build_component_ref (get_eh_info (), get_identifier ("type"),
-                             NULL_TREE, 0);
+  if (decl_is_java_type (type, 0))
+    exp = build_java_class_ref (TREE_TYPE (type));
+  else
+    exp = get_tinfo_decl (type);
+
+  return exp;
 }
 
-/* Returns a reference to whether or not the current exception
-   has been caught.  */
+/* Build the address of a typeinfo decl for use in the runtime
+   matching field of the exception model.  */
 
 static tree
-get_eh_caught ()
+build_eh_type_type (tree type)
 {
-  return build_component_ref (get_eh_info (), get_identifier ("caught"),
-                             NULL_TREE, 0);
-}
+  tree exp = eh_type_info (type);
 
-/* Returns a reference to whether or not the current exception
-   has been caught.  */
+  if (!exp)
+    return NULL;
 
-static tree
-get_eh_handlers ()
+  mark_used (exp);
+
+  return convert (ptr_type_node, build_address (exp));
+}
+
+tree
+build_exc_ptr (void)
 {
-  return build_component_ref (get_eh_info (), get_identifier ("handlers"),
-                             NULL_TREE, 0);
+  return build0 (EXC_PTR_EXPR, ptr_type_node);
 }
-#endif
 
-/* Build a type value for use at runtime for a type that is matched
-   against by the exception handling system.  */
+/* Build up a call to __cxa_get_exception_ptr so that we can build a
+   copy constructor for the thrown object.  */
 
 static tree
-build_eh_type_type (type)
-     tree type;
+do_get_exception_ptr (void)
 {
-  if (type == error_mark_node)
-    return error_mark_node;
-
-  /* peel back references, so they match.  */
-  if (TREE_CODE (type) == REFERENCE_TYPE)
-    type = TREE_TYPE (type);
+  tree fn;
 
-  /* Peel off cv qualifiers.  */
-  type = TYPE_MAIN_VARIANT (type);
+  fn = get_identifier ("__cxa_get_exception_ptr");
+  if (!get_global_value_if_present (fn, &fn))
+    {
+      /* Declare void* __cxa_get_exception_ptr (void *).  */
+      tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
+      fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp));
+    }
 
-  return build1 (ADDR_EXPR, ptr_type_node, get_typeid_1 (type));
+  return build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
+                                            NULL_TREE));
 }
 
-/* Build the address of a typeinfo function for use in the runtime
-   matching field of the new exception model */
+/* Build up a call to __cxa_begin_catch, to tell the runtime that the
+   exception has been handled.  */
 
 static tree
-build_eh_type_type_ref (type)
-     tree type;
+do_begin_catch (void)
 {
-  tree exp;
+  tree fn;
 
-  if (type == error_mark_node)
-    return error_mark_node;
+  fn = get_identifier ("__cxa_begin_catch");
+  if (!get_global_value_if_present (fn, &fn))
+    {
+      /* Declare void* __cxa_begin_catch (void *).  */
+      tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
+      fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp));
+    }
 
-  /* peel back references, so they match.  */
-  if (TREE_CODE (type) == REFERENCE_TYPE)
-    type = TREE_TYPE (type);
+  return build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
+                                            NULL_TREE));
+}
 
-  /* Peel off cv qualifiers.  */
-  type = TYPE_MAIN_VARIANT (type);
+/* Returns nonzero if cleaning up an exception of type TYPE (which can be
+   NULL_TREE for a ... handler) will not throw an exception.  */
+
+static int
+dtor_nothrow (tree type)
+{
+  if (type == NULL_TREE)
+    return 0;
 
-  exp = get_tinfo_fn (type);
-  exp = build1 (ADDR_EXPR, ptr_type_node, exp);
+  if (!CLASS_TYPE_P (type))
+    return 1;
 
-  return (exp);
-}
+  if (CLASSTYPE_LAZY_DESTRUCTOR (type))
+    lazily_declare_fn (sfk_destructor, type);
 
-/* This routine is called to mark all the symbols representing runtime
-   type functions in the exception table as having been referenced.
-   This will make sure code is emitted for them. Called from finish_file. */
-void 
-mark_all_runtime_matches () 
-{
-  int x,num;
-  void **ptr;
-  tree exp;
-  
-  num = find_all_handler_type_matches (&ptr);
-  if (num == 0 || ptr == NULL)
-    return;
-  
-  for (x=0; x <num; x++)
-    {
-      exp = (tree) ptr[x];
-      if (TREE_CODE (exp) == ADDR_EXPR)
-        {
-          exp = TREE_OPERAND (exp, 0);
-          if (TREE_CODE (exp) == FUNCTION_DECL)
-            TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (exp)) = 1;
-        }
-    }
-  
-  free (ptr);
+  return TREE_NOTHROW (CLASSTYPE_DESTRUCTORS (type));
 }
 
-/* Build up a call to __cp_pop_exception, to destroy the exception object
-   for the current catch block.  HANDLER is either true or false, telling
-   the library whether or not it is being called from an exception handler;
-   if it is, it avoids destroying the object on rethrow.  */
+/* Build up a call to __cxa_end_catch, to destroy the exception object
+   for the current catch block if no others are currently using it.  */
 
 static tree
-do_pop_exception ()
+do_end_catch (tree type)
 {
   tree fn, cleanup;
-  fn = get_identifier ("__cp_pop_exception");
-  if (IDENTIFIER_GLOBAL_VALUE (fn))
-    fn = IDENTIFIER_GLOBAL_VALUE (fn);
-  else
+
+  fn = get_identifier ("__cxa_end_catch");
+  if (!get_global_value_if_present (fn, &fn))
     {
-      /* Declare void __cp_pop_exception (void *),
-        as defined in exception.cc. */
-      fn = build_lang_decl
-       (FUNCTION_DECL, fn,
-        build_function_type (void_type_node, tree_cons
-                             (NULL_TREE, ptr_type_node, void_list_node)));
-      DECL_EXTERNAL (fn) = 1;
-      TREE_PUBLIC (fn) = 1;
-      DECL_ARTIFICIAL (fn) = 1;
-      pushdecl_top_level (fn);
-      make_function_rtl (fn);
+      /* Declare void __cxa_end_catch ().  */
+      fn = push_void_library_fn (fn, void_list_node);
+      /* This can throw if the destructor for the exception throws.  */
+      TREE_NOTHROW (fn) = 0;
     }
 
-  mark_used (fn);
-  /* Arrange to do a dynamically scoped cleanup upon exit from this region.  */
-  cleanup = lookup_name (get_identifier ("__exception_info"), 0);
-  cleanup = build_function_call (fn, tree_cons
-                                (NULL_TREE, cleanup, NULL_TREE));
+  cleanup = build_function_call (fn, NULL_TREE);
+  TREE_NOTHROW (cleanup) = dtor_nothrow (type);
+
   return cleanup;
 }
 
 /* This routine creates the cleanup for the current exception.  */
 
 static void
-push_eh_cleanup ()
+push_eh_cleanup (tree type)
 {
-  finish_decl_cleanup (NULL_TREE, do_pop_exception ());
+  finish_decl_cleanup (NULL_TREE, do_end_catch (type));
 }
 
-/* Build up a call to terminate on the function obstack, for use as an
-   exception handler.  */
+/* Return nonzero value if DECL is a Java type suitable for catch or
+   throw.  */
 
-static tree
-build_terminate_handler ()
+static bool
+decl_is_java_type (tree decl, int err)
 {
-  return build_function_call (terminate_node, NULL_TREE);
+  bool r = (TREE_CODE (decl) == POINTER_TYPE
+           && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
+           && TYPE_FOR_JAVA (TREE_TYPE (decl)));
+
+  if (err)
+    {
+      if (TREE_CODE (decl) == REFERENCE_TYPE
+         && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
+         && TYPE_FOR_JAVA (TREE_TYPE (decl)))
+       {
+         /* Can't throw a reference.  */
+         error ("type %qT is disallowed in Java %<throw%> or %<catch%>",
+                decl);
+       }
+
+      if (r)
+       {
+         tree jthrow_node
+           = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jthrowable"));
+
+         if (jthrow_node == NULL_TREE)
+           fatal_error
+             ("call to Java %<catch%> or %<throw%> with %<jthrowable%> undefined");
+
+         jthrow_node = TREE_TYPE (TREE_TYPE (jthrow_node));
+
+         if (! DERIVED_FROM_P (jthrow_node, TREE_TYPE (decl)))
+           {
+             /* Thrown object must be a Throwable.  */
+             error ("type %qT is not derived from %<java::lang::Throwable%>",
+                    TREE_TYPE (decl));
+           }
+       }
+    }
+
+  return r;
+}
+
+/* Select the personality routine to be used for exception handling,
+   or issue an error if we need two different ones in the same
+   translation unit.
+   ??? At present eh_personality_libfunc is set to
+   __gxx_personality_(sj|v)0 in init_exception_processing - should it
+   be done here instead?  */
+void
+choose_personality_routine (enum languages lang)
+{
+  static enum {
+    chose_none,
+    chose_cpp,
+    chose_java,
+    gave_error
+  } state;
+
+  switch (state)
+    {
+    case gave_error:
+      return;
+
+    case chose_cpp:
+      if (lang != lang_cplusplus)
+       goto give_error;
+      return;
+
+    case chose_java:
+      if (lang != lang_java)
+       goto give_error;
+      return;
+
+    case chose_none:
+      ; /* Proceed to language selection.  */
+    }
+
+  switch (lang)
+    {
+    case lang_cplusplus:
+      state = chose_cpp;
+      break;
+
+    case lang_java:
+      state = chose_java;
+      eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
+                                                ? "__gcj_personality_sj0"
+                                                : "__gcj_personality_v0");
+      break;
+
+    default:
+      gcc_unreachable ();
+    }
+  return;
+
+ give_error:
+  error ("mixing C++ and Java catches in a single translation unit");
+  state = gave_error;
 }
 
 /* Initialize the catch parameter DECL.  */
 
-static void 
-initialize_handler_parm (decl)
-     tree decl;
+static void
+initialize_handler_parm (tree decl, tree exp)
 {
-  tree exp;
   tree init;
   tree init_type;
 
@@ -462,21 +361,23 @@ initialize_handler_parm (decl)
      warning about an unused ((anonymous)).  */
   TREE_USED (decl) = 1;
 
-  /* Figure out the type that the initializer is.  */
+  /* Figure out the type that the initializer is.  Pointers are returned
+     adjusted by value from __cxa_begin_catch.  Others are returned by
+     reference.  */
   init_type = TREE_TYPE (decl);
-  if (TREE_CODE (init_type) != REFERENCE_TYPE
-      && TREE_CODE (init_type) != POINTER_TYPE)
+  if (!POINTER_TYPE_P (init_type))
     init_type = build_reference_type (init_type);
 
-  exp = get_eh_value ();
+  choose_personality_routine (decl_is_java_type (init_type, 0)
+                             ? lang_java : lang_cplusplus);
 
   /* Since pointers are passed by value, initialize a reference to
-     pointer catch parm with the address of the value slot.  */ 
-  if (TREE_CODE (init_type) == REFERENCE_TYPE 
-      && TREE_CODE (TREE_TYPE (init_type)) == POINTER_TYPE)
+     pointer catch parm with the address of the temporary.  */
+  if (TREE_CODE (init_type) == REFERENCE_TYPE
+      && TYPE_PTR_P (TREE_TYPE (init_type)))
     exp = build_unary_op (ADDR_EXPR, exp, 1);
 
-  exp = ocp_convert (init_type , exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
+  exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
 
   init = convert_from_reference (exp);
 
@@ -488,8 +389,7 @@ initialize_handler_parm (decl)
         See also expand_default_init.  */
       init = ocp_convert (TREE_TYPE (decl), init,
                          CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
-      init = build (TRY_CATCH_EXPR, TREE_TYPE (init), init,
-                   build_terminate_handler ());
+      init = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (init), init);
     }
 
   /* Let `cp_finish_decl' know that this initializer is ok.  */
@@ -504,11 +404,9 @@ initialize_handler_parm (decl)
 /* Call this to start a catch block.  DECL is the catch parameter.  */
 
 tree
-expand_start_catch_block (decl)
-     tree decl;
+expand_start_catch_block (tree decl)
 {
-  tree compound_stmt_1;
-  tree compound_stmt_2;
+  tree exp;
   tree type;
 
   if (! doing_eh (1))
@@ -518,26 +416,57 @@ expand_start_catch_block (decl)
   if (decl && !complete_ptr_ref_or_void_ptr_p (TREE_TYPE (decl), NULL_TREE))
     decl = NULL_TREE;
 
-  /* Create a binding level for the eh_info and the exception object
-     cleanup.  */
-  compound_stmt_1 = begin_compound_stmt (/*has_no_scope=*/0);
-
   if (decl)
-    type = build_eh_type_type_ref (TREE_TYPE (decl));
+    type = prepare_eh_type (TREE_TYPE (decl));
   else
     type = NULL_TREE;
-  begin_catch_block (type);
 
-  push_eh_info ();
-  push_eh_cleanup ();
+  if (decl && decl_is_java_type (type, 1))
+    {
+      /* Java only passes object via pointer and doesn't require
+        adjusting.  The java object is immediately before the
+        generic exception header.  */
+      exp = build_exc_ptr ();
+      exp = build1 (NOP_EXPR, build_pointer_type (type), exp);
+      exp = build2 (MINUS_EXPR, TREE_TYPE (exp), exp,
+                   TYPE_SIZE_UNIT (TREE_TYPE (exp)));
+      exp = build_indirect_ref (exp, NULL);
+      initialize_handler_parm (decl, exp);
+      return type;
+    }
 
-  /* Create a binding level for the parm.  */
-  compound_stmt_2 = begin_compound_stmt (/*has_no_scope=*/0);
+  /* Call __cxa_end_catch at the end of processing the exception.  */
+  push_eh_cleanup (type);
 
-  if (decl)
-    initialize_handler_parm (decl);
+  /* If there's no decl at all, then all we need to do is make sure
+     to tell the runtime that we've begun handling the exception.  */
+  if (decl == NULL)
+    finish_expr_stmt (do_begin_catch ());
 
-  return build_tree_list (compound_stmt_1, compound_stmt_2);
+  /* If the C++ object needs constructing, we need to do that before
+     calling __cxa_begin_catch, so that std::uncaught_exception gets
+     the right value during the copy constructor.  */
+  else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
+    {
+      exp = do_get_exception_ptr ();
+      initialize_handler_parm (decl, exp);
+      finish_expr_stmt (do_begin_catch ());
+    }
+
+  /* Otherwise the type uses a bitwise copy, and we don't have to worry
+     about the value of std::uncaught_exception and therefore can do the
+     copy with the return value of __cxa_end_catch instead.  */
+  else
+    {
+      tree init = do_begin_catch ();
+      exp = create_temporary_var (ptr_type_node);
+      DECL_REGISTER (exp) = 1;
+      cp_finish_decl (exp, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
+      finish_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
+      initialize_handler_parm (decl, exp);
+    }
+
+  return type;
 }
 
 
@@ -546,12 +475,8 @@ expand_start_catch_block (decl)
    the label to jump to if this catch block didn't match.  */
 
 void
-expand_end_catch_block (blocks)
-     tree blocks;
+expand_end_catch_block (void)
 {
-  tree compound_stmt_1 = blocks ? TREE_PURPOSE (blocks): NULL_TREE;
-  tree compound_stmt_2 = blocks ? TREE_VALUE (blocks): NULL_TREE;
-
   if (! doing_eh (1))
     return;
 
@@ -561,431 +486,481 @@ expand_end_catch_block (blocks)
       && (DECL_CONSTRUCTOR_P (current_function_decl)
          || DECL_DESTRUCTOR_P (current_function_decl)))
     finish_expr_stmt (build_throw (NULL_TREE));
-
-  /* Cleanup the EH parameter.  */
-  finish_compound_stmt (/*has_no_scope=*/0, compound_stmt_2);
-    /* Cleanup the EH object.  */
-  finish_compound_stmt (/*has_no_scope=*/0, compound_stmt_1);
 }
 
-/* An exception spec is implemented more or less like:
-
-   try {
-     function body;
-   } catch (...) {
-     void *p[] = { typeid(raises) };
-     __check_eh_spec (p, count);
-   }
-
-   __check_eh_spec in exception.cc handles all the details.  */
-
 tree
-expand_start_eh_spec ()
+begin_eh_spec_block (void)
 {
-  return begin_try_block ();
+  tree r = build_stmt (EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
+  add_stmt (r);
+  EH_SPEC_STMTS (r) = push_stmt_list ();
+  return r;
 }
 
 void
-expand_end_eh_spec (raises, try_block)
-     tree raises;
-     tree try_block;
+finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
 {
-  tree tmp, fn, decl, types = NULL_TREE;
-  tree blocks;
-  tree handler;
-  int count = 0;
+  tree raises;
 
-  finish_try_block (try_block);
-  handler = begin_handler ();
-  blocks = finish_handler_parms (NULL_TREE, handler);
+  EH_SPEC_STMTS (eh_spec_block) = pop_stmt_list (EH_SPEC_STMTS (eh_spec_block));
 
-  /* Build up an array of type_infos.  */
-  for (; raises && TREE_VALUE (raises); raises = TREE_CHAIN (raises))
+  /* Strip cv quals, etc, from the specification types.  */
+  for (raises = NULL_TREE;
+       raw_raises && TREE_VALUE (raw_raises);
+       raw_raises = TREE_CHAIN (raw_raises))
     {
-      types = tree_cons
-       (NULL_TREE, build_eh_type_type (TREE_VALUE (raises)), types);
-      ++count;
-    }
-
-  types = build_nt (CONSTRUCTOR, NULL_TREE, types);
-  TREE_HAS_CONSTRUCTOR (types) = 1;
-
-  /* We can't pass the CONSTRUCTOR directly, so stick it in a variable.  */
-  tmp = build_cplus_array_type (const_ptr_type_node, NULL_TREE);
-  decl = build_decl (VAR_DECL, NULL_TREE, tmp);
-  DECL_ARTIFICIAL (decl) = 1;
-  DECL_INITIAL (decl) = types;
-  cp_finish_decl (decl, types, NULL_TREE, 0);
-
-  decl = decay_conversion (decl);
+      tree type = prepare_eh_type (TREE_VALUE (raw_raises));
+      tree tinfo = eh_type_info (type);
 
-  fn = get_identifier ("__check_eh_spec");
-  if (IDENTIFIER_GLOBAL_VALUE (fn))
-    fn = IDENTIFIER_GLOBAL_VALUE (fn);
-  else
-    {
-      tmp = tree_cons
-       (NULL_TREE, integer_type_node, tree_cons
-        (NULL_TREE, TREE_TYPE (decl), void_list_node));
-      tmp = build_function_type        (void_type_node, tmp);
-  
-      fn = build_lang_decl (FUNCTION_DECL, fn, tmp);
-      DECL_EXTERNAL (fn) = 1;
-      TREE_PUBLIC (fn) = 1;
-      DECL_ARTIFICIAL (fn) = 1;
-      TREE_THIS_VOLATILE (fn) = 1;
-      pushdecl_top_level (fn);
-      make_function_rtl (fn);
+      mark_used (tinfo);
+      raises = tree_cons (NULL_TREE, type, raises);
     }
 
-  mark_used (fn);
-  tmp = tree_cons (NULL_TREE, build_int_2 (count, 0), 
-                  tree_cons (NULL_TREE, decl, NULL_TREE));
-  tmp = build_call (fn, TREE_TYPE (TREE_TYPE (fn)), tmp);
-  finish_expr_stmt (tmp);
-
-  finish_handler (blocks, handler);
-  finish_handler_sequence (try_block);
+  EH_SPEC_RAISES (eh_spec_block) = raises;
 }
 
-/* This is called to expand all the toplevel exception handling
-   finalization for a function.  It should only be called once per
-   function.  */
+/* Return a pointer to a buffer for an exception object of type TYPE.  */
 
-void
-expand_exception_blocks ()
+static tree
+do_allocate_exception (tree type)
 {
-  do_pending_stack_adjust ();
+  tree fn;
 
-  if (catch_clauses)
+  fn = get_identifier ("__cxa_allocate_exception");
+  if (!get_global_value_if_present (fn, &fn))
     {
-      rtx funcend = gen_label_rtx ();
-      emit_jump (funcend);
-
-      /* We cannot protect n regions this way if we must flow into the
-        EH region through the top of the region, as we have to with
-        the setjmp/longjmp approach.  */
-      if (exceptions_via_longjmp == 0)
-       expand_eh_region_start ();
-
-      emit_insns (catch_clauses);
-      catch_clauses = NULL_RTX;
-
-      if (exceptions_via_longjmp == 0)
-       expand_eh_region_end (build_terminate_handler ());
-
-      emit_insns (catch_clauses);
-      catch_clauses = NULL_RTX;
-      emit_label (funcend);
+      /* Declare void *__cxa_allocate_exception(size_t).  */
+      tree tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
+      fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp));
     }
-}
 
-tree
-start_anon_func ()
-{
-  static int counter = 0;
-  int old_interface_unknown = interface_unknown;
-  char name[32];
-  tree params;
-  tree t;
-
-  push_to_top_level ();
-
-  /* No need to mangle this.  */
-  push_lang_context (lang_name_c);
-
-  interface_unknown = 1;
-
-  params = void_list_node;
-  /* tcf stands for throw clean function.  */
-  sprintf (name, "__tcf_%d", counter++);
-  t = make_call_declarator (get_identifier (name), params, NULL_TREE,
-                           NULL_TREE);
-  start_function (decl_tree_cons (NULL_TREE, get_identifier ("static"),
-                                 void_list_node),
-                 t, NULL_TREE, SF_DEFAULT);
-  do_pushlevel ();
-
-  interface_unknown = old_interface_unknown;
-
-  pop_lang_context ();
-
-  return current_function_decl;
+  return build_function_call (fn, tree_cons (NULL_TREE, size_in_bytes (type),
+                                            NULL_TREE));
 }
 
-void
-end_anon_func ()
+/* Call __cxa_free_exception from a cleanup.  This is never invoked
+   directly, but see the comment for stabilize_throw_expr.  */
+
+static tree
+do_free_exception (tree ptr)
 {
-  do_poplevel ();
+  tree fn;
 
-  expand_body (finish_function (lineno, 0));
+  fn = get_identifier ("__cxa_free_exception");
+  if (!get_global_value_if_present (fn, &fn))
+    {
+      /* Declare void __cxa_free_exception (void *).  */
+      fn = push_void_library_fn (fn, tree_cons (NULL_TREE, ptr_type_node,
+                                               void_list_node));
+    }
 
-  pop_from_top_level ();
+  return build_function_call (fn, tree_cons (NULL_TREE, ptr, NULL_TREE));
 }
 
-/* Return a pointer to a buffer for an exception object of type TYPE.  */
+/* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
+   Called from build_throw via walk_tree_without_duplicates.  */
 
 static tree
-alloc_eh_object (type)
-     tree type;
+wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
+                void *data ATTRIBUTE_UNUSED)
 {
-  tree fn, exp;
+  tree exp = *tp;
+  tree cleanup;
 
-  fn = get_identifier ("__eh_alloc");
-  if (IDENTIFIER_GLOBAL_VALUE (fn))
-    fn = IDENTIFIER_GLOBAL_VALUE (fn);
-  else
+  /* Don't walk into types.  */
+  if (TYPE_P (exp))
     {
-      /* Declare __eh_alloc (size_t), as defined in exception.cc.  */
-      tree tmp;
-      tmp = tree_cons (NULL_TREE, sizetype, void_list_node);
-      fn = build_lang_decl (FUNCTION_DECL, fn,
-                           build_function_type (ptr_type_node, tmp));
-      DECL_EXTERNAL (fn) = 1;
-      TREE_PUBLIC (fn) = 1;
-      DECL_ARTIFICIAL (fn) = 1;
-      pushdecl_top_level (fn);
-      make_function_rtl (fn);
+      *walk_subtrees = 0;
+      return NULL_TREE;
     }
+  if (TREE_CODE (exp) != TARGET_EXPR)
+    return NULL_TREE;
 
-  mark_used (fn);
-  exp = build_function_call (fn, tree_cons
-                            (NULL_TREE, size_in_bytes (type), NULL_TREE));
-  exp = build1 (NOP_EXPR, build_pointer_type (type), exp);
-  return exp;
-}
+  cleanup = TARGET_EXPR_CLEANUP (exp);
+  if (cleanup)
+    {
+      cleanup = build1 (MUST_NOT_THROW_EXPR, void_type_node, cleanup);
+      TARGET_EXPR_CLEANUP (exp) = cleanup;
+    }
 
-/* Expand a throw statement.  This follows the following
-   algorithm:
+  /* Keep iterating.  */
+  return NULL_TREE;
+}
 
-       1. Allocate space to save the current PC onto the stack.
-       2. Generate and emit a label and save its address into the
-               newly allocated stack space since we can't save the pc directly.
-       3. If this is the first call to throw in this function:
-               generate a label for the throw block
-       4. jump to the throw block label.  */
+/* Build a throw expression.  */
 
 tree
-expand_throw (exp)
-     tree exp;
+build_throw (tree exp)
 {
   tree fn;
 
+  if (exp == error_mark_node)
+    return exp;
+
+  if (processing_template_decl)
+    {
+      current_function_returns_abnormally = 1;
+      return build_min (THROW_EXPR, void_type_node, exp);
+    }
+
+  if (exp == null_node)
+    warning (0, "throwing NULL, which has integral, not pointer type");
+
+  if (exp != NULL_TREE)
+    {
+      if (!is_admissible_throw_operand (exp))
+       return error_mark_node;
+    }
+
   if (! doing_eh (1))
     return error_mark_node;
 
-  if (exp)
+  if (exp && decl_is_java_type (TREE_TYPE (exp), 1))
+    {
+      tree fn = get_identifier ("_Jv_Throw");
+      if (!get_global_value_if_present (fn, &fn))
+       {
+         /* Declare void _Jv_Throw (void *).  */
+         tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
+         tmp = build_function_type (ptr_type_node, tmp);
+         fn = push_throw_library_fn (fn, tmp);
+       }
+      else if (really_overloaded_fn (fn))
+       {
+         error ("%qD should never be overloaded", fn);
+         return error_mark_node;
+       }
+      fn = OVL_CURRENT (fn);
+      exp = build_function_call (fn, tree_cons (NULL_TREE, exp, NULL_TREE));
+    }
+  else if (exp)
     {
       tree throw_type;
-      tree cleanup = NULL_TREE, e;
-      tree stmt_expr;
-      tree compound_stmt;
-      tree try_block;
+      tree cleanup;
+      tree object, ptr;
+      tree tmp;
+      tree temp_expr, allocate_expr;
+      bool elided;
 
-      begin_init_stmts (&stmt_expr, &compound_stmt);
+      /* The CLEANUP_TYPE is the internal type of a destructor.  */
+      if (!cleanup_type)
+       {
+         tmp = void_list_node;
+         tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
+         tmp = build_function_type (void_type_node, tmp);
+         cleanup_type = build_pointer_type (tmp);
+       }
+
+      fn = get_identifier ("__cxa_throw");
+      if (!get_global_value_if_present (fn, &fn))
+       {
+         /* Declare void __cxa_throw (void*, void*, void (*)(void*)).  */
+         /* ??? Second argument is supposed to be "std::type_info*".  */
+         tmp = void_list_node;
+         tmp = tree_cons (NULL_TREE, cleanup_type, tmp);
+         tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
+         tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
+         tmp = build_function_type (void_type_node, tmp);
+         fn = push_throw_library_fn (fn, tmp);
+       }
 
       /* throw expression */
       /* First, decay it.  */
       exp = decay_conversion (exp);
 
-      /* cleanup_type is void (*)(void *, int),
-        the internal type of a destructor. */
-      if (cleanup_type == NULL_TREE)
-       cleanup_type = build_pointer_type
-         (build_function_type
-          (void_type_node, tree_cons
-           (NULL_TREE, ptr_type_node, tree_cons
-            (NULL_TREE, integer_type_node, void_list_node))));
-
-      if (TYPE_PTR_P (TREE_TYPE (exp)))
-       throw_type = build_eh_type_type (TREE_TYPE (exp));
-      else
+      /* OK, this is kind of wacky.  The standard says that we call
+        terminate when the exception handling mechanism, after
+        completing evaluation of the expression to be thrown but
+        before the exception is caught (_except.throw_), calls a
+        user function that exits via an uncaught exception.
+
+        So we have to protect the actual initialization of the
+        exception object with terminate(), but evaluate the
+        expression first.  Since there could be temps in the
+        expression, we need to handle that, too.  We also expand
+        the call to __cxa_allocate_exception first (which doesn't
+        matter, since it can't throw).  */
+
+      /* Allocate the space for the exception.  */
+      allocate_expr = do_allocate_exception (TREE_TYPE (exp));
+      allocate_expr = get_target_expr (allocate_expr);
+      ptr = TARGET_EXPR_SLOT (allocate_expr);
+      object = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (exp)), ptr);
+      object = build_indirect_ref (object, NULL);
+
+      elided = (TREE_CODE (exp) == TARGET_EXPR);
+
+      /* And initialize the exception object.  */
+      exp = build_init (object, exp, LOOKUP_ONLYCONVERTING);
+      if (exp == error_mark_node)
        {
-         tree object, ptr;
-
-         /* OK, this is kind of wacky.  The WP says that we call
-            terminate
-
-            when the exception handling mechanism, after completing
-            evaluation of the expression to be thrown but before the
-            exception is caught (_except.throw_), calls a user function
-            that exits via an uncaught exception.
-
-            So we have to protect the actual initialization of the
-            exception object with terminate(), but evaluate the expression
-            first.  We also expand the call to __eh_alloc
-            first.  Since there could be temps in the expression, we need
-            to handle that, too.  */
-
-         my_friendly_assert (stmts_are_full_exprs_p == 1, 19990926);
-
-         /* Store the throw expression into a temp.  This can be less
-            efficient than storing it into the allocated space directly, but
-            oh well.  To do this efficiently we would need to insinuate
-            ourselves into expand_call.  */
-         if (TREE_SIDE_EFFECTS (exp))
-           {
-             tree temp = create_temporary_var (TREE_TYPE (exp));
-             DECL_INITIAL (temp) = exp;
-             cp_finish_decl (temp, exp, NULL_TREE, LOOKUP_ONLYCONVERTING);
-             exp = temp;
-           }
-
-         /* Allocate the space for the exception.  */
-         ptr = save_expr (alloc_eh_object (TREE_TYPE (exp)));
-         finish_expr_stmt (ptr);
-
-         try_block = begin_try_block ();
-         object = build_indirect_ref (ptr, NULL_PTR);
-         exp = build_modify_expr (object, INIT_EXPR, exp);
-
-         if (exp == error_mark_node)
-           error ("  in thrown expression");
-
-         finish_expr_stmt (exp);
-         finish_cleanup_try_block (try_block);
-         finish_cleanup (build_terminate_handler (), try_block);
-
-         throw_type = build_eh_type_type (TREE_TYPE (object));
-
-         if (TYPE_HAS_DESTRUCTOR (TREE_TYPE (object)))
-           {
-             cleanup = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
-                                        dtor_identifier, 0);
-             cleanup = TREE_VALUE (cleanup);
-             mark_used (cleanup);
-             mark_addressable (cleanup);
-             /* Pretend it's a normal function.  */
-             cleanup = build1 (ADDR_EXPR, cleanup_type, cleanup);
-           }
-
-         exp = ptr;
+         error ("  in thrown expression");
+         return error_mark_node;
        }
 
-      /* Cast EXP to `void *' so that it will match the prototype for
-        __cp_push_exception.  */
-      exp = convert (ptr_type_node, exp);
+      /* Pre-evaluate the thrown expression first, since if we allocated
+        the space first we would have to deal with cleaning it up if
+        evaluating this expression throws.
+
+        The case where EXP the initializer is a cast or a function
+        returning a class is a bit of a grey area in the standard; it's
+        unclear whether or not it should be allowed to throw.  We used to
+        say no, as that allowed us to optimize this case without worrying
+        about deallocating the exception object if it does.  But that
+        conflicted with expectations (PR 13944) and the EDG compiler; now
+        we wrap the initialization in a TRY_CATCH_EXPR to call
+        do_free_exception rather than in a MUST_NOT_THROW_EXPR, for this
+        case only.
+
+        BUT: Issue 475 may do away with this inconsistency by removing the
+        terminate() in this situation.
+
+        Note that we don't check the return value from stabilize_init
+        because it will only return false in cases where elided is true,
+        and therefore we don't need to work around the failure to
+        preevaluate.  */
+      temp_expr = NULL_TREE;
+      stabilize_init (exp, &temp_expr);
+
+      if (elided)
+       exp = build2 (TRY_CATCH_EXPR, void_type_node, exp,
+                     do_free_exception (ptr));
+      else
+       exp = build1 (MUST_NOT_THROW_EXPR, void_type_node, exp);
 
-      if (cleanup == NULL_TREE)
+      /* Prepend the allocation.  */
+      exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
+      if (temp_expr)
        {
-         cleanup = build_int_2 (0, 0);
-         TREE_TYPE (cleanup) = cleanup_type;
+         /* Prepend the calculation of the throw expression.  Also, force
+            any cleanups from the expression to be evaluated here so that
+            we don't have to do them during unwinding.  But first wrap
+            them in MUST_NOT_THROW_EXPR, since they are run after the
+            exception object is initialized.  */
+         walk_tree_without_duplicates (&temp_expr, wrap_cleanups_r, 0);
+         exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), temp_expr, exp);
+         exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
        }
 
-      fn = get_identifier ("__cp_push_exception");
-      if (IDENTIFIER_GLOBAL_VALUE (fn))
-       fn = IDENTIFIER_GLOBAL_VALUE (fn);
-      else
+      throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
+
+      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
        {
-         /* Declare __cp_push_exception (void*, void*, void (*)(void*, int)),
-            as defined in exception.cc.  */
-         tree tmp;
-         tmp = tree_cons
-           (NULL_TREE, ptr_type_node, tree_cons
-            (NULL_TREE, ptr_type_node, tree_cons
-             (NULL_TREE, cleanup_type, void_list_node)));
-         fn = build_lang_decl (FUNCTION_DECL, fn,
-                               build_function_type (void_type_node, tmp));
-         DECL_EXTERNAL (fn) = 1;
-         TREE_PUBLIC (fn) = 1;
-         DECL_ARTIFICIAL (fn) = 1;
-         pushdecl_top_level (fn);
-         make_function_rtl (fn);
+         cleanup = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
+                                    complete_dtor_identifier, 0);
+         cleanup = BASELINK_FUNCTIONS (cleanup);
+         mark_used (cleanup);
+         cxx_mark_addressable (cleanup);
+         /* Pretend it's a normal function.  */
+         cleanup = build1 (ADDR_EXPR, cleanup_type, cleanup);
        }
+      else
+       cleanup = build_int_cst (cleanup_type, 0);
 
-      mark_used (fn);
-      e = tree_cons (NULL_TREE, exp, tree_cons
-                    (NULL_TREE, throw_type, tree_cons
-                     (NULL_TREE, cleanup, NULL_TREE)));
-      finish_expr_stmt (build_function_call (fn, e));
+      tmp = tree_cons (NULL_TREE, cleanup, NULL_TREE);
+      tmp = tree_cons (NULL_TREE, throw_type, tmp);
+      tmp = tree_cons (NULL_TREE, ptr, tmp);
+      /* ??? Indicate that this function call throws throw_type.  */
+      tmp = build_function_call (fn, tmp);
 
-      exp = finish_init_stmts (stmt_expr, compound_stmt);
+      /* Tack on the initialization stuff.  */
+      exp = build2 (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp);
     }
   else
     {
-      /* rethrow current exception; note that it's no longer caught.  */
+      /* Rethrow current exception.  */
 
-      tree fn = get_identifier ("__uncatch_exception");
-      if (IDENTIFIER_GLOBAL_VALUE (fn))
-       fn = IDENTIFIER_GLOBAL_VALUE (fn);
-      else
+      tree fn = get_identifier ("__cxa_rethrow");
+      if (!get_global_value_if_present (fn, &fn))
        {
-         /* Declare void __uncatch_exception (void)
-            as defined in exception.cc. */
-         fn = build_lang_decl (FUNCTION_DECL, fn,
-                               build_function_type (void_type_node,
-                                                    void_list_node));
-         DECL_EXTERNAL (fn) = 1;
-         TREE_PUBLIC (fn) = 1;
-         DECL_ARTIFICIAL (fn) = 1;
-         pushdecl_top_level (fn);
-         make_function_rtl (fn);
+         /* Declare void __cxa_rethrow (void).  */
+         fn = push_throw_library_fn
+           (fn, build_function_type (void_type_node, void_list_node));
        }
 
-      mark_used (fn);
+      /* ??? Indicate that this function call allows exceptions of the type
+        of the enclosing catch block (if known).  */
       exp = build_function_call (fn, NULL_TREE);
     }
 
-  return exp;
-}
-
-/* Build a throw expression.  */
-
-tree
-build_throw (e)
-     tree e;
-{
-  if (e == error_mark_node)
-    return e;
+  exp = build1 (THROW_EXPR, void_type_node, exp);
 
-  if (processing_template_decl)
-    return build_min (THROW_EXPR, void_type_node, e);
-
-  if (e == null_node)
-    cp_warning ("throwing NULL, which has integral, not pointer type");
-  
-  if (e != NULL_TREE)
-    {
-      if (!complete_ptr_ref_or_void_ptr_p (TREE_TYPE (e), e))
-        return error_mark_node;
-    }
-
-  e = expand_throw (e);
-  e = build1 (THROW_EXPR, void_type_node, e);
-  TREE_SIDE_EFFECTS (e) = 1;
-  TREE_USED (e) = 1;
-
-  return e;
+  return exp;
 }
 
 /* Make sure TYPE is complete, pointer to complete, reference to
    complete, or pointer to cv void. Issue diagnostic on failure.
-   Return the zero on failure and non-zero on success. FROM can be
+   Return the zero on failure and nonzero on success. FROM can be
    the expr or decl from whence TYPE came, if available.  */
 
 static int
-complete_ptr_ref_or_void_ptr_p (type, from)
-     tree type;
-     tree from;
+complete_ptr_ref_or_void_ptr_p (tree type, tree from)
 {
   int is_ptr;
-  
+
   /* Check complete.  */
   type = complete_type_or_else (type, from);
   if (!type)
     return 0;
-  
+
   /* Or a pointer or ref to one, or cv void *.  */
   is_ptr = TREE_CODE (type) == POINTER_TYPE;
   if (is_ptr || TREE_CODE (type) == REFERENCE_TYPE)
     {
       tree core = TREE_TYPE (type);
-  
-      if (is_ptr && same_type_p (TYPE_MAIN_VARIANT (core), void_type_node))
-        /* OK */;
+
+      if (is_ptr && VOID_TYPE_P (core))
+       /* OK */;
       else if (!complete_type_or_else (core, from))
-        return 0;
+       return 0;
     }
   return 1;
 }
 
+/* Return truth-value if EXPRESSION is admissible in throw-expression,
+   i.e. if it is not of incomplete type or a pointer/reference to such
+   a type or of an abstract class type.  */
+
+static bool
+is_admissible_throw_operand (tree expr)
+{
+  tree type = TREE_TYPE (expr);
+
+  /* 15.1/4 [...] The type of the throw-expression shall not be an
+           incomplete type, or a pointer or a reference to an incomplete
+           type, other than void*, const void*, volatile void*, or
+           const volatile void*.  Except for these restriction and the
+           restrictions on type matching mentioned in 15.3, the operand
+           of throw is treated exactly as a function argument in a call
+           (5.2.2) or the operand of a return statement.  */
+  if (!complete_ptr_ref_or_void_ptr_p (type, expr))
+    return false;
+
+  /* 10.4/3 An abstract class shall not be used as a parameter type,
+           as a function return type or as type of an explicit
+           conversion.  */
+  else if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
+    {
+      error ("expression %qE of abstract class type %qT cannot "
+            "be used in throw-expression", expr, type);
+      return false;
+    }
+
+  return true;
+}
+
+/* Returns nonzero if FN is a declaration of a standard C library
+   function which is known not to throw.
+
+   [lib.res.on.exception.handling]: None of the functions from the
+   Standard C library shall report an error by throwing an
+   exception, unless it calls a program-supplied function that
+   throws an exception.  */
+
+#include "cfns.h"
+
+int
+nothrow_libfn_p (tree fn)
+{
+  tree id;
+
+  if (TREE_PUBLIC (fn)
+      && DECL_EXTERNAL (fn)
+      && DECL_NAMESPACE_SCOPE_P (fn)
+      && DECL_EXTERN_C_P (fn))
+    /* OK */;
+  else
+    /* Can't be a C library function.  */
+    return 0;
+
+  /* Being a C library function, DECL_ASSEMBLER_NAME == DECL_NAME
+     unless the system headers are playing rename tricks, and if
+     they are, we don't want to be confused by them.  */
+  id = DECL_NAME (fn);
+  return !!libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
+}
+
+/* Returns nonzero if an exception of type FROM will be caught by a
+   handler for type TO, as per [except.handle].  */
+
+static int
+can_convert_eh (tree to, tree from)
+{
+  to = non_reference (to);
+  from = non_reference (from);
+
+  if (TREE_CODE (to) == POINTER_TYPE && TREE_CODE (from) == POINTER_TYPE)
+    {
+      to = TREE_TYPE (to);
+      from = TREE_TYPE (from);
+
+      if (! at_least_as_qualified_p (to, from))
+       return 0;
+
+      if (TREE_CODE (to) == VOID_TYPE)
+       return 1;
+
+      /* Else fall through.  */
+    }
+
+  if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
+      && PUBLICLY_UNIQUELY_DERIVED_P (to, from))
+    return 1;
+
+  return 0;
+}
+
+/* Check whether any of the handlers in I are shadowed by another handler
+   accepting TYPE.  Note that the shadowing may not be complete; even if
+   an exception of type B would be caught by a handler for A, there could
+   be a derived class C for which A is an ambiguous base but B is not, so
+   the handler for B would catch an exception of type C.  */
+
+static void
+check_handlers_1 (tree master, tree_stmt_iterator i)
+{
+  tree type = TREE_TYPE (master);
+
+  for (; !tsi_end_p (i); tsi_next (&i))
+    {
+      tree handler = tsi_stmt (i);
+      if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
+       {
+         warning (0, "%Hexception of type %qT will be caught",
+                  EXPR_LOCUS (handler), TREE_TYPE (handler));
+         warning (0, "%H   by earlier handler for %qT",
+                  EXPR_LOCUS (master), type);
+         break;
+       }
+    }
+}
+
+/* Given a STATEMENT_LIST of HANDLERs, make sure that they're OK.  */
+
+void
+check_handlers (tree handlers)
+{
+  tree_stmt_iterator i;
+
+  /* If we don't have a STATEMENT_LIST, then we've just got one
+     handler, and thus nothing to warn about.  */
+  if (TREE_CODE (handlers) != STATEMENT_LIST)
+    return;
+
+  i = tsi_start (handlers);
+  if (!tsi_end_p (i))
+    while (1)
+      {
+       tree handler = tsi_stmt (i);
+       tsi_next (&i);
+
+       /* No more handlers; nothing to shadow.  */
+       if (tsi_end_p (i))
+         break;
+       if (TREE_TYPE (handler) == NULL_TREE)
+         pedwarn ("%H%<...%> handler must be the last handler for"
+                  " its try block", EXPR_LOCUS (handler));
+       else
+         check_handlers_1 (handler, i);
+      }
+}