OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc1/] [gcc/] [cp/] [except.c] - Diff between revs 283 and 338

Only display areas with differences | Details | Blame | View Log

Rev 283 Rev 338
/* Handle exceptional things in C++.
/* Handle exceptional things in C++.
   Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
   Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
   Contributed by Michael Tiemann <tiemann@cygnus.com>
   Contributed by Michael Tiemann <tiemann@cygnus.com>
   Rewritten by Mike Stump <mrs@cygnus.com>, based upon an
   Rewritten by Mike Stump <mrs@cygnus.com>, based upon an
   initial re-implementation courtesy Tad Hunt.
   initial re-implementation courtesy Tad Hunt.
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC 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
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
the Free Software Foundation; either version 3, or (at your option)
any later version.
any later version.
 
 
GCC 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
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
<http://www.gnu.org/licenses/>.  */
 
 
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
#include "coretypes.h"
#include "coretypes.h"
#include "tm.h"
#include "tm.h"
#include "tree.h"
#include "tree.h"
#include "rtl.h"
#include "rtl.h"
#include "expr.h"
#include "expr.h"
#include "libfuncs.h"
#include "libfuncs.h"
#include "cp-tree.h"
#include "cp-tree.h"
#include "flags.h"
#include "flags.h"
#include "output.h"
#include "output.h"
#include "except.h"
#include "except.h"
#include "toplev.h"
#include "toplev.h"
#include "tree-inline.h"
#include "tree-inline.h"
#include "tree-iterator.h"
#include "tree-iterator.h"
#include "target.h"
#include "target.h"
#include "gimple.h"
#include "gimple.h"
 
 
static void push_eh_cleanup (tree);
static void push_eh_cleanup (tree);
static tree prepare_eh_type (tree);
static tree prepare_eh_type (tree);
static tree do_begin_catch (void);
static tree do_begin_catch (void);
static int dtor_nothrow (tree);
static int dtor_nothrow (tree);
static tree do_end_catch (tree);
static tree do_end_catch (tree);
static bool decl_is_java_type (tree decl, int err);
static bool decl_is_java_type (tree decl, int err);
static void initialize_handler_parm (tree, tree);
static void initialize_handler_parm (tree, tree);
static tree do_allocate_exception (tree);
static tree do_allocate_exception (tree);
static tree wrap_cleanups_r (tree *, int *, void *);
static tree wrap_cleanups_r (tree *, int *, void *);
static int complete_ptr_ref_or_void_ptr_p (tree, tree);
static int complete_ptr_ref_or_void_ptr_p (tree, tree);
static bool is_admissible_throw_operand (tree);
static bool is_admissible_throw_operand (tree);
static int can_convert_eh (tree, tree);
static int can_convert_eh (tree, tree);
static tree 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
/* Sets up all the global eh stuff that needs to be initialized at the
   start of compilation.  */
   start of compilation.  */
 
 
void
void
init_exception_processing (void)
init_exception_processing (void)
{
{
  tree tmp;
  tree tmp;
 
 
  /* void std::terminate (); */
  /* void std::terminate (); */
  push_namespace (std_identifier);
  push_namespace (std_identifier);
  tmp = build_function_type (void_type_node, void_list_node);
  tmp = build_function_type (void_type_node, void_list_node);
  terminate_node = build_cp_library_fn_ptr ("terminate", tmp);
  terminate_node = build_cp_library_fn_ptr ("terminate", tmp);
  TREE_THIS_VOLATILE (terminate_node) = 1;
  TREE_THIS_VOLATILE (terminate_node) = 1;
  TREE_NOTHROW (terminate_node) = 1;
  TREE_NOTHROW (terminate_node) = 1;
  pop_namespace ();
  pop_namespace ();
 
 
  /* void __cxa_call_unexpected(void *); */
  /* void __cxa_call_unexpected(void *); */
  tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
  tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
  tmp = build_function_type (void_type_node, tmp);
  tmp = build_function_type (void_type_node, tmp);
  call_unexpected_node
  call_unexpected_node
    = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
    = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
 
 
  lang_protect_cleanup_actions = &cp_protect_cleanup_actions;
  lang_protect_cleanup_actions = &cp_protect_cleanup_actions;
}
}
 
 
/* Returns an expression to be executed if an unhandled exception is
/* Returns an expression to be executed if an unhandled exception is
   propagated out of a cleanup region.  */
   propagated out of a cleanup region.  */
 
 
static tree
static tree
cp_protect_cleanup_actions (void)
cp_protect_cleanup_actions (void)
{
{
  /* [except.terminate]
  /* [except.terminate]
 
 
     When the destruction of an object during stack unwinding exits
     When the destruction of an object during stack unwinding exits
     using an exception ... void terminate(); is called.  */
     using an exception ... void terminate(); is called.  */
  return terminate_node;
  return terminate_node;
}
}
 
 
static tree
static tree
prepare_eh_type (tree type)
prepare_eh_type (tree type)
{
{
  if (type == NULL_TREE)
  if (type == NULL_TREE)
    return type;
    return type;
  if (type == error_mark_node)
  if (type == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  /* peel back references, so they match.  */
  /* peel back references, so they match.  */
  type = non_reference (type);
  type = non_reference (type);
 
 
  /* Peel off cv qualifiers.  */
  /* Peel off cv qualifiers.  */
  type = TYPE_MAIN_VARIANT (type);
  type = TYPE_MAIN_VARIANT (type);
 
 
  /* Functions and arrays decay to pointers.  */
  /* Functions and arrays decay to pointers.  */
  type = type_decays_to (type);
  type = type_decays_to (type);
 
 
  return type;
  return type;
}
}
 
 
/* Return the type info for TYPE as used by EH machinery.  */
/* Return the type info for TYPE as used by EH machinery.  */
tree
tree
eh_type_info (tree type)
eh_type_info (tree type)
{
{
  tree exp;
  tree exp;
 
 
  if (type == NULL_TREE || type == error_mark_node)
  if (type == NULL_TREE || type == error_mark_node)
    return type;
    return type;
 
 
  if (decl_is_java_type (type, 0))
  if (decl_is_java_type (type, 0))
    exp = build_java_class_ref (TREE_TYPE (type));
    exp = build_java_class_ref (TREE_TYPE (type));
  else
  else
    exp = get_tinfo_decl (type);
    exp = get_tinfo_decl (type);
 
 
  return exp;
  return exp;
}
}
 
 
/* Build the address of a typeinfo decl for use in the runtime
/* Build the address of a typeinfo decl for use in the runtime
   matching field of the exception model.  */
   matching field of the exception model.  */
 
 
tree
tree
build_eh_type_type (tree type)
build_eh_type_type (tree type)
{
{
  tree exp = eh_type_info (type);
  tree exp = eh_type_info (type);
 
 
  if (!exp)
  if (!exp)
    return NULL;
    return NULL;
 
 
  mark_used (exp);
  mark_used (exp);
 
 
  return convert (ptr_type_node, build_address (exp));
  return convert (ptr_type_node, build_address (exp));
}
}
 
 
tree
tree
build_exc_ptr (void)
build_exc_ptr (void)
{
{
  return build_call_n (built_in_decls [BUILT_IN_EH_POINTER],
  return build_call_n (built_in_decls [BUILT_IN_EH_POINTER],
                       1, integer_zero_node);
                       1, integer_zero_node);
}
}
 
 
/* Declare a function NAME, returning RETURN_TYPE, taking a single
/* Declare a function NAME, returning RETURN_TYPE, taking a single
   parameter PARM_TYPE, with an empty exception specification.
   parameter PARM_TYPE, with an empty exception specification.
 
 
   Note that the C++ ABI document does not have a throw-specifier on
   Note that the C++ ABI document does not have a throw-specifier on
   the routines declared below via this function.  The declarations
   the routines declared below via this function.  The declarations
   are consistent with the actual implementations in libsupc++.  */
   are consistent with the actual implementations in libsupc++.  */
 
 
static tree
static tree
declare_nothrow_library_fn (tree name, tree return_type, tree parm_type)
declare_nothrow_library_fn (tree name, tree return_type, tree parm_type)
{
{
  tree tmp = tree_cons (NULL_TREE, parm_type, void_list_node);
  tree tmp = tree_cons (NULL_TREE, parm_type, void_list_node);
  return push_library_fn (name, build_function_type (return_type, tmp),
  return push_library_fn (name, build_function_type (return_type, tmp),
                          empty_except_spec);
                          empty_except_spec);
}
}
 
 
/* Build up a call to __cxa_get_exception_ptr so that we can build a
/* Build up a call to __cxa_get_exception_ptr so that we can build a
   copy constructor for the thrown object.  */
   copy constructor for the thrown object.  */
 
 
static tree
static tree
do_get_exception_ptr (void)
do_get_exception_ptr (void)
{
{
  tree fn;
  tree fn;
 
 
  fn = get_identifier ("__cxa_get_exception_ptr");
  fn = get_identifier ("__cxa_get_exception_ptr");
  if (!get_global_value_if_present (fn, &fn))
  if (!get_global_value_if_present (fn, &fn))
    {
    {
      /* Declare void* __cxa_get_exception_ptr (void *) throw().  */
      /* Declare void* __cxa_get_exception_ptr (void *) throw().  */
      fn = declare_nothrow_library_fn (fn, ptr_type_node, ptr_type_node);
      fn = declare_nothrow_library_fn (fn, ptr_type_node, ptr_type_node);
    }
    }
 
 
  return cp_build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
  return cp_build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
                                                NULL_TREE),
                                                NULL_TREE),
                                 tf_warning_or_error);
                                 tf_warning_or_error);
}
}
 
 
/* Build up a call to __cxa_begin_catch, to tell the runtime that the
/* Build up a call to __cxa_begin_catch, to tell the runtime that the
   exception has been handled.  */
   exception has been handled.  */
 
 
static tree
static tree
do_begin_catch (void)
do_begin_catch (void)
{
{
  tree fn;
  tree fn;
 
 
  fn = get_identifier ("__cxa_begin_catch");
  fn = get_identifier ("__cxa_begin_catch");
  if (!get_global_value_if_present (fn, &fn))
  if (!get_global_value_if_present (fn, &fn))
    {
    {
      /* Declare void* __cxa_begin_catch (void *) throw().  */
      /* Declare void* __cxa_begin_catch (void *) throw().  */
      fn = declare_nothrow_library_fn (fn, ptr_type_node, ptr_type_node);
      fn = declare_nothrow_library_fn (fn, ptr_type_node, ptr_type_node);
    }
    }
 
 
  return cp_build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
  return cp_build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
                                                NULL_TREE),
                                                NULL_TREE),
                                 tf_warning_or_error);
                                 tf_warning_or_error);
}
}
 
 
/* Returns nonzero if cleaning up an exception of type TYPE (which can be
/* Returns nonzero if cleaning up an exception of type TYPE (which can be
   NULL_TREE for a ... handler) will not throw an exception.  */
   NULL_TREE for a ... handler) will not throw an exception.  */
 
 
static int
static int
dtor_nothrow (tree type)
dtor_nothrow (tree type)
{
{
  if (type == NULL_TREE || type == error_mark_node)
  if (type == NULL_TREE || type == error_mark_node)
    return 0;
    return 0;
 
 
  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
    return 1;
    return 1;
 
 
  if (CLASSTYPE_LAZY_DESTRUCTOR (type))
  if (CLASSTYPE_LAZY_DESTRUCTOR (type))
    lazily_declare_fn (sfk_destructor, type);
    lazily_declare_fn (sfk_destructor, type);
 
 
  return TREE_NOTHROW (CLASSTYPE_DESTRUCTORS (type));
  return TREE_NOTHROW (CLASSTYPE_DESTRUCTORS (type));
}
}
 
 
/* Build up a call to __cxa_end_catch, to destroy the exception object
/* 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.  */
   for the current catch block if no others are currently using it.  */
 
 
static tree
static tree
do_end_catch (tree type)
do_end_catch (tree type)
{
{
  tree fn, cleanup;
  tree fn, cleanup;
 
 
  fn = get_identifier ("__cxa_end_catch");
  fn = get_identifier ("__cxa_end_catch");
  if (!get_global_value_if_present (fn, &fn))
  if (!get_global_value_if_present (fn, &fn))
    {
    {
      /* Declare void __cxa_end_catch ().  */
      /* Declare void __cxa_end_catch ().  */
      fn = push_void_library_fn (fn, void_list_node);
      fn = push_void_library_fn (fn, void_list_node);
      /* This can throw if the destructor for the exception throws.  */
      /* This can throw if the destructor for the exception throws.  */
      TREE_NOTHROW (fn) = 0;
      TREE_NOTHROW (fn) = 0;
    }
    }
 
 
  cleanup = cp_build_function_call (fn, NULL_TREE, tf_warning_or_error);
  cleanup = cp_build_function_call (fn, NULL_TREE, tf_warning_or_error);
  TREE_NOTHROW (cleanup) = dtor_nothrow (type);
  TREE_NOTHROW (cleanup) = dtor_nothrow (type);
 
 
  return cleanup;
  return cleanup;
}
}
 
 
/* This routine creates the cleanup for the current exception.  */
/* This routine creates the cleanup for the current exception.  */
 
 
static void
static void
push_eh_cleanup (tree type)
push_eh_cleanup (tree type)
{
{
  finish_decl_cleanup (NULL_TREE, do_end_catch (type));
  finish_decl_cleanup (NULL_TREE, do_end_catch (type));
}
}
 
 
/* Return nonzero value if DECL is a Java type suitable for catch or
/* Return nonzero value if DECL is a Java type suitable for catch or
   throw.  */
   throw.  */
 
 
static bool
static bool
decl_is_java_type (tree decl, int err)
decl_is_java_type (tree decl, int err)
{
{
  bool r = (TREE_CODE (decl) == POINTER_TYPE
  bool r = (TREE_CODE (decl) == POINTER_TYPE
            && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
            && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
            && TYPE_FOR_JAVA (TREE_TYPE (decl)));
            && TYPE_FOR_JAVA (TREE_TYPE (decl)));
 
 
  if (err)
  if (err)
    {
    {
      if (TREE_CODE (decl) == REFERENCE_TYPE
      if (TREE_CODE (decl) == REFERENCE_TYPE
          && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
          && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
          && TYPE_FOR_JAVA (TREE_TYPE (decl)))
          && TYPE_FOR_JAVA (TREE_TYPE (decl)))
        {
        {
          /* Can't throw a reference.  */
          /* Can't throw a reference.  */
          error ("type %qT is disallowed in Java %<throw%> or %<catch%>",
          error ("type %qT is disallowed in Java %<throw%> or %<catch%>",
                 decl);
                 decl);
        }
        }
 
 
      if (r)
      if (r)
        {
        {
          tree jthrow_node
          tree jthrow_node
            = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jthrowable"));
            = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jthrowable"));
 
 
          if (jthrow_node == NULL_TREE)
          if (jthrow_node == NULL_TREE)
            fatal_error
            fatal_error
              ("call to Java %<catch%> or %<throw%> with %<jthrowable%> undefined");
              ("call to Java %<catch%> or %<throw%> with %<jthrowable%> undefined");
 
 
          jthrow_node = TREE_TYPE (TREE_TYPE (jthrow_node));
          jthrow_node = TREE_TYPE (TREE_TYPE (jthrow_node));
 
 
          if (! DERIVED_FROM_P (jthrow_node, TREE_TYPE (decl)))
          if (! DERIVED_FROM_P (jthrow_node, TREE_TYPE (decl)))
            {
            {
              /* Thrown object must be a Throwable.  */
              /* Thrown object must be a Throwable.  */
              error ("type %qT is not derived from %<java::lang::Throwable%>",
              error ("type %qT is not derived from %<java::lang::Throwable%>",
                     TREE_TYPE (decl));
                     TREE_TYPE (decl));
            }
            }
        }
        }
    }
    }
 
 
  return r;
  return r;
}
}
 
 
/* Select the personality routine to be used for exception handling,
/* Select the personality routine to be used for exception handling,
   or issue an error if we need two different ones in the same
   or issue an error if we need two different ones in the same
   translation unit.
   translation unit.
   ??? At present eh_personality_decl is set to
   ??? At present eh_personality_decl is set to
   __gxx_personality_(sj|v)0 in init_exception_processing - should it
   __gxx_personality_(sj|v)0 in init_exception_processing - should it
   be done here instead?  */
   be done here instead?  */
void
void
choose_personality_routine (enum languages lang)
choose_personality_routine (enum languages lang)
{
{
  static enum {
  static enum {
    chose_none,
    chose_none,
    chose_cpp,
    chose_cpp,
    chose_java,
    chose_java,
    gave_error
    gave_error
  } state;
  } state;
 
 
  switch (state)
  switch (state)
    {
    {
    case gave_error:
    case gave_error:
      return;
      return;
 
 
    case chose_cpp:
    case chose_cpp:
      if (lang != lang_cplusplus)
      if (lang != lang_cplusplus)
        goto give_error;
        goto give_error;
      return;
      return;
 
 
    case chose_java:
    case chose_java:
      if (lang != lang_java)
      if (lang != lang_java)
        goto give_error;
        goto give_error;
      return;
      return;
 
 
    case chose_none:
    case chose_none:
      ; /* Proceed to language selection.  */
      ; /* Proceed to language selection.  */
    }
    }
 
 
  switch (lang)
  switch (lang)
    {
    {
    case lang_cplusplus:
    case lang_cplusplus:
      state = chose_cpp;
      state = chose_cpp;
      break;
      break;
 
 
    case lang_java:
    case lang_java:
      state = chose_java;
      state = chose_java;
      terminate_node = built_in_decls [BUILT_IN_ABORT];
      terminate_node = built_in_decls [BUILT_IN_ABORT];
      pragma_java_exceptions = true;
      pragma_java_exceptions = true;
      break;
      break;
 
 
    default:
    default:
      gcc_unreachable ();
      gcc_unreachable ();
    }
    }
  return;
  return;
 
 
 give_error:
 give_error:
  error ("mixing C++ and Java catches in a single translation unit");
  error ("mixing C++ and Java catches in a single translation unit");
  state = gave_error;
  state = gave_error;
}
}
 
 
/* Initialize the catch parameter DECL.  */
/* Initialize the catch parameter DECL.  */
 
 
static void
static void
initialize_handler_parm (tree decl, tree exp)
initialize_handler_parm (tree decl, tree exp)
{
{
  tree init;
  tree init;
  tree init_type;
  tree init_type;
 
 
  /* Make sure we mark the catch param as used, otherwise we'll get a
  /* Make sure we mark the catch param as used, otherwise we'll get a
     warning about an unused ((anonymous)).  */
     warning about an unused ((anonymous)).  */
  TREE_USED (decl) = 1;
  TREE_USED (decl) = 1;
 
 
  /* Figure out the type that the initializer is.  Pointers are returned
  /* 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.  */
     reference.  */
  init_type = TREE_TYPE (decl);
  init_type = TREE_TYPE (decl);
  if (!POINTER_TYPE_P (init_type))
  if (!POINTER_TYPE_P (init_type))
    init_type = build_reference_type (init_type);
    init_type = build_reference_type (init_type);
 
 
  choose_personality_routine (decl_is_java_type (init_type, 0)
  choose_personality_routine (decl_is_java_type (init_type, 0)
                              ? lang_java : lang_cplusplus);
                              ? lang_java : lang_cplusplus);
 
 
  /* Since pointers are passed by value, initialize a reference to
  /* Since pointers are passed by value, initialize a reference to
     pointer catch parm with the address of the temporary.  */
     pointer catch parm with the address of the temporary.  */
  if (TREE_CODE (init_type) == REFERENCE_TYPE
  if (TREE_CODE (init_type) == REFERENCE_TYPE
      && TYPE_PTR_P (TREE_TYPE (init_type)))
      && TYPE_PTR_P (TREE_TYPE (init_type)))
    exp = cp_build_unary_op (ADDR_EXPR, exp, 1, tf_warning_or_error);
    exp = cp_build_unary_op (ADDR_EXPR, exp, 1, 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);
 
 
  init = convert_from_reference (exp);
  init = convert_from_reference (exp);
 
 
  /* If the constructor for the catch parm exits via an exception, we
  /* If the constructor for the catch parm exits via an exception, we
     must call terminate.  See eh23.C.  */
     must call terminate.  See eh23.C.  */
  if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
  if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
    {
    {
      /* Generate the copy constructor call directly so we can wrap it.
      /* Generate the copy constructor call directly so we can wrap it.
         See also expand_default_init.  */
         See also expand_default_init.  */
      init = ocp_convert (TREE_TYPE (decl), init,
      init = ocp_convert (TREE_TYPE (decl), init,
                          CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
                          CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
      /* Force cleanups now to avoid nesting problems with the
      /* Force cleanups now to avoid nesting problems with the
         MUST_NOT_THROW_EXPR.  */
         MUST_NOT_THROW_EXPR.  */
      init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
      init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
      init = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (init), init);
      init = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (init), init);
    }
    }
 
 
  decl = pushdecl (decl);
  decl = pushdecl (decl);
 
 
  start_decl_1 (decl, true);
  start_decl_1 (decl, true);
  cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
  cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
                  LOOKUP_ONLYCONVERTING|DIRECT_BIND);
                  LOOKUP_ONLYCONVERTING|DIRECT_BIND);
}
}
 
 
/* Call this to start a catch block.  DECL is the catch parameter.  */
/* Call this to start a catch block.  DECL is the catch parameter.  */
 
 
tree
tree
expand_start_catch_block (tree decl)
expand_start_catch_block (tree decl)
{
{
  tree exp;
  tree exp;
  tree type, init;
  tree type, init;
 
 
  if (! doing_eh (1))
  if (! doing_eh (1))
    return NULL_TREE;
    return NULL_TREE;
 
 
  /* Make sure this declaration is reasonable.  */
  /* Make sure this declaration is reasonable.  */
  if (decl && !complete_ptr_ref_or_void_ptr_p (TREE_TYPE (decl), NULL_TREE))
  if (decl && !complete_ptr_ref_or_void_ptr_p (TREE_TYPE (decl), NULL_TREE))
    decl = error_mark_node;
    decl = error_mark_node;
 
 
  if (decl)
  if (decl)
    type = prepare_eh_type (TREE_TYPE (decl));
    type = prepare_eh_type (TREE_TYPE (decl));
  else
  else
    type = NULL_TREE;
    type = NULL_TREE;
 
 
  if (decl && decl_is_java_type (type, 1))
  if (decl && decl_is_java_type (type, 1))
    {
    {
      /* Java only passes object via pointer and doesn't require
      /* Java only passes object via pointer and doesn't require
         adjusting.  The java object is immediately before the
         adjusting.  The java object is immediately before the
         generic exception header.  */
         generic exception header.  */
      exp = build_exc_ptr ();
      exp = build_exc_ptr ();
      exp = build1 (NOP_EXPR, build_pointer_type (type), exp);
      exp = build1 (NOP_EXPR, build_pointer_type (type), exp);
      exp = build2 (POINTER_PLUS_EXPR, TREE_TYPE (exp), exp,
      exp = build2 (POINTER_PLUS_EXPR, TREE_TYPE (exp), exp,
                    fold_build1_loc (input_location,
                    fold_build1_loc (input_location,
                                 NEGATE_EXPR, sizetype,
                                 NEGATE_EXPR, sizetype,
                                 TYPE_SIZE_UNIT (TREE_TYPE (exp))));
                                 TYPE_SIZE_UNIT (TREE_TYPE (exp))));
      exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
      exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
      initialize_handler_parm (decl, exp);
      initialize_handler_parm (decl, exp);
      return type;
      return type;
    }
    }
 
 
  /* Call __cxa_end_catch at the end of processing the exception.  */
  /* Call __cxa_end_catch at the end of processing the exception.  */
  push_eh_cleanup (type);
  push_eh_cleanup (type);
 
 
  init = do_begin_catch ();
  init = do_begin_catch ();
 
 
  /* If there's no decl at all, then all we need to do is make sure
  /* 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.  */
     to tell the runtime that we've begun handling the exception.  */
  if (decl == NULL || decl == error_mark_node || init == error_mark_node)
  if (decl == NULL || decl == error_mark_node || init == error_mark_node)
    finish_expr_stmt (init);
    finish_expr_stmt (init);
 
 
  /* If the C++ object needs constructing, we need to do that before
  /* If the C++ object needs constructing, we need to do that before
     calling __cxa_begin_catch, so that std::uncaught_exception gets
     calling __cxa_begin_catch, so that std::uncaught_exception gets
     the right value during the copy constructor.  */
     the right value during the copy constructor.  */
  else if (flag_use_cxa_get_exception_ptr
  else if (flag_use_cxa_get_exception_ptr
           && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
           && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
    {
    {
      exp = do_get_exception_ptr ();
      exp = do_get_exception_ptr ();
      initialize_handler_parm (decl, exp);
      initialize_handler_parm (decl, exp);
      finish_expr_stmt (init);
      finish_expr_stmt (init);
    }
    }
 
 
  /* Otherwise the type uses a bitwise copy, and we don't have to worry
  /* 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
     about the value of std::uncaught_exception and therefore can do the
     copy with the return value of __cxa_end_catch instead.  */
     copy with the return value of __cxa_end_catch instead.  */
  else
  else
    {
    {
      tree init_type = type;
      tree init_type = type;
 
 
      /* Pointers are passed by values, everything else by reference.  */
      /* Pointers are passed by values, everything else by reference.  */
      if (!TYPE_PTR_P (type))
      if (!TYPE_PTR_P (type))
        init_type = build_pointer_type (type);
        init_type = build_pointer_type (type);
      if (init_type != TREE_TYPE (init))
      if (init_type != TREE_TYPE (init))
        init = build1 (NOP_EXPR, init_type, init);
        init = build1 (NOP_EXPR, init_type, init);
      exp = create_temporary_var (init_type);
      exp = create_temporary_var (init_type);
      DECL_REGISTER (exp) = 1;
      DECL_REGISTER (exp) = 1;
      cp_finish_decl (exp, init, /*init_const_expr=*/false,
      cp_finish_decl (exp, init, /*init_const_expr=*/false,
                      NULL_TREE, LOOKUP_ONLYCONVERTING);
                      NULL_TREE, LOOKUP_ONLYCONVERTING);
      initialize_handler_parm (decl, exp);
      initialize_handler_parm (decl, exp);
    }
    }
 
 
  return type;
  return type;
}
}
 
 
 
 
/* Call this to end a catch block.  Its responsible for emitting the
/* Call this to end a catch block.  Its responsible for emitting the
   code to handle jumping back to the correct place, and for emitting
   code to handle jumping back to the correct place, and for emitting
   the label to jump to if this catch block didn't match.  */
   the label to jump to if this catch block didn't match.  */
 
 
void
void
expand_end_catch_block (void)
expand_end_catch_block (void)
{
{
  if (! doing_eh (1))
  if (! doing_eh (1))
    return;
    return;
 
 
  /* The exception being handled is rethrown if control reaches the end of
  /* The exception being handled is rethrown if control reaches the end of
     a handler of the function-try-block of a constructor or destructor.  */
     a handler of the function-try-block of a constructor or destructor.  */
  if (in_function_try_handler
  if (in_function_try_handler
      && (DECL_CONSTRUCTOR_P (current_function_decl)
      && (DECL_CONSTRUCTOR_P (current_function_decl)
          || DECL_DESTRUCTOR_P (current_function_decl)))
          || DECL_DESTRUCTOR_P (current_function_decl)))
    finish_expr_stmt (build_throw (NULL_TREE));
    finish_expr_stmt (build_throw (NULL_TREE));
}
}
 
 
tree
tree
begin_eh_spec_block (void)
begin_eh_spec_block (void)
{
{
  tree r = build_stmt (input_location, EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
  tree r = build_stmt (input_location, EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
  add_stmt (r);
  add_stmt (r);
  EH_SPEC_STMTS (r) = push_stmt_list ();
  EH_SPEC_STMTS (r) = push_stmt_list ();
  return r;
  return r;
}
}
 
 
void
void
finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
{
{
  tree raises;
  tree raises;
 
 
  EH_SPEC_STMTS (eh_spec_block) = pop_stmt_list (EH_SPEC_STMTS (eh_spec_block));
  EH_SPEC_STMTS (eh_spec_block) = pop_stmt_list (EH_SPEC_STMTS (eh_spec_block));
 
 
  /* Strip cv quals, etc, from the specification types.  */
  /* Strip cv quals, etc, from the specification types.  */
  for (raises = NULL_TREE;
  for (raises = NULL_TREE;
       raw_raises && TREE_VALUE (raw_raises);
       raw_raises && TREE_VALUE (raw_raises);
       raw_raises = TREE_CHAIN (raw_raises))
       raw_raises = TREE_CHAIN (raw_raises))
    {
    {
      tree type = prepare_eh_type (TREE_VALUE (raw_raises));
      tree type = prepare_eh_type (TREE_VALUE (raw_raises));
      tree tinfo = eh_type_info (type);
      tree tinfo = eh_type_info (type);
 
 
      mark_used (tinfo);
      mark_used (tinfo);
      raises = tree_cons (NULL_TREE, type, raises);
      raises = tree_cons (NULL_TREE, type, raises);
    }
    }
 
 
  EH_SPEC_RAISES (eh_spec_block) = raises;
  EH_SPEC_RAISES (eh_spec_block) = raises;
}
}
 
 
/* Return a pointer to a buffer for an exception object of type TYPE.  */
/* Return a pointer to a buffer for an exception object of type TYPE.  */
 
 
static tree
static tree
do_allocate_exception (tree type)
do_allocate_exception (tree type)
{
{
  tree fn;
  tree fn;
 
 
  fn = get_identifier ("__cxa_allocate_exception");
  fn = get_identifier ("__cxa_allocate_exception");
  if (!get_global_value_if_present (fn, &fn))
  if (!get_global_value_if_present (fn, &fn))
    {
    {
      /* Declare void *__cxa_allocate_exception(size_t) throw().  */
      /* Declare void *__cxa_allocate_exception(size_t) throw().  */
      fn = declare_nothrow_library_fn (fn, ptr_type_node, size_type_node);
      fn = declare_nothrow_library_fn (fn, ptr_type_node, size_type_node);
    }
    }
 
 
  return cp_build_function_call (fn,
  return cp_build_function_call (fn,
                                 tree_cons (NULL_TREE, size_in_bytes (type),
                                 tree_cons (NULL_TREE, size_in_bytes (type),
                                            NULL_TREE),
                                            NULL_TREE),
                                 tf_warning_or_error);
                                 tf_warning_or_error);
}
}
 
 
/* Call __cxa_free_exception from a cleanup.  This is never invoked
/* Call __cxa_free_exception from a cleanup.  This is never invoked
   directly, but see the comment for stabilize_throw_expr.  */
   directly, but see the comment for stabilize_throw_expr.  */
 
 
static tree
static tree
do_free_exception (tree ptr)
do_free_exception (tree ptr)
{
{
  tree fn;
  tree fn;
 
 
  fn = get_identifier ("__cxa_free_exception");
  fn = get_identifier ("__cxa_free_exception");
  if (!get_global_value_if_present (fn, &fn))
  if (!get_global_value_if_present (fn, &fn))
    {
    {
      /* Declare void __cxa_free_exception (void *) throw().  */
      /* Declare void __cxa_free_exception (void *) throw().  */
      fn = declare_nothrow_library_fn (fn, void_type_node, ptr_type_node);
      fn = declare_nothrow_library_fn (fn, void_type_node, ptr_type_node);
    }
    }
 
 
  return cp_build_function_call (fn, tree_cons (NULL_TREE, ptr, NULL_TREE),
  return cp_build_function_call (fn, tree_cons (NULL_TREE, ptr, NULL_TREE),
                                 tf_warning_or_error);
                                 tf_warning_or_error);
}
}
 
 
/* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
/* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
   Called from build_throw via walk_tree_without_duplicates.  */
   Called from build_throw via walk_tree_without_duplicates.  */
 
 
static tree
static tree
wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
                 void *data ATTRIBUTE_UNUSED)
                 void *data ATTRIBUTE_UNUSED)
{
{
  tree exp = *tp;
  tree exp = *tp;
  tree cleanup;
  tree cleanup;
 
 
  /* Don't walk into types.  */
  /* Don't walk into types.  */
  if (TYPE_P (exp))
  if (TYPE_P (exp))
    {
    {
      *walk_subtrees = 0;
      *walk_subtrees = 0;
      return NULL_TREE;
      return NULL_TREE;
    }
    }
  if (TREE_CODE (exp) != TARGET_EXPR)
  if (TREE_CODE (exp) != TARGET_EXPR)
    return NULL_TREE;
    return NULL_TREE;
 
 
  cleanup = TARGET_EXPR_CLEANUP (exp);
  cleanup = TARGET_EXPR_CLEANUP (exp);
  if (cleanup)
  if (cleanup)
    {
    {
      cleanup = build1 (MUST_NOT_THROW_EXPR, void_type_node, cleanup);
      cleanup = build1 (MUST_NOT_THROW_EXPR, void_type_node, cleanup);
      TARGET_EXPR_CLEANUP (exp) = cleanup;
      TARGET_EXPR_CLEANUP (exp) = cleanup;
    }
    }
 
 
  /* Keep iterating.  */
  /* Keep iterating.  */
  return NULL_TREE;
  return NULL_TREE;
}
}
 
 
/* Build a throw expression.  */
/* Build a throw expression.  */
 
 
tree
tree
build_throw (tree exp)
build_throw (tree exp)
{
{
  tree fn;
  tree fn;
 
 
  if (exp == error_mark_node)
  if (exp == error_mark_node)
    return exp;
    return exp;
 
 
  if (processing_template_decl)
  if (processing_template_decl)
    {
    {
      if (cfun)
      if (cfun)
        current_function_returns_abnormally = 1;
        current_function_returns_abnormally = 1;
      return build_min (THROW_EXPR, void_type_node, exp);
      return build_min (THROW_EXPR, void_type_node, exp);
    }
    }
 
 
  if (exp == null_node)
  if (exp == null_node)
    warning (0, "throwing NULL, which has integral, not pointer type");
    warning (0, "throwing NULL, which has integral, not pointer type");
 
 
  if (exp != NULL_TREE)
  if (exp != NULL_TREE)
    {
    {
      if (!is_admissible_throw_operand (exp))
      if (!is_admissible_throw_operand (exp))
        return error_mark_node;
        return error_mark_node;
    }
    }
 
 
  if (! doing_eh (1))
  if (! doing_eh (1))
    return error_mark_node;
    return error_mark_node;
 
 
  if (exp && decl_is_java_type (TREE_TYPE (exp), 1))
  if (exp && decl_is_java_type (TREE_TYPE (exp), 1))
    {
    {
      tree fn = get_identifier ("_Jv_Throw");
      tree fn = get_identifier ("_Jv_Throw");
      if (!get_global_value_if_present (fn, &fn))
      if (!get_global_value_if_present (fn, &fn))
        {
        {
          /* Declare void _Jv_Throw (void *).  */
          /* Declare void _Jv_Throw (void *).  */
          tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
          tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
          tmp = build_function_type (ptr_type_node, tmp);
          tmp = build_function_type (ptr_type_node, tmp);
          fn = push_throw_library_fn (fn, tmp);
          fn = push_throw_library_fn (fn, tmp);
        }
        }
      else if (really_overloaded_fn (fn))
      else if (really_overloaded_fn (fn))
        {
        {
          error ("%qD should never be overloaded", fn);
          error ("%qD should never be overloaded", fn);
          return error_mark_node;
          return error_mark_node;
        }
        }
      fn = OVL_CURRENT (fn);
      fn = OVL_CURRENT (fn);
      exp = cp_build_function_call (fn, tree_cons (NULL_TREE, exp, NULL_TREE),
      exp = cp_build_function_call (fn, tree_cons (NULL_TREE, exp, NULL_TREE),
                                    tf_warning_or_error);
                                    tf_warning_or_error);
    }
    }
  else if (exp)
  else if (exp)
    {
    {
      tree throw_type;
      tree throw_type;
      tree temp_type;
      tree temp_type;
      tree cleanup;
      tree cleanup;
      tree object, ptr;
      tree object, ptr;
      tree tmp;
      tree tmp;
      tree temp_expr, allocate_expr;
      tree temp_expr, allocate_expr;
      bool elided;
      bool elided;
 
 
      /* The CLEANUP_TYPE is the internal type of a destructor.  */
      /* The CLEANUP_TYPE is the internal type of a destructor.  */
      if (!cleanup_type)
      if (!cleanup_type)
        {
        {
          tmp = void_list_node;
          tmp = void_list_node;
          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);
          tmp = build_function_type (void_type_node, tmp);
          cleanup_type = build_pointer_type (tmp);
          cleanup_type = build_pointer_type (tmp);
        }
        }
 
 
      fn = get_identifier ("__cxa_throw");
      fn = get_identifier ("__cxa_throw");
      if (!get_global_value_if_present (fn, &fn))
      if (!get_global_value_if_present (fn, &fn))
        {
        {
          /* Declare void __cxa_throw (void*, void*, void (*)(void*)).  */
          /* Declare void __cxa_throw (void*, void*, void (*)(void*)).  */
          /* ??? Second argument is supposed to be "std::type_info*".  */
          /* ??? Second argument is supposed to be "std::type_info*".  */
          tmp = void_list_node;
          tmp = void_list_node;
          tmp = tree_cons (NULL_TREE, cleanup_type, tmp);
          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 = 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);
          tmp = build_function_type (void_type_node, tmp);
          fn = push_throw_library_fn (fn, tmp);
          fn = push_throw_library_fn (fn, tmp);
        }
        }
 
 
      /* [except.throw]
      /* [except.throw]
 
 
         A throw-expression initializes a temporary object, the type
         A throw-expression initializes a temporary object, the type
         of which is determined by removing any top-level
         of which is determined by removing any top-level
         cv-qualifiers from the static type of the operand of throw
         cv-qualifiers from the static type of the operand of throw
         and adjusting the type from "array of T" or "function return
         and adjusting the type from "array of T" or "function return
         T" to "pointer to T" or "pointer to function returning T"
         T" to "pointer to T" or "pointer to function returning T"
         respectively.  */
         respectively.  */
      temp_type = is_bitfield_expr_with_lowered_type (exp);
      temp_type = is_bitfield_expr_with_lowered_type (exp);
      if (!temp_type)
      if (!temp_type)
        temp_type = type_decays_to (TREE_TYPE (exp));
        temp_type = type_decays_to (TREE_TYPE (exp));
 
 
      /* OK, this is kind of wacky.  The standard says that we call
      /* OK, this is kind of wacky.  The standard says that we call
         terminate when the exception handling mechanism, after
         terminate when the exception handling mechanism, after
         completing evaluation of the expression to be thrown but
         completing evaluation of the expression to be thrown but
         before the exception is caught (_except.throw_), calls a
         before the exception is caught (_except.throw_), calls a
         user function that exits via an uncaught exception.
         user function that exits via an uncaught exception.
 
 
         So we have to protect the actual initialization of the
         So we have to protect the actual initialization of the
         exception object with terminate(), but evaluate the
         exception object with terminate(), but evaluate the
         expression first.  Since there could be temps in the
         expression first.  Since there could be temps in the
         expression, we need to handle that, too.  We also expand
         expression, we need to handle that, too.  We also expand
         the call to __cxa_allocate_exception first (which doesn't
         the call to __cxa_allocate_exception first (which doesn't
         matter, since it can't throw).  */
         matter, since it can't throw).  */
 
 
      /* Allocate the space for the exception.  */
      /* Allocate the space for the exception.  */
      allocate_expr = do_allocate_exception (temp_type);
      allocate_expr = do_allocate_exception (temp_type);
      allocate_expr = get_target_expr (allocate_expr);
      allocate_expr = get_target_expr (allocate_expr);
      ptr = TARGET_EXPR_SLOT (allocate_expr);
      ptr = TARGET_EXPR_SLOT (allocate_expr);
      object = build_nop (build_pointer_type (temp_type), ptr);
      object = build_nop (build_pointer_type (temp_type), ptr);
      object = cp_build_indirect_ref (object, RO_NULL, tf_warning_or_error);
      object = cp_build_indirect_ref (object, RO_NULL, tf_warning_or_error);
 
 
      elided = (TREE_CODE (exp) == TARGET_EXPR);
      elided = (TREE_CODE (exp) == TARGET_EXPR);
 
 
      /* And initialize the exception object.  */
      /* And initialize the exception object.  */
      if (CLASS_TYPE_P (temp_type))
      if (CLASS_TYPE_P (temp_type))
        {
        {
          int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
          int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
          VEC(tree,gc) *exp_vec;
          VEC(tree,gc) *exp_vec;
 
 
          /* Under C++0x [12.8/16 class.copy], a thrown lvalue is sometimes
          /* Under C++0x [12.8/16 class.copy], a thrown lvalue is sometimes
             treated as an rvalue for the purposes of overload resolution
             treated as an rvalue for the purposes of overload resolution
             to favor move constructors over copy constructors.  */
             to favor move constructors over copy constructors.  */
          if (/* Must be a local, automatic variable.  */
          if (/* Must be a local, automatic variable.  */
              TREE_CODE (exp) == VAR_DECL
              TREE_CODE (exp) == VAR_DECL
              && DECL_CONTEXT (exp) == current_function_decl
              && DECL_CONTEXT (exp) == current_function_decl
              && ! TREE_STATIC (exp)
              && ! TREE_STATIC (exp)
              /* The variable must not have the `volatile' qualifier.  */
              /* The variable must not have the `volatile' qualifier.  */
              && !(cp_type_quals (TREE_TYPE (exp)) & TYPE_QUAL_VOLATILE))
              && !(cp_type_quals (TREE_TYPE (exp)) & TYPE_QUAL_VOLATILE))
            flags = flags | LOOKUP_PREFER_RVALUE;
            flags = flags | LOOKUP_PREFER_RVALUE;
 
 
          /* Call the copy constructor.  */
          /* Call the copy constructor.  */
          exp_vec = make_tree_vector_single (exp);
          exp_vec = make_tree_vector_single (exp);
          exp = (build_special_member_call
          exp = (build_special_member_call
                 (object, complete_ctor_identifier, &exp_vec,
                 (object, complete_ctor_identifier, &exp_vec,
                  TREE_TYPE (object), flags, tf_warning_or_error));
                  TREE_TYPE (object), flags, tf_warning_or_error));
          release_tree_vector (exp_vec);
          release_tree_vector (exp_vec);
          if (exp == error_mark_node)
          if (exp == error_mark_node)
            {
            {
              error ("  in thrown expression");
              error ("  in thrown expression");
              return error_mark_node;
              return error_mark_node;
            }
            }
        }
        }
      else
      else
        {
        {
          tmp = decay_conversion (exp);
          tmp = decay_conversion (exp);
          if (tmp == error_mark_node)
          if (tmp == error_mark_node)
            return error_mark_node;
            return error_mark_node;
          exp = build2 (INIT_EXPR, temp_type, object, tmp);
          exp = build2 (INIT_EXPR, temp_type, object, tmp);
        }
        }
 
 
      /* Pre-evaluate the thrown expression first, since if we allocated
      /* Pre-evaluate the thrown expression first, since if we allocated
         the space first we would have to deal with cleaning it up if
         the space first we would have to deal with cleaning it up if
         evaluating this expression throws.
         evaluating this expression throws.
 
 
         The case where EXP the initializer is a cast or a function
         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
         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
         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
         say no, as that allowed us to optimize this case without worrying
         about deallocating the exception object if it does.  But that
         about deallocating the exception object if it does.  But that
         conflicted with expectations (PR 13944) and the EDG compiler; now
         conflicted with expectations (PR 13944) and the EDG compiler; now
         we wrap the initialization in a TRY_CATCH_EXPR to call
         we wrap the initialization in a TRY_CATCH_EXPR to call
         do_free_exception rather than in a MUST_NOT_THROW_EXPR, for this
         do_free_exception rather than in a MUST_NOT_THROW_EXPR, for this
         case only.
         case only.
 
 
         BUT: Issue 475 may do away with this inconsistency by removing the
         BUT: Issue 475 may do away with this inconsistency by removing the
         terminate() in this situation.
         terminate() in this situation.
 
 
         Note that we don't check the return value from stabilize_init
         Note that we don't check the return value from stabilize_init
         because it will only return false in cases where elided is true,
         because it will only return false in cases where elided is true,
         and therefore we don't need to work around the failure to
         and therefore we don't need to work around the failure to
         preevaluate.  */
         preevaluate.  */
      temp_expr = NULL_TREE;
      temp_expr = NULL_TREE;
      stabilize_init (exp, &temp_expr);
      stabilize_init (exp, &temp_expr);
 
 
      /* Wrap the initialization in a CLEANUP_POINT_EXPR so that cleanups
      /* Wrap the initialization in a CLEANUP_POINT_EXPR so that cleanups
         for temporaries within the initialization are run before the one
         for temporaries within the initialization are run before the one
         for the exception object, preserving LIFO order.  */
         for the exception object, preserving LIFO order.  */
      exp = build1 (CLEANUP_POINT_EXPR, void_type_node, exp);
      exp = build1 (CLEANUP_POINT_EXPR, void_type_node, exp);
 
 
      if (elided)
      if (elided)
        exp = build2 (TRY_CATCH_EXPR, void_type_node, exp,
        exp = build2 (TRY_CATCH_EXPR, void_type_node, exp,
                      do_free_exception (ptr));
                      do_free_exception (ptr));
      else
      else
        exp = build1 (MUST_NOT_THROW_EXPR, void_type_node, exp);
        exp = build1 (MUST_NOT_THROW_EXPR, void_type_node, exp);
 
 
      /* Prepend the allocation.  */
      /* Prepend the allocation.  */
      exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
      exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
      if (temp_expr)
      if (temp_expr)
        {
        {
          /* Prepend the calculation of the throw expression.  Also, force
          /* Prepend the calculation of the throw expression.  Also, force
             any cleanups from the expression to be evaluated here so that
             any cleanups from the expression to be evaluated here so that
             we don't have to do them during unwinding.  But first wrap
             we don't have to do them during unwinding.  But first wrap
             them in MUST_NOT_THROW_EXPR, since they are run after the
             them in MUST_NOT_THROW_EXPR, since they are run after the
             exception object is initialized.  */
             exception object is initialized.  */
          cp_walk_tree_without_duplicates (&temp_expr, wrap_cleanups_r, 0);
          cp_walk_tree_without_duplicates (&temp_expr, wrap_cleanups_r, 0);
          exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), temp_expr, exp);
          exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), temp_expr, exp);
          exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
          exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
        }
        }
 
 
      throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
      throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
 
 
      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
        {
        {
          cleanup = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
          cleanup = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
                                     complete_dtor_identifier, 0);
                                     complete_dtor_identifier, 0);
          cleanup = BASELINK_FUNCTIONS (cleanup);
          cleanup = BASELINK_FUNCTIONS (cleanup);
          mark_used (cleanup);
          mark_used (cleanup);
          cxx_mark_addressable (cleanup);
          cxx_mark_addressable (cleanup);
          /* Pretend it's a normal function.  */
          /* Pretend it's a normal function.  */
          cleanup = build1 (ADDR_EXPR, cleanup_type, cleanup);
          cleanup = build1 (ADDR_EXPR, cleanup_type, cleanup);
        }
        }
      else
      else
        cleanup = build_int_cst (cleanup_type, 0);
        cleanup = build_int_cst (cleanup_type, 0);
 
 
      tmp = tree_cons (NULL_TREE, cleanup, NULL_TREE);
      tmp = tree_cons (NULL_TREE, cleanup, NULL_TREE);
      tmp = tree_cons (NULL_TREE, throw_type, tmp);
      tmp = tree_cons (NULL_TREE, throw_type, tmp);
      tmp = tree_cons (NULL_TREE, ptr, tmp);
      tmp = tree_cons (NULL_TREE, ptr, tmp);
      /* ??? Indicate that this function call throws throw_type.  */
      /* ??? Indicate that this function call throws throw_type.  */
      tmp = cp_build_function_call (fn, tmp, tf_warning_or_error);
      tmp = cp_build_function_call (fn, tmp, tf_warning_or_error);
 
 
      /* Tack on the initialization stuff.  */
      /* Tack on the initialization stuff.  */
      exp = build2 (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp);
      exp = build2 (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp);
    }
    }
  else
  else
    {
    {
      /* Rethrow current exception.  */
      /* Rethrow current exception.  */
 
 
      tree fn = get_identifier ("__cxa_rethrow");
      tree fn = get_identifier ("__cxa_rethrow");
      if (!get_global_value_if_present (fn, &fn))
      if (!get_global_value_if_present (fn, &fn))
        {
        {
          /* Declare void __cxa_rethrow (void).  */
          /* Declare void __cxa_rethrow (void).  */
          fn = push_throw_library_fn
          fn = push_throw_library_fn
            (fn, build_function_type (void_type_node, void_list_node));
            (fn, build_function_type (void_type_node, void_list_node));
        }
        }
 
 
      /* ??? Indicate that this function call allows exceptions of the type
      /* ??? 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 = cp_build_function_call (fn, NULL_TREE, tf_warning_or_error);
      exp = cp_build_function_call (fn, NULL_TREE, tf_warning_or_error);
    }
    }
 
 
  exp = build1 (THROW_EXPR, void_type_node, exp);
  exp = build1 (THROW_EXPR, void_type_node, exp);
 
 
  return exp;
  return exp;
}
}
 
 
/* Make sure TYPE is complete, pointer to complete, reference to
/* Make sure TYPE is complete, pointer to complete, reference to
   complete, or pointer to cv void. Issue diagnostic on failure.
   complete, or pointer to cv void. Issue diagnostic on failure.
   Return the zero on failure and nonzero 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.  */
   the expr or decl from whence TYPE came, if available.  */
 
 
static int
static int
complete_ptr_ref_or_void_ptr_p (tree type, tree from)
complete_ptr_ref_or_void_ptr_p (tree type, tree from)
{
{
  int is_ptr;
  int is_ptr;
 
 
  /* Check complete.  */
  /* Check complete.  */
  type = complete_type_or_else (type, from);
  type = complete_type_or_else (type, from);
  if (!type)
  if (!type)
    return 0;
    return 0;
 
 
  /* Or a pointer or ref to one, or cv void *.  */
  /* Or a pointer or ref to one, or cv void *.  */
  is_ptr = TREE_CODE (type) == POINTER_TYPE;
  is_ptr = TREE_CODE (type) == POINTER_TYPE;
  if (is_ptr || TREE_CODE (type) == REFERENCE_TYPE)
  if (is_ptr || TREE_CODE (type) == REFERENCE_TYPE)
    {
    {
      tree core = TREE_TYPE (type);
      tree core = TREE_TYPE (type);
 
 
      if (is_ptr && VOID_TYPE_P (core))
      if (is_ptr && VOID_TYPE_P (core))
        /* OK */;
        /* OK */;
      else if (!complete_type_or_else (core, from))
      else if (!complete_type_or_else (core, from))
        return 0;
        return 0;
    }
    }
  return 1;
  return 1;
}
}
 
 
/* Return truth-value if EXPRESSION is admissible in throw-expression,
/* 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
   i.e. if it is not of incomplete type or a pointer/reference to such
   a type or of an abstract class type.  */
   a type or of an abstract class type.  */
 
 
static bool
static bool
is_admissible_throw_operand (tree expr)
is_admissible_throw_operand (tree expr)
{
{
  tree type = TREE_TYPE (expr);
  tree type = TREE_TYPE (expr);
 
 
  /* 15.1/4 [...] The type of the throw-expression shall not be an
  /* 15.1/4 [...] The type of the throw-expression shall not be an
            incomplete type, or a pointer or a reference to an incomplete
            incomplete type, or a pointer or a reference to an incomplete
            type, other than void*, const void*, volatile void*, or
            type, other than void*, const void*, volatile void*, or
            const volatile void*.  Except for these restriction and the
            const volatile void*.  Except for these restriction and the
            restrictions on type matching mentioned in 15.3, the operand
            restrictions on type matching mentioned in 15.3, the operand
            of throw is treated exactly as a function argument in a call
            of throw is treated exactly as a function argument in a call
            (5.2.2) or the operand of a return statement.  */
            (5.2.2) or the operand of a return statement.  */
  if (!complete_ptr_ref_or_void_ptr_p (type, expr))
  if (!complete_ptr_ref_or_void_ptr_p (type, expr))
    return false;
    return false;
 
 
  /* 10.4/3 An abstract class shall not be used as a parameter type,
  /* 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
            as a function return type or as type of an explicit
            conversion.  */
            conversion.  */
  else if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
  else if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
    {
    {
      error ("expression %qE of abstract class type %qT cannot "
      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;
      return false;
    }
    }
 
 
  return true;
  return true;
}
}
 
 
/* Returns nonzero if FN is a declaration of a standard C library
/* Returns nonzero if FN is a declaration of a standard C library
   function which is known not to throw.
   function which is known not to throw.
 
 
   [lib.res.on.exception.handling]: None of the functions from the
   [lib.res.on.exception.handling]: None of the functions from the
   Standard C library shall report an error by throwing an
   Standard C library shall report an error by throwing an
   exception, unless it calls a program-supplied function that
   exception, unless it calls a program-supplied function that
   throws an exception.  */
   throws an exception.  */
 
 
#include "cfns.h"
#include "cfns.h"
 
 
int
int
nothrow_libfn_p (const_tree fn)
nothrow_libfn_p (const_tree fn)
{
{
  tree id;
  tree id;
 
 
  if (TREE_PUBLIC (fn)
  if (TREE_PUBLIC (fn)
      && DECL_EXTERNAL (fn)
      && DECL_EXTERNAL (fn)
      && DECL_NAMESPACE_SCOPE_P (fn)
      && DECL_NAMESPACE_SCOPE_P (fn)
      && DECL_EXTERN_C_P (fn))
      && DECL_EXTERN_C_P (fn))
    /* OK */;
    /* OK */;
  else
  else
    /* Can't be a C library function.  */
    /* Can't be a C library function.  */
    return 0;
    return 0;
 
 
  /* Being a C library function, DECL_ASSEMBLER_NAME == DECL_NAME
  /* Being a C library function, DECL_ASSEMBLER_NAME == DECL_NAME
     unless the system headers are playing rename tricks, and if
     unless the system headers are playing rename tricks, and if
     they are, we don't want to be confused by them.  */
     they are, we don't want to be confused by them.  */
  id = DECL_NAME (fn);
  id = DECL_NAME (fn);
  return !!libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
  return !!libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
}
}
 
 
/* Returns nonzero if an exception of type FROM will be caught by a
/* Returns nonzero if an exception of type FROM will be caught by a
   handler for type TO, as per [except.handle].  */
   handler for type TO, as per [except.handle].  */
 
 
static int
static int
can_convert_eh (tree to, tree from)
can_convert_eh (tree to, tree from)
{
{
  to = non_reference (to);
  to = non_reference (to);
  from = non_reference (from);
  from = non_reference (from);
 
 
  if (TREE_CODE (to) == POINTER_TYPE && TREE_CODE (from) == POINTER_TYPE)
  if (TREE_CODE (to) == POINTER_TYPE && TREE_CODE (from) == POINTER_TYPE)
    {
    {
      to = TREE_TYPE (to);
      to = TREE_TYPE (to);
      from = TREE_TYPE (from);
      from = TREE_TYPE (from);
 
 
      if (! at_least_as_qualified_p (to, from))
      if (! at_least_as_qualified_p (to, from))
        return 0;
        return 0;
 
 
      if (TREE_CODE (to) == VOID_TYPE)
      if (TREE_CODE (to) == VOID_TYPE)
        return 1;
        return 1;
 
 
      /* Else fall through.  */
      /* Else fall through.  */
    }
    }
 
 
  if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
  if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
      && PUBLICLY_UNIQUELY_DERIVED_P (to, from))
      && PUBLICLY_UNIQUELY_DERIVED_P (to, from))
    return 1;
    return 1;
 
 
  return 0;
  return 0;
}
}
 
 
/* Check whether any of the handlers in I are shadowed by another handler
/* 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
   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
   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
   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.  */
   the handler for B would catch an exception of type C.  */
 
 
static void
static void
check_handlers_1 (tree master, tree_stmt_iterator i)
check_handlers_1 (tree master, tree_stmt_iterator i)
{
{
  tree type = TREE_TYPE (master);
  tree type = TREE_TYPE (master);
 
 
  for (; !tsi_end_p (i); tsi_next (&i))
  for (; !tsi_end_p (i); tsi_next (&i))
    {
    {
      tree handler = tsi_stmt (i);
      tree handler = tsi_stmt (i);
      if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
      if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
        {
        {
          warning_at (EXPR_LOCATION (handler), 0,
          warning_at (EXPR_LOCATION (handler), 0,
                      "exception of type %qT will be caught",
                      "exception of type %qT will be caught",
                      TREE_TYPE (handler));
                      TREE_TYPE (handler));
          warning_at (EXPR_LOCATION (master), 0,
          warning_at (EXPR_LOCATION (master), 0,
                      "   by earlier handler for %qT", type);
                      "   by earlier handler for %qT", type);
          break;
          break;
        }
        }
    }
    }
}
}
 
 
/* Given a STATEMENT_LIST of HANDLERs, make sure that they're OK.  */
/* Given a STATEMENT_LIST of HANDLERs, make sure that they're OK.  */
 
 
void
void
check_handlers (tree handlers)
check_handlers (tree handlers)
{
{
  tree_stmt_iterator i;
  tree_stmt_iterator i;
 
 
  /* If we don't have a STATEMENT_LIST, then we've just got one
  /* If we don't have a STATEMENT_LIST, then we've just got one
     handler, and thus nothing to warn about.  */
     handler, and thus nothing to warn about.  */
  if (TREE_CODE (handlers) != STATEMENT_LIST)
  if (TREE_CODE (handlers) != STATEMENT_LIST)
    return;
    return;
 
 
  i = tsi_start (handlers);
  i = tsi_start (handlers);
  if (!tsi_end_p (i))
  if (!tsi_end_p (i))
    while (1)
    while (1)
      {
      {
        tree handler = tsi_stmt (i);
        tree handler = tsi_stmt (i);
        tsi_next (&i);
        tsi_next (&i);
 
 
        /* No more handlers; nothing to shadow.  */
        /* No more handlers; nothing to shadow.  */
        if (tsi_end_p (i))
        if (tsi_end_p (i))
          break;
          break;
        if (TREE_TYPE (handler) == NULL_TREE)
        if (TREE_TYPE (handler) == NULL_TREE)
          permerror (EXPR_LOCATION (handler), "%<...%>"
          permerror (EXPR_LOCATION (handler), "%<...%>"
                     " handler must be the last handler for its try block");
                     " handler must be the last handler for its try block");
        else
        else
          check_handlers_1 (handler, i);
          check_handlers_1 (handler, i);
      }
      }
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.