Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
27 changes: 18 additions & 9 deletions 27 src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ pub(crate) fn to_known_assoc_constant(associated_to: &Path, name: &str) -> Optio
Some(format!("{}_{}", prefix, name))
}

#[derive(Debug, Clone)]
pub struct LiteralStructField {
pub value: Literal,
pub cfg: Option<Cfg>,
}

#[derive(Debug, Clone)]
pub enum Literal {
Expr(String),
Expand All @@ -101,7 +107,7 @@ pub enum Literal {
Struct {
path: Path,
export_name: String,
fields: HashMap<String, Literal>,
fields: HashMap<String, LiteralStructField>,
},
Cast {
ty: Type,
Expand Down Expand Up @@ -135,7 +141,7 @@ impl Literal {
self_ty.name().clone_into(export_name);
}
for ref mut expr in fields.values_mut() {
expr.replace_self_with(self_ty);
expr.value.replace_self_with(self_ty);
}
}
Literal::Cast {
Expand Down Expand Up @@ -203,7 +209,7 @@ impl Literal {
Literal::FieldAccess { ref base, .. } => base.visit(visitor),
Literal::Struct { ref fields, .. } => {
for (_name, field) in fields.iter() {
if !field.visit(visitor) {
if !field.value.visit(visitor) {
return false;
}
}
Expand Down Expand Up @@ -250,7 +256,7 @@ impl Literal {
} => {
config.export.rename(export_name);
for lit in fields.values_mut() {
lit.rename_for_config(config);
lit.value.rename_for_config(config);
}
}
Literal::FieldAccess { ref mut base, .. } => {
Expand Down Expand Up @@ -388,12 +394,13 @@ impl Literal {
} => name,
_ => return Err(format!("Unsupported call expression. {:?}", *expr)),
};
let mut fields = HashMap::<String, Literal>::default();
let mut fields = HashMap::<String, LiteralStructField>::default();
for (index, arg) in args.iter().enumerate() {
let ident =
member_to_ident(&syn::Member::Unnamed(syn::Index::from(index))).to_string();
let value = Literal::load(arg)?;
fields.insert(ident, value);
let field = LiteralStructField { value, cfg: None };
fields.insert(ident, field);
}
Ok(Literal::Struct {
path: Path::new(struct_name.clone()),
Expand All @@ -408,11 +415,13 @@ impl Literal {
..
}) => {
let struct_name = path.segments[0].ident.unraw().to_string();
let mut field_map = HashMap::<String, Literal>::default();
let mut field_map = HashMap::<String, LiteralStructField>::default();
for field in fields {
let ident = member_to_ident(&field.member).to_string();
let cfg = Cfg::load(&field.attrs);
let value = Literal::load(&field.expr)?;
field_map.insert(ident, value);
let field = LiteralStructField { value, cfg };
field_map.insert(ident, field);
}
Ok(Literal::Struct {
path: Path::new(struct_name.clone()),
Expand Down Expand Up @@ -682,7 +691,7 @@ impl Constant {
if !out.bindings().struct_is_transparent(path) {
break;
}
value = fields.iter().next().unwrap().1
value = &fields.iter().next().unwrap().1.value
}

language_backend.write_documentation(out, self.documentation());
Expand Down
10 changes: 5 additions & 5 deletions 10 src/bindgen/language_backend/clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,19 +912,19 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
let ordered_fields = out.bindings().struct_field_names(path);
for (i, ordered_key) in ordered_fields.iter().enumerate() {
if let Some(lit) = fields.get(ordered_key) {
let condition = lit.cfg.to_condition(self.config);
if is_constexpr {
out.new_line();

condition.write_before(self.config, out);
// TODO: Some C++ versions (c++20?) now support designated
// initializers, consider generating them.
write!(out, "/* .{} = */ ", ordered_key);
self.write_literal(out, lit);
self.write_literal(out, &lit.value);
if i + 1 != ordered_fields.len() {
write!(out, ",");
if !is_constexpr {
write!(out, " ");
}
}
condition.write_after(self.config, out);
} else {
if i > 0 {
write!(out, ", ");
Expand All @@ -937,7 +937,7 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
} else {
write!(out, ".{} = ", ordered_key);
}
self.write_literal(out, lit);
self.write_literal(out, &lit.value);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion 2 src/bindgen/language_backend/cython.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
} else {
is_first_field = false;
}
self.write_literal(out, lit);
self.write_literal(out, &lit.value);
}
}
write!(out, " }}");
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/expectations/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ typedef struct {
#endif
;
} ConditionalField;
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

typedef struct {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/expectations/cfg.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ typedef struct {
#endif
;
} ConditionalField;
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

typedef struct {
int32_t x;
Expand Down
10 changes: 10 additions & 0 deletions 10 tests/expectations/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ struct ConditionalField {
#endif
;
};
constexpr static const ConditionalField ConditionalField_ZERO = ConditionalField{
#if defined(X11)
/* .field = */ 0
#endif
};
constexpr static const ConditionalField ConditionalField_ONE = ConditionalField{
#if defined(X11)
/* .field = */ 1
#endif
};

struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/expectations/cfg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ cdef extern from *:

ctypedef struct ConditionalField:
int32_t field;
const ConditionalField ConditionalField_ZERO # = <ConditionalField>{ 0 }
const ConditionalField ConditionalField_ONE # = <ConditionalField>{ 1 }

ctypedef struct Normal:
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/expectations/cfg_both.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ typedef struct ConditionalField {
#endif
;
} ConditionalField;
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

typedef struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/expectations/cfg_both.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ typedef struct ConditionalField {
#endif
;
} ConditionalField;
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

typedef struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/expectations/cfg_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ struct ConditionalField {
#endif
;
};
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/expectations/cfg_tag.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct ConditionalField {
#endif
;
};
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/expectations/cfg_tag.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ cdef extern from *:

cdef struct ConditionalField:
int32_t field;
const ConditionalField ConditionalField_ZERO # = <ConditionalField>{ 0 }
const ConditionalField ConditionalField_ONE # = <ConditionalField>{ 1 }

cdef struct Normal:
int32_t x;
Expand Down
11 changes: 11 additions & 0 deletions 11 tests/rust/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ struct ConditionalField {
field: i32,
}

impl ConditionalField {
pub const ZERO: Self = Self {
#[cfg(x11)]
field: 0,
};
pub const ONE: Self = Self {
#[cfg(x11)]
field: 1,
};
}

#[cfg(all(unix, x11))]
#[no_mangle]
pub extern "C" fn root(a: FooHandle, c: C)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.