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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/decl_inits.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<a_token_cache>(*curr_lexical_state_cache()));
pop_lexical_state_stack();
} /* if */
db_exit();
} /* initializer */

Expand Down
1 change: 1 addition & 0 deletions src/error_msg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
###############################################################################

260 changes: 259 additions & 1 deletion src/ifc_modules_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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. */
Comment thread
soroshsabz marked this conversation as resolved.
Resolved

/* 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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
/* 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)
/*
Expand Down Expand Up @@ -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 */
Expand Down
23 changes: 23 additions & 0 deletions src/modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading
Loading