builtin-attrs.def (ATTR_NORETURN_NOTHROW_LEAF_COLD_LIST, [...]): New.
[gcc.git] / gcc / cp / except.c
index 351e685a68702279d902e4d512ae069cec507d50..ab7ab5db1586d6b9303c096c61d84cb593440759 100644 (file)
@@ -1,7 +1,5 @@
 /* Handle exceptional things in C++.
-   Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
-   Free Software Foundation, Inc.
+   Copyright (C) 1989-2017 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.
@@ -26,28 +24,22 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tm.h"
-#include "tree.h"
 #include "cp-tree.h"
-#include "flags.h"
-#include "output.h"
-#include "toplev.h"
-#include "tree-inline.h"
+#include "stringpool.h"
+#include "trans-mem.h"
+#include "attribs.h"
 #include "tree-iterator.h"
-#include "target.h"
-#include "gimple.h"
 
 static void push_eh_cleanup (tree);
 static tree prepare_eh_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 bool is_admissible_throw_operand_or_catch_parameter (tree, bool);
 static int can_convert_eh (tree, tree);
 
 /* Sets up all the global eh stuff that needs to be initialized at the
@@ -61,14 +53,16 @@ init_exception_processing (void)
   /* void std::terminate (); */
   push_namespace (std_identifier);
   tmp = build_function_type_list (void_type_node, NULL_TREE);
-  terminate_node = build_cp_library_fn_ptr ("terminate", tmp);
-  TREE_THIS_VOLATILE (terminate_node) = 1;
-  TREE_NOTHROW (terminate_node) = 1;
+  terminate_fn = build_cp_library_fn_ptr ("terminate", tmp,
+                                          ECF_NOTHROW | ECF_NORETURN
+                                          | ECF_COLD);
+  gcc_checking_assert (TREE_THIS_VOLATILE (terminate_fn)
+                      && TREE_NOTHROW (terminate_fn));
   pop_namespace ();
 
   /* void __cxa_call_unexpected(void *); */
   tmp = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
-  call_unexpected_node
+  call_unexpected_fn
     = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
 }
 
@@ -82,7 +76,7 @@ cp_protect_cleanup_actions (void)
 
      When the destruction of an object during stack unwinding exits
      using an exception ... void terminate(); is called.  */
-  return terminate_node;
+  return terminate_fn;
 }
 
 static tree
@@ -109,17 +103,10 @@ prepare_eh_type (tree type)
 tree
 eh_type_info (tree type)
 {
-  tree exp;
-
   if (type == NULL_TREE || type == error_mark_node)
     return type;
 
-  if (decl_is_java_type (type, 0))
-    exp = build_java_class_ref (TREE_TYPE (type));
-  else
-    exp = get_tinfo_decl (type);
-
-  return exp;
+  return get_tinfo_decl (type);
 }
 
 /* Build the address of a typeinfo decl for use in the runtime
@@ -141,24 +128,43 @@ build_eh_type_type (tree type)
 tree
 build_exc_ptr (void)
 {
-  return build_call_n (built_in_decls [BUILT_IN_EH_POINTER],
+  return build_call_n (builtin_decl_explicit (BUILT_IN_EH_POINTER),
                       1, integer_zero_node);
 }
 
-/* Declare a function NAME, returning RETURN_TYPE, taking a single
-   parameter PARM_TYPE, with an empty exception specification.
+/* Find or declare a function NAME, returning RTYPE, taking a single
+   parameter PTYPE, with an empty exception specification. ECF are the
+   library fn flags.  If TM_ECF is non-zero, also find or create a
+   transaction variant and record it as a replacement, when flag_tm is
+   in effect.
 
    Note that the C++ ABI document does not have a throw-specifier on
    the routines declared below via this function.  The declarations
    are consistent with the actual implementations in libsupc++.  */
 
 static tree
-declare_nothrow_library_fn (tree name, tree return_type, tree parm_type)
+declare_library_fn (const char *name, tree rtype, tree ptype,
+                   int ecf, int tm_ecf)
 {
-  return push_library_fn (name, build_function_type_list (return_type,
-                                                         parm_type,
-                                                         NULL_TREE),
-                         empty_except_spec);
+  tree ident = get_identifier (name);
+  tree res = IDENTIFIER_GLOBAL_VALUE (ident);
+  if (!res)
+    {
+      tree type = build_function_type_list (rtype, ptype, NULL_TREE);
+      tree except = ecf & ECF_NOTHROW ? empty_except_spec : NULL_TREE;
+      res = push_library_fn (ident, type, except, ecf);
+      if (tm_ecf && flag_tm)
+       {
+         char *tm_name = concat ("_ITM_", name + 2, NULL_TREE);
+         tree tm_ident = get_identifier (tm_name);
+         free (tm_name);
+         tree tm_fn = IDENTIFIER_GLOBAL_VALUE (tm_ident);
+         if (!tm_fn)
+           tm_fn = push_library_fn (tm_ident, type, except, ecf | tm_ecf);
+         record_tm_replacement (res, tm_fn);
+       }
+    }
+  return res;
 }
 
 /* Build up a call to __cxa_get_exception_ptr so that we can build a
@@ -167,16 +173,16 @@ declare_nothrow_library_fn (tree name, tree return_type, tree parm_type)
 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 *) throw().  */
-      fn = declare_nothrow_library_fn (fn, ptr_type_node, ptr_type_node);
-    }
-
-  return cp_build_function_call_nary (fn, tf_warning_or_error,
+  if (!get_exception_ptr_fn)
+    /* Declare void* __cxa_get_exception_ptr (void *) throw().  */
+    get_exception_ptr_fn
+      = declare_library_fn ("__cxa_get_exception_ptr",
+                           ptr_type_node, ptr_type_node,
+                           ECF_NOTHROW | ECF_PURE | ECF_LEAF | ECF_TM_PURE,
+                           0);
+
+  return cp_build_function_call_nary (get_exception_ptr_fn,
+                                     tf_warning_or_error,
                                      build_exc_ptr (), NULL_TREE);
 }
 
@@ -186,16 +192,14 @@ do_get_exception_ptr (void)
 static tree
 do_begin_catch (void)
 {
-  tree fn;
-
-  fn = get_identifier ("__cxa_begin_catch");
-  if (!get_global_value_if_present (fn, &fn))
-    {
-      /* Declare void* __cxa_begin_catch (void *) throw().  */
-      fn = declare_nothrow_library_fn (fn, ptr_type_node, ptr_type_node);
-    }
-
-  return cp_build_function_call_nary (fn, tf_warning_or_error,
+  if (!begin_catch_fn)
+    /* Declare void* __cxa_begin_catch (void *) throw().  */
+    begin_catch_fn
+      = declare_library_fn ("__cxa_begin_catch",
+                           ptr_type_node, ptr_type_node, ECF_NOTHROW,
+                           ECF_TM_PURE);
+
+  return cp_build_function_call_nary (begin_catch_fn, tf_warning_or_error,
                                      build_exc_ptr (), NULL_TREE);
 }
 
@@ -223,18 +227,15 @@ dtor_nothrow (tree type)
 static tree
 do_end_catch (tree type)
 {
-  tree fn, cleanup;
-
-  fn = get_identifier ("__cxa_end_catch");
-  if (!get_global_value_if_present (fn, &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;
-    }
-
-  cleanup = cp_build_function_call_vec (fn, NULL, tf_warning_or_error);
+  if (!end_catch_fn)
+    /* Declare void __cxa_end_catch ().
+       This can throw if the destructor for the exception throws.  */
+    end_catch_fn
+      = declare_library_fn ("__cxa_end_catch", void_type_node,
+                           NULL_TREE, 0, ECF_TM_PURE);
+
+  tree cleanup = cp_build_function_call_vec (end_catch_fn,
+                                            NULL, tf_warning_or_error);
   TREE_NOTHROW (cleanup) = dtor_nothrow (type);
 
   return cleanup;
@@ -248,107 +249,35 @@ push_eh_cleanup (tree type)
   finish_decl_cleanup (NULL_TREE, do_end_catch (type));
 }
 
-/* Return nonzero value if DECL is a Java type suitable for catch or
-   throw.  */
-
-static bool
-decl_is_java_type (tree decl, int err)
-{
-  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));
+/* Wrap EXPR in a MUST_NOT_THROW_EXPR expressing that EXPR must
+   not throw any exceptions if COND is true.  A condition of
+   NULL_TREE is treated as 'true'.  */
 
-         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_decl 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)
+tree
+build_must_not_throw_expr (tree body, tree cond)
 {
-  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;
+  tree type = body ? TREE_TYPE (body) : void_type_node;
 
-    case chose_none:
-      ; /* Proceed to language selection.  */
-    }
+  if (!flag_exceptions)
+    return body;
 
-  switch (lang)
+  if (cond && !value_dependent_expression_p (cond))
     {
-    case lang_cplusplus:
-      state = chose_cpp;
-      break;
-
-    case lang_java:
-      state = chose_java;
-      terminate_node = built_in_decls [BUILT_IN_ABORT];
-      pragma_java_exceptions = true;
-      break;
-
-    default:
-      gcc_unreachable ();
+      cond = perform_implicit_conversion_flags (boolean_type_node, cond,
+                                               tf_warning_or_error,
+                                               LOOKUP_NORMAL);
+      cond = instantiate_non_dependent_expr (cond);
+      cond = cxx_constant_value (cond);
+      if (integer_zerop (cond))
+       return body;
+      else if (integer_onep (cond))
+       cond = NULL_TREE;
     }
-  return;
 
- give_error:
-  error ("mixing C++ and Java catches in a single translation unit");
-  state = gave_error;
+  return build2 (MUST_NOT_THROW_EXPR, type, body, cond);
 }
 
+
 /* Initialize the catch parameter DECL.  */
 
 static void
@@ -369,16 +298,14 @@ initialize_handler_parm (tree decl, tree exp)
   if (!POINTER_TYPE_P (init_type))
     init_type = build_reference_type (init_type);
 
-  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 temporary.  */
   if (TREE_CODE (init_type) == REFERENCE_TYPE
       && TYPE_PTR_P (TREE_TYPE (init_type)))
-    exp = cp_build_unary_op (ADDR_EXPR, exp, 1, tf_warning_or_error);
+    exp = cp_build_addr_expr (exp, tf_warning_or_error);
 
-  exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
+  exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0,
+                    tf_warning_or_error);
 
   init = convert_from_reference (exp);
 
@@ -389,11 +316,12 @@ initialize_handler_parm (tree decl, tree exp)
       /* Generate the copy constructor call directly so we can wrap it.
         See also expand_default_init.  */
       init = ocp_convert (TREE_TYPE (decl), init,
-                         CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
+                         CONV_IMPLICIT|CONV_FORCE_TEMP, 0,
+                         tf_warning_or_error);
       /* Force cleanups now to avoid nesting problems with the
         MUST_NOT_THROW_EXPR.  */
       init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
-      init = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (init), init);
+      init = build_must_not_throw_expr (init, NULL_TREE);
     }
 
   decl = pushdecl (decl);
@@ -438,30 +366,16 @@ expand_start_catch_block (tree decl)
   if (! doing_eh ())
     return NULL_TREE;
 
-  /* Make sure this declaration is reasonable.  */
-  if (decl && !complete_ptr_ref_or_void_ptr_p (TREE_TYPE (decl), NULL_TREE))
-    decl = error_mark_node;
-
   if (decl)
-    type = prepare_eh_type (TREE_TYPE (decl));
-  else
-    type = NULL_TREE;
-
-  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 (POINTER_PLUS_EXPR, TREE_TYPE (exp), exp,
-                   fold_build1_loc (input_location,
-                                NEGATE_EXPR, sizetype,
-                                TYPE_SIZE_UNIT (TREE_TYPE (exp))));
-      exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
-      initialize_handler_parm (decl, exp);
-      return type;
+      if (!is_admissible_throw_operand_or_catch_parameter (decl, false))
+       decl = error_mark_node;
+
+      type = prepare_eh_type (TREE_TYPE (decl));
+      mark_used (eh_type_info (type));
     }
+  else
+    type = NULL_TREE;
 
   /* Call __cxa_end_catch at the end of processing the exception.  */
   push_eh_cleanup (type);
@@ -497,9 +411,9 @@ expand_start_catch_block (tree decl)
       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);
+      DECL_REGISTER (exp) = 1;
       initialize_handler_parm (decl, exp);
     }
 
@@ -522,22 +436,29 @@ expand_end_catch_block (void)
   if (in_function_try_handler
       && (DECL_CONSTRUCTOR_P (current_function_decl)
          || DECL_DESTRUCTOR_P (current_function_decl)))
-    finish_expr_stmt (build_throw (NULL_TREE));
+    {
+      tree rethrow = build_throw (NULL_TREE);
+      TREE_NO_WARNING (rethrow) = true;
+      finish_expr_stmt (rethrow);
+    }
 }
 
 tree
 begin_eh_spec_block (void)
 {
   tree r;
+  location_t spec_location = DECL_SOURCE_LOCATION (current_function_decl);
+
   /* 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);
+      r = build_stmt (spec_location, MUST_NOT_THROW_EXPR,
+                     NULL_TREE, NULL_TREE);
       TREE_SIDE_EFFECTS (r) = 1;
     }
   else
-    r = build_stmt (input_location, EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
+    r = build_stmt (spec_location, EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
   add_stmt (r);
   TREE_OPERAND (r, 0) = push_stmt_list ();
   return r;
@@ -574,16 +495,15 @@ finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
 static tree
 do_allocate_exception (tree type)
 {
-  tree fn;
-
-  fn = get_identifier ("__cxa_allocate_exception");
-  if (!get_global_value_if_present (fn, &fn))
-    {
-      /* Declare void *__cxa_allocate_exception(size_t) throw().  */
-      fn = declare_nothrow_library_fn (fn, ptr_type_node, size_type_node);
-    }
-
-  return cp_build_function_call_nary (fn, tf_warning_or_error,
+  if (!allocate_exception_fn)
+    /* Declare void *__cxa_allocate_exception(size_t) throw().  */
+    allocate_exception_fn
+      = declare_library_fn ("__cxa_allocate_exception",
+                           ptr_type_node, size_type_node,
+                           ECF_NOTHROW | ECF_MALLOC, ECF_TM_PURE);
+
+  return cp_build_function_call_nary (allocate_exception_fn,
+                                     tf_warning_or_error,
                                      size_in_bytes (type), NULL_TREE);
 }
 
@@ -593,24 +513,22 @@ do_allocate_exception (tree type)
 static tree
 do_free_exception (tree ptr)
 {
-  tree fn;
-
-  fn = get_identifier ("__cxa_free_exception");
-  if (!get_global_value_if_present (fn, &fn))
-    {
-      /* Declare void __cxa_free_exception (void *) throw().  */
-      fn = declare_nothrow_library_fn (fn, void_type_node, ptr_type_node);
-    }
-
-  return cp_build_function_call_nary (fn, tf_warning_or_error, ptr, NULL_TREE);
+  if (!free_exception_fn)
+    /* Declare void __cxa_free_exception (void *) throw().  */
+    free_exception_fn
+      = declare_library_fn ("__cxa_free_exception",
+                           void_type_node, ptr_type_node,
+                           ECF_NOTHROW | ECF_LEAF, ECF_TM_PURE);
+
+  return cp_build_function_call_nary (free_exception_fn,
+                                     tf_warning_or_error, ptr, NULL_TREE);
 }
 
 /* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
    Called from build_throw via walk_tree_without_duplicates.  */
 
 static tree
-wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
-                void *data ATTRIBUTE_UNUSED)
+wrap_cleanups_r (tree *tp, int *walk_subtrees, void * /*data*/)
 {
   tree exp = *tp;
   tree cleanup;
@@ -627,7 +545,8 @@ wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
   cleanup = TARGET_EXPR_CLEANUP (exp);
   if (cleanup)
     {
-      cleanup = build1 (MUST_NOT_THROW_EXPR, void_type_node, cleanup);
+      cleanup = build2 (MUST_NOT_THROW_EXPR, void_type_node, cleanup,
+                       NULL_TREE);
       TARGET_EXPR_CLEANUP (exp) = cleanup;
     }
 
@@ -640,8 +559,6 @@ wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
 tree
 build_throw (tree exp)
 {
-  tree fn;
-
   if (exp == error_mark_node)
     return exp;
 
@@ -649,7 +566,9 @@ build_throw (tree exp)
     {
       if (cfun)
        current_function_returns_abnormally = 1;
-      return build_min (THROW_EXPR, void_type_node, exp);
+      exp = build_min (THROW_EXPR, void_type_node, exp);
+      SET_EXPR_LOCATION (exp, input_location);
+      return exp;
     }
 
   if (exp == null_node)
@@ -657,34 +576,14 @@ build_throw (tree exp)
 
   if (exp != NULL_TREE)
     {
-      if (!is_admissible_throw_operand (exp))
+      if (!is_admissible_throw_operand_or_catch_parameter (exp, true))
        return error_mark_node;
     }
 
   if (! doing_eh ())
     return error_mark_node;
 
-  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;
-         tmp = build_function_type_list (ptr_type_node,
-                                         ptr_type_node, NULL_TREE);
-         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 = cp_build_function_call_nary (fn, tf_warning_or_error,
-                                        exp, NULL_TREE);
-    }
-  else if (exp)
+  if (exp)
     {
       tree throw_type;
       tree temp_type;
@@ -701,15 +600,29 @@ build_throw (tree exp)
          cleanup_type = build_pointer_type (tmp);
        }
 
-      fn = get_identifier ("__cxa_throw");
-      if (!get_global_value_if_present (fn, &fn))
+      if (!throw_fn)
        {
-         /* Declare void __cxa_throw (void*, void*, void (*)(void*)).  */
-         /* ??? Second argument is supposed to be "std::type_info*".  */
-         tmp = build_function_type_list (void_type_node,
-                                         ptr_type_node, ptr_type_node,
-                                         cleanup_type, NULL_TREE);
-         fn = push_throw_library_fn (fn, tmp);
+         tree name = get_identifier ("__cxa_throw");
+         throw_fn = IDENTIFIER_GLOBAL_VALUE (name);
+         if (!throw_fn)
+           {
+             /* Declare void __cxa_throw (void*, void*, void (*)(void*)).  */
+             /* ??? Second argument is supposed to be "std::type_info*".  */
+             tmp = build_function_type_list (void_type_node,
+                                             ptr_type_node, ptr_type_node,
+                                             cleanup_type, NULL_TREE);
+             throw_fn = push_throw_library_fn (name, tmp);
+
+             if (flag_tm)
+               {
+                 tree itm_name = get_identifier ("_ITM_cxa_throw");
+                 tree itm_fn = IDENTIFIER_GLOBAL_VALUE (itm_name);
+                 if (!itm_fn)
+                   itm_fn = push_throw_library_fn (itm_name, tmp);
+                 apply_tm_attr (itm_fn, get_identifier ("transaction_pure"));
+                 record_tm_replacement (throw_fn, itm_fn);
+               }
+           }
        }
 
       /* [except.throw]
@@ -722,7 +635,7 @@ build_throw (tree exp)
         respectively.  */
       temp_type = is_bitfield_expr_with_lowered_type (exp);
       if (!temp_type)
-       temp_type = type_decays_to (TREE_TYPE (exp));
+       temp_type = cv_unqualified (type_decays_to (TREE_TYPE (exp)));
 
       /* OK, this is kind of wacky.  The standard says that we call
         terminate when the exception handling mechanism, after
@@ -751,13 +664,13 @@ build_throw (tree exp)
       if (CLASS_TYPE_P (temp_type))
        {
          int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
-         VEC(tree,gc) *exp_vec;
+         vec<tree, va_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
             to favor move constructors over copy constructors.  */
          if (/* Must be a local, automatic variable.  */
-             TREE_CODE (exp) == VAR_DECL
+             VAR_P (exp)
              && DECL_CONTEXT (exp) == current_function_decl
              && ! TREE_STATIC (exp)
              /* The variable must not have the `volatile' qualifier.  */
@@ -778,7 +691,7 @@ build_throw (tree exp)
        }
       else
        {
-         tmp = decay_conversion (exp);
+         tmp = decay_conversion (exp, tf_warning_or_error);
          if (tmp == error_mark_node)
            return error_mark_node;
          exp = build2 (INIT_EXPR, temp_type, object, tmp);
@@ -797,21 +710,25 @@ build_throw (tree exp)
 
       throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
 
-      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
+      cleanup = NULL_TREE;
+      if (type_build_dtor_call (TREE_TYPE (object)))
        {
-         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);
+         tree dtor_fn = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
+                                         complete_dtor_identifier, 0);
+         dtor_fn = BASELINK_FUNCTIONS (dtor_fn);
+         mark_used (dtor_fn);
+         if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
+           {
+             cxx_mark_addressable (dtor_fn);
+             /* Pretend it's a normal function.  */
+             cleanup = build1 (ADDR_EXPR, cleanup_type, dtor_fn);
+           }
        }
-      else
+      if (cleanup == NULL_TREE)
        cleanup = build_int_cst (cleanup_type, 0);
 
       /* ??? Indicate that this function call throws throw_type.  */
-      tmp = cp_build_function_call_nary (fn, tf_warning_or_error,
+      tmp = cp_build_function_call_nary (throw_fn, tf_warning_or_error,
                                         ptr, throw_type, cleanup, NULL_TREE);
 
       /* Tack on the initialization stuff.  */
@@ -820,21 +737,26 @@ build_throw (tree exp)
   else
     {
       /* Rethrow current exception.  */
-
-      tree fn = get_identifier ("__cxa_rethrow");
-      if (!get_global_value_if_present (fn, &fn))
+      if (!rethrow_fn)
        {
-         /* Declare void __cxa_rethrow (void).  */
-         fn = push_throw_library_fn
-           (fn, build_function_type_list (void_type_node, NULL_TREE));
+         tree name = get_identifier ("__cxa_rethrow");
+         rethrow_fn = IDENTIFIER_GLOBAL_VALUE (name);
+         if (!rethrow_fn)
+           /* Declare void __cxa_rethrow (void).  */
+           rethrow_fn = push_throw_library_fn
+             (name, build_function_type_list (void_type_node, NULL_TREE));
+
+         if (flag_tm)
+           apply_tm_attr (rethrow_fn, get_identifier ("transaction_pure"));
        }
 
       /* ??? Indicate that this function call allows exceptions of the type
         of the enclosing catch block (if known).  */
-      exp = cp_build_function_call_vec (fn, NULL, tf_warning_or_error);
+      exp = cp_build_function_call_vec (rethrow_fn, NULL, tf_warning_or_error);
     }
 
   exp = build1 (THROW_EXPR, void_type_node, exp);
+  SET_EXPR_LOCATION (exp, input_location);
 
   return exp;
 }
@@ -855,7 +777,7 @@ complete_ptr_ref_or_void_ptr_p (tree type, tree from)
     return 0;
 
   /* Or a pointer or ref to one, or cv void *.  */
-  is_ptr = TREE_CODE (type) == POINTER_TYPE;
+  is_ptr = TYPE_PTR_P (type);
   if (is_ptr || TREE_CODE (type) == REFERENCE_TYPE)
     {
       tree core = TREE_TYPE (type);
@@ -868,14 +790,21 @@ complete_ptr_ref_or_void_ptr_p (tree type, tree from)
   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.  */
+/* If IS_THROW is true return truth-value if T is an expression 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.
+   If IS_THROW is false, likewise for a catch parameter, same requirements
+   for its type plus rvalue reference type is also not admissible.  */
 
 static bool
-is_admissible_throw_operand (tree expr)
+is_admissible_throw_operand_or_catch_parameter (tree t, bool is_throw)
 {
-  tree type = TREE_TYPE (expr);
+  tree expr = is_throw ? t : NULL_TREE;
+  tree type = TREE_TYPE (t);
+
+  /* C++11 [except.handle] The exception-declaration shall not denote
+     an incomplete type, an abstract class type, or an rvalue reference 
+     type.  */
 
   /* 15.1/4 [...] The type of the throw-expression shall not be an
            incomplete type, or a pointer or a reference to an incomplete
@@ -890,10 +819,24 @@ is_admissible_throw_operand (tree expr)
   /* 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))
+  else if (abstract_virtuals_error (is_throw ? ACU_THROW : ACU_CATCH, type))
+    return false;
+  else if (!is_throw
+          && TREE_CODE (type) == REFERENCE_TYPE
+          && TYPE_REF_IS_RVALUE (type))
     {
-      error ("expression %qE of abstract class type %qT cannot "
-            "be used in throw-expression", expr, type);
+      error ("cannot declare catch parameter to be of rvalue "
+            "reference type %qT", type);
+      return false;
+    }
+  else if (variably_modified_type_p (type, NULL_TREE))
+    {
+      if (is_throw)
+       error ("cannot throw expression of type %qT because it involves "
+              "types of variable size", type);
+      else
+       error ("cannot catch type %qT because it involves types of "
+              "variable size", type);
       return false;
     }
 
@@ -928,7 +871,17 @@ nothrow_libfn_p (const_tree fn)
      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));
+  const struct libc_name_struct *s
+    = libc_name::libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
+  if (s == NULL)
+    return 0;
+  switch (s->c_ver)
+    {
+    case 89: return 1;
+    case 99: return !flag_iso || flag_isoc99;
+    case 11: return !flag_iso || flag_isoc11;
+    default: gcc_unreachable ();
+    }
 }
 
 /* Returns nonzero if an exception of type FROM will be caught by a
@@ -940,7 +893,7 @@ 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)
+  if (TYPE_PTR_P (to) && TYPE_PTR_P (from))
     {
       to = TREE_TYPE (to);
       from = TREE_TYPE (from);
@@ -948,14 +901,14 @@ can_convert_eh (tree to, tree from)
       if (! at_least_as_qualified_p (to, from))
        return 0;
 
-      if (TREE_CODE (to) == VOID_TYPE)
+      if (VOID_TYPE_P (to))
        return 1;
 
       /* Else fall through.  */
     }
 
   if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
-      && PUBLICLY_UNIQUELY_DERIVED_P (to, from))
+      && publicly_uniquely_derived_p (to, from))
     return 1;
 
   return 0;
@@ -1032,12 +985,11 @@ check_handlers (tree handlers)
      expression whose type is a polymorphic class type (10.3).  */
 
 static tree
-check_noexcept_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
-                 void *data ATTRIBUTE_UNUSED)
+check_noexcept_r (tree *tp, int * /*walk_subtrees*/, void * /*data*/)
 {
   tree t = *tp;
   enum tree_code code = TREE_CODE (t);
-  if (code == CALL_EXPR
+  if ((code == CALL_EXPR && CALL_EXPR_FN (t))
       || code == AGGR_INIT_EXPR)
     {
       /* We can only use the exception specification of the called function
@@ -1046,21 +998,27 @@ check_noexcept_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
         translation unit, creating ODR problems.
 
          We could use TREE_NOTHROW (t) for !TREE_PUBLIC fns, though... */
-      tree fn = (code == AGGR_INIT_EXPR
-                ? AGGR_INIT_EXPR_FN (t) : CALL_EXPR_FN (t));
-      tree type = TREE_TYPE (TREE_TYPE (fn));
+      tree fn = cp_get_callee (t);
+      tree type = TREE_TYPE (fn);
+      gcc_assert (POINTER_TYPE_P (type));
+      type = TREE_TYPE (type);
 
       STRIP_NOPS (fn);
       if (TREE_CODE (fn) == ADDR_EXPR)
+       fn = TREE_OPERAND (fn, 0);
+      if (TREE_CODE (fn) == FUNCTION_DECL)
        {
          /* We do use TREE_NOTHROW for ABI internals like __dynamic_cast,
             and for C library functions known not to throw.  */
-         fn = TREE_OPERAND (fn, 0);
-         if (TREE_CODE (fn) == FUNCTION_DECL
-             && DECL_EXTERN_C_P (fn)
+         if (DECL_EXTERN_C_P (fn)
              && (DECL_ARTIFICIAL (fn)
                  || nothrow_libfn_p (fn)))
            return TREE_NOTHROW (fn) ? NULL_TREE : fn;
+         /* A call to a constexpr function is noexcept if the call
+            is a constant expression.  */
+         if (DECL_DECLARED_CONSTEXPR_P (fn)
+             && is_sub_constant_expr (t))
+           return NULL_TREE;
        }
       if (!TYPE_NOTHROW_P (type))
        return fn;
@@ -1072,13 +1030,11 @@ check_noexcept_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
 /* If a function that causes a noexcept-expression to be false isn't
    defined yet, remember it and check it for TREE_NOTHROW again at EOF.  */
 
-typedef struct GTY(()) pending_noexcept {
+struct GTY(()) pending_noexcept {
   tree fn;
   location_t loc;
-} pending_noexcept;
-DEF_VEC_O(pending_noexcept);
-DEF_VEC_ALLOC_O(pending_noexcept,gc);
-static GTY(()) VEC(pending_noexcept,gc) *pending_noexcept_checks;
+};
+static GTY(()) vec<pending_noexcept, va_gc> *pending_noexcept_checks;
 
 /* FN is a FUNCTION_DECL that caused a noexcept-expr to be false.  Warn if
    it can't throw.  */
@@ -1090,8 +1046,9 @@ maybe_noexcept_warning (tree fn)
     {
       warning (OPT_Wnoexcept, "noexcept-expression evaluates to %<false%> "
               "because of a call to %qD", fn);
-      warning (OPT_Wnoexcept, "but %q+D does not throw; perhaps "
-              "it should be declared %<noexcept%>", fn);
+      warning_at (DECL_SOURCE_LOCATION (fn), OPT_Wnoexcept,
+                 "but %qD does not throw; perhaps "
+                 "it should be declared %<noexcept%>", fn);
     }
 }
 
@@ -1104,7 +1061,7 @@ perform_deferred_noexcept_checks (void)
   int i;
   pending_noexcept *p;
   location_t saved_loc = input_location;
-  FOR_EACH_VEC_ELT (pending_noexcept, pending_noexcept_checks, i, p)
+  FOR_EACH_VEC_SAFE_ELT (pending_noexcept_checks, i, p)
     {
       input_location = p->loc;
       maybe_noexcept_warning (p->fn);
@@ -1117,11 +1074,27 @@ perform_deferred_noexcept_checks (void)
 tree
 finish_noexcept_expr (tree expr, tsubst_flags_t complain)
 {
-  tree fn;
+  if (expr == error_mark_node)
+    return error_mark_node;
 
   if (processing_template_decl)
     return build_min (NOEXCEPT_EXPR, boolean_type_node, expr);
 
+  return (expr_noexcept_p (expr, complain)
+         ? boolean_true_node : boolean_false_node);
+}
+
+/* Returns whether EXPR is noexcept, possibly warning if allowed by
+   COMPLAIN.  */
+
+bool
+expr_noexcept_p (tree expr, tsubst_flags_t complain)
+{
+  tree fn;
+
+  if (expr == error_mark_node)
+    return false;
+
   fn = cp_walk_tree_without_duplicates (&expr, check_noexcept_r, 0);
   if (fn)
     {
@@ -1131,19 +1104,16 @@ finish_noexcept_expr (tree expr, tsubst_flags_t complain)
          if (!DECL_INITIAL (fn))
            {
              /* Not defined yet; check again at EOF.  */
-             pending_noexcept *p
-               = VEC_safe_push (pending_noexcept, gc,
-                                pending_noexcept_checks, NULL);
-             p->fn = fn;
-             p->loc = input_location;
+             pending_noexcept p = {fn, input_location};
+             vec_safe_push (pending_noexcept_checks, p);
            }
          else
            maybe_noexcept_warning (fn);
        }
-      return boolean_false_node;
+      return false;
     }
   else
-    return boolean_true_node;
+    return true;
 }
 
 /* Return true iff SPEC is throw() or noexcept(true).  */
@@ -1151,15 +1121,18 @@ finish_noexcept_expr (tree expr, tsubst_flags_t complain)
 bool
 nothrow_spec_p (const_tree spec)
 {
-  if (spec == NULL_TREE
-      || TREE_VALUE (spec) != NULL_TREE
-      || spec == noexcept_false_spec)
-    return false;
-  if (TREE_PURPOSE (spec) == NULL_TREE
+  gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
+
+  if (spec == empty_except_spec
       || spec == noexcept_true_spec)
     return true;
-  gcc_assert (processing_template_decl
-             || TREE_PURPOSE (spec) == error_mark_node);
+
+  gcc_assert (!spec
+             || TREE_VALUE (spec)
+             || spec == noexcept_false_spec
+             || TREE_PURPOSE (spec) == error_mark_node
+             || processing_template_decl);
+
   return false;
 }
 
@@ -1171,6 +1144,7 @@ bool
 type_noexcept_p (const_tree type)
 {
   tree spec = TYPE_RAISES_EXCEPTIONS (type);
+  gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
   if (flag_nothrow_opt)
     return nothrow_spec_p (spec);
   else
@@ -1184,6 +1158,7 @@ bool
 type_throw_all_p (const_tree type)
 {
   tree spec = TYPE_RAISES_EXCEPTIONS (type);
+  gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
   return spec == NULL_TREE || spec == noexcept_false_spec;
 }
 
@@ -1193,16 +1168,63 @@ type_throw_all_p (const_tree type)
 tree
 build_noexcept_spec (tree expr, int complain)
 {
-  expr = perform_implicit_conversion_flags (boolean_type_node, expr,
-                                           complain,
-                                           LOOKUP_NORMAL);
-  if (expr == boolean_true_node)
-    return noexcept_true_spec;
-  else if (expr == boolean_false_node)
-    return noexcept_false_spec;
+  /* This isn't part of the signature, so don't bother trying to evaluate
+     it until instantiation.  */
+  if (!processing_template_decl && TREE_CODE (expr) != DEFERRED_NOEXCEPT)
+    {
+      expr = perform_implicit_conversion_flags (boolean_type_node, expr,
+                                               complain,
+                                               LOOKUP_NORMAL);
+      expr = cxx_constant_value (expr);
+    }
+  if (TREE_CODE (expr) == INTEGER_CST)
+    {
+      if (operand_equal_p (expr, boolean_true_node, 0))
+       return noexcept_true_spec;
+      else
+       {
+         gcc_checking_assert (operand_equal_p (expr, boolean_false_node, 0));
+         return noexcept_false_spec;
+       }
+    }
+  else if (expr == error_mark_node)
+    return error_mark_node;
   else
     {
-      gcc_assert (processing_template_decl || expr == error_mark_node);
+      gcc_assert (processing_template_decl
+                 || TREE_CODE (expr) == DEFERRED_NOEXCEPT);
       return build_tree_list (expr, NULL_TREE);
     }
 }
+
+/* Returns a noexcept-specifier to be evaluated later, for an
+   implicitly-declared or explicitly defaulted special member function.  */
+
+tree
+unevaluated_noexcept_spec (void)
+{
+  if (!noexcept_deferred_spec)
+    noexcept_deferred_spec
+      = build_noexcept_spec (make_node (DEFERRED_NOEXCEPT), tf_none);
+  return noexcept_deferred_spec;
+}
+
+/* Returns a TRY_CATCH_EXPR that will put TRY_LIST and CATCH_LIST in the
+   TRY and CATCH locations.  CATCH_LIST must be a STATEMENT_LIST */
+
+tree
+create_try_catch_expr (tree try_expr, tree catch_list)
+{
+  location_t loc = EXPR_LOCATION (try_expr);
+  append_to_statement_list (do_begin_catch (), &catch_list);
+  append_to_statement_list (build_throw (NULL_TREE), &catch_list);
+  tree catch_tf_expr = build_stmt (loc, TRY_FINALLY_EXPR, catch_list, 
+                                  do_end_catch (NULL_TREE));
+  catch_list = build2 (CATCH_EXPR, void_type_node, NULL_TREE,
+                      catch_tf_expr);
+  tree try_catch_expr = build_stmt (loc, TRY_CATCH_EXPR, try_expr, catch_list);
+  return try_catch_expr;
+}
+
+#include "gt-cp-except.h"