OSDN Git Service

PR c++/41468
[pf3gnuchains/gcc-fork.git] / gcc / cp / except.c
index 7813d08..c0867ef 100644 (file)
@@ -1,6 +1,6 @@
 /* Handle exceptional things in C++.
    Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
+   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
    Contributed by Michael Tiemann <tiemann@cygnus.com>
    Rewritten by Mike Stump <mrs@cygnus.com>, based upon an
@@ -43,7 +43,6 @@ along with GCC; see the file COPYING3.  If not see
 
 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);
@@ -54,7 +53,7 @@ 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 gimple cp_protect_cleanup_actions (void);
+static tree cp_protect_cleanup_actions (void);
 
 /* Sets up all the global eh stuff that needs to be initialized at the
    start of compilation.  */
@@ -78,29 +77,20 @@ init_exception_processing (void)
   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 ();
-
-  lang_eh_runtime_type = build_eh_type_type;
   lang_protect_cleanup_actions = &cp_protect_cleanup_actions;
 }
 
 /* Returns an expression to be executed if an unhandled exception is
    propagated out of a cleanup region.  */
 
-static gimple
+static tree
 cp_protect_cleanup_actions (void)
 {
   /* [except.terminate]
 
      When the destruction of an object during stack unwinding exits
      using an exception ... void terminate(); is called.  */
-  return gimple_build_call (terminate_node, 0);
+  return terminate_node;
 }
 
 static tree
@@ -143,7 +133,7 @@ eh_type_info (tree type)
 /* Build the address of a typeinfo decl for use in the runtime
    matching field of the exception model.  */
 
-static tree
+tree
 build_eh_type_type (tree type)
 {
   tree exp = eh_type_info (type);
@@ -159,7 +149,8 @@ build_eh_type_type (tree type)
 tree
 build_exc_ptr (void)
 {
-  return build0 (EXC_PTR_EXPR, ptr_type_node);
+  return build_call_n (built_in_decls [BUILT_IN_EH_POINTER],
+                      1, integer_zero_node);
 }
 
 /* Declare a function NAME, returning RETURN_TYPE, taking a single
@@ -223,10 +214,10 @@ do_begin_catch (void)
 static int
 dtor_nothrow (tree type)
 {
-  if (type == NULL_TREE)
+  if (type == NULL_TREE || type == error_mark_node)
     return 0;
 
-  if (!CLASS_TYPE_P (type))
+  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
     return 1;
 
   if (CLASSTYPE_LAZY_DESTRUCTOR (type))
@@ -313,7 +304,7 @@ decl_is_java_type (tree decl, int err)
 /* 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
+   ??? At present eh_personality_decl is set to
    __gxx_personality_(sj|v)0 in init_exception_processing - should it
    be done here instead?  */
 void
@@ -353,9 +344,8 @@ choose_personality_routine (enum languages lang)
 
     case lang_java:
       state = chose_java;
-      eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
-                                                ? "__gcj_personality_sj0"
-                                                : "__gcj_personality_v0");
+      terminate_node = built_in_decls [BUILT_IN_ABORT];
+      pragma_java_exceptions = true;
       break;
 
     default:
@@ -379,6 +369,7 @@ initialize_handler_parm (tree decl, tree exp)
   /* Make sure we mark the catch param as used, otherwise we'll get a
      warning about an unused ((anonymous)).  */
   TREE_USED (decl) = 1;
+  DECL_READ_P (decl) = 1;
 
   /* Figure out the type that the initializer is.  Pointers are returned
      adjusted by value from __cxa_begin_catch.  Others are returned by
@@ -427,7 +418,7 @@ tree
 expand_start_catch_block (tree decl)
 {
   tree exp;
-  tree type;
+  tree type, init;
 
   if (! doing_eh (1))
     return NULL_TREE;
@@ -449,9 +440,10 @@ expand_start_catch_block (tree decl)
       exp = build_exc_ptr ();
       exp = build1 (NOP_EXPR, build_pointer_type (type), exp);
       exp = build2 (POINTER_PLUS_EXPR, TREE_TYPE (exp), exp,
-                   fold_build1 (NEGATE_EXPR, sizetype,
+                   fold_build1_loc (input_location,
+                                NEGATE_EXPR, sizetype,
                                 TYPE_SIZE_UNIT (TREE_TYPE (exp))));
-      exp = cp_build_indirect_ref (exp, NULL, tf_warning_or_error);
+      exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
       initialize_handler_parm (decl, exp);
       return type;
     }
@@ -459,10 +451,12 @@ expand_start_catch_block (tree decl)
   /* Call __cxa_end_catch at the end of processing the exception.  */
   push_eh_cleanup (type);
 
+  init = do_begin_catch ();
+
   /* 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 ());
+  if (decl == NULL || decl == error_mark_node || init == error_mark_node)
+    finish_expr_stmt (init);
 
   /* If the C++ object needs constructing, we need to do that before
      calling __cxa_begin_catch, so that std::uncaught_exception gets
@@ -472,7 +466,7 @@ expand_start_catch_block (tree decl)
     {
       exp = do_get_exception_ptr ();
       initialize_handler_parm (decl, exp);
-      finish_expr_stmt (do_begin_catch ());
+      finish_expr_stmt (init);
     }
 
   /* Otherwise the type uses a bitwise copy, and we don't have to worry
@@ -480,7 +474,6 @@ expand_start_catch_block (tree decl)
      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.  */
@@ -520,9 +513,18 @@ expand_end_catch_block (void)
 tree
 begin_eh_spec_block (void)
 {
-  tree r = build_stmt (EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
+  tree r;
+  /* A noexcept specification (or throw() with -fnothrow-opt) is a
+     MUST_NOT_THROW_EXPR.  */
+  if (TYPE_NOEXCEPT_P (TREE_TYPE (current_function_decl)))
+    {
+      r = build_stmt (input_location, MUST_NOT_THROW_EXPR, NULL_TREE);
+      TREE_SIDE_EFFECTS (r) = 1;
+    }
+  else
+    r = build_stmt (input_location, EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
   add_stmt (r);
-  EH_SPEC_STMTS (r) = push_stmt_list ();
+  TREE_OPERAND (r, 0) = push_stmt_list ();
   return r;
 }
 
@@ -531,7 +533,11 @@ finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
 {
   tree raises;
 
-  EH_SPEC_STMTS (eh_spec_block) = pop_stmt_list (EH_SPEC_STMTS (eh_spec_block));
+  TREE_OPERAND (eh_spec_block, 0)
+    = pop_stmt_list (TREE_OPERAND (eh_spec_block, 0));
+
+  if (TREE_CODE (eh_spec_block) == MUST_NOT_THROW_EXPR)
+    return;
 
   /* Strip cv quals, etc, from the specification types.  */
   for (raises = NULL_TREE;
@@ -672,8 +678,7 @@ build_throw (tree exp)
       tree cleanup;
       tree object, ptr;
       tree tmp;
-      tree temp_expr, allocate_expr;
-      bool elided;
+      tree allocate_expr;
 
       /* The CLEANUP_TYPE is the internal type of a destructor.  */
       if (!cleanup_type)
@@ -726,15 +731,17 @@ build_throw (tree exp)
       allocate_expr = do_allocate_exception (temp_type);
       allocate_expr = get_target_expr (allocate_expr);
       ptr = TARGET_EXPR_SLOT (allocate_expr);
-      object = build_nop (build_pointer_type (temp_type), ptr);
-      object = cp_build_indirect_ref (object, NULL, tf_warning_or_error);
+      TARGET_EXPR_CLEANUP (allocate_expr) = do_free_exception (ptr);
+      CLEANUP_EH_ONLY (allocate_expr) = 1;
 
-      elided = (TREE_CODE (exp) == TARGET_EXPR);
+      object = build_nop (build_pointer_type (temp_type), ptr);
+      object = cp_build_indirect_ref (object, RO_NULL, tf_warning_or_error);
 
       /* And initialize the exception object.  */
       if (CLASS_TYPE_P (temp_type))
        {
          int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
+         VEC(tree,gc) *exp_vec;
 
          /* Under C++0x [12.8/16 class.copy], a thrown lvalue is sometimes
             treated as an rvalue for the purposes of overload resolution
@@ -748,11 +755,11 @@ build_throw (tree exp)
            flags = flags | LOOKUP_PREFER_RVALUE;
 
          /* Call the copy constructor.  */
+         exp_vec = make_tree_vector_single (exp);
          exp = (build_special_member_call
-                (object, complete_ctor_identifier,
-                 build_tree_list (NULL_TREE, exp),
-                 TREE_TYPE (object),
-                 flags, tf_warning_or_error));
+                (object, complete_ctor_identifier, &exp_vec,
+                 TREE_TYPE (object), flags, tf_warning_or_error));
+         release_tree_vector (exp_vec);
          if (exp == error_mark_node)
            {
              error ("  in thrown expression");
@@ -760,57 +767,23 @@ build_throw (tree exp)
            }
        }
       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 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);
+       {
+         tmp = decay_conversion (exp);
+         if (tmp == error_mark_node)
+           return error_mark_node;
+         exp = build2 (INIT_EXPR, temp_type, object, tmp);
+       }
 
-      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);
+      /* Mark any cleanups from the initialization as MUST_NOT_THROW, since
+        they are run after the exception object is initialized.  */
+      cp_walk_tree_without_duplicates (&exp, wrap_cleanups_r, 0);
 
       /* Prepend the allocation.  */
       exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
-      if (temp_expr)
-       {
-         /* 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.  */
-         cp_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);
-       }
+
+      /* Force all the cleanups to be evaluated here so that we don't have
+        to do them during unwinding.  */
+      exp = build1 (CLEANUP_POINT_EXPR, void_type_node, exp);
 
       throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
 
@@ -996,10 +969,11 @@ 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 (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);
+         warning_at (EXPR_LOCATION (handler), 0,
+                     "exception of type %qT will be caught",
+                     TREE_TYPE (handler));
+         warning_at (EXPR_LOCATION (master), 0,
+                     "   by earlier handler for %qT", type);
          break;
        }
     }
@@ -1028,8 +1002,8 @@ check_handlers (tree handlers)
        if (tsi_end_p (i))
          break;
        if (TREE_TYPE (handler) == NULL_TREE)
-         permerror (input_location, "%H%<...%> handler must be the last handler for"
-                    " its try block", EXPR_LOCUS (handler));
+         permerror (EXPR_LOCATION (handler), "%<...%>"
+                    " handler must be the last handler for its try block");
        else
          check_handlers_1 (handler, i);
       }