diff options
Diffstat (limited to 'gcc/cp/except.c')
-rw-r--r-- | gcc/cp/except.c | 206 |
1 files changed, 182 insertions, 24 deletions
diff --git a/gcc/cp/except.c b/gcc/cp/except.c index 6f7f70ad804..79bab76cc5a 100644 --- a/gcc/cp/except.c +++ b/gcc/cp/except.c @@ -31,7 +31,6 @@ along with GCC; see the file COPYING3. If not see #include "cp-tree.h" #include "flags.h" #include "output.h" -#include "except.h" #include "toplev.h" #include "tree-inline.h" #include "tree-iterator.h" @@ -50,7 +49,6 @@ static tree wrap_cleanups_r (tree *, int *, void *); static int complete_ptr_ref_or_void_ptr_p (tree, tree); static bool is_admissible_throw_operand (tree); static int can_convert_eh (tree, tree); -static tree cp_protect_cleanup_actions (void); /* Sets up all the global eh stuff that needs to be initialized at the start of compilation. */ @@ -62,25 +60,22 @@ init_exception_processing (void) /* void std::terminate (); */ push_namespace (std_identifier); - tmp = build_function_type (void_type_node, void_list_node); + 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; pop_namespace (); /* void __cxa_call_unexpected(void *); */ - tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node); - tmp = build_function_type (void_type_node, tmp); + tmp = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE); call_unexpected_node = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp); - - lang_protect_cleanup_actions = &cp_protect_cleanup_actions; } /* Returns an expression to be executed if an unhandled exception is propagated out of a cleanup region. */ -static tree +tree cp_protect_cleanup_actions (void) { /* [except.terminate] @@ -160,8 +155,9 @@ build_exc_ptr (void) static tree declare_nothrow_library_fn (tree name, tree return_type, tree parm_type) { - 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_list (return_type, + parm_type, + NULL_TREE), empty_except_spec); } @@ -407,6 +403,30 @@ initialize_handler_parm (tree decl, tree exp) LOOKUP_ONLYCONVERTING|DIRECT_BIND); } + +/* Routine to see if exception handling is turned on. + DO_WARN is nonzero if we want to inform the user that exception + handling is turned off. + + This is used to ensure that -fexceptions has been specified if the + compiler tries to use any exception-specific functions. */ + +static inline int +doing_eh (void) +{ + if (! flag_exceptions) + { + static int warned = 0; + if (! warned) + { + error ("exception handling disabled, use -fexceptions to enable"); + warned = 1; + } + return 0; + } + return 1; +} + /* Call this to start a catch block. DECL is the catch parameter. */ tree @@ -415,7 +435,7 @@ expand_start_catch_block (tree decl) tree exp; tree type, init; - if (! doing_eh (1)) + if (! doing_eh ()) return NULL_TREE; /* Make sure this declaration is reasonable. */ @@ -494,7 +514,7 @@ expand_start_catch_block (tree decl) void expand_end_catch_block (void) { - if (! doing_eh (1)) + if (! doing_eh ()) return; /* The exception being handled is rethrown if control reaches the end of @@ -641,7 +661,7 @@ build_throw (tree exp) return error_mark_node; } - if (! doing_eh (1)) + if (! doing_eh ()) return error_mark_node; if (exp && decl_is_java_type (TREE_TYPE (exp), 1)) @@ -650,8 +670,9 @@ build_throw (tree exp) if (!get_global_value_if_present (fn, &fn)) { /* Declare void _Jv_Throw (void *). */ - tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node); - tmp = build_function_type (ptr_type_node, tmp); + 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)) @@ -675,9 +696,8 @@ build_throw (tree exp) /* The CLEANUP_TYPE is the internal type of a destructor. */ if (!cleanup_type) { - tmp = void_list_node; - tmp = tree_cons (NULL_TREE, ptr_type_node, tmp); - tmp = build_function_type (void_type_node, tmp); + tmp = build_function_type_list (void_type_node, + ptr_type_node, NULL_TREE); cleanup_type = build_pointer_type (tmp); } @@ -686,11 +706,9 @@ build_throw (tree exp) { /* Declare void __cxa_throw (void*, void*, void (*)(void*)). */ /* ??? Second argument is supposed to be "std::type_info*". */ - tmp = void_list_node; - tmp = tree_cons (NULL_TREE, cleanup_type, tmp); - tmp = tree_cons (NULL_TREE, ptr_type_node, tmp); - tmp = tree_cons (NULL_TREE, ptr_type_node, tmp); - tmp = build_function_type (void_type_node, tmp); + 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); } @@ -808,7 +826,7 @@ build_throw (tree exp) { /* Declare void __cxa_rethrow (void). */ fn = push_throw_library_fn - (fn, build_function_type (void_type_node, void_list_node)); + (fn, build_function_type_list (void_type_node, NULL_TREE)); } /* ??? Indicate that this function call allows exceptions of the type @@ -998,3 +1016,143 @@ check_handlers (tree handlers) check_handlers_1 (handler, i); } } + +/* walk_tree helper for finish_noexcept_expr. Returns non-null if the + expression *TP causes the noexcept operator to evaluate to false. + + 5.3.7 [expr.noexcept]: The result of the noexcept operator is false if + in a potentially-evaluated context the expression would contain + * a potentially evaluated call to a function, member function, + function pointer, or member function pointer that does not have a + non-throwing exception-specification (15.4), + * a potentially evaluated throw-expression (15.1), + * a potentially evaluated dynamic_cast expression dynamic_cast<T>(v), + where T is a reference type, that requires a run-time check (5.2.7), or + * a potentially evaluated typeid expression (5.2.8) applied to a glvalue + 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) +{ + tree t = *tp; + enum tree_code code = TREE_CODE (t); + if (code == CALL_EXPR + || code == AGGR_INIT_EXPR) + { + /* We can only use the exception specification of the called function + for determining the value of a noexcept expression; we can't use + TREE_NOTHROW, as it might have a different value in another + 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)); + + STRIP_NOPS (fn); + if (TREE_CODE (fn) == ADDR_EXPR) + { + /* 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) + && (DECL_ARTIFICIAL (fn) + || nothrow_libfn_p (fn))) + return TREE_NOTHROW (fn) ? NULL_TREE : fn; + } + if (!TYPE_NOTHROW_P (type)) + return fn; + } + + return NULL_TREE; +} + +/* Evaluate noexcept ( EXPR ). */ + +tree +finish_noexcept_expr (tree expr, tsubst_flags_t complain) +{ + tree fn; + + if (processing_template_decl) + return build_min (NOEXCEPT_EXPR, boolean_type_node, expr); + + fn = cp_walk_tree_without_duplicates (&expr, check_noexcept_r, 0); + if (fn) + { + if ((complain & tf_warning) && TREE_CODE (fn) == FUNCTION_DECL + && TREE_NOTHROW (fn) && !DECL_ARTIFICIAL (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); + } + return boolean_false_node; + } + else + return boolean_true_node; +} + +/* Return true iff SPEC is throw() or noexcept(true). */ + +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 + || spec == noexcept_true_spec) + return true; + gcc_assert (processing_template_decl + || TREE_PURPOSE (spec) == error_mark_node); + return false; +} + +/* For FUNCTION_TYPE or METHOD_TYPE, true if NODE is noexcept. This is the + case for things declared noexcept(true) and, with -fnothrow-opt, for + throw() functions. */ + +bool +type_noexcept_p (const_tree type) +{ + tree spec = TYPE_RAISES_EXCEPTIONS (type); + if (flag_nothrow_opt) + return nothrow_spec_p (spec); + else + return spec == noexcept_true_spec; +} + +/* For FUNCTION_TYPE or METHOD_TYPE, true if NODE can throw any type, + i.e. no exception-specification or noexcept(false). */ + +bool +type_throw_all_p (const_tree type) +{ + tree spec = TYPE_RAISES_EXCEPTIONS (type); + return spec == NULL_TREE || spec == noexcept_false_spec; +} + +/* Create a representation of the noexcept-specification with + constant-expression of EXPR. COMPLAIN is as for tsubst. */ + +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; + else + { + gcc_assert (processing_template_decl || expr == error_mark_node); + return build_tree_list (expr, NULL_TREE); + } +} |