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

Implement runtime binding generation #923

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
353 changes: 21 additions & 332 deletions src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ use std::rc::Rc;

use crate::bindgen::config::{Config, Language};
use crate::bindgen::ir::{
Constant, Function, ItemContainer, ItemMap, Path as BindgenPath, Static, Struct, Typedef,
Constant, Function, ItemContainer, ItemMap, Path as BindgenPath, Static, Struct, Type, Typedef,
};
use crate::bindgen::writer::{Source, SourceWriter};
use crate::bindgen::language_backend::{
CLikeLanguageBackend, CythonLanguageBackend, LanguageBackend,
};
use crate::bindgen::writer::SourceWriter;

/// A bindings header that can be written.
pub struct Bindings {
Expand All @@ -25,22 +28,16 @@ pub struct Bindings {
struct_map: ItemMap<Struct>,
typedef_map: ItemMap<Typedef>,
struct_fileds_memo: RefCell<HashMap<BindgenPath, Rc<Vec<String>>>>,
globals: Vec<Static>,
constants: Vec<Constant>,
items: Vec<ItemContainer>,
functions: Vec<Function>,
pub globals: Vec<Static>,
pub constants: Vec<Constant>,
pub items: Vec<ItemContainer>,
pub functions: Vec<Function>,
source_files: Vec<path::PathBuf>,
/// Bindings are generated by a recursive call to cbindgen
/// and shouldn't do anything when written anywhere.
noop: bool,
}

#[derive(PartialEq, Eq)]
enum NamespaceOperation {
Open,
Close,
}

impl Bindings {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
Expand Down Expand Up @@ -77,8 +74,6 @@ impl Bindings {

/// Peels through typedefs to allow resolving structs.
fn resolved_struct_path<'a>(&self, path: &'a BindgenPath) -> Cow<'a, BindgenPath> {
use crate::bindgen::ir::Type;

let mut resolved_path = Cow::Borrowed(path);
loop {
let mut found = None;
Expand Down Expand Up @@ -204,334 +199,28 @@ impl Bindings {
}
}

pub fn write_headers<F: Write>(&self, out: &mut SourceWriter<F>) {
if self.noop {
return;
}

if let Some(ref f) = self.config.header {
out.new_line_if_not_start();
write!(out, "{}", f);
out.new_line();
}
if let Some(f) = self.config.include_guard() {
out.new_line_if_not_start();
write!(out, "#ifndef {}", f);
out.new_line();
write!(out, "#define {}", f);
out.new_line();
}
if self.config.pragma_once && self.config.language != Language::Cython {
out.new_line_if_not_start();
write!(out, "#pragma once");
out.new_line();
}
if self.config.include_version {
out.new_line_if_not_start();
write!(
out,
"/* Generated with cbindgen:{} */",
crate::bindgen::config::VERSION
);
out.new_line();
}
if let Some(ref f) = self.config.autogen_warning {
out.new_line_if_not_start();
write!(out, "{}", f);
out.new_line();
}

if self.config.no_includes
&& self.config.sys_includes().is_empty()
&& self.config.includes().is_empty()
&& (self.config.cython.cimports.is_empty() || self.config.language != Language::Cython)
&& self.config.after_includes.is_none()
{
return;
}

out.new_line_if_not_start();

if !self.config.no_includes {
match self.config.language {
Language::C => {
out.write("#include <stdarg.h>");
out.new_line();
out.write("#include <stdbool.h>");
out.new_line();
if self.config.usize_is_size_t {
out.write("#include <stddef.h>");
out.new_line();
}
out.write("#include <stdint.h>");
out.new_line();
out.write("#include <stdlib.h>");
out.new_line();
}
Language::Cxx => {
out.write("#include <cstdarg>");
out.new_line();
if self.config.usize_is_size_t {
out.write("#include <cstddef>");
out.new_line();
}
out.write("#include <cstdint>");
out.new_line();
out.write("#include <cstdlib>");
out.new_line();
out.write("#include <ostream>");
out.new_line();
out.write("#include <new>");
out.new_line();
if self.config.enumeration.cast_assert_name.is_none()
&& (self.config.enumeration.derive_mut_casts
|| self.config.enumeration.derive_const_casts)
{
out.write("#include <cassert>");
out.new_line();
}
}
Language::Cython => {
out.write(
"from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t",
);
out.new_line();
out.write(
"from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t",
);
out.new_line();
out.write("cdef extern from *");
out.open_brace();
out.write("ctypedef bint bool");
out.new_line();
out.write("ctypedef struct va_list");
out.new_line();
out.close_brace(false);
}
pub fn write<F: Write>(&self, file: F) {
match self.config.language {
Language::Cxx | Language::C => {
self.write_with_backend(file, &mut CLikeLanguageBackend::new(&self.config))
}
}

for include in self.config.sys_includes() {
write!(out, "#include <{}>", include);
out.new_line();
}

for include in self.config.includes() {
write!(out, "#include \"{}\"", include);
out.new_line();
}

if self.config.language == Language::Cython {
for (module, names) in &self.config.cython.cimports {
write!(out, "from {} cimport {}", module, names.join(", "));
out.new_line();
Language::Cython => {
self.write_with_backend(file, &mut CythonLanguageBackend::new(&self.config))
}
}

if let Some(ref line) = self.config.after_includes {
write!(out, "{}", line);
out.new_line();
}
}

pub fn write<F: Write>(&self, file: F) {
pub fn write_with_backend<F: Write, LB: LanguageBackend>(
&self,
file: F,
language_backend: &mut LB,
) {
if self.noop {
return;
}

let mut out = SourceWriter::new(file, self);

self.write_headers(&mut out);

self.open_namespaces(&mut out);

for constant in &self.constants {
if constant.uses_only_primitive_types() {
out.new_line_if_not_start();
constant.write(&self.config, &mut out, None);
out.new_line();
}
}

for item in &self.items {
if item
.deref()
.annotations()
.bool("no-export")
.unwrap_or(false)
{
continue;
}

out.new_line_if_not_start();
match *item {
ItemContainer::Constant(..) => unreachable!(),
ItemContainer::Static(..) => unreachable!(),
ItemContainer::Enum(ref x) => x.write(&self.config, &mut out),
ItemContainer::Struct(ref x) => x.write(&self.config, &mut out),
ItemContainer::Union(ref x) => x.write(&self.config, &mut out),
ItemContainer::OpaqueItem(ref x) => x.write(&self.config, &mut out),
ItemContainer::Typedef(ref x) => x.write(&self.config, &mut out),
}
out.new_line();
}

for constant in &self.constants {
if !constant.uses_only_primitive_types() {
out.new_line_if_not_start();
constant.write(&self.config, &mut out, None);
out.new_line();
}
}

if !self.functions.is_empty() || !self.globals.is_empty() {
if self.config.cpp_compatible_c() {
out.new_line_if_not_start();
out.write("#ifdef __cplusplus");
}

if self.config.language == Language::Cxx {
if let Some(ref using_namespaces) = self.config.using_namespaces {
for namespace in using_namespaces {
out.new_line();
write!(out, "using namespace {};", namespace);
}
out.new_line();
}
}

if self.config.language == Language::Cxx || self.config.cpp_compatible_c() {
out.new_line();
out.write("extern \"C\" {");
out.new_line();
}

if self.config.cpp_compatible_c() {
out.write("#endif // __cplusplus");
out.new_line();
}

for global in &self.globals {
out.new_line_if_not_start();
global.write(&self.config, &mut out);
out.new_line();
}

for function in &self.functions {
out.new_line_if_not_start();
function.write(&self.config, &mut out);
out.new_line();
}

if self.config.cpp_compatible_c() {
out.new_line();
out.write("#ifdef __cplusplus");
}

if self.config.language == Language::Cxx || self.config.cpp_compatible_c() {
out.new_line();
out.write("} // extern \"C\"");
out.new_line();
}

if self.config.cpp_compatible_c() {
out.write("#endif // __cplusplus");
out.new_line();
}
}

if self.config.language == Language::Cython
&& self.globals.is_empty()
&& self.constants.is_empty()
&& self.items.is_empty()
&& self.functions.is_empty()
{
out.write("pass");
}

self.close_namespaces(&mut out);

if let Some(f) = self.config.include_guard() {
out.new_line_if_not_start();
if self.config.language == Language::C {
write!(out, "#endif /* {} */", f);
} else {
write!(out, "#endif // {}", f);
}
out.new_line();
}
if let Some(ref f) = self.config.trailer {
out.new_line_if_not_start();
write!(out, "{}", f);
if !f.ends_with('\n') {
out.new_line();
}
}
}

fn all_namespaces(&self) -> Vec<&str> {
if self.config.language != Language::Cxx && !self.config.cpp_compatible_c() {
return vec![];
}
let mut ret = vec![];
if let Some(ref namespace) = self.config.namespace {
ret.push(&**namespace);
}
if let Some(ref namespaces) = self.config.namespaces {
for namespace in namespaces {
ret.push(&**namespace);
}
}
ret
}

fn open_close_namespaces<F: Write>(&self, op: NamespaceOperation, out: &mut SourceWriter<F>) {
if self.config.language == Language::Cython {
if op == NamespaceOperation::Open {
out.new_line();
let header = self.config.cython.header.as_deref().unwrap_or("*");
write!(out, "cdef extern from {}", header);
out.open_brace();
} else {
out.close_brace(false);
}
return;
}

let mut namespaces = self.all_namespaces();
if namespaces.is_empty() {
return;
}

if op == NamespaceOperation::Close {
namespaces.reverse();
}

if self.config.cpp_compatible_c() {
out.new_line_if_not_start();
out.write("#ifdef __cplusplus");
}

for namespace in namespaces {
out.new_line();
match op {
NamespaceOperation::Open => write!(out, "namespace {} {{", namespace),
NamespaceOperation::Close => write!(out, "}} // namespace {}", namespace),
}
}

out.new_line();
if self.config.cpp_compatible_c() {
out.write("#endif // __cplusplus");
out.new_line();
}
}

pub(crate) fn open_namespaces<F: Write>(&self, out: &mut SourceWriter<F>) {
self.open_close_namespaces(NamespaceOperation::Open, out);
}

pub(crate) fn close_namespaces<F: Write>(&self, out: &mut SourceWriter<F>) {
self.open_close_namespaces(NamespaceOperation::Close, out);
language_backend.write_bindings(&mut out, self);
}
}