Skip to content

Commit 4a07a54

Browse files
C++26: Library support for exceptions in constant evaluation [GH #368]
1 parent f5dab52 commit 4a07a54

46 files changed

Lines changed: 1676 additions & 142 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/cp_gen_be.c‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7260,14 +7260,12 @@ successfully emitted.
72607260
#endif /* TEMPLATE_INSTANTIATIONS_IN_SOURCE_SEQUENCE_LISTS */
72617261
#if BUILTIN_FUNCTIONS_ENABLED
72627262
if (use_name_reference &&
7263-
(nrp->special_kind ==
7264-
(a_special_function_kind)sfk_builtin_operator_new ||
7265-
nrp->special_kind ==
7266-
(a_special_function_kind)sfk_builtin_operator_delete ||
7267-
nrp->special_kind ==
7268-
(a_special_function_kind)sfk_gnu_atomic_nongeneric_function ||
7269-
nrp->special_kind ==
7270-
(a_special_function_kind)sfk_gnu_sync_concrete_function)) {
7263+
(special_kind_is(nrp, sfk_builtin_operator_new) ||
7264+
special_kind_is(nrp, sfk_builtin_operator_delete) ||
7265+
special_kind_is(nrp, sfk_builtin_current_exception) ||
7266+
special_kind_is(nrp, sfk_builtin_uncaught_exceptions) ||
7267+
special_kind_is(nrp, sfk_gnu_atomic_nongeneric_function) ||
7268+
special_kind_is(nrp, sfk_gnu_sync_concrete_function))) {
72717269
/* These are special cases handled by gen_name_from_routine_node. */
72727270
use_name_reference = FALSE;
72737271
} /* if */
@@ -7438,7 +7436,9 @@ qualified is TRUE, force the generation of a qualified name.
74387436
(special_kind_is(nrp, sfk_gnu_sync_concrete_function) ||
74397437
special_kind_is(nrp, sfk_gnu_atomic_nongeneric_function) ||
74407438
special_kind_is(nrp, sfk_builtin_operator_new) ||
7441-
special_kind_is(nrp, sfk_builtin_operator_delete))) {
7439+
special_kind_is(nrp, sfk_builtin_operator_delete) ||
7440+
special_kind_is(nrp, sfk_builtin_current_exception) ||
7441+
special_kind_is(nrp, sfk_builtin_uncaught_exceptions))) {
74427442
switch (nrp->special_kind) {
74437443
case sfk_gnu_sync_concrete_function:
74447444
case sfk_gnu_atomic_nongeneric_function:
@@ -7484,6 +7484,12 @@ qualified is TRUE, force the generation of a qualified name.
74847484
case sfk_builtin_operator_delete:
74857485
write_tok_str("__builtin_operator_delete");
74867486
break;
7487+
case sfk_builtin_current_exception:
7488+
write_tok_str("__builtin_current_exception");
7489+
break;
7490+
case sfk_builtin_uncaught_exceptions:
7491+
write_tok_str("__builtin_uncaught_exceptions");
7492+
break;
74877493
default:
74887494
unexpected_condition();
74897495
} /* switch */

‎src/decls.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10585,6 +10585,14 @@ skip_overloading:;
1058510585
locator->symbol_header->has_intrinsic_name) {
1058610586
check_for_constexpr_intrinsic(routine_ptr, locator->symbol_header);
1058710587
} /* if */
10588+
} else if (!redeclaration && innermost_function_scope == NULL &&
10589+
locator->symbol_header != NULL &&
10590+
locator->symbol_header->has_intrinsic_name) {
10591+
/* Some library functions that can be evaluated during constant evaluation
10592+
are not necessarily declared constexpr (e.g., the GCC C++ standard
10593+
library, as of GCC 16, does not declare std::current_exception
10594+
constexpr). See check_for_constexpr_intrinsic. */
10595+
check_for_constexpr_intrinsic(routine_ptr, locator->symbol_header);
1058810596
} /* if */
1058910597
#if GNU_EXTENSIONS_ALLOWED
1059010598
if (gcc_pragma_options_stack != NULL && !func_info->is_main_function) {

‎src/error_msg.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,5 +3689,8 @@ ec_constexpr_exception_escapes_nonthrowing;;"std::terminate is called because an
36893689
ec_constexpr_exception_during_unwinding;;"std::terminate is called because an exception escapes from a destructor during stack unwinding"
36903690
ec_constexpr_exception_in_handler_init;;"std::terminate is called because an exception escapes from the initialization of a handler parameter"
36913691
ec_constexpr_exception_from_exception_dtor;;"std::terminate is called because the destructor of an exception object exits by throwing an exception"
3692+
ec_constexpr_exception_thrown_here;;"the exception was thrown here"
3693+
ec_constexpr_exception_object_not_destroyed;;"an exception object of type %t is not destroyed at the end of constant evaluation"
3694+
ec_constexpr_rethrow_null_exception_ptr;;"std::rethrow_exception is called with a null std::exception_ptr"
36923695
###############################################################################
36933696

‎src/expr.c‎

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5956,14 +5956,15 @@ typedef struct a_builtin_call_adjustment {
59565956
arguments, this field will be NULL. */
59575957
a_symbol_ptr
59585958
overloaded_function_symbol;
5959-
/* If non-NULL, the symbol for operator new/delete
5960-
to be used in place of __builtin_operator_new/
5961-
builtin_operator_delete. */
5959+
/* If non-NULL, the symbol for the function to be
5960+
called in place of the builtin (e.g., operator
5961+
new/delete in place of __builtin_operator_new/
5962+
__builtin_operator_delete). */
59625963
a_name_reference_ptr
59635964
name_reference;
59645965
/* If non-NULL, the special name reference to use
5965-
to represent __builtin_operator_new/
5966-
__builtin_operator_delete to a back end. */
5966+
to represent the builtin (e.g.,
5967+
__builtin_operator_new) to a back end. */
59675968
int n_args; /* The number of arguments the builtin expects. */
59685969
int dispatch_arg;
59695970
/* The argument number of the "dispatch type", if any.
@@ -6257,6 +6258,38 @@ be called to check and adjust the argument and routine types as needed.
62576258
} /* if */
62586259
}
62596260
break;
6261+
case bufk_current_exception:
6262+
case bufk_uncaught_exceptions:
6263+
{ /* The GCC C++ standard library uses __builtin_current_exception in
6264+
constexpr functions, since std::current_exception is not a
6265+
constexpr function. The builtin is equivalent to
6266+
std::current_exception, and so the call is replaced by a call to
6267+
that function, with a name reference that lets a back end recreate
6268+
the original source and that lets the call be evaluated during
6269+
constant evaluation (see nonconstexpr_intrinsic_call_allowed in
6270+
interpret.c). Likewise for __builtin_uncaught_exceptions and
6271+
std::uncaught_exceptions. If the std function is not declared,
6272+
the builtin is kept (and __builtin_uncaught_exceptions is then
6273+
evaluated directly; see do_constexpr_builtin_function). */
6274+
a_symbol_ptr sym = look_up_name_string_in_std(
6275+
bfk == bufk_current_exception
6276+
? "current_exception"
6277+
: "uncaught_exceptions");
6278+
if (sym != NULL) sym = fundamental_symbol_of(sym);
6279+
if (sym != NULL && (symbol_is(sym, sk_routine) ||
6280+
symbol_is(sym, sk_overloaded_function))) {
6281+
bcap->overloaded_function_symbol = sym;
6282+
bcap->name_reference = alloc_name_reference();
6283+
bcap->name_reference->special_kind =
6284+
bfk == bufk_current_exception
6285+
? sfk_builtin_current_exception
6286+
: sfk_builtin_uncaught_exceptions;
6287+
bcap->callback = nullptr;
6288+
} else {
6289+
requires_processing = FALSE;
6290+
} /* if */
6291+
}
6292+
break;
62606293
case bfk_elementwise_abs:
62616294
case bfk_elementwise_acos:
62626295
case bfk_elementwise_asin:
@@ -8180,6 +8213,7 @@ and bound_function_selector are expected to be NULL in that case.
81808213
an_expr_node_ptr castexp;
81818214
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
81828215
a_boolean call_folded_to_constant = FALSE;
8216+
an_expr_node_ptr function_call_node = NULL;
81838217
#if BUILTIN_FUNCTIONS_ENABLED
81848218
a_routine_ptr operand_routine;
81858219
a_builtin_call_adjustment
@@ -8762,8 +8796,9 @@ and bound_function_selector are expected to be NULL in that case.
87628796
routine = NULL;
87638797
unknown_dependent_function = FALSE;
87648798
if (bcap->overloaded_function_symbol != NULL) {
8765-
/* The builtin is being replaced by a call to a global overloaded
8766-
operator function; use that symbol to do the overload resolution. */
8799+
/* The builtin is being replaced by a call to another function (e.g.,
8800+
a global overloaded operator function); use that symbol to do the
8801+
overload resolution. */
87678802
overloaded_function_symbol = bcap->overloaded_function_symbol;
87688803
overloaded_function_case = TRUE;
87698804
operand->is_qualified_name = TRUE;
@@ -9060,7 +9095,6 @@ and bound_function_selector are expected to be NULL in that case.
90609095
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
90619096
} /* if */
90629097
} /* if */
9063-
an_expr_node_ptr function_call_node = NULL;
90649098
assemble_function_call(operand, bound_function_selector, argument_list,
90659099
/*compiler_generated=*/FALSE,
90669100
arg_dep_lookup_suppressed,
@@ -9104,13 +9138,15 @@ and bound_function_selector are expected to be NULL in that case.
91049138
#if BUILTIN_FUNCTIONS_ENABLED
91059139
if (bcap != NULL) {
91069140
if (bcap->name_reference != NULL && !is_error_operand(result)) {
9107-
/* The builtin call has been replaced by a call to the appropriate
9108-
operator new/delete; use a name reference to communicate this to the
9109-
back end so the original source can be recreated. */
9141+
/* The builtin call has been replaced by a call to another function
9142+
(e.g., the appropriate operator new/delete); use a name reference to
9143+
communicate this to the back end so the original source can be
9144+
recreated. (The call node can be below the expression in result,
9145+
e.g., when the function returns a class.) */
91109146
an_expr_node_ptr operand_node;
9111-
check_assertion(result->kind == ok_expression &&
9112-
node_is_operator(result->variant.expression, eok_call));
9113-
operand_node = result->variant.expression->variant.operation.operands;
9147+
check_assertion(function_call_node != NULL &&
9148+
node_is_operator(function_call_node, eok_call));
9149+
operand_node = function_call_node->variant.operation.operands;
91149150
switch (operand_node->kind) {
91159151
case enk_routine:
91169152
operand_node->variant.routine.name_reference = bcap->name_reference;

‎src/folding.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11043,6 +11043,7 @@ pseudo_call can be NULL if that information is not needed.
1104311043
case bufk_FILE_NAME:
1104411044
case bfk_FUNCTION:
1104511045
case bufk_FUNCSIG:
11046+
case bufk_eh_ptr_adjust_ref:
1104611047
case bfk_add_overflow_p:
1104711048
case bfk_add_overflow:
1104811049
case bfk_uadd_overflow:

‎src/ifc_modules_write.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,8 +2137,10 @@ Return the declaration index for the routine.
21372137
/* FIXME: Implement these. */
21382138
break;
21392139
#if BUILTIN_FUNCTIONS_ENABLED
2140+
case sfk_builtin_current_exception:
21402141
case sfk_builtin_operator_delete:
21412142
case sfk_builtin_operator_new:
2143+
case sfk_builtin_uncaught_exceptions:
21422144
#endif /* BUILTIN_FUNCTIONS_ENABLED */
21432145
case sfk_deduction_guide:
21442146
#if MICROSOFT_EXTENSIONS_ALLOWED
@@ -3122,8 +3124,10 @@ for the function type.
31223124
/* FIXME: Implement these. */
31233125
break;
31243126
#if BUILTIN_FUNCTIONS_ENABLED
3127+
case sfk_builtin_current_exception:
31253128
case sfk_builtin_operator_delete:
31263129
case sfk_builtin_operator_new:
3130+
case sfk_builtin_uncaught_exceptions:
31273131
#endif /* BUILTIN_FUNCTIONS_ENABLED */
31283132
case sfk_deduction_guide:
31293133
#if MICROSOFT_EXTENSIONS_ALLOWED

‎src/il_def.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,6 +2627,12 @@ enum a_special_function_kind : a_byte {
26272627
sfk_builtin_operator_delete,
26282628
/* Represents __builtin_operator_delete (which has been
26292629
replaced by operator delete). */
2630+
sfk_builtin_current_exception,
2631+
/* Represents __builtin_current_exception (which has
2632+
been replaced by std::current_exception). */
2633+
sfk_builtin_uncaught_exceptions,
2634+
/* Represents __builtin_uncaught_exceptions (which has
2635+
been replaced by std::uncaught_exceptions). */
26302636
#endif /* BUILTIN_FUNCTIONS_ENABLED */
26312637
sfk_last /* Must be last. */
26322638
};
@@ -4594,6 +4600,8 @@ EXTERN_CONSTINIT_ARRAY(a_const_char*, db_special_function_kinds, sfk_last + 1)
45944600
"gnu atomic generic function",
45954601
"__builtin_operator_new",
45964602
"__builtin_operator_delete",
4603+
"__builtin_current_exception",
4604+
"__builtin_uncaught_exceptions",
45974605
#endif /* BUILTIN_FUNCTIONS_ENABLED */
45984606
"last" /* used to check that initialization is right. */
45994607
}

‎src/il_display.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3575,6 +3575,12 @@ Print the name of a special function kind.
35753575
case sfk_builtin_operator_delete:
35763576
s = "sfk_builtin_operator_delete";
35773577
break;
3578+
case sfk_builtin_current_exception:
3579+
s = "sfk_builtin_current_exception";
3580+
break;
3581+
case sfk_builtin_uncaught_exceptions:
3582+
s = "sfk_builtin_uncaught_exceptions";
3583+
break;
35783584
#endif /* BUILTIN_FUNCTIONS_ENABLED */
35793585
default: s = "**BAD SPECIAL FUNCTION KIND**";
35803586
} /* switch */

0 commit comments

Comments
 (0)