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

Generate member functions #36

Closed
Closed
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
4 changes: 4 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ pub struct StructConfig {
pub derive_gt: bool,
/// Whether to generate a greater than or equal to operator on structs with one field
pub derive_gte: bool,
/// Whether to generate member functions to wrap functions taking a pointer to this struct as
/// first argument
pub generate_member_functions: bool,
}

impl Default for StructConfig {
Expand All @@ -172,6 +175,7 @@ impl Default for StructConfig {
derive_lte: false,
derive_gt: false,
derive_gte: false,
generate_member_functions: true,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/bindgen/ir/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ impl Specialization {
.collect(),
generic_params: self.generic_params.clone(),
documentation: aliased.documentation.clone(),
functions: Vec::new(),
destructor: None,
})))
}
PathValue::Enum(ref aliased) => {
Expand Down Expand Up @@ -230,9 +232,7 @@ impl Typedef {

impl Source for Typedef {
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
if config.documentation {
self.documentation.write(config, out);
}
self.documentation.write(config, out);
out.write("typedef ");
(self.name.clone(), self.aliased.clone()).write(config, out);
out.write(";");
Expand Down
92 changes: 80 additions & 12 deletions src/bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ use bindgen::library::*;
use bindgen::rename::*;
use bindgen::utilities::*;
use bindgen::writer::*;
use bindgen::mangle;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FunctionWriteMode {
Global,
MemberFunction,
}

#[derive(Debug, Clone)]
pub struct Function {
Expand Down Expand Up @@ -46,6 +53,25 @@ impl Function {
})
}

pub fn add_member_function(&self, out: &mut MemberFunctions) {
if let Some(&(_, ref ty)) = self.args.get(0) {
match *ty {
Type::ConstPtr(ref t) | Type::Ptr(ref t) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to make these member functions const correct.

fn do_something(foo: *const Foo)

Should generate

struct Foo {
  void do_something() const {
    ::do_something(this)
  }
}

let t = match **t {
Type::Path(ref p, ref g) => {
Type::Path(mangle::mangle_path(p, g), Vec::new())
}
_ => return
};
out.entry(t)
.or_insert_with(Vec::new)
.push(self.clone())
}
_ => {}
}
}
}

pub fn add_deps(&self, library: &Library, out: &mut DependencyList) {
self.ret.add_deps(library, out);
for &(_, ref ty) in &self.args {
Expand Down Expand Up @@ -86,11 +112,17 @@ impl Function {
ty.mangle_paths(monomorphs);
}
}
}

impl Source for Function {
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
fn write_1<W: Write>(func: &Function, config: &Config, out: &mut SourceWriter<W>) {
pub fn write_formated<F: Write>(&self,
config: &Config,
out: &mut SourceWriter<F>,
mode: FunctionWriteMode)
{
fn write_1<W: Write>(func: &Function,
config: &Config,
out: &mut SourceWriter<W>,
mode: FunctionWriteMode)
{
let prefix = config.function.prefix(&func.annotations);
let postfix = config.function.postfix(&func.annotations);

Expand All @@ -99,15 +131,32 @@ impl Source for Function {
out.write(prefix);
out.write(" ");
}
cdecl::write_func(out, &func, false);
if mode == FunctionWriteMode::Global {
cdecl::write_func(out, &func, false);
} else {
let f = Function {
args: func.args[1..].to_owned(),
..func.clone()
};
cdecl::write_func(out, &f, false);
if let Type::ConstPtr(_) = func.args[0].1 {
out.write(" const");
}
}
if let Some(ref postfix) = postfix {
out.write(" ");
out.write(postfix);
}
out.write(";");
if mode == FunctionWriteMode::Global {
out.write(";");
}
}

fn write_2<W: Write>(func: &Function, config: &Config, out: &mut SourceWriter<W>) {
fn write_2<W: Write>(func: &Function,
config: &Config,
out: &mut SourceWriter<W>,
mode: FunctionWriteMode)
{
let prefix = config.function.prefix(&func.annotations);
let postfix = config.function.postfix(&func.annotations);

Expand All @@ -116,25 +165,44 @@ impl Source for Function {
out.write(prefix);
out.new_line();
}
cdecl::write_func(out, &func, true);
if mode == FunctionWriteMode::Global {
cdecl::write_func(out, &func, true);
} else {
let f = Function {
args: func.args[1..].to_owned(),
..func.clone()
};
cdecl::write_func(out, &f, true);
if let Type::ConstPtr(_) = func.args[0].1 {
out.write(" const");
}
}
if let Some(ref postfix) = postfix {
out.new_line();
out.write(postfix);
}
out.write(";");
if mode == FunctionWriteMode::Global {
out.write(";");
}
};

let option_1 = out.measure(|out| write_1(self, config, out));
let option_1 = out.measure(|out| write_1(self, config, out, mode));

if (config.function.args == Layout::Auto && option_1 <= config.line_length) ||
config.function.args == Layout::Horizontal {
write_1(self, config, out);
write_1(self, config, out, mode);
} else {
write_2(self, config, out);
write_2(self, config, out, mode);
}
}
}

impl Source for Function {
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
self.write_formated(config, out, FunctionWriteMode::Global)
}
}

pub trait SynFnArgHelpers {
fn as_ident_and_type(&self) -> Result<Option<(String, Type)>, String>;
}
Expand Down
Loading