OSDN Git Service

PR c++/28878
[pf3gnuchains/gcc-fork.git] / gcc / cp / except.c
index 4a0ccb9..f97ab06 100644 (file)
@@ -19,8 +19,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 
 #include "config.h"
@@ -38,6 +38,7 @@ Boston, MA 02111-1307, USA.  */
 #include "toplev.h"
 #include "tree-inline.h"
 #include "tree-iterator.h"
+#include "target.h"
 
 static void push_eh_cleanup (tree);
 static tree prepare_eh_type (tree);
@@ -79,6 +80,10 @@ init_exception_processing (void)
   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 ();
 
   lang_eh_runtime_type = build_eh_type_type;
   lang_protect_cleanup_actions = &cp_protect_cleanup_actions;
@@ -95,7 +100,7 @@ cp_protect_cleanup_actions (void)
      When the destruction of an object during stack unwinding exits
      using an exception ... void terminate(); is called.  */
   return build_call (terminate_node, NULL_TREE);
-}     
+}
 
 static tree
 prepare_eh_type (tree type)
@@ -153,6 +158,26 @@ build_exc_ptr (void)
   return build0 (EXC_PTR_EXPR, ptr_type_node);
 }
 
+/* Build up a call to __cxa_get_exception_ptr so that we can build a
+   copy constructor for the thrown object.  */
+
+static tree
+do_get_exception_ptr (void)
+{
+  tree fn;
+
+  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 build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
+                                            NULL_TREE));
+}
+
 /* Build up a call to __cxa_begin_catch, to tell the runtime that the
    exception has been handled.  */
 
@@ -240,7 +265,7 @@ decl_is_java_type (tree decl, int err)
        {
          /* Can't throw a reference.  */
          error ("type %qT is disallowed in Java %<throw%> or %<catch%>",
-                 decl);
+                decl);
        }
 
       if (r)
@@ -258,7 +283,7 @@ decl_is_java_type (tree decl, int err)
            {
              /* Thrown object must be a Throwable.  */
              error ("type %qT is not derived from %<java::lang::Throwable%>",
-                     TREE_TYPE (decl));
+                    TREE_TYPE (decl));
            }
        }
     }
@@ -326,7 +351,7 @@ choose_personality_routine (enum languages lang)
 
 /* Initialize the catch parameter DECL.  */
 
-static void 
+static void
 initialize_handler_parm (tree decl, tree exp)
 {
   tree init;
@@ -337,7 +362,7 @@ initialize_handler_parm (tree decl, tree exp)
   TREE_USED (decl) = 1;
 
   /* Figure out the type that the initializer is.  Pointers are returned
-     adjusted by value from __cxa_begin_catch.  Others are returned by 
+     adjusted by value from __cxa_begin_catch.  Others are returned by
      reference.  */
   init_type = TREE_TYPE (decl);
   if (!POINTER_TYPE_P (init_type))
@@ -367,12 +392,10 @@ initialize_handler_parm (tree decl, tree exp)
       init = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (init), init);
     }
 
-  /* Let `cp_finish_decl' know that this initializer is ok.  */
-  DECL_INITIAL (decl) = error_mark_node;
   decl = pushdecl (decl);
 
-  start_decl_1 (decl);
-  cp_finish_decl (decl, init, NULL_TREE,
+  start_decl_1 (decl, true);
+  cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
                  LOOKUP_ONLYCONVERTING|DIRECT_BIND);
 }
 
@@ -381,61 +404,73 @@ initialize_handler_parm (tree decl, tree exp)
 tree
 expand_start_catch_block (tree decl)
 {
-  tree exp = NULL_TREE;
+  tree exp;
   tree type;
-  bool is_java;
 
   if (! doing_eh (1))
     return NULL_TREE;
 
   /* Make sure this declaration is reasonable.  */
   if (decl && !complete_ptr_ref_or_void_ptr_p (TREE_TYPE (decl), NULL_TREE))
-    decl = NULL_TREE;
+    decl = error_mark_node;
 
   if (decl)
     type = prepare_eh_type (TREE_TYPE (decl));
   else
     type = NULL_TREE;
 
-  is_java = false;
-  if (decl)
+  if (decl && decl_is_java_type (type, 1))
     {
-      tree init;
-
-      if (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.  */
-         init = build_exc_ptr ();
-         init = build1 (NOP_EXPR, build_pointer_type (type), init);
-         init = build2 (MINUS_EXPR, TREE_TYPE (init), init,
-                        TYPE_SIZE_UNIT (TREE_TYPE (init)));
-         init = build_indirect_ref (init, NULL);
-         is_java = true;
-       }
-      else
-       {
-         /* C++ requires that we call __cxa_begin_catch to get the
-            pointer to the actual object.  */
-         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));
+      /* 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;
     }
-  else
+
+  /* Call __cxa_end_catch at the end of processing the exception.  */
+  push_eh_cleanup (type);
+
+  /* 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 || decl == error_mark_node)
     finish_expr_stmt (do_begin_catch ());
 
-  /* C++ requires that we call __cxa_end_catch at the end of
-     processing the exception.  */
-  if (! is_java)
-    push_eh_cleanup (type);
+  /* 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 (flag_use_cxa_get_exception_ptr
+          && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
+    {
+      exp = do_get_exception_ptr ();
+      initialize_handler_parm (decl, exp);
+      finish_expr_stmt (do_begin_catch ());
+    }
 
-  if (decl)
-    initialize_handler_parm (decl, exp);
+  /* 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 ();
+      tree init_type = type;
+
+      /* Pointers are passed by values, everything else by reference.  */
+      if (!TYPE_PTR_P (type))
+       init_type = build_pointer_type (type);
+      if (init_type != TREE_TYPE (init))
+       init = build1 (NOP_EXPR, init_type, init);
+      exp = create_temporary_var (init_type);
+      DECL_REGISTER (exp) = 1;
+      cp_finish_decl (exp, init, /*init_const_expr=*/false,
+                     NULL_TREE, LOOKUP_ONLYCONVERTING);
+      initialize_handler_parm (decl, exp);
+    }
 
   return type;
 }
@@ -504,7 +539,7 @@ do_allocate_exception (tree type)
       tree tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
       fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp));
     }
-  
+
   return build_function_call (fn, tree_cons (NULL_TREE, size_in_bytes (type),
                                             NULL_TREE));
 }
@@ -533,7 +568,7 @@ do_free_exception (tree ptr)
 
 static tree
 wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
-                 void *data ATTRIBUTE_UNUSED)
+                void *data ATTRIBUTE_UNUSED)
 {
   tree exp = *tp;
   tree cleanup;
@@ -570,17 +605,18 @@ build_throw (tree exp)
 
   if (processing_template_decl)
     {
-      current_function_returns_abnormally = 1;
+      if (cfun)
+       current_function_returns_abnormally = 1;
       return build_min (THROW_EXPR, void_type_node, exp);
     }
 
   if (exp == null_node)
-    warning ("throwing NULL, which has integral, not pointer type");
-  
+    warning (0, "throwing NULL, which has integral, not pointer type");
+
   if (exp != NULL_TREE)
     {
       if (!is_admissible_throw_operand (exp))
-        return error_mark_node;
+       return error_mark_node;
     }
 
   if (! doing_eh (1))
@@ -607,6 +643,7 @@ build_throw (tree exp)
   else if (exp)
     {
       tree throw_type;
+      tree temp_type;
       tree cleanup;
       tree object, ptr;
       tree tmp;
@@ -621,7 +658,7 @@ build_throw (tree exp)
          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))
        {
@@ -634,10 +671,18 @@ build_throw (tree exp)
          tmp = build_function_type (void_type_node, tmp);
          fn = push_throw_library_fn (fn, tmp);
        }
-      
-      /* throw expression */
-      /* First, decay it.  */
-      exp = decay_conversion (exp);
+
+      /* [except.throw]
+
+        A throw-expression initializes a temporary object, the type
+        of which is determined by removing any top-level
+        cv-qualifiers from the static type of the operand of throw
+        and adjusting the type from "array of T" or "function return
+        T" to "pointer to T" or "pointer to function returning T"
+        respectively.  */
+      temp_type = is_bitfield_expr_with_lowered_type (exp);
+      if (!temp_type)
+       temp_type = type_decays_to (TYPE_MAIN_VARIANT (TREE_TYPE (exp)));
 
       /* OK, this is kind of wacky.  The standard says that we call
         terminate when the exception handling mechanism, after
@@ -653,43 +698,62 @@ build_throw (tree exp)
         matter, since it can't throw).  */
 
       /* Allocate the space for the exception.  */
-      allocate_expr = do_allocate_exception (TREE_TYPE (exp));
+      allocate_expr = do_allocate_exception (temp_type);
       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_nop (build_pointer_type (temp_type), 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)
+      if (CLASS_TYPE_P (temp_type))
        {
-         error ("  in thrown expression");
-         return error_mark_node;
+         /* Call the copy constructor.  */
+         exp = (build_special_member_call
+                (object, complete_ctor_identifier,
+                 build_tree_list (NULL_TREE, exp),
+                 TREE_TYPE (object),
+                 LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING));
+         if (exp == error_mark_node)
+           {
+             error ("  in thrown expression");
+             return error_mark_node;
+           }
        }
+      else
+       exp = build2 (INIT_EXPR, temp_type, object,
+                     decay_conversion (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 call to a constructor 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.
-
-         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.  */
+        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);
 
+      /* Wrap the initialization in a CLEANUP_POINT_EXPR so that cleanups
+        for temporaries within the initialization are run before the one
+        for the exception object, preserving LIFO order.  */
+      exp = build1 (CLEANUP_POINT_EXPR, void_type_node, exp);
+
       if (elided)
        exp = build2 (TRY_CATCH_EXPR, void_type_node, exp,
                      do_free_exception (ptr));
@@ -724,7 +788,7 @@ build_throw (tree exp)
        }
       else
        cleanup = build_int_cst (cleanup_type, 0);
-       
+
       tmp = tree_cons (NULL_TREE, cleanup, NULL_TREE);
       tmp = tree_cons (NULL_TREE, throw_type, tmp);
       tmp = tree_cons (NULL_TREE, ptr, tmp);
@@ -747,7 +811,7 @@ build_throw (tree exp)
        }
 
       /* ??? Indicate that this function call allows exceptions of the type
-        of the enclosing catch block (if known).  */    
+        of the enclosing catch block (if known).  */
       exp = build_function_call (fn, NULL_TREE);
     }
 
@@ -765,22 +829,22 @@ static int
 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 && VOID_TYPE_P (core))
-        /* OK */;
+       /* OK */;
       else if (!complete_type_or_else (core, from))
-        return 0;
+       return 0;
     }
   return 1;
 }
@@ -795,22 +859,22 @@ 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.  */
+           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.  */
+           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);
+            "be used in throw-expression", expr, type);
       return false;
     }
 
@@ -894,12 +958,12 @@ check_handlers_1 (tree master, tree_stmt_iterator i)
       tree handler = tsi_stmt (i);
       if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
        {
-         warning ("%Hexception of type %qT will be caught",
+         warning (0, "%Hexception of type %qT will be caught",
                   EXPR_LOCUS (handler), TREE_TYPE (handler));
-         warning ("%H   by earlier handler for %qT",
+         warning (0, "%H   by earlier handler for %qT",
                   EXPR_LOCUS (master), type);
          break;
-        }
+       }
     }
 }
 
@@ -919,7 +983,7 @@ check_handlers (tree handlers)
   if (!tsi_end_p (i))
     while (1)
       {
-        tree handler = tsi_stmt (i);
+       tree handler = tsi_stmt (i);
        tsi_next (&i);
 
        /* No more handlers; nothing to shadow.  */