Skip to content
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

Other calling conventions #979

Open
wants to merge 8 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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ toml = "0.8.8"
proc-macro2 = "1.0.60"
quote = "1"
heck = "0.4"
indoc = "2.0.5"

[dependencies.syn]
version = "2.0.64"
Expand Down
12 changes: 12 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,18 @@ rename_args = "PascalCase"
# default: "None"
sort_by = "Name"

# This rule specifies whether calling conventions specified in Rust should be
# translated and emitted in C-like outputs.
#
# For example, `extern "cdecl" fn foo() {}` will be emitted as `void
# __cbindgen_abi_cdecl foo();` and a compiler-specific definition (for Clang, GCC, ICX,
# and MSVC) will be emitted at the top of the file defining `__cbindgen_abi_cdecl` to
# the appropriate form for the compiler in the case of `cdecl`, this is `__cdecl` for
# Clang, ICX, and MSVC and `__attribute__((cdecl))` for GCC.
#
# default: true
emit_calling_convention = true

[struct]
# A rule to use to rename struct field names. The renaming assumes the input is
# the Rust standard snake_case, however it acccepts all the different rename_args
Expand Down
7 changes: 7 additions & 0 deletions src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::bindgen::ir::{
use crate::bindgen::language_backend::{
CLikeLanguageBackend, CythonLanguageBackend, LanguageBackend,
};
use crate::bindgen::predefines::Predefines;
use crate::bindgen::writer::SourceWriter;

/// A bindings header that can be written.
Expand All @@ -28,6 +29,10 @@ pub struct Bindings {
struct_map: ItemMap<Struct>,
typedef_map: ItemMap<Typedef>,
struct_fileds_memo: RefCell<HashMap<BindgenPath, Rc<Vec<String>>>>,
/// Raw code that needs to be inserted at the top of the generated bindings, usually
/// based on some condition found in the code itself (motivating example being
/// defining compiler and platform independent calling conventions)
pub predefines: Predefines,
pub globals: Vec<Static>,
pub constants: Vec<Constant>,
pub items: Vec<ItemContainer>,
Expand All @@ -46,6 +51,7 @@ impl Bindings {
struct_map: ItemMap<Struct>,
typedef_map: ItemMap<Typedef>,
constants: Vec<Constant>,
predefines: Predefines,
globals: Vec<Static>,
items: Vec<ItemContainer>,
functions: Vec<Function>,
Expand All @@ -58,6 +64,7 @@ impl Bindings {
struct_map,
typedef_map,
struct_fileds_memo: Default::default(),
predefines,
globals,
constants,
items,
Expand Down
1 change: 1 addition & 0 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ impl Builder {
Default::default(),
Default::default(),
Default::default(),
Default::default(),
true,
String::new(),
));
Expand Down
13 changes: 13 additions & 0 deletions src/bindgen/cdecl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::bindgen::language_backend::LanguageBackend;
use crate::bindgen::writer::{ListType, SourceWriter};
use crate::bindgen::{Config, Language};

use super::ir::FunctionAbi;

// This code is for translating Rust types into C declarations.
// See Section 6.7, Declarations, in the C standard for background.
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
Expand Down Expand Up @@ -40,6 +42,7 @@ struct CDecl {
type_name: String,
type_generic_args: Vec<GenericArgument>,
declarators: Vec<CDeclarator>,
abi: Option<FunctionAbi>,
type_ctype: Option<DeclarationType>,
deprecated: Option<String>,
}
Expand All @@ -51,6 +54,7 @@ impl CDecl {
type_name: String::new(),
type_generic_args: Vec::new(),
declarators: Vec::new(),
abi: None,
type_ctype: None,
deprecated: None,
}
Expand Down Expand Up @@ -103,6 +107,7 @@ impl CDecl {
never_return: f.never_return,
});
self.deprecated.clone_from(&f.annotations.deprecated);
self.abi = Some(f.abi.clone());
self.build_type(&f.ret, false, config);
}

Expand Down Expand Up @@ -267,6 +272,14 @@ impl CDecl {
}
}

if config.language != Language::Cython && config.function.emit_calling_convention {
if let Some(ref abi) = self.abi {
if let Some(attribute) = abi.as_attribute() {
write!(out, "{} ", attribute);
}
}
}

// Write the identifier
if let Some(ident) = ident {
write!(out, "{}", ident);
Expand Down
3 changes: 3 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ pub struct FunctionConfig {
pub sort_by: Option<SortKey>,
/// Optional text to output after functions which return `!`.
pub no_return: Option<String>,
/// Whether to emit calling convention attributes for functions which declare them
pub emit_calling_convention: bool,
}

impl Default for FunctionConfig {
Expand All @@ -445,6 +447,7 @@ impl Default for FunctionConfig {
swift_name_macro: None,
sort_by: None,
no_return: None,
emit_calling_convention: true,
}
}
}
Expand Down
Loading
Loading