extern cpp_reader *parse_in;
-static tree start_objects (bool, unsigned);
+static tree start_objects (bool, unsigned, bool);
static tree finish_objects (bool, unsigned, tree);
static tree start_partial_init_fini_fn (bool, unsigned, unsigned);
static void finish_partial_init_fini_fn (tree);
/* Start a global constructor or destructor function. */
static tree
-start_objects (bool initp, unsigned priority)
+start_objects (bool initp, unsigned priority, bool has_body)
{
- int module_init = 0;
-
- if (priority == DEFAULT_INIT_PRIORITY && initp)
- module_init = module_initializer_kind ();
-
+ bool default_init = initp && priority == DEFAULT_INIT_PRIORITY;
+ bool is_module_init = default_init && module_global_init_needed ();
tree name = NULL_TREE;
- if (module_init > 0)
+
+ if (is_module_init)
name = mangle_module_global_init (0);
else
{
tree fntype = build_function_type (void_type_node, void_list_node);
tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
- if (module_init > 0)
+ if (is_module_init)
{
SET_DECL_ASSEMBLER_NAME (fndecl, name);
TREE_PUBLIC (fndecl) = true;
tree body = begin_compound_stmt (BCS_FN_BODY);
- if (module_init > 0)
+ bool has_import_inits = default_init && module_has_import_inits ();
+ if (is_module_init && (has_import_inits || has_body))
{
+ // If the function is going to be empty, don't emit idempotency.
// 'static bool __in_chrg = false;
// if (__inchrg) return;
// __inchrg = true
finish_expr_stmt (assign);
}
- if (module_init)
+ if (has_import_inits)
module_add_import_initializers ();
return body;
{
input_location = locus;
- tree body = start_objects (initp, priority);
+ tree body = start_objects (initp, priority, bool (fns));
/* To make sure dynamic construction doesn't access globals from other
compilation units where they might not be yet constructed, for
if (initp && (flag_sanitize & SANITIZE_ADDRESS))
finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
- /* Close out the function. */
+ /* Close out the function, and arrange for it to be called at init
+ or fini time. (Even module initializer functions need this, as
+ we cannot guarantee the module is imported somewhere in the programq.) */
expand_or_defer_fn (finish_objects (initp, priority, body));
}
push_lang_context (lang_name_c);
if ((c_dialect_objc () && objc_static_init_needed_p ())
- || module_initializer_kind ())
+ || module_global_init_needed () || module_has_import_inits ())
{
// Make sure there's a default priority entry.
if (!static_init_fini_fns[true])
tree
vtv_start_verification_constructor_init_function (void)
{
- return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1);
+ return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true);
}
tree
}
}
-/* +1, we're the primary or a partition. Therefore emitting a
- globally-callable idemportent initializer function.
- -1, we have direct imports. Therefore emitting calls to their
- initializers. */
+/* Return true IFF we must emit a module global initializer function
+ (which will be called by importers' init code). */
-int
-module_initializer_kind ()
+bool
+module_global_init_needed ()
{
- int result = 0;
+ return module_has_cmi_p () && !header_module_p ();
+}
- if (module_has_cmi_p () && !header_module_p ())
- result = +1;
- else if (num_init_calls_needed)
- result = -1;
+/* Return true IFF we have import global inits to call. */
- return result;
+bool
+module_has_import_inits ()
+{
+ return bool (num_init_calls_needed);
}
/* Emit calls to each direct import's global initializer. Including