Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/pending_changes/gh-623-constexpr-deferred-instantiation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[GH #623]
Deferred instantiations during constexpr template instantiation

Previously, instantiations that had been deferred (for example, that of a
constexpr destructor needed by a default argument) could be performed in the
middle of instantiating a constexpr function template instance or a variable
template instance, such as when that instantiation caused a class template to
be instantiated. If a deferred instantiation constant-evaluated the instance
whose definition was not yet complete, a spurious error was issued. Such
instantiations are now deferred until the instance is complete. For example,
with --c++20:

template <class T> struct A {};
template <class T> constexpr int f() { return sizeof(A<T>); }
template <class T> struct B {
constexpr ~B() { static_assert(f<T>()); } // Previously an error.
};
struct S {
void g(B<int> = {}) { f<int>(); }
};

The same error was issued if f was replaced by a variable template.
14 changes: 14 additions & 0 deletions src/symbol_tbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,20 @@ be NULL, in which case nothing is done.
} /* free_list_of_symbol_list_entries */


void a_symbol_list_segment::append(a_symbol_list_entry_ptr slep)
/*
Append the given symbol list entry to *this.
*/
{
if (this->head == NULL) {
this->head = slep;
} else {
this->tail->next = slep;
} /* if */
this->tail = slep;
} /* a_symbol_list_segment::append */


static void set_identifier_for_symbol_header(
a_symbol_header_ptr hdr_ptr,
a_const_char *string,
Expand Down
16 changes: 16 additions & 0 deletions src/symbol_tbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,22 @@ typedef struct a_symbol_list_entry {
} a_symbol_list_entry;


typedef struct a_symbol_list_segment {
/* The first and last entries of a list, or part of a list, of symbol list
entries. Both are NULL if the segment is empty. Ownership of the
entries is not implied. */
a_symbol_list_entry_ptr
head;
/* The first entry of the segment. */
a_symbol_list_entry_ptr
tail;
/* The last entry of the segment. */
void append(a_symbol_list_entry_ptr slep);
void clear()
{ this->head = NULL; this->tail = NULL; }
} a_symbol_list_segment;


typedef struct a_type_list_entry {
/* Entry created to produce a list of types for some special purpose.
For example, such a list is used when building a list of associated
Expand Down
102 changes: 67 additions & 35 deletions src/templates.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,13 @@ STATIC_THREAD a_symbol_list_entry_ptr
exported_templates_tail;
/* The end of the exported templates list. */

STATIC_THREAD a_symbol_list_entry_ptr
STATIC_THREAD a_symbol_list_segment
deferred_instantiations;
/* A list of symbol entries for instantiations that
were deferred while a class definition was
pending. These entities are instantiated when
a class definition is no longer pending. */

STATIC_THREAD a_symbol_list_entry_ptr
deferred_instantiations_tail;
/* The end of the deferred_instantiations list. */

STATIC_THREAD a_partial_order_candidate_ptr
avail_partial_order_candidates;
/* Previously allocated entries available for reuse. */
Expand Down Expand Up @@ -7629,12 +7625,50 @@ done:;
} /* instantiate_template_function_full */


static void save_deferred_instantiations(a_symbol_list_segment *p_saved)
/*
Empty the deferred_instantiations list, returning its entries in *p_saved, so
that further requests can be processed separately.
*/
{
*p_saved = deferred_instantiations;
deferred_instantiations.clear();
} /* save_deferred_instantiations */


static void restore_deferred_instantiations(a_symbol_list_segment saved)
/*
Put the requests saved by save_deferred_instantiations back at the front of
the deferred_instantiations list.
*/
{
if (saved.head != NULL) {
saved.tail->next = deferred_instantiations.head;
deferred_instantiations.head = saved.head;
if (deferred_instantiations.tail == NULL) {
deferred_instantiations.tail = saved.tail;
} /* if */
} /* if */
} /* restore_deferred_instantiations */


static void instantiate_template_function(a_template_instance_ptr tip)
/*
Instantiate the body of the template function associated with tip.
*/
{
a_routine_ptr routine = tip->instance_sym->variant.routine.ptr;
a_routine_ptr routine = tip->instance_sym->variant.routine.ptr;
a_boolean is_constexpr = routine->is_constexpr;
a_symbol_list_segment saved_deferred_instantiations = { NULL, NULL };

if (is_constexpr) {
/* Deferred instantiation requests may constant-evaluate this function,
so the ones already made must wait until its body is complete. New
requests are handled as usual: immediate ones so that consteval
escalation (P2564R3) can see them, and deferred ones while this
function is still in the instantiation context of diagnostics. */
save_deferred_instantiations(&saved_deferred_instantiations);
} /* if */

#if GNU_FUNCTION_MULTIVERSIONING
/* For a GNU multiversion target-versioned function, instantiate all of the
Expand All @@ -7658,6 +7692,12 @@ Instantiate the body of the template function associated with tip.
{
instantiate_template_function_full(tip, routine);
} /* if */
restore_deferred_instantiations(saved_deferred_instantiations);
if (saved_deferred_instantiations.head != NULL &&
defer_instantiations == 0) {
/* The body is complete, so the saved requests can now be processed. */
process_deferred_instantiation_requests();
} /* if */
} /* instantiate_template_function */

#if ONE_INSTANTIATION_PER_OBJECT
Expand Down Expand Up @@ -7729,7 +7769,12 @@ expression context) rather than a declaration.
a_template_cache_ptr body_cache, decl_cache;
int idx = 0;
a_host_large_integer intrinsic_val;
a_symbol_list_segment saved_deferred_instantiations;

/* Deferred instantiation requests may use the value of this variable in a
constant expression, so the ones already made must wait until its
initializer is complete. */
save_deferred_instantiations(&saved_deferred_instantiations);
is_var_templ_instance = symbol_is(var_sym, sk_variable);
tssp = template_supplement_for_symbol(template_sym);
if (template_sym->header->has_intrinsic_name) {
Expand Down Expand Up @@ -8134,6 +8179,13 @@ expression context) rather than a declaration.
is now present. */
establish_variable_instantiation_corresp(var_ptr);
done:
restore_deferred_instantiations(saved_deferred_instantiations);
if (saved_deferred_instantiations.head != NULL &&
defer_instantiations == 0) {
/* The initializer is complete, so the saved requests can now be
processed. */
process_deferred_instantiation_requests();
} /* if */
return;
} /* instantiate_template_variable */

Expand Down Expand Up @@ -21425,8 +21477,7 @@ mode in-class specialization.
a_type_ptr parent_class;
a_boolean trans_unit_pushed;
a_boolean is_deduction_guide;
a_symbol_list_entry_ptr saved_deferred_instantiations;
a_symbol_list_entry_ptr saved_deferred_instantiations_tail;
a_symbol_list_segment saved_deferred_instantiations;

db_enter(4, "make_template_function");
#if CHECKING
Expand All @@ -21450,10 +21501,7 @@ mode in-class specialization.
instance. This is done to make sure a second reference to this routine
is not attempted before the routine is completed. */
defer_instantiations++;
saved_deferred_instantiations = deferred_instantiations;
saved_deferred_instantiations_tail = deferred_instantiations_tail;
deferred_instantiations = NULL;
deferred_instantiations_tail = NULL;
save_deferred_instantiations(&saved_deferred_instantiations);
parent_class = templ_sym->is_class_member ? sym_parent_class(templ_sym)
: (a_type_ptr)NULL;
rp = alloc_routine();
Expand Down Expand Up @@ -21986,18 +22034,7 @@ mode in-class specialization.
/* Do any instantiations that were deferred while this routine was being
created. */
process_deferred_class_fixups_and_instantiations(/*for_instantiation=*/TRUE);
if (saved_deferred_instantiations == NULL) {
/* The current values should be used as the list was previously empty. */
} else {
/* Add the new list to the end of the previous list. */
saved_deferred_instantiations_tail->next = deferred_instantiations;
deferred_instantiations = saved_deferred_instantiations;
if (deferred_instantiations_tail == NULL) {
/* If the new list is non-NULL, it now already points to the list to be
used. */
deferred_instantiations_tail = saved_deferred_instantiations_tail;
} /* if */
} /* if */
restore_deferred_instantiations(saved_deferred_instantiations);
if (special_kind_is(rp, sfk_deduction_guide)) {
/* Don't do exception specification processing for deduction guides. */
} else if (ms_version_is(<1920) || !rp->source_corresp.is_class_member) {
Expand Down Expand Up @@ -40060,11 +40097,7 @@ unless the SIR_CLEAR_VALUE flag is set in "options".
slep = alloc_symbol_list_entry();
slep->symbol = sym;
/* Add this entry to the end of the deferred instantiations list. */
if (deferred_instantiations == NULL) deferred_instantiations = slep;
if (deferred_instantiations_tail != NULL) {
deferred_instantiations_tail->next = slep;
} /* if */
deferred_instantiations_tail = slep;
deferred_instantiations.append(slep);
check_assertion(!defer_instantiation); /* Suppress set-but-never-used. */
defer_instantiation = TRUE;
} else {
Expand Down Expand Up @@ -40303,16 +40336,16 @@ update_instantiation_required_flag to do the appropriate processing.
curr_class_fixup_header(/*for_instantiation=*/TRUE)->
pending_class_definitions == 0) {
deferred_instantiations_in_process = TRUE;
for (slep = deferred_instantiations; slep != NULL; slep = slep->next) {
for (slep = deferred_instantiations.head; slep != NULL;
slep = slep->next) {
a_template_instance_ptr tip;
a_symbol_ptr sym = slep->symbol;
tip = template_instance_for_symbol(sym);
update_instantiation_required_flag(tip, /*value=*/TRUE, SIR_NONE);
} /* for */
/* Free any list entries that were used. */
free_list_of_symbol_list_entries(deferred_instantiations);
deferred_instantiations = NULL;
deferred_instantiations_tail = NULL;
free_list_of_symbol_list_entries(deferred_instantiations.head);
deferred_instantiations.clear();
deferred_instantiations_in_process = FALSE;
} /* if */
} /* process_deferred_instantiation_requests */
Expand Down Expand Up @@ -46709,8 +46742,7 @@ Initializations for template.
{
curr_default_args = NULL;
defer_instantiations = 0;
deferred_instantiations = NULL;
deferred_instantiations_tail = NULL;
deferred_instantiations.clear();
avail_partial_order_candidates = NULL;
avail_variadic_param_infos = NULL;
#if MICROSOFT_EXTENSIONS_ALLOWED
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --g++ --gnu_version 150200 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=2 --clang_version 220100 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=3 --ms_c++20 --microsoft_version 1950 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --g++ --gnu_version 150200 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=2 --clang_version 220100 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=3 --ms_c++20 --microsoft_version 1950 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//type:fp
//options:--g++ --gnu_version 150200 --c++20:--clang_version 220100 --c++20:--ms_c++20 --microsoft_version 1950
//remark:Defer instantiations while a constexpr function template instance is instantiated.

template <class T> struct A {};
template <class T> constexpr int f() { return sizeof(A<T>); }
template <class T> struct B {
constexpr ~B() { static_assert(f<T>()); }
};
struct S {
void g(B<int> = {}) { f<int>(); }
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//type:fp
//options:--g++ --gnu_version 150200 --c++20:--clang_version 220100 --c++20:--ms_c++20 --microsoft_version 1950
//remark:Defer instantiations while a variable template instance is instantiated.

template <class T> struct A {};
template <class T> constexpr int v = sizeof(A<T>);
template <class T> struct B {
constexpr ~B() { static_assert(v<T>); }
};
struct S {
void g(B<int> = {}) { (void)v<int>; }
};
Loading