Skip to content

Explicitly instantiate function return values that are template classes #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ pub struct ExportConfig {
pub renaming_overrides_prefixing: bool,
/// Mangling configuration.
pub mangle: MangleConfig,
/// Whether to instantiate the monomorphs of template types used as function return values. This
/// is needed for C compatibility, because otherwise compilers warn (`-Wreturn-type-c-linkage`
/// on gcc/clang) or even reject (MSVC) those function definitions. The compensation is made by
/// emitting a single struct with one field for each monomorphized type. The emitted wrapper
/// struct's name can optionally be overridden by [`return_value_monomorphs_struct_name`].
pub instantiate_return_value_monomorphs: bool,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I think this is kind of a workaround... But yeah it's not pretty. I don't think you need a struct or so tho, you can just explicitly instantiate them, right?

See here for example.

Copy link
Contributor Author

@scovich scovich May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay responding... misrouted github notification email...

From the PR description:

We take the dummy struct approach because explicit instantiation changes semantics of C++ templates and can cause linker errors if multiple compilation units do it -- not good for a general header file.

The approach linked above would violate the One Definition Rule (ODR), if included by multiple different compilation units of the same project. For example, according to https://en.cppreference.com/w/cpp/language/class_template:

An explicit instantiation definition forces instantiation of the class, struct, or union they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the entire program, no diagnostic required.

The "no diagnostic required" part is annoying -- the compiler/linker isn't required to say anything, the resulting binary just has undefined behavior. See e.g.
https://stackoverflow.com/questions/45120323/why-c-linker-is-silent-about-odr-violation
https://stackoverflow.com/questions/21534435/separate-compilation-and-template-explicit-instantiation

C++11 added support for "explicit instantiation declarations" by prepending the extern keyword, but that just pushes the problem somewhere else:

An explicit instantiation declaration (an extern template) skips implicit instantiation step: the code that would otherwise cause an implicit instantiation instead uses the explicit instantiation definition provided elsewhere (resulting in link errors if no such instantiation exists).

By "using" a specific template instantiation inside a struct definition, the compiler implicitly instantiates the template instead, which makes its definition available without violating the ODR -- a special case that would otherwise cause header file class definitions to violate ODR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also updated the PR description with some of this info, since it wasn't clear before)

/// Overrides the struct name to use when [`instantiate_return_value_monomorphs`] is enabled
/// (ignored otherwise). If not specified, the default is `__cbindgen_return_value_monomorphs`.
pub return_value_monomorphs_struct_name: Option<String>,
}

/// Mangling-specific configuration.
Expand Down
10 changes: 5 additions & 5 deletions src/bindgen/ir/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> DefineKey<'a> {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum Cfg {
Boolean(String),
Named(String, String),
Expand Down Expand Up @@ -129,10 +129,10 @@ impl syn::parse::Parse for Cfg {

impl Cfg {
pub fn join(cfgs: &[Cfg]) -> Option<Cfg> {
if cfgs.is_empty() {
None
} else {
Some(Cfg::All(cfgs.to_owned()))
match cfgs {
[] => None,
[cfg] => Some(cfg.clone()),
_ => Some(Cfg::All(cfgs.to_owned())),
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/bindgen/ir/documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::bindgen::utilities::SynAttributeHelpers;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct Documentation {
pub doc_comment: Vec<String>,
}
Expand All @@ -27,8 +27,6 @@ impl Documentation {
}

pub fn none() -> Self {
Documentation {
doc_comment: Vec::new(),
}
Self::default()
}
}
12 changes: 11 additions & 1 deletion src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::bindgen::ir::{
use crate::bindgen::language_backend::LanguageBackend;
use crate::bindgen::library::Library;
use crate::bindgen::mangle;
use crate::bindgen::monomorph::Monomorphs;
use crate::bindgen::monomorph::{Monomorphs, ReturnValueMonomorphs};
use crate::bindgen::rename::{IdentifierType, RenameRule};
use crate::bindgen::reserved;
use crate::bindgen::writer::{ListType, SourceWriter};
Expand Down Expand Up @@ -317,6 +317,16 @@ impl Enum {
repr.style != ReprStyle::C
}

pub fn find_return_value_monomorphs(&self, monomorphs: &mut ReturnValueMonomorphs<'_>) {
monomorphs.with_active_cfg(self.cfg.clone(), |m| {
for v in &self.variants {
if let VariantBody::Body { ref body, .. } = v.body {
body.find_return_value_monomorphs(m);
}
}
});
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
if self.is_generic() {
return;
Expand Down
16 changes: 14 additions & 2 deletions src/bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use syn::ext::IdentExt;
use crate::bindgen::config::{Config, Language};
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{AnnotationSet, Cfg, Documentation, GenericPath, Path, Type};
use crate::bindgen::ir::{
AnnotationSet, Cfg, Documentation, GenericParams, GenericPath, Path, Type,
};
use crate::bindgen::library::Library;
use crate::bindgen::monomorph::Monomorphs;
use crate::bindgen::monomorph::{Monomorphs, ReturnValueMonomorphs};
use crate::bindgen::rename::{IdentifierType, RenameRule};
use crate::bindgen::reserved;
use crate::bindgen::utilities::IterHelpers;
Expand Down Expand Up @@ -47,6 +49,11 @@ impl Function {
attrs: &[syn::Attribute],
mod_cfg: Option<&Cfg>,
) -> Result<Function, String> {
let GenericParams(generics) = GenericParams::load(&sig.generics)?;
if !generics.is_empty() {
return Err("Generic functions are not supported".to_owned());
}

let mut args = sig.inputs.iter().try_skip_map(|x| x.as_argument())?;
if sig.variadic.is_some() {
args.push(FunctionArgument {
Expand Down Expand Up @@ -129,6 +136,11 @@ impl Function {
}
}

pub fn find_return_value_monomorphs(&self, monomorphs: &mut ReturnValueMonomorphs<'_>) {
monomorphs.with_active_cfg(self.cfg.clone(), |m| {
m.handle_function(&self.ret, self.args.iter().map(|arg| &arg.ty));
});
}
pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
self.ret.add_monomorphs(library, out);
for arg in &self.args {
Expand Down
17 changes: 12 additions & 5 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::bindgen::ir::{
};
use crate::bindgen::library::Library;
use crate::bindgen::mangle;
use crate::bindgen::monomorph::Monomorphs;
use crate::bindgen::monomorph::{Monomorphs, ReturnValueMonomorphs};
use crate::bindgen::rename::{IdentifierType, RenameRule};
use crate::bindgen::reserved;
use crate::bindgen::utilities::IterHelpers;
Expand Down Expand Up @@ -168,10 +168,17 @@ impl Struct {

/// Attempts to convert this struct to a typedef (only works for transparent structs).
pub fn as_typedef(&self) -> Option<Typedef> {
match self.fields.first() {
Some(field) if self.is_transparent => Some(Typedef::new_from_struct_field(self, field)),
_ => None,
}
let field = self.fields.first()?;
self.is_transparent
.then(|| Typedef::new_from_struct_field(self, field))
}

pub fn find_return_value_monomorphs(&self, monomorphs: &mut ReturnValueMonomorphs<'_>) {
monomorphs.with_active_cfg(self.cfg.clone(), |m| {
for field in &self.fields {
field.ty.find_return_value_monomorphs(m, false);
}
});
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
Expand Down
18 changes: 17 additions & 1 deletion src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{GenericArgument, GenericParams, GenericPath, Path};
use crate::bindgen::library::Library;
use crate::bindgen::monomorph::Monomorphs;
use crate::bindgen::monomorph::{Monomorphs, ReturnValueMonomorphs};
use crate::bindgen::utilities::IterHelpers;

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
Expand Down Expand Up @@ -739,6 +739,22 @@ impl Type {
self.add_dependencies_ignoring_generics(&GenericParams::default(), library, out)
}

pub fn find_return_value_monomorphs(
&self,
monomorphs: &mut ReturnValueMonomorphs<'_>,
is_return_value: bool,
) {
match self {
Type::Ptr { ty, .. } => ty.find_return_value_monomorphs(monomorphs, false),
Type::Path(generic) => monomorphs.handle_return_value_path(generic, is_return_value),
Type::Primitive(_) => {}
Type::Array(ty, _) => ty.find_return_value_monomorphs(monomorphs, false),
Type::FuncPtr { ret, args, .. } => {
monomorphs.handle_function(ret, args.iter().map(|(_, arg)| arg))
}
}
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
match *self {
Type::Ptr { ref ty, .. } => {
Expand Down
13 changes: 12 additions & 1 deletion src/bindgen/ir/typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::bindgen::ir::{
};
use crate::bindgen::library::Library;
use crate::bindgen::mangle;
use crate::bindgen::monomorph::Monomorphs;
use crate::bindgen::monomorph::{Monomorphs, ReturnValueMonomorphs};

/// A type alias that is represented as a C typedef
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -102,6 +102,17 @@ impl Typedef {
}
}

pub fn find_return_value_monomorphs(
&self,
monomorphs: &mut ReturnValueMonomorphs<'_>,
is_return_value: bool,
) {
monomorphs.with_active_cfg(self.cfg.clone(), |m| {
self.aliased
.find_return_value_monomorphs(m, is_return_value);
});
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
// Generic structs can instantiate monomorphs only once they've been
// instantiated. See `instantiate_monomorph` for more details.
Expand Down
10 changes: 9 additions & 1 deletion src/bindgen/ir/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::bindgen::ir::{
};
use crate::bindgen::library::Library;
use crate::bindgen::mangle;
use crate::bindgen::monomorph::Monomorphs;
use crate::bindgen::monomorph::{Monomorphs, ReturnValueMonomorphs};
use crate::bindgen::rename::{IdentifierType, RenameRule};
use crate::bindgen::utilities::IterHelpers;

Expand Down Expand Up @@ -100,6 +100,14 @@ impl Union {
}
}

pub fn find_return_value_monomorphs(&self, monomorphs: &mut ReturnValueMonomorphs<'_>) {
monomorphs.with_active_cfg(self.cfg.clone(), |m| {
for field in &self.fields {
field.ty.find_return_value_monomorphs(m, false);
}
});
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
// Generic unions can instantiate monomorphs only once they've been
// instantiated. See `instantiate_monomorph` for more details.
Expand Down
38 changes: 35 additions & 3 deletions src/bindgen/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ use crate::bindgen::config::{Config, Language, SortKey};
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::error::Error;
use crate::bindgen::ir::{Constant, Enum, Function, Item, ItemContainer, ItemMap};
use crate::bindgen::ir::{OpaqueItem, Path, Static, Struct, Typedef, Union};
use crate::bindgen::monomorph::Monomorphs;
use crate::bindgen::ir::{
Constant, Enum, Function, Item, ItemContainer, ItemMap, OpaqueItem, Path, Static, Struct,
Typedef, Union,
};
use crate::bindgen::monomorph::{Monomorphs, ReturnValueMonomorphs};
use crate::bindgen::ItemType;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -81,6 +83,12 @@ impl Library {

let mut dependencies = Dependencies::new();

if self.config.language == Language::Cxx
&& self.config.export.instantiate_return_value_monomorphs
{
self.instantiate_return_value_monomorphs(&mut dependencies);
}

for function in &self.functions {
function.add_dependencies(&self, &mut dependencies);
}
Expand Down Expand Up @@ -447,4 +455,28 @@ impl Library {
x.mangle_paths(&monomorphs);
}
}

fn instantiate_return_value_monomorphs(&mut self, dependencies: &mut Dependencies) {
let mut monomorphs = ReturnValueMonomorphs::new(self);
self.structs
.for_all_items(|x| x.find_return_value_monomorphs(&mut monomorphs));
self.unions
.for_all_items(|x| x.find_return_value_monomorphs(&mut monomorphs));
self.enums
.for_all_items(|x| x.find_return_value_monomorphs(&mut monomorphs));
self.typedefs
.for_all_items(|x| x.find_return_value_monomorphs(&mut monomorphs, false));
for x in &self.functions {
x.find_return_value_monomorphs(&mut monomorphs);
}

let struct_name = match self.config.export.return_value_monomorphs_struct_name {
Some(ref name) => name,
_ => "__cbindgen_return_value_monomorphs",
};
if let Some((struct_name, struct_def)) = monomorphs.into_struct(struct_name) {
self.structs.try_insert(struct_def);
struct_name.add_dependencies(self, dependencies);
}
}
}
Loading
Loading