Skip to content
Merged
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
26 changes: 24 additions & 2 deletions src/bindgen/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use std::cmp::Ordering;
use std::collections::HashSet;

use crate::bindgen::ir::{ItemContainer, Path};
use crate::bindgen::{
ir::{ItemContainer, Path},
library::Library,
};

/// A dependency list is used for gathering what order to output the types.
#[derive(Default)]
Expand All @@ -22,14 +25,33 @@ impl Dependencies {
}
}

pub fn add(&mut self, library: &Library, path: &Path) {
let Some(items) = library.get_items(path) else {
warn!(
"Can't find {path}. This usually means that this type was incompatible or not found."
);
return;
};
if self.items.contains(path) {
return;
}
self.items.insert(path.clone());
for item in &items {
item.deref().add_dependencies(library, self);
}
for item in items {
self.order.push(item);
}
}

pub fn sort(&mut self) {
// Sort untagged enums and opaque structs into their own layers because they don't
// depend on each other or anything else.
let ordering = |a: &ItemContainer, b: &ItemContainer| match (a, b) {
(ItemContainer::Enum(x), ItemContainer::Enum(y))
if x.tag.is_none() && y.tag.is_none() =>
{
x.path.cmp(&y.path)
Ordering::Equal
}
(ItemContainer::Enum(x), _) if x.tag.is_none() => Ordering::Less,
(_, ItemContainer::Enum(x)) if x.tag.is_none() => Ordering::Greater,
Expand Down
23 changes: 22 additions & 1 deletion src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,27 @@ pub enum Literal {
}

impl Literal {
fn replace_self_with(&mut self, self_ty: &Path) {
pub fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
self.visit(&mut |lit| {
match lit {
Literal::Struct {
ref path,
export_name: _,
fields: _,
}
| Literal::Path {
associated_to: Some((ref path, _)),
name: _,
} => {
out.add(library, path);
}
_ => {}
}
true
});
}

pub fn replace_self_with(&mut self, self_ty: &Path) {
match *self {
Literal::PostfixUnaryOp { ref mut value, .. } => {
value.replace_self_with(self_ty);
Expand Down Expand Up @@ -579,6 +599,7 @@ impl Item for Constant {

fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
self.ty.add_dependencies(library, out);
self.value.add_dependencies(library, out);
}

fn export_name(&self) -> &str {
Expand Down
9 changes: 8 additions & 1 deletion src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ impl EnumVariant {
config: &Config,
) -> Result<Self, String> {
let discriminant = match variant.discriminant {
Some((_, ref expr)) => Some(Literal::load(expr)?),
Some((_, ref expr)) => {
let mut discriminant = Literal::load(expr)?;
discriminant.replace_self_with(self_path);
Some(discriminant)
}
None => None,
};

Expand Down Expand Up @@ -258,6 +262,9 @@ impl EnumVariant {
if let VariantBody::Body { ref body, .. } = self.body {
body.add_dependencies(library, out);
}
if let Some(ref d) = self.discriminant {
d.add_dependencies(library, out);
}
}

fn resolve_declaration_types(&mut self, resolver: &DeclarationTypeResolver) {
Expand Down
18 changes: 1 addition & 17 deletions src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,23 +701,7 @@ impl Type {
}
let path = generic.path();
if !generic_params.iter().any(|param| param.name() == path) {
if let Some(items) = library.get_items(path) {
if !out.items.contains(path) {
out.items.insert(path.clone());

for item in &items {
item.deref().add_dependencies(library, out);
}
for item in items {
out.order.push(item);
}
}
} else {
warn!(
"Can't find {path}. This usually means that this type was incompatible or \
not found."
);
}
out.add(library, path);
}
}
Type::Primitive(_) => {}
Expand Down
3 changes: 3 additions & 0 deletions tests/expectations-symbols/enum_self_flags.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
12 changes: 6 additions & 6 deletions tests/expectations/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ DEF M_32 = 0
#include <stdint.h>
#include <stdlib.h>

#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType {
#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType {
A,
B,
C,
};
typedef uint32_t BarType;
typedef uint32_t FooType;
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType {
#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType {
A,
B,
C,
};
typedef uint32_t FooType;
typedef uint32_t BarType;
#endif

typedef struct {
Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/cfg.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ DEF M_32 = 0
#include <stdint.h>
#include <stdlib.h>

#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType
#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
Expand All @@ -23,12 +23,12 @@ enum BarType
C,
};
#ifndef __cplusplus
typedef uint32_t BarType;
typedef uint32_t FooType;
#endif // __cplusplus
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType
#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
Expand All @@ -38,7 +38,7 @@ enum FooType
C,
};
#ifndef __cplusplus
typedef uint32_t FooType;
typedef uint32_t BarType;
#endif // __cplusplus
#endif

Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ DEF M_32 = 0
#include <ostream>
#include <new>

#if (defined(PLATFORM_WIN) || defined(M_32))
enum class BarType : uint32_t {
#if (defined(PLATFORM_UNIX) && defined(X11))
enum class FooType : uint32_t {
A,
B,
C,
};
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
enum class FooType : uint32_t {
#if (defined(PLATFORM_WIN) || defined(M_32))
enum class BarType : uint32_t {
A,
B,
C,
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/cfg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ cdef extern from *:

cdef extern from *:

IF (PLATFORM_WIN or M_32):
IF (PLATFORM_UNIX and X11):
cdef enum:
A,
B,
C,
ctypedef uint32_t BarType;
ctypedef uint32_t FooType;

IF (PLATFORM_UNIX and X11):
IF (PLATFORM_WIN or M_32):
cdef enum:
A,
B,
C,
ctypedef uint32_t FooType;
ctypedef uint32_t BarType;

ctypedef struct Flags:
uint8_t _0;
Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/cfg_both.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ DEF M_32 = 0
#include <stdint.h>
#include <stdlib.h>

#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType {
#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType {
A,
B,
C,
};
typedef uint32_t BarType;
typedef uint32_t FooType;
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType {
#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType {
A,
B,
C,
};
typedef uint32_t FooType;
typedef uint32_t BarType;
#endif

typedef struct Flags {
Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/cfg_both.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ DEF M_32 = 0
#include <stdint.h>
#include <stdlib.h>

#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType
#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
Expand All @@ -23,12 +23,12 @@ enum BarType
C,
};
#ifndef __cplusplus
typedef uint32_t BarType;
typedef uint32_t FooType;
#endif // __cplusplus
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType
#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
Expand All @@ -38,7 +38,7 @@ enum FooType
C,
};
#ifndef __cplusplus
typedef uint32_t FooType;
typedef uint32_t BarType;
#endif // __cplusplus
#endif

Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/cfg_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ DEF M_32 = 0
#include <stdint.h>
#include <stdlib.h>

#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType {
#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType {
A,
B,
C,
};
typedef uint32_t BarType;
typedef uint32_t FooType;
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType {
#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType {
A,
B,
C,
};
typedef uint32_t FooType;
typedef uint32_t BarType;
#endif

struct Flags {
Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/cfg_tag.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ DEF M_32 = 0
#include <stdint.h>
#include <stdlib.h>

#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType
#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
Expand All @@ -23,12 +23,12 @@ enum BarType
C,
};
#ifndef __cplusplus
typedef uint32_t BarType;
typedef uint32_t FooType;
#endif // __cplusplus
#endif

#if (defined(PLATFORM_UNIX) && defined(X11))
enum FooType
#if (defined(PLATFORM_WIN) || defined(M_32))
enum BarType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
Expand All @@ -38,7 +38,7 @@ enum FooType
C,
};
#ifndef __cplusplus
typedef uint32_t FooType;
typedef uint32_t BarType;
#endif // __cplusplus
#endif

Expand Down
Loading