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

Fix order of structs #42

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
23 changes: 23 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 @@ -22,6 +22,7 @@ serde_derive = "0.9"
serde_json = "0.9"
tempdir = "0.3"
toml = "0.3"
petgraph = "0.4"

[dependencies.syn]
version = "0.11"
Expand Down
13 changes: 13 additions & 0 deletions compile-tests/cycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#[repr(C)]
pub struct A {
b: *const B,
}

#[repr(C)]
pub struct B {
a: *const A,
}

#[no_mangle]
pub extern "C" fn foo(a: A) {}
21 changes: 15 additions & 6 deletions src/bindgen/ir/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ impl Specialization {
}
}

pub fn add_specializations(&self, library: &Library, out: &mut SpecializationList) {
pub fn add_specializations(&self, library: &Library,
out: &mut SpecializationList,
cycle_check: &mut CycleCheckList)
{
match self.specialize(library) {
Ok(Some(specialization)) => {
if !out.items.contains(specialization.name()) {
out.items.insert(specialization.name().to_owned());

specialization.add_specializations(library, out);
specialization.add_specializations(library, out, cycle_check);

out.order.push(specialization);
}
Expand Down Expand Up @@ -215,12 +218,18 @@ impl Typedef {
self.aliased.add_deps(library, out);
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
self.aliased.add_monomorphs(library, out);
pub fn add_monomorphs(&self, library: &Library,
out: &mut Monomorphs,
cycle_check: &mut CycleCheckList)
{
self.aliased.add_monomorphs(library, out, cycle_check);
}

pub fn add_specializations(&self, library: &Library, out: &mut SpecializationList) {
self.aliased.add_specializations(library, out);
pub fn add_specializations(&self, library: &Library,
out: &mut SpecializationList,
cycle_check: &mut CycleCheckList)
{
self.aliased.add_specializations(library, out, cycle_check);
}

pub fn mangle_paths(&mut self, monomorphs: &Monomorphs) {
Expand Down
18 changes: 12 additions & 6 deletions src/bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,23 @@ impl Function {
}
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
self.ret.add_monomorphs(library, out);
pub fn add_monomorphs(&self, library: &Library,
out: &mut Monomorphs,
cycle_check: &mut CycleCheckList)
{
self.ret.add_monomorphs(library, out, cycle_check);
for &(_, ref ty) in &self.args {
ty.add_monomorphs(library, out);
ty.add_monomorphs(library, out, cycle_check);
}
}

pub fn add_specializations(&self, library: &Library, out: &mut SpecializationList) {
self.ret.add_specializations(library, out);
pub fn add_specializations(&self, library: &Library,
out: &mut SpecializationList,
cycle_check: &mut CycleCheckList)
{
self.ret.add_specializations(library, out, cycle_check);
for &(_, ref ty) in &self.args {
ty.add_specializations(library, out);
ty.add_specializations(library, out, cycle_check);
}
}

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

use std::collections::BTreeMap;
use std::io::Write;
use std::fmt::{Display, self};

use syn;

Expand All @@ -25,6 +26,12 @@ pub struct Struct {
pub documentation: Documentation,
}

impl Display for Struct {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Struct {}", self.name)
}
}

impl Struct {
pub fn load(name: String,
annotations: AnnotationSet,
Expand Down Expand Up @@ -66,18 +73,31 @@ impl Struct {
})
}

pub fn as_opaque(&self) -> OpaqueItem {
OpaqueItem {
name: self.name.clone(),
generic_params: self.generic_params.clone(),
annotations: self.annotations.clone(),
documentation: self.documentation.clone(),
}
}

pub fn add_deps(&self, library: &Library, out: &mut DependencyList) {
for &(_, ref ty, _) in &self.fields {
ty.add_deps_with_generics(&self.generic_params, library, out);
}
}

pub fn add_monomorphs(&self, library: &Library, generic_values: &Vec<Type>, out: &mut Monomorphs) {
pub fn add_monomorphs(&self, library: &Library,
generic_values: &Vec<Type>,
out: &mut Monomorphs,
cycle_check: &mut CycleCheckList)
{
assert!(self.generic_params.len() == generic_values.len());

if self.generic_params.len() == 0 {
for &(_, ref ty, _) in &self.fields {
ty.add_monomorphs(library, out);
ty.add_monomorphs(library, out, cycle_check);
}
return;
}
Expand All @@ -97,19 +117,22 @@ impl Struct {
};

for &(_, ref ty, _) in &monomorph.fields {
ty.add_monomorphs(library, out);
ty.add_monomorphs(library, out, cycle_check);
}

if !out.contains_key(&self.name) {
out.insert(self.name.clone(), BTreeMap::new());
}
out.get_mut(&self.name).unwrap().insert(generic_values.clone(),
out.get_mut(&self.name).unwrap().insert(generic_values.clone(),
Monomorph::Struct(monomorph));
}

pub fn add_specializations(&self, library: &Library, out: &mut SpecializationList) {
pub fn add_specializations(&self, library: &Library,
out: &mut SpecializationList,
cycle_check: &mut CycleCheckList)
{
for &(_, ref ty, _) in &self.fields {
ty.add_specializations(library, out);
ty.add_specializations(library, out, cycle_check);
}
}

Expand Down
Loading