From 5566e02473b3bf682bf620a5873d3438efa5a3a8 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Wed, 2 Dec 2026 09:47:21 +0330 Subject: [PATCH 1/6] C++20 modules: write exported variables to IFC files. [GH #58] --- src/Changes | 17 +++ src/decl_inits.c | 25 ++++ src/ifc_modules_write.c | 118 ++++++++++++++++++ src/modules.c | 23 ++++ src/modules.h | 7 ++ .../.test.rto/default.1.1.txt | 1 + .../.test.rto/default.1.2.txt | 2 + .../.test.rto/default.1.3.txt | 1 + .../.test.rto/edg_x86_32_cp.1.2.txt | 3 + .../.test.rto/edg_x86_64_cp.1.2.txt | 3 + .../eifc/modules/import-variable-gnu/foo.ixx | 9 ++ .../modules/import-variable-gnu/test.mft.cpp | 11 ++ .../import-variable/.test.rto/default.1.1.txt | 1 + .../import-variable/.test.rto/default.1.2.txt | 2 + .../import-variable/.test.rto/default.1.3.txt | 1 + .../.test.rto/edg_x86_32_cp.1.2.txt | 3 + .../.test.rto/edg_x86_64_cp.1.2.txt | 3 + .../eifc/modules/import-variable/foo.ixx | 9 ++ .../eifc/modules/import-variable/test.mft.cpp | 11 ++ 19 files changed, 250 insertions(+) create mode 100644 tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.1.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_32_cp.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_64_cp.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx create mode 100644 tests/tests/modules/eifc/modules/import-variable-gnu/test.mft.cpp create mode 100644 tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.1.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_32_cp.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_64_cp.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable/foo.ixx create mode 100644 tests/tests/modules/eifc/modules/import-variable/test.mft.cpp diff --git a/src/Changes b/src/Changes index da843c8a82..35e1927c14 100644 --- a/src/Changes +++ b/src/Changes @@ -1,5 +1,22 @@ Version 7.1 +12/2/26 [GH #58] +C++20 modules: exported variables in module interface files + +Variables exported from a module interface unit were not written to the +module interface file, so they were not visible to importing translation +units. For example, with --c++20: + + export module m; + export int counter = 42; + export namespace math { constexpr int answer = 42; } + +An import of module m previously produced errors for "counter" and +"math::answer". Variable declarations at namespace scope, including their +initializers, are now written to EDG IFC module interface files and are +imported like other exported entities. Variable templates are not yet +covered by this change. + 11/30/26 [GH #592] Argument-dependent lookup in range-based for and structured bindings diff --git a/src/decl_inits.c b/src/decl_inits.c index fd8e906aa4..e9013bc829 100644 --- a/src/decl_inits.c +++ b/src/decl_inits.c @@ -5609,10 +5609,29 @@ returned set to TRUE. a_decl_parse_state *saved_decl_parse_state; a_boolean saved_in_consteval_context; a_boolean namespace_reactivation_pushed = FALSE; + a_boolean need_var_tokens_for_module; + /* TRUE if the initializer tokens are cached so they + can be written to a module output file. */ db_enter(3, "initializer"); dps->has_initializer = TRUE; dps->init_state.decl_parse_state = dps; + /* When a module interface unit is being compiled, cache the tokens of + initializers of variables at namespace scope: they are written to the + module output file as the initializers of the exported variables (see + an_ifc_il_map::enter_variable). This mirrors the caching of function + definitions (see func_def.c) and field initializers (see class_decl.c). + Only the tokens of the initializer itself are captured; the current + token is the first token of the initializer. */ + need_var_tokens_for_module = + create_module_unit && !dps->is_old_style_param_decl && + symbol_ptr != NULL && !symbol_ptr->is_error && + symbol_is(symbol_ptr, sk_variable) && + depth_innermost_function_scope == NO_SCOPE_DEPTH; + if (need_var_tokens_for_module) { + push_lexical_state_stack(); + begin_caching_fetched_tokens(/*include_curr_token=*/TRUE); + } /* if */ /* There are a number of tests to determine whether the variable can take an initializer. If it cannot, set var_err; it will be checked later to decide whether to update the variable with information about the @@ -6489,6 +6508,12 @@ returned set to TRUE. } /* if */ } /* if */ #endif /* DEBUG */ + if (need_var_tokens_for_module) { + end_caching_fetched_tokens(); + save_variable_initializer_for_module_write( + vp, shared_obj(*curr_lexical_state_cache())); + pop_lexical_state_stack(); + } /* if */ db_exit(); } /* initializer */ diff --git a/src/ifc_modules_write.c b/src/ifc_modules_write.c index 4281d9422b..32517ee241 100644 --- a/src/ifc_modules_write.c +++ b/src/ifc_modules_write.c @@ -1356,6 +1356,8 @@ struct an_ifc_il_map { an_ifc_decl_index enter_enum(a_type_ptr type); an_ifc_decl_index find_or_enter_namespace(a_namespace_ptr nsp); an_ifc_decl_index enter_namespace(a_namespace_ptr nsp); + an_ifc_decl_index find_or_enter_variable(a_variable_ptr vp); + an_ifc_decl_index enter_variable(a_variable_ptr vp); an_ifc_decl_index find_or_enter_routine(a_routine_ptr rp); an_ifc_decl_index enter_routine(a_routine_ptr rp); an_ifc_decl_index find_or_enter_template(a_template_ptr templ); @@ -2093,6 +2095,93 @@ state. Return the declaration index for the entered declaration. } /* an_ifc_il_map::enter_namespace */ +an_ifc_decl_index an_ifc_il_map::find_or_enter_variable(a_variable_ptr vp) +/* +For the given variable find or enter the variable declaration into the IFC +output state. Return the declaration index for the variable. +*/ +{ + an_ifc_decl_index result = this->il_entry_to_decl.get(make_tagged_ptr(vp)); + + if (is_null_index(result)) { + result = this->enter_variable(vp); + } /* if */ + return result; +} /* an_ifc_il_map::find_or_enter_variable */ + + +an_ifc_decl_index an_ifc_il_map::enter_variable(a_variable_ptr vp) +/* +For the given variable enter the variable declaration into the IFC output +state. Return the declaration index of the variable declaration. +*/ +{ + an_ifc_decl_variable + var_decl; + an_ifc_decl_index + result = this->map_new_decl(vp, &var_decl); + an_ifc_reachable_properties_bitfield_query + properties = (an_ifc_reachable_properties_bitfield_query)0; + + /* Set the name information. */ + an_ifc_name_index + ifc_name_index = this->entity_name_as_name_index(vp); + set_ifc_name(&var_decl, ifc_name_index); + + /* Set the source location information. */ + an_ifc_source_location + ifc_src_pos = this->find_or_enter_entity_pos(vp); + set_ifc_locus(&var_decl, ifc_src_pos); + + /* Set the type information. */ + an_ifc_type_index + type_idx = this->find_or_enter_type(vp->type); + set_ifc_type(&var_decl, type_idx); + + /* Set the scope information. */ + an_ifc_decl_index + scope_decl_idx = this->associate_entity_home_scope(vp); + set_ifc_home_scope(&var_decl, scope_decl_idx); + + /* Set the initializer. The tokens of the initializer were cached when the + initializer was scanned (see decl_inits.c) and are written as a token + cache expression (mirroring field initializers). A variable without a + cached initializer (e.g., a declaration without an initializer) has no + initializer written. */ + if (vp->has_explicit_initializer) { + a_shared_token_cache + init_cache = get_variable_initializer_for_module_write(vp); + + if (init_cache.ptr() != NULL) { + an_ifc_output_token_cache + init_token_cache; + + this->enter_token_cache(&init_token_cache, init_cache.ptr()); + an_ifc_expr_index + ifc_init_expr = this->output_state->alloc_token_cache_expr( + init_token_cache); + set_ifc_initializer(&var_decl, ifc_init_expr); + /* Flag the presence of the initializer. */ + properties = properties | ifc_rpb_initializer; + } /* if */ + } /* if */ + /* FIXME: Set alignment. */ + /* FIXME: Set traits. */ + /* FIXME: Set specifiers. */ + + /* Set the access specifier. */ + set_ifc_access(&var_decl, ifc_as_none); + + an_ifc_reachable_properties_bitfield_storage + ifc_raw_properties = to_bitmask(var_decl.get_file(), + properties); + an_ifc_reachable_properties_bitfield + ifc_properties(var_decl.get_file(), ifc_raw_properties); + set_ifc_properties(&var_decl, ifc_properties); + return result; +} /* an_ifc_il_map::enter_variable */ + + an_ifc_decl_index an_ifc_il_map::find_or_enter_routine(a_routine_ptr rp) /* For the given routine find or enter the function declaration into the IFC @@ -6176,6 +6265,34 @@ Add all the routines in the given scope to the given IL -> IFC mapping. } /* dump_scope_routines */ +static void dump_scope_variables(an_ifc_il_map *il_map, + a_scope_ptr scope) +/* +Add all the variables in the given scope to the given IL -> IFC mapping. +*/ +{ + for (a_variable_ptr vp = scope->variables; vp != NULL; vp = vp->next) { + if (vp->is_template_variable) { + /* This is a variable template (or a static data member generated + from a template) which will be handled when dump_scope_templates + is called. */ + continue; + } /* if */ + if (vp->is_struct_binding || vp->is_struct_binding_container) { + /* Structured bindings cannot be exported. */ + continue; + } /* if */ + if (vp->source_corresp.name == NULL || + vp->source_corresp.name[0] == '\0') { + /* Skip unnamed variables (e.g., those associated with anonymous + unions). */ + continue; + } /* if */ + (void)il_map->find_or_enter_variable(vp); + } /* for */ +} /* dump_scope_variables */ + + static void dump_scope_namespaces(an_ifc_il_map *il_map, a_scope_ptr scope) /* @@ -6217,6 +6334,7 @@ IFC mapping. dump_scope_types(il_map, scope); dump_scope_templates(il_map, scope); dump_scope_routines(il_map, scope); + dump_scope_variables(il_map, scope); dump_scope_namespaces(il_map, scope); dump_scope_using_directives(il_map, scope); } /* dump_scope_recursively */ diff --git a/src/modules.c b/src/modules.c index 82422ab3f1..fcd356ef15 100644 --- a/src/modules.c +++ b/src/modules.c @@ -2154,6 +2154,29 @@ module output file. } /* get_field_initializer_for_module_write */ +void save_variable_initializer_for_module_write( + a_variable_ptr vp, + const a_shared_token_cache &token) +/* +Save the tokens composing the initializer of the given variable for use writing +a module output file. +*/ +{ + entity_to_initializer_tokens->map(make_tagged_ptr(vp), token); +} /* save_variable_initializer_for_module_write */ + + +a_shared_token_cache get_variable_initializer_for_module_write(a_variable_ptr vp) +/* +Get the tokens composing the initializer of the given variable for use writing +a module output file. Returns a null cache if no initializer was saved for +the given variable. +*/ +{ + return entity_to_initializer_tokens->get(make_tagged_ptr(vp)); +} /* get_variable_initializer_for_module_write */ + + #if DEBUG void db_mep_stack() diff --git a/src/modules.h b/src/modules.h index 4682bc492c..a0930ba147 100644 --- a/src/modules.h +++ b/src/modules.h @@ -839,6 +839,13 @@ extern void save_field_initializer_for_module_write( extern a_shared_token_cache get_field_initializer_for_module_write( a_field_ptr fp); +extern void save_variable_initializer_for_module_write( + a_variable_ptr vp, + const a_shared_token_cache &token); + +extern a_shared_token_cache get_variable_initializer_for_module_write( + a_variable_ptr vp); + #if DEBUG extern void db_tokens(a_module_token_cache_ptr cache); diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.1.txt new file mode 100644 index 0000000000..03cf6e3b66 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules --module_interface foo.ixx \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.2.txt new file mode 100644 index 0000000000..345e3386bf --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.2.txt @@ -0,0 +1,2 @@ +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules foo.o Test_name.c +4 modules declarations processed (0 failed). \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt new file mode 100644 index 0000000000..02c009d408 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt @@ -0,0 +1 @@ +./a.out \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_32_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_32_cp.1.2.txt new file mode 100644 index 0000000000..f048c87cf0 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_32_cp.1.2.txt @@ -0,0 +1,3 @@ +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules foo.o Test_name.c +4 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_64_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_64_cp.1.2.txt new file mode 100644 index 0000000000..f048c87cf0 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_64_cp.1.2.txt @@ -0,0 +1,3 @@ +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules foo.o Test_name.c +4 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx b/tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx new file mode 100644 index 0000000000..820ca8cc17 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx @@ -0,0 +1,9 @@ +export module foo; + +export int variable = 42; + +export constexpr int constant = 7; + +export namespace ns { + int member = 5; +} diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/test.mft.cpp b/tests/tests/modules/eifc/modules/import-variable-gnu/test.mft.cpp new file mode 100644 index 0000000000..deb0ea4bf4 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/test.mft.cpp @@ -0,0 +1,11 @@ +//type:rp +//module_files:foo.ixx +//options_all:--module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules + +import foo; + +static_assert(constant == 7); + +int main() { + return variable + constant + ns::member - 54; +} diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.1.txt new file mode 100644 index 0000000000..ba64b67786 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --module_interface foo.ixx \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.2.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.2.txt new file mode 100644 index 0000000000..b8b24d3202 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.2.txt @@ -0,0 +1,2 @@ +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 foo.o Test_name.c +4 modules declarations processed (0 failed). \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt new file mode 100644 index 0000000000..02c009d408 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt @@ -0,0 +1 @@ +./a.out \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_32_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_32_cp.1.2.txt new file mode 100644 index 0000000000..2e581b7ff1 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_32_cp.1.2.txt @@ -0,0 +1,3 @@ +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 foo.o Test_name.c +4 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_64_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_64_cp.1.2.txt new file mode 100644 index 0000000000..2e581b7ff1 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_64_cp.1.2.txt @@ -0,0 +1,3 @@ +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 foo.o Test_name.c +4 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). \ No newline at end of file diff --git a/tests/tests/modules/eifc/modules/import-variable/foo.ixx b/tests/tests/modules/eifc/modules/import-variable/foo.ixx new file mode 100644 index 0000000000..820ca8cc17 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/foo.ixx @@ -0,0 +1,9 @@ +export module foo; + +export int variable = 42; + +export constexpr int constant = 7; + +export namespace ns { + int member = 5; +} diff --git a/tests/tests/modules/eifc/modules/import-variable/test.mft.cpp b/tests/tests/modules/eifc/modules/import-variable/test.mft.cpp new file mode 100644 index 0000000000..dddc2c1def --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/test.mft.cpp @@ -0,0 +1,11 @@ +//type:rp +//module_files:foo.ixx +//options_all:--module_import_diagnostics -d-module_report --c++20 + +import foo; + +static_assert(constant == 7); + +int main() { + return variable + constant + ns::member - 54; +} From 480592bd0edf975fff6a1b5e9a0d921c722f4821 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Wed, 2 Dec 2026 15:09:24 +0330 Subject: [PATCH 2/6] Address review: array types, constexpr traits, unwritable literal initializers, pending changes entry, and recording fixes [GH #58] --- src/Changes | 17 --- src/ifc_modules_write.c | 129 +++++++++++++++++- src/pending_changes/gh-58-ifc-variables.txt | 21 +++ .../.test.rto/default.1.1.txt | 2 +- .../.test.rto/default.1.2.txt | 2 +- .../.test.rto/default.1.3.txt | 2 +- .../.test.rto/edg_x86_32_cp.1.2.txt | 4 +- .../.test.rto/edg_x86_64_cp.1.2.txt | 4 +- .../eifc/modules/import-variable-gnu/foo.ixx | 2 + .../modules/import-variable-gnu/test.mft.cpp | 2 +- .../import-variable/.test.rto/default.1.1.txt | 2 +- .../import-variable/.test.rto/default.1.2.txt | 2 +- .../import-variable/.test.rto/default.1.3.txt | 2 +- .../.test.rto/edg_x86_32_cp.1.2.txt | 4 +- .../.test.rto/edg_x86_64_cp.1.2.txt | 4 +- .../eifc/modules/import-variable/foo.ixx | 2 + .../eifc/modules/import-variable/test.mft.cpp | 2 +- 17 files changed, 166 insertions(+), 37 deletions(-) create mode 100644 src/pending_changes/gh-58-ifc-variables.txt diff --git a/src/Changes b/src/Changes index 35e1927c14..da843c8a82 100644 --- a/src/Changes +++ b/src/Changes @@ -1,22 +1,5 @@ Version 7.1 -12/2/26 [GH #58] -C++20 modules: exported variables in module interface files - -Variables exported from a module interface unit were not written to the -module interface file, so they were not visible to importing translation -units. For example, with --c++20: - - export module m; - export int counter = 42; - export namespace math { constexpr int answer = 42; } - -An import of module m previously produced errors for "counter" and -"math::answer". Variable declarations at namespace scope, including their -initializers, are now written to EDG IFC module interface files and are -imported like other exported entities. Variable templates are not yet -covered by this change. - 11/30/26 [GH #592] Argument-dependent lookup in range-based for and structured bindings diff --git a/src/ifc_modules_write.c b/src/ifc_modules_write.c index 32517ee241..95eb4cafc5 100644 --- a/src/ifc_modules_write.c +++ b/src/ifc_modules_write.c @@ -1421,6 +1421,7 @@ struct an_ifc_il_map { an_ifc_decl_index param_decl_idx); an_ifc_type_index enter_typedef_type(a_type_ptr type); an_ifc_type_index enter_void_type(a_type_ptr type); + an_ifc_type_index enter_array_type(a_type_ptr type); /* Functions for entering portions of other larger entities. */ an_ifc_sequence enter_enumerators(a_type_ptr type); @@ -1717,6 +1718,9 @@ index for the type file. case tk_void: result = this->enter_void_type(type); break; + case tk_array: + result = this->enter_array_type(type); + break; case tk_error: #if FIXED_POINT_ALLOWED case tk_fixed_point: @@ -1725,7 +1729,6 @@ index for the type file. case tk_imaginary: case tk_complex: #endif /* C99_IL_EXTENSIONS_SUPPORTED */ - case tk_array: case tk_ptr_to_member: #if GNU_VECTOR_TYPES_ALLOWED case tk_vector: @@ -2095,6 +2098,54 @@ state. Return the declaration index for the entered declaration. } /* an_ifc_il_map::enter_namespace */ +static a_boolean token_is_writable_for_ifc(const a_shared_token &tok) +/* +Return TRUE if the given cached token can be written to an IFC token cache; +otherwise, return FALSE. Only tokens that enter_token_cache can represent are +writable: in particular, only integer constants are currently representable +(the IFC constant partition has no records for other constants, e.g., +floating-point and string literals; see enter_constant). +*/ +{ + a_boolean result = FALSE; + + switch (tok->get_extra_kind()) { + case teik_none: + case teik_identifier: + result = TRUE; + break; + case teik_constant: + result = tok->get_constant()->kind == (a_constant_repr_kind)ck_integer; + break; + default: + /* Other extra kinds are not currently writable (see enter_token_cache + and enter_constant). */ + break; + } /* switch */ + return result; +} /* token_is_writable_for_ifc */ + + +static a_boolean token_cache_is_writable_for_ifc(a_token_cache_ptr fe_cache) +/* +Return TRUE if every token of the given front end token cache can be written +to an IFC token cache; otherwise, return FALSE. +*/ +{ + a_token_cache_iterator it = fe_cache->begin(); + a_token_cache_iterator it_end = fe_cache->end(); + a_boolean result = TRUE; + + for (; it != it_end; ++it) { + if (!token_is_writable_for_ifc(*it)) { + result = FALSE; + break; + } /* if */ + } /* for */ + return result; +} /* token_cache_is_writable_for_ifc */ + + an_ifc_decl_index an_ifc_il_map::find_or_enter_variable(a_variable_ptr vp) /* For the given variable find or enter the variable declaration into the IFC @@ -2147,12 +2198,16 @@ state. Return the declaration index of the variable declaration. initializer was scanned (see decl_inits.c) and are written as a token cache expression (mirroring field initializers). A variable without a cached initializer (e.g., a declaration without an initializer) has no - initializer written. */ + initializer written. A cached initializer whose tokens cannot currently + be represented in an IFC token cache (e.g., a floating-point or string + literal) is skipped: the variable declaration itself is still written, + which preserves its visibility to importing translation units. */ if (vp->has_explicit_initializer) { a_shared_token_cache init_cache = get_variable_initializer_for_module_write(vp); - if (init_cache.ptr() != NULL) { + if (init_cache.ptr() != NULL && + token_cache_is_writable_for_ifc(init_cache.ptr())) { an_ifc_output_token_cache init_token_cache; @@ -2166,12 +2221,25 @@ state. Return the declaration index of the variable declaration. } /* if */ } /* if */ /* FIXME: Set alignment. */ - /* FIXME: Set traits. */ + /* Set the traits. The reader determines whether an imported variable is + constexpr from the constexpr object trait (see is_var_constexpr). */ + an_ifc_object_traits_bitfield_query + traits = (an_ifc_object_traits_bitfield_query)0; + + if (vp->is_constexpr) { + traits = traits | ifc_otb_constexpr; + } /* if */ /* FIXME: Set specifiers. */ /* Set the access specifier. */ set_ifc_access(&var_decl, ifc_as_none); + an_ifc_object_traits_bitfield_storage + ifc_raw_traits = to_bitmask(var_decl.get_file(), traits); + an_ifc_object_traits_bitfield + ifc_traits(var_decl.get_file(), ifc_raw_traits); + set_ifc_traits(&var_decl, ifc_traits); + an_ifc_reachable_properties_bitfield_storage ifc_raw_properties = to_bitmask(var_decl.get_file(), properties); @@ -3176,6 +3244,59 @@ for the pointer type. } /* an_ifc_il_map::enter_pointer_type */ +an_ifc_type_index an_ifc_il_map::enter_array_type(a_type_ptr type) +/* +Enter the given array type into the IFC output state. Return the type index +for the array type. +*/ +{ + check_assertion(type->kind == tk_array); + an_ifc_type_array + arr_type; + an_ifc_type_index + result = this->map_new_type(type, &arr_type); + an_ifc_type_index + ifc_element_type = this->find_or_enter_type( + type->variant.array.element_type); + + set_ifc_element(&arr_type, ifc_element_type); + if (type->variant.array.is_template_dependent_size_array) { + /* FIXME: Implement template-dependent array sizes. */ + ifc_write_catastrophe(); + } else if (type->variant.array.variant.number_of_elements > 0) { + /* Set the extent to a constant expression giving the number of + elements. The reader loads the extent as a dimension constant by + scanning the constant expression (load_dimension_constant), so the + count is written as a token cache expression holding an integer + constant (mirroring enumerator initializers). */ + a_constant_ptr + elem_count = local_constant(); + set_unsigned_integer_constant(elem_count, + (a_host_large_unsigned) + type->variant.array.variant.number_of_elements, + (an_integer_kind)ik_unsigned_long_long); + an_ifc_edg_constant_index + ifc_constant = this->enter_constant(elem_count); + an_ifc_edg_complex_token_index + ifc_constant_token = this->enter_constant_token( + ifc_ects_int_constant, + ifc_constant); + an_ifc_output_token_cache + extent_token_cache; + + extent_token_cache.add_complex(ifc_constant_token); + an_ifc_expr_index + ifc_extent = this->output_state->alloc_token_cache_expr( + extent_token_cache); + set_ifc_extent(&arr_type, ifc_extent); + release_local_constant(&elem_count); + } else { + /* An incomplete array ("[]"): leave the extent unset. */ + } /* if */ + return result; +} /* an_ifc_il_map::enter_array_type */ + + an_ifc_type_index an_ifc_il_map::enter_routine_params_type(a_type_ptr type) /* Given a routine type, find or enter the types for the routine's parameters. diff --git a/src/pending_changes/gh-58-ifc-variables.txt b/src/pending_changes/gh-58-ifc-variables.txt new file mode 100644 index 0000000000..358327427e --- /dev/null +++ b/src/pending_changes/gh-58-ifc-variables.txt @@ -0,0 +1,21 @@ +[GH #58] +C++20 modules: exported variables in module interface files + +Variables exported from a module interface unit were not written to the +module interface file, so they were not visible to importing translation +units. For example, with --c++20: + + export module m; + export int counter = 42; + export namespace math { constexpr int answer = 42; } + +An import of module m previously produced errors for "counter" and +"math::answer". Variable declarations at namespace scope are now written to +EDG IFC module interface files and are imported like other exported entities. +The initializer of a variable is written along with its declaration when its +tokens can be represented in the module interface file; initializers +containing floating-point or string literals are currently not representable +(the IFC constant partition has no records for such constants, which also +affects inline function bodies), so such variables are written without their +initializer (their declaration remains visible to importers). Variable +templates are not yet covered by this change. diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.1.txt index 03cf6e3b66..1fe269d1e7 100644 --- a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.1.txt +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.1.txt @@ -1 +1 @@ -eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules --module_interface foo.ixx \ No newline at end of file +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules --module_interface foo.ixx diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.2.txt index 345e3386bf..7718e5e6ea 100644 --- a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.2.txt @@ -1,2 +1,2 @@ eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules foo.o Test_name.c -4 modules declarations processed (0 failed). \ No newline at end of file +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt index 02c009d408..ff2642c6d5 100644 --- a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt @@ -1 +1 @@ -./a.out \ No newline at end of file +./a.out diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_32_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_32_cp.1.2.txt index f048c87cf0..83c9439177 100644 --- a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_32_cp.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_32_cp.1.2.txt @@ -1,3 +1,3 @@ eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules foo.o Test_name.c -4 modules declarations processed (0 failed). -4 modules declarations processed (0 failed). \ No newline at end of file +5 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_64_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_64_cp.1.2.txt index f048c87cf0..83c9439177 100644 --- a/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_64_cp.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/edg_x86_64_cp.1.2.txt @@ -1,3 +1,3 @@ eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules foo.o Test_name.c -4 modules declarations processed (0 failed). -4 modules declarations processed (0 failed). \ No newline at end of file +5 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx b/tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx index 820ca8cc17..2d4e25dbae 100644 --- a/tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx @@ -7,3 +7,5 @@ export constexpr int constant = 7; export namespace ns { int member = 5; } + +export const char *message = "hello"; diff --git a/tests/tests/modules/eifc/modules/import-variable-gnu/test.mft.cpp b/tests/tests/modules/eifc/modules/import-variable-gnu/test.mft.cpp index deb0ea4bf4..9732c676e5 100644 --- a/tests/tests/modules/eifc/modules/import-variable-gnu/test.mft.cpp +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/test.mft.cpp @@ -7,5 +7,5 @@ import foo; static_assert(constant == 7); int main() { - return variable + constant + ns::member - 54; + return variable + constant + ns::member + (message[0] == 'h' ? 0 : 1) - 54; } diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.1.txt index ba64b67786..51c30536b2 100644 --- a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.1.txt +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.1.txt @@ -1 +1 @@ -eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --module_interface foo.ixx \ No newline at end of file +eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --module_interface foo.ixx diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.2.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.2.txt index b8b24d3202..8dfbaa4a99 100644 --- a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.2.txt @@ -1,2 +1,2 @@ eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 foo.o Test_name.c -4 modules declarations processed (0 failed). \ No newline at end of file +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt index 02c009d408..ff2642c6d5 100644 --- a/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt @@ -1 +1 @@ -./a.out \ No newline at end of file +./a.out diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_32_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_32_cp.1.2.txt index 2e581b7ff1..1c63e002cd 100644 --- a/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_32_cp.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_32_cp.1.2.txt @@ -1,3 +1,3 @@ eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 foo.o Test_name.c -4 modules declarations processed (0 failed). -4 modules declarations processed (0 failed). \ No newline at end of file +5 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_64_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_64_cp.1.2.txt index 2e581b7ff1..1c63e002cd 100644 --- a/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_64_cp.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/edg_x86_64_cp.1.2.txt @@ -1,3 +1,3 @@ eccp -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 foo.o Test_name.c -4 modules declarations processed (0 failed). -4 modules declarations processed (0 failed). \ No newline at end of file +5 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable/foo.ixx b/tests/tests/modules/eifc/modules/import-variable/foo.ixx index 820ca8cc17..2d4e25dbae 100644 --- a/tests/tests/modules/eifc/modules/import-variable/foo.ixx +++ b/tests/tests/modules/eifc/modules/import-variable/foo.ixx @@ -7,3 +7,5 @@ export constexpr int constant = 7; export namespace ns { int member = 5; } + +export const char *message = "hello"; diff --git a/tests/tests/modules/eifc/modules/import-variable/test.mft.cpp b/tests/tests/modules/eifc/modules/import-variable/test.mft.cpp index dddc2c1def..eaad257e4f 100644 --- a/tests/tests/modules/eifc/modules/import-variable/test.mft.cpp +++ b/tests/tests/modules/eifc/modules/import-variable/test.mft.cpp @@ -7,5 +7,5 @@ import foo; static_assert(constant == 7); int main() { - return variable + constant + ns::member - 54; + return variable + constant + ns::member + (message[0] == 'h' ? 0 : 1) - 54; } From 2ba90c5792360190ac2860a5f0ddeea4885d9005 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Wed, 2 Dec 2026 21:34:04 +0330 Subject: [PATCH 3/6] Address review: thread_local trait, constexpr initializer warning, and exclude static data members [GH #58] --- src/error_msg.txt | 1 + src/ifc_modules_write.c | 27 ++++++++++++++++--- src/pending_changes/gh-58-ifc-variables.txt | 8 ++++-- .../.test.rto/default.1.1.txt | 1 + .../.test.rto/default.1.2.txt | 18 +++++++++++++ .../import-variable-traits-neg/foo.ixx | 7 +++++ .../import-variable-traits-neg/test.mft.cpp | 17 ++++++++++++ .../.test.rto/default.1.1.txt | 5 ++++ .../.test.rto/default.1.2.txt | 2 ++ .../.test.rto/edg_x86_32_cp.1.2.txt | 3 +++ .../.test.rto/edg_x86_64_cp.1.2.txt | 3 +++ .../modules/import-variable-traits/foo.ixx | 9 +++++++ .../import-variable-traits/test.mft.cpp | 16 +++++++++++ 13 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits-neg/.test.rto/default.1.1.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits-neg/.test.rto/default.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits-neg/foo.ixx create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits-neg/test.mft.cpp create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.1.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx create mode 100644 tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp diff --git a/src/error_msg.txt b/src/error_msg.txt index e564d5e25b..57e49399a2 100644 --- a/src/error_msg.txt +++ b/src/error_msg.txt @@ -3700,5 +3700,6 @@ ec_redeclaration_of_expansion_init_decl_name;;"%sq, declared in the init-stateme ec_redeclaration_of_expansion_range_decl_name;;"%sq, declared in the for-range-declaration of an expansion statement, may not be redeclared in this scope" ec_expansion_statement_context;INTERNAL;"expansion of element %d of the expansion statement %p" ec_det_during_expansion_statement_context;INTERNAL;"detected during expansion of element %d of the expansion statement %p" +ec_module_var_constexpr_init_unwritten;;"the initializer of the exported constexpr variable %sq1 cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions" ############################################################################### diff --git a/src/ifc_modules_write.c b/src/ifc_modules_write.c index 95eb4cafc5..30c216f427 100644 --- a/src/ifc_modules_write.c +++ b/src/ifc_modules_write.c @@ -2201,13 +2201,21 @@ state. Return the declaration index of the variable declaration. initializer written. A cached initializer whose tokens cannot currently be represented in an IFC token cache (e.g., a floating-point or string literal) is skipped: the variable declaration itself is still written, - which preserves its visibility to importing translation units. */ + which preserves its visibility to importing translation units. Since + that would silently leave a constexpr variable without a value that an + importer can evaluate, a warning is issued in that case. */ if (vp->has_explicit_initializer) { a_shared_token_cache init_cache = get_variable_initializer_for_module_write(vp); - if (init_cache.ptr() != NULL && - token_cache_is_writable_for_ifc(init_cache.ptr())) { + if (init_cache.ptr() == NULL || + !token_cache_is_writable_for_ifc(init_cache.ptr())) { + if (vp->is_constexpr) { + pos_warning(ec_module_var_constexpr_init_unwritten, + &vp->source_corresp.decl_position, + vp->source_corresp.name); + } /* if */ + } else { an_ifc_output_token_cache init_token_cache; @@ -2222,13 +2230,17 @@ state. Return the declaration index of the variable declaration. } /* if */ /* FIXME: Set alignment. */ /* Set the traits. The reader determines whether an imported variable is - constexpr from the constexpr object trait (see is_var_constexpr). */ + constexpr from the constexpr object trait (see is_var_constexpr), and + whether it is thread_local from the thread_local object trait. */ an_ifc_object_traits_bitfield_query traits = (an_ifc_object_traits_bitfield_query)0; if (vp->is_constexpr) { traits = traits | ifc_otb_constexpr; } /* if */ + if (vp->is_thread_local) { + traits = traits | ifc_otb_thread_local; + } /* if */ /* FIXME: Set specifiers. */ /* Set the access specifier. */ @@ -6392,6 +6404,13 @@ static void dump_scope_variables(an_ifc_il_map *il_map, Add all the variables in the given scope to the given IL -> IFC mapping. */ { + if (scope->kind == sck_class_struct_union) { + /* The variables of a class scope are its static data members, which + are not covered by the variable writing support (they belong to the + class member machinery; writing them as scope variables does not + make them members of the imported class). */ + return; + } /* if */ for (a_variable_ptr vp = scope->variables; vp != NULL; vp = vp->next) { if (vp->is_template_variable) { /* This is a variable template (or a static data member generated diff --git a/src/pending_changes/gh-58-ifc-variables.txt b/src/pending_changes/gh-58-ifc-variables.txt index 358327427e..10fd736a82 100644 --- a/src/pending_changes/gh-58-ifc-variables.txt +++ b/src/pending_changes/gh-58-ifc-variables.txt @@ -17,5 +17,9 @@ tokens can be represented in the module interface file; initializers containing floating-point or string literals are currently not representable (the IFC constant partition has no records for such constants, which also affects inline function bodies), so such variables are written without their -initializer (their declaration remains visible to importers). Variable -templates are not yet covered by this change. +initializer (their declaration remains visible to importers). A warning +is issued when this drops the initializer of a constexpr variable, since +importing translation units then cannot use its value in constant +expressions. The thread_local property of an exported variable is now +also written. Variable templates and static data members are not yet +covered by this change. diff --git a/tests/tests/modules/eifc/modules/import-variable-traits-neg/.test.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/import-variable-traits-neg/.test.rto/default.1.1.txt new file mode 100644 index 0000000000..984e547988 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits-neg/.test.rto/default.1.1.txt @@ -0,0 +1 @@ +cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules --module_interface foo.ixx diff --git a/tests/tests/modules/eifc/modules/import-variable-traits-neg/.test.rto/default.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits-neg/.test.rto/default.1.2.txt new file mode 100644 index 0000000000..44105a076d --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits-neg/.test.rto/default.1.2.txt @@ -0,0 +1,18 @@ +cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c +"Test_name.c", line 10: error: expression must have a constant value + constexpr int *p = &counter; + ^ +"Test_name.c", line 10: note: a pointer or reference to variable "counter" (declared at line 3 of module "foo" ("foo.ixx")) cannot be used as a constant + constexpr int *p = &counter; + ^ + +"Test_name.c", line 13: error: class "S" has no member "member" + int array[S::member]; + ^ + +"Test_name.c", line 10: warning: variable "p" was declared but never referenced + constexpr int *p = &counter; + ^ + +2 modules declarations processed (0 failed). +2 errors detected in the compilation of "Test_name.c". diff --git a/tests/tests/modules/eifc/modules/import-variable-traits-neg/foo.ixx b/tests/tests/modules/eifc/modules/import-variable-traits-neg/foo.ixx new file mode 100644 index 0000000000..7531401256 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits-neg/foo.ixx @@ -0,0 +1,7 @@ +export module foo; + +export thread_local int counter = 3; + +export struct S { + static constexpr int member = 42; +}; diff --git a/tests/tests/modules/eifc/modules/import-variable-traits-neg/test.mft.cpp b/tests/tests/modules/eifc/modules/import-variable-traits-neg/test.mft.cpp new file mode 100644 index 0000000000..517f905e8a --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits-neg/test.mft.cpp @@ -0,0 +1,17 @@ +//type:cn +//module_files:foo.ixx +//options_all:--module_import_diagnostics -d-module_report --c++20 --modules + +import foo; + +/* A thread_local variable cannot be used in a constant expression, so taking + its address here must be rejected: that pins the thread_local trait of the + imported variable (an imported plain static variable would be accepted). */ +constexpr int *p = &counter; + +/* Static data members are not yet written to module interface files. */ +int array[S::member]; + +int main() { + return 0; +} diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.1.txt new file mode 100644 index 0000000000..3a3540a017 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.1.txt @@ -0,0 +1,5 @@ +cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules --module_interface foo.ixx +"foo.ixx", line 5: warning: the initializer of the exported constexpr variable "ratio" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions + export constexpr double ratio = 1.5; + ^ + diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt new file mode 100644 index 0000000000..4756956213 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt @@ -0,0 +1,2 @@ +cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c +2 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt new file mode 100644 index 0000000000..21393e6899 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt @@ -0,0 +1,3 @@ +cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c +2 modules declarations processed (0 failed). +2 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt new file mode 100644 index 0000000000..21393e6899 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt @@ -0,0 +1,3 @@ +cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c +2 modules declarations processed (0 failed). +2 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx b/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx new file mode 100644 index 0000000000..db1cc64326 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx @@ -0,0 +1,9 @@ +export module foo; + +export thread_local int counter = 3; + +export constexpr double ratio = 1.5; + +export struct S { + static constexpr int member = 42; +}; diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp b/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp new file mode 100644 index 0000000000..2d83adc652 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp @@ -0,0 +1,16 @@ +//type:cp +//module_files:foo.ixx +//options_all:--module_import_diagnostics -d-module_report --c++20 --modules + +import foo; + +/* The thread_local variable is usable (with thread storage duration), and the + constexpr variable with an unwritable initializer is usable outside of + constant expressions (its value comes from the interface object). */ +int bump() { return ++counter; } + +double scaled() { return ratio * 2; } + +int main() { + return 0; +} From 781d2b3e747110f5e8062ccd57ab623af1d6e5bd Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Wed, 2 Dec 2026 23:03:16 +0330 Subject: [PATCH 4/6] Address review: pointer-to-member variable types, constexpr initializer error, and error-path tests [GH #58] --- src/ifc_modules_write.c | 38 ++++++++++++++++--- src/pending_changes/gh-58-ifc-variables.txt | 12 +++--- .../default.1.1.txt | 6 +++ .../default.1.1.txt | 6 +++ .../modules/export-constexpr-float.sft.cpp | 6 +++ .../modules/export-constexpr-string.sft.cpp | 6 +++ .../.test.rto/default.1.1.txt | 4 -- .../.test.rto/default.1.2.txt | 2 +- .../.test.rto/edg_x86_32_cp.1.2.txt | 4 +- .../.test.rto/edg_x86_64_cp.1.2.txt | 4 +- .../modules/import-variable-traits/foo.ixx | 6 +-- .../import-variable-traits/test.mft.cpp | 7 ++-- 12 files changed, 73 insertions(+), 28 deletions(-) create mode 100644 tests/tests/modules/eifc/modules/.export-constexpr-float.rto/default.1.1.txt create mode 100644 tests/tests/modules/eifc/modules/.export-constexpr-string.rto/default.1.1.txt create mode 100644 tests/tests/modules/eifc/modules/export-constexpr-float.sft.cpp create mode 100644 tests/tests/modules/eifc/modules/export-constexpr-string.sft.cpp diff --git a/src/ifc_modules_write.c b/src/ifc_modules_write.c index 30c216f427..e07ddd7db3 100644 --- a/src/ifc_modules_write.c +++ b/src/ifc_modules_write.c @@ -1414,6 +1414,7 @@ struct an_ifc_il_map { an_ifc_type_index enter_member_function_type(a_type_ptr type); an_ifc_type_index enter_nullptr_type(a_type_ptr type); an_ifc_type_index enter_pointer_type(a_type_ptr type); + an_ifc_type_index enter_ptr_to_member_type(a_type_ptr type); an_ifc_type_index enter_routine_params_type(a_type_ptr type); an_ifc_type_index enter_routine_type(a_type_ptr type); an_ifc_type_index enter_template_type_param_type( @@ -1649,6 +1650,9 @@ index for the type file. case tk_pointer: result = this->enter_pointer_type(type); break; + case tk_ptr_to_member: + result = this->enter_ptr_to_member_type(type); + break; case tk_routine: result = this->enter_routine_type(type); break; @@ -1729,7 +1733,6 @@ index for the type file. case tk_imaginary: case tk_complex: #endif /* C99_IL_EXTENSIONS_SUPPORTED */ - case tk_ptr_to_member: #if GNU_VECTOR_TYPES_ALLOWED case tk_vector: case tk_scalable_vector: @@ -2202,8 +2205,9 @@ state. Return the declaration index of the variable declaration. be represented in an IFC token cache (e.g., a floating-point or string literal) is skipped: the variable declaration itself is still written, which preserves its visibility to importing translation units. Since - that would silently leave a constexpr variable without a value that an - importer can evaluate, a warning is issued in that case. */ + that would leave a constexpr variable without a value that an importer + can evaluate, an error is issued in that case (writing such constant + values needs IFC constant records that do not exist yet). */ if (vp->has_explicit_initializer) { a_shared_token_cache init_cache = get_variable_initializer_for_module_write(vp); @@ -2211,9 +2215,9 @@ state. Return the declaration index of the variable declaration. if (init_cache.ptr() == NULL || !token_cache_is_writable_for_ifc(init_cache.ptr())) { if (vp->is_constexpr) { - pos_warning(ec_module_var_constexpr_init_unwritten, - &vp->source_corresp.decl_position, - vp->source_corresp.name); + pos_error(ec_module_var_constexpr_init_unwritten, + &vp->source_corresp.decl_position, + vp->source_corresp.name); } /* if */ } else { an_ifc_output_token_cache @@ -3256,6 +3260,28 @@ for the pointer type. } /* an_ifc_il_map::enter_pointer_type */ +an_ifc_type_index an_ifc_il_map::enter_ptr_to_member_type(a_type_ptr type) +/* +Enter the given pointer-to-member type into the IFC output state. Return the +type index for the pointer-to-member type. +*/ +{ + check_assertion(type->kind == tk_ptr_to_member); + an_ifc_type_pointer_to_member + ptr_to_member_type; + an_ifc_type_index result = this->map_new_type(type, &ptr_to_member_type); + + /* The "scope" of an IFC pointer-to-member type is the type of the class of + which the member pointed to is a member. */ + set_ifc_scope(&ptr_to_member_type, + this->find_or_enter_type( + type->variant.ptr_to_member.class_of_which_a_member)); + set_ifc_member(&ptr_to_member_type, + this->find_or_enter_type(type->variant.ptr_to_member.type)); + return result; +} /* an_ifc_il_map::enter_ptr_to_member_type */ + + an_ifc_type_index an_ifc_il_map::enter_array_type(a_type_ptr type) /* Enter the given array type into the IFC output state. Return the type index diff --git a/src/pending_changes/gh-58-ifc-variables.txt b/src/pending_changes/gh-58-ifc-variables.txt index 10fd736a82..1f0376b739 100644 --- a/src/pending_changes/gh-58-ifc-variables.txt +++ b/src/pending_changes/gh-58-ifc-variables.txt @@ -17,9 +17,9 @@ tokens can be represented in the module interface file; initializers containing floating-point or string literals are currently not representable (the IFC constant partition has no records for such constants, which also affects inline function bodies), so such variables are written without their -initializer (their declaration remains visible to importers). A warning -is issued when this drops the initializer of a constexpr variable, since -importing translation units then cannot use its value in constant -expressions. The thread_local property of an exported variable is now -also written. Variable templates and static data members are not yet -covered by this change. +initializer (their declaration remains visible to importers). Since the +value of a constexpr variable must be available to importing translation +units, an error is issued when its initializer cannot be written. Pointer- +to-member variable types and the thread_local property of an exported +variable are now also written. Variable templates and static data members +are not yet covered by this change. diff --git a/tests/tests/modules/eifc/modules/.export-constexpr-float.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/.export-constexpr-float.rto/default.1.1.txt new file mode 100644 index 0000000000..58b6563f60 --- /dev/null +++ b/tests/tests/modules/eifc/modules/.export-constexpr-float.rto/default.1.1.txt @@ -0,0 +1,6 @@ +fe_only -DTEST_NUMBER=1 --c++20 --modules --module_interface Test_name.c +"Test_name.c", line 6: error: the initializer of the exported constexpr variable "ratio" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions + export constexpr double ratio = 1.5; + ^ + +1 error detected in the compilation of "Test_name.c". diff --git a/tests/tests/modules/eifc/modules/.export-constexpr-string.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/.export-constexpr-string.rto/default.1.1.txt new file mode 100644 index 0000000000..d24dc05e40 --- /dev/null +++ b/tests/tests/modules/eifc/modules/.export-constexpr-string.rto/default.1.1.txt @@ -0,0 +1,6 @@ +fe_only -DTEST_NUMBER=1 --c++20 --modules --module_interface Test_name.c +"Test_name.c", line 6: error: the initializer of the exported constexpr variable "name" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions + export constexpr const char *name = "hello"; + ^ + +1 error detected in the compilation of "Test_name.c". diff --git a/tests/tests/modules/eifc/modules/export-constexpr-float.sft.cpp b/tests/tests/modules/eifc/modules/export-constexpr-float.sft.cpp new file mode 100644 index 0000000000..00e3c02a93 --- /dev/null +++ b/tests/tests/modules/eifc/modules/export-constexpr-float.sft.cpp @@ -0,0 +1,6 @@ +//type:fn +//options_all:--c++20 --modules --module_interface + +export module foo; + +export constexpr double ratio = 1.5; diff --git a/tests/tests/modules/eifc/modules/export-constexpr-string.sft.cpp b/tests/tests/modules/eifc/modules/export-constexpr-string.sft.cpp new file mode 100644 index 0000000000..23d4c960c2 --- /dev/null +++ b/tests/tests/modules/eifc/modules/export-constexpr-string.sft.cpp @@ -0,0 +1,6 @@ +//type:fn +//options_all:--c++20 --modules --module_interface + +export module foo; + +export constexpr const char *name = "hello"; diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.1.txt index 3a3540a017..984e547988 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.1.txt +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.1.txt @@ -1,5 +1 @@ cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules --module_interface foo.ixx -"foo.ixx", line 5: warning: the initializer of the exported constexpr variable "ratio" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions - export constexpr double ratio = 1.5; - ^ - diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt index 4756956213..43266edd0d 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt @@ -1,2 +1,2 @@ cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c -2 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt index 21393e6899..cd214bc2fc 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt @@ -1,3 +1,3 @@ cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c -2 modules declarations processed (0 failed). -2 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt index 21393e6899..cd214bc2fc 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt @@ -1,3 +1,3 @@ cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c -2 modules declarations processed (0 failed). -2 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). +4 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx b/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx index db1cc64326..61e524a6d7 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx +++ b/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx @@ -2,8 +2,8 @@ export module foo; export thread_local int counter = 3; -export constexpr double ratio = 1.5; - export struct S { - static constexpr int member = 42; + int value; }; + +export int S::*member_ptr = &S::value; diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp b/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp index 2d83adc652..7b32274696 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp +++ b/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp @@ -4,12 +4,11 @@ import foo; -/* The thread_local variable is usable (with thread storage duration), and the - constexpr variable with an unwritable initializer is usable outside of - constant expressions (its value comes from the interface object). */ +/* The thread_local variable is usable (with thread storage duration). */ int bump() { return ++counter; } -double scaled() { return ratio * 2; } +/* The pointer-to-member variable is written with a valid type. */ +int fetch(S &s) { return s.*member_ptr; } int main() { return 0; From c94191b32b3c71cfc7a8a20f3a00b92af29b30a6 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Thu, 3 Dec 2026 00:38:44 +0330 Subject: [PATCH 5/6] Address review: scope the constant initializer error to exported variables and cover const integral variables [GH #58] --- src/error_msg.txt | 2 +- src/ifc_modules_write.c | 22 ++++++++++++++----- src/pending_changes/gh-58-ifc-variables.txt | 6 +++-- .../.export-const-int.rto/default.1.1.txt | 6 +++++ .../default.1.1.txt | 2 +- .../default.1.1.txt | 2 +- .../eifc/modules/export-const-int.sft.cpp | 6 +++++ .../modules/import-variable-traits/foo.ixx | 8 +++++++ 8 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 tests/tests/modules/eifc/modules/.export-const-int.rto/default.1.1.txt create mode 100644 tests/tests/modules/eifc/modules/export-const-int.sft.cpp diff --git a/src/error_msg.txt b/src/error_msg.txt index 57e49399a2..71ee97c953 100644 --- a/src/error_msg.txt +++ b/src/error_msg.txt @@ -3700,6 +3700,6 @@ ec_redeclaration_of_expansion_init_decl_name;;"%sq, declared in the init-stateme ec_redeclaration_of_expansion_range_decl_name;;"%sq, declared in the for-range-declaration of an expansion statement, may not be redeclared in this scope" ec_expansion_statement_context;INTERNAL;"expansion of element %d of the expansion statement %p" ec_det_during_expansion_statement_context;INTERNAL;"detected during expansion of element %d of the expansion statement %p" -ec_module_var_constexpr_init_unwritten;;"the initializer of the exported constexpr variable %sq1 cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions" +ec_module_var_const_init_unwritten;;"the initializer of the exported variable %sq1 cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions" ############################################################################### diff --git a/src/ifc_modules_write.c b/src/ifc_modules_write.c index e07ddd7db3..d23517c55a 100644 --- a/src/ifc_modules_write.c +++ b/src/ifc_modules_write.c @@ -2176,6 +2176,13 @@ state. Return the declaration index of the variable declaration. result = this->map_new_decl(vp, &var_decl); an_ifc_reachable_properties_bitfield_query properties = (an_ifc_reachable_properties_bitfield_query)0; + /* TRUE if an importing translation unit can need the value of the variable + in a constant expression: a constexpr variable, or a const variable of + integral or enumeration type. */ + a_boolean needs_constant_value = + vp->is_constexpr || + (is_const_qualified_type(vp->type) && + is_integral_or_enum_type(vp->type)); /* Set the name information. */ an_ifc_name_index @@ -2204,18 +2211,21 @@ state. Return the declaration index of the variable declaration. initializer written. A cached initializer whose tokens cannot currently be represented in an IFC token cache (e.g., a floating-point or string literal) is skipped: the variable declaration itself is still written, - which preserves its visibility to importing translation units. Since - that would leave a constexpr variable without a value that an importer - can evaluate, an error is issued in that case (writing such constant - values needs IFC constant records that do not exist yet). */ + which preserves its visibility to importing translation units. When an + importing translation unit can need the value in a constant expression, + an error is issued instead (writing such constant values needs IFC + constant records that do not exist yet). A non-exported const variable + has internal linkage, so it cannot be named by an importer and its + initializer is simply not needed there. */ if (vp->has_explicit_initializer) { a_shared_token_cache init_cache = get_variable_initializer_for_module_write(vp); if (init_cache.ptr() == NULL || !token_cache_is_writable_for_ifc(init_cache.ptr())) { - if (vp->is_constexpr) { - pos_error(ec_module_var_constexpr_init_unwritten, + if (needs_constant_value && + vp->source_corresp.name_linkage != nlk_internal) { + pos_error(ec_module_var_const_init_unwritten, &vp->source_corresp.decl_position, vp->source_corresp.name); } /* if */ diff --git a/src/pending_changes/gh-58-ifc-variables.txt b/src/pending_changes/gh-58-ifc-variables.txt index 1f0376b739..e6f583899c 100644 --- a/src/pending_changes/gh-58-ifc-variables.txt +++ b/src/pending_changes/gh-58-ifc-variables.txt @@ -18,8 +18,10 @@ containing floating-point or string literals are currently not representable (the IFC constant partition has no records for such constants, which also affects inline function bodies), so such variables are written without their initializer (their declaration remains visible to importers). Since the -value of a constexpr variable must be available to importing translation -units, an error is issued when its initializer cannot be written. Pointer- +value of an exported constexpr variable, or of an exported const variable +of integral or enumeration type, can be needed by an importing translation +unit in a constant expression, an error is issued when the initializer of +such a variable cannot be written. Pointer- to-member variable types and the thread_local property of an exported variable are now also written. Variable templates and static data members are not yet covered by this change. diff --git a/tests/tests/modules/eifc/modules/.export-const-int.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/.export-const-int.rto/default.1.1.txt new file mode 100644 index 0000000000..6371c4dfbb --- /dev/null +++ b/tests/tests/modules/eifc/modules/.export-const-int.rto/default.1.1.txt @@ -0,0 +1,6 @@ +fe_only -DTEST_NUMBER=1 --c++20 --modules --module_interface Test_name.c +"Test_name.c", line 6: error: the initializer of the exported variable "length" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions + export const int length = sizeof("abc"); + ^ + +1 error detected in the compilation of "Test_name.c". diff --git a/tests/tests/modules/eifc/modules/.export-constexpr-float.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/.export-constexpr-float.rto/default.1.1.txt index 58b6563f60..057b9b3dfa 100644 --- a/tests/tests/modules/eifc/modules/.export-constexpr-float.rto/default.1.1.txt +++ b/tests/tests/modules/eifc/modules/.export-constexpr-float.rto/default.1.1.txt @@ -1,5 +1,5 @@ fe_only -DTEST_NUMBER=1 --c++20 --modules --module_interface Test_name.c -"Test_name.c", line 6: error: the initializer of the exported constexpr variable "ratio" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions +"Test_name.c", line 6: error: the initializer of the exported variable "ratio" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions export constexpr double ratio = 1.5; ^ diff --git a/tests/tests/modules/eifc/modules/.export-constexpr-string.rto/default.1.1.txt b/tests/tests/modules/eifc/modules/.export-constexpr-string.rto/default.1.1.txt index d24dc05e40..21539a950c 100644 --- a/tests/tests/modules/eifc/modules/.export-constexpr-string.rto/default.1.1.txt +++ b/tests/tests/modules/eifc/modules/.export-constexpr-string.rto/default.1.1.txt @@ -1,5 +1,5 @@ fe_only -DTEST_NUMBER=1 --c++20 --modules --module_interface Test_name.c -"Test_name.c", line 6: error: the initializer of the exported constexpr variable "name" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions +"Test_name.c", line 6: error: the initializer of the exported variable "name" cannot be represented in the module interface file; importing translation units cannot use its value in constant expressions export constexpr const char *name = "hello"; ^ diff --git a/tests/tests/modules/eifc/modules/export-const-int.sft.cpp b/tests/tests/modules/eifc/modules/export-const-int.sft.cpp new file mode 100644 index 0000000000..b477385366 --- /dev/null +++ b/tests/tests/modules/eifc/modules/export-const-int.sft.cpp @@ -0,0 +1,6 @@ +//type:fn +//options_all:--c++20 --modules --module_interface + +export module foo; + +export const int length = sizeof("abc"); diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx b/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx index 61e524a6d7..7529082df9 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx +++ b/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx @@ -7,3 +7,11 @@ export struct S { }; export int S::*member_ptr = &S::value; + +/* Non-exported variables whose initializers cannot be represented in the + module interface file are fine: they have internal linkage, so importing + translation units never need their values. */ +constexpr double impl_ratio = 1.5; +const int impl_length = sizeof("abc"); + +export double scaled() { return impl_ratio * impl_length; } From 2bf6a8a4b059ee676b98c91920f7a08b35337b90 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Thu, 3 Dec 2026 00:53:39 +0330 Subject: [PATCH 6/6] Address review: require a constant initializer before rejecting a const integral variable [GH #58] --- src/ifc_modules_write.c | 7 +++++-- src/pending_changes/gh-58-ifc-variables.txt | 6 +++--- .../import-variable-traits/.test.rto/default.1.2.txt | 2 +- .../import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt | 4 ++-- .../import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt | 4 ++-- .../modules/eifc/modules/import-variable-traits/foo.ixx | 6 ++++++ .../eifc/modules/import-variable-traits/test.mft.cpp | 4 ++++ 7 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/ifc_modules_write.c b/src/ifc_modules_write.c index d23517c55a..c6654b3823 100644 --- a/src/ifc_modules_write.c +++ b/src/ifc_modules_write.c @@ -2178,10 +2178,13 @@ state. Return the declaration index of the variable declaration. properties = (an_ifc_reachable_properties_bitfield_query)0; /* TRUE if an importing translation unit can need the value of the variable in a constant expression: a constexpr variable, or a const variable of - integral or enumeration type. */ + integral or enumeration type with a constant initializer (a const + variable with a runtime initializer, e.g., a call of a non-constexpr + function, cannot be used in a constant expression). */ a_boolean needs_constant_value = vp->is_constexpr || - (is_const_qualified_type(vp->type) && + (vp->init_kind == initk_static && + is_const_qualified_type(vp->type) && is_integral_or_enum_type(vp->type)); /* Set the name information. */ diff --git a/src/pending_changes/gh-58-ifc-variables.txt b/src/pending_changes/gh-58-ifc-variables.txt index e6f583899c..72af7f17bb 100644 --- a/src/pending_changes/gh-58-ifc-variables.txt +++ b/src/pending_changes/gh-58-ifc-variables.txt @@ -19,9 +19,9 @@ containing floating-point or string literals are currently not representable affects inline function bodies), so such variables are written without their initializer (their declaration remains visible to importers). Since the value of an exported constexpr variable, or of an exported const variable -of integral or enumeration type, can be needed by an importing translation -unit in a constant expression, an error is issued when the initializer of -such a variable cannot be written. Pointer- +of integral or enumeration type with a constant initializer, can be needed +by an importing translation unit in a constant expression, an error is +issued when the initializer of such a variable cannot be written. Pointer- to-member variable types and the thread_local property of an exported variable are now also written. Variable templates and static data members are not yet covered by this change. diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt index 43266edd0d..7586b8df21 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/default.1.2.txt @@ -1,2 +1,2 @@ cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c -4 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt index cd214bc2fc..17955068c8 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_32_cp.1.2.txt @@ -1,3 +1,3 @@ cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c -4 modules declarations processed (0 failed). -4 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt index cd214bc2fc..17955068c8 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt +++ b/tests/tests/modules/eifc/modules/import-variable-traits/.test.rto/edg_x86_64_cp.1.2.txt @@ -1,3 +1,3 @@ cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --modules Test_name.c -4 modules declarations processed (0 failed). -4 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). +5 modules declarations processed (0 failed). diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx b/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx index 7529082df9..1b68bf5ae6 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx +++ b/tests/tests/modules/eifc/modules/import-variable-traits/foo.ixx @@ -14,4 +14,10 @@ export int S::*member_ptr = &S::value; constexpr double impl_ratio = 1.5; const int impl_length = sizeof("abc"); +/* An exported const variable with a runtime initializer cannot be used in a + constant expression, so its initializer is not needed by importers + either. */ +export int load(const char *s); +export const int runtime_value = load("abc"); + export double scaled() { return impl_ratio * impl_length; } diff --git a/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp b/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp index 7b32274696..d48cc3957c 100644 --- a/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp +++ b/tests/tests/modules/eifc/modules/import-variable-traits/test.mft.cpp @@ -10,6 +10,10 @@ int bump() { return ++counter; } /* The pointer-to-member variable is written with a valid type. */ int fetch(S &s) { return s.*member_ptr; } +/* The const variable with a runtime initializer is usable outside of + constant expressions. */ +int get_runtime() { return runtime_value; } + int main() { return 0; }