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/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 4281d9422b..30c216f427 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); @@ -1419,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); @@ -1715,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: @@ -1723,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: @@ -2093,6 +2098,170 @@ 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 +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. 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 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 (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; + + 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. */ + /* Set the traits. The reader determines whether an imported variable is + 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. */ + 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); + 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 @@ -3087,6 +3256,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. @@ -6176,6 +6398,41 @@ 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. +*/ +{ + 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 + 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 +6474,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/src/pending_changes/gh-58-ifc-variables.txt b/src/pending_changes/gh-58-ifc-variables.txt new file mode 100644 index 0000000000..10fd736a82 --- /dev/null +++ b/src/pending_changes/gh-58-ifc-variables.txt @@ -0,0 +1,25 @@ +[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). 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-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..1fe269d1e7 --- /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 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..7718e5e6ea --- /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 +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 new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/.test.rto/default.1.3.txt @@ -0,0 +1 @@ +./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 new file mode 100644 index 0000000000..83c9439177 --- /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 +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 new file mode 100644 index 0000000000..83c9439177 --- /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 +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 new file mode 100644 index 0000000000..2d4e25dbae --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable-gnu/foo.ixx @@ -0,0 +1,11 @@ +export module foo; + +export int variable = 42; + +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 new file mode 100644 index 0000000000..9732c676e5 --- /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 + (message[0] == 'h' ? 0 : 1) - 54; +} 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; +} 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..51c30536b2 --- /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 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..8dfbaa4a99 --- /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 +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 new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/.test.rto/default.1.3.txt @@ -0,0 +1 @@ +./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 new file mode 100644 index 0000000000..1c63e002cd --- /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 +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 new file mode 100644 index 0000000000..1c63e002cd --- /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 +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 new file mode 100644 index 0000000000..2d4e25dbae --- /dev/null +++ b/tests/tests/modules/eifc/modules/import-variable/foo.ixx @@ -0,0 +1,11 @@ +export module foo; + +export int variable = 42; + +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 new file mode 100644 index 0000000000..eaad257e4f --- /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 + (message[0] == 'h' ? 0 : 1) - 54; +}