Partially support #[cfg]s on fields

This commit is contained in:
Yurii Rashkovskii 2019-04-08 14:24:52 +02:00 committed by Emilio Cobos Álvarez
parent dfcee869ba
commit 1963f0c92e
14 changed files with 113 additions and 1 deletions

@ -112,6 +112,7 @@ impl EnumVariant {
None => i.to_string(),
},
ty,
cfg: Cfg::load(&field.attrs),
annotations: AnnotationSet::load(&field.attrs)?,
documentation: Documentation::load(&field.attrs),
});

@ -2,13 +2,15 @@ use std::io::Write;
use crate::bindgen::cdecl;
use crate::bindgen::config::{Config, Language};
use crate::bindgen::ir::{AnnotationSet, Documentation, Path, Type};
use crate::bindgen::ir::{AnnotationSet, Cfg, ConditionWrite};
use crate::bindgen::ir::{Documentation, Path, ToCondition, Type};
use crate::bindgen::writer::{Source, SourceWriter};
#[derive(Debug, Clone)]
pub struct Field {
pub name: String,
pub ty: Type,
pub cfg: Option<Cfg>,
pub annotations: AnnotationSet,
pub documentation: Documentation,
}
@ -18,6 +20,7 @@ impl Field {
Field {
name,
ty,
cfg: None,
annotations: AnnotationSet::new(),
documentation: Documentation::none(),
}
@ -33,6 +36,7 @@ impl Field {
.ok_or_else(|| "field is missing identifier".to_string())?
.to_string(),
ty,
cfg: Cfg::load(&field.attrs),
annotations: AnnotationSet::load(&field.attrs)?,
documentation: Documentation::load(&field.attrs),
})
@ -44,6 +48,12 @@ impl Field {
impl Source for Field {
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
// Cython doesn't support conditional fields.
let condition = self.cfg.to_condition(config);
if config.language != Language::Cython {
condition.write_before(config, out);
}
self.documentation.write(config, out);
cdecl::write_field(out, &self.ty, &self.name, config);
// Cython extern declarations don't manage layouts, layouts are defined entierly by the
@ -53,5 +63,15 @@ impl Source for Field {
write!(out, ": {}", bitfield.unwrap_or_default());
}
}
if config.language != Language::Cython {
condition.write_after(config, out);
// FIXME(#634): `write_vertical_source_list` should support
// configuring list elements natively. For now we print a newline
// here to avoid printing `#endif;` with semicolon.
if condition.is_some() {
out.new_line();
}
}
}
}

@ -88,6 +88,7 @@ impl Struct {
out.push(Field {
name: format!("{}", current),
ty,
cfg: Cfg::load(&field.attrs),
annotations: AnnotationSet::load(&field.attrs)?,
documentation: Documentation::load(&field.attrs),
});
@ -191,6 +192,7 @@ impl Struct {
.map(|field| Field {
name: field.name.clone(),
ty: field.ty.specialize(mappings),
cfg: field.cfg.clone(),
annotations: field.annotations.clone(),
documentation: field.documentation.clone(),
})

@ -180,6 +180,7 @@ impl Item for Union {
overriden_fields.push(Field {
name: o[i].clone(),
ty: field.ty.clone(),
cfg: field.cfg.clone(),
annotations: field.annotations.clone(),
documentation: field.documentation.clone(),
});
@ -196,6 +197,7 @@ impl Item for Union {
.apply(&field.name, IdentifierType::StructMember)
.into_owned(),
ty: field.ty.clone(),
cfg: field.cfg.clone(),
annotations: field.annotations.clone(),
documentation: field.documentation.clone(),
})
@ -256,6 +258,7 @@ impl Item for Union {
.map(|field| Field {
name: field.name.clone(),
ty: field.ty.specialize(&mappings),
cfg: field.cfg.clone(),
annotations: field.annotations.clone(),
documentation: field.documentation.clone(),
})

@ -71,6 +71,13 @@ typedef struct BarHandle {
} BarHandle;
#endif
typedef struct ConditionalField {
#if defined(X11)
int32_t field
#endif
;
} ConditionalField;
#if (defined(PLATFORM_UNIX) && defined(X11))
void root(struct FooHandle a, union C c);
#endif
@ -78,3 +85,5 @@ void root(struct FooHandle a, union C c);
#if (defined(PLATFORM_WIN) || defined(M_32))
void root(struct BarHandle a, union C c);
#endif
void cond(struct ConditionalField a);

@ -89,6 +89,13 @@ typedef struct BarHandle {
} BarHandle;
#endif
typedef struct ConditionalField {
#if defined(X11)
int32_t field
#endif
;
} ConditionalField;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@ -101,6 +108,8 @@ void root(struct FooHandle a, union C c);
void root(struct BarHandle a, union C c);
#endif
void cond(struct ConditionalField a);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

@ -71,6 +71,13 @@ typedef struct {
} BarHandle;
#endif
typedef struct {
#if defined(X11)
int32_t field
#endif
;
} ConditionalField;
#if (defined(PLATFORM_UNIX) && defined(X11))
void root(FooHandle a, C c);
#endif
@ -78,3 +85,5 @@ void root(FooHandle a, C c);
#if (defined(PLATFORM_WIN) || defined(M_32))
void root(BarHandle a, C c);
#endif
void cond(ConditionalField a);

@ -89,6 +89,13 @@ typedef struct {
} BarHandle;
#endif
typedef struct {
#if defined(X11)
int32_t field
#endif
;
} ConditionalField;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@ -101,6 +108,8 @@ void root(FooHandle a, C c);
void root(BarHandle a, C c);
#endif
void cond(ConditionalField a);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

@ -195,6 +195,13 @@ struct BarHandle {
};
#endif
struct ConditionalField {
#if defined(X11)
int32_t field
#endif
;
};
extern "C" {
#if (defined(PLATFORM_UNIX) && defined(X11))
@ -205,4 +212,6 @@ void root(FooHandle a, C c);
void root(BarHandle a, C c);
#endif
void cond(ConditionalField a);
} // extern "C"

@ -55,8 +55,13 @@ cdef extern from *:
int32_t x;
float y;
ctypedef struct ConditionalField:
int32_t field;
IF (PLATFORM_UNIX and X11):
void root(FooHandle a, C c);
IF (PLATFORM_WIN or M_32):
void root(BarHandle a, C c);
void cond(ConditionalField a);

@ -71,6 +71,13 @@ struct BarHandle {
};
#endif
struct ConditionalField {
#if defined(X11)
int32_t field
#endif
;
};
#if (defined(PLATFORM_UNIX) && defined(X11))
void root(struct FooHandle a, union C c);
#endif
@ -78,3 +85,5 @@ void root(struct FooHandle a, union C c);
#if (defined(PLATFORM_WIN) || defined(M_32))
void root(struct BarHandle a, union C c);
#endif
void cond(struct ConditionalField a);

@ -89,6 +89,13 @@ struct BarHandle {
};
#endif
struct ConditionalField {
#if defined(X11)
int32_t field
#endif
;
};
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@ -101,6 +108,8 @@ void root(struct FooHandle a, union C c);
void root(struct BarHandle a, union C c);
#endif
void cond(struct ConditionalField a);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

@ -55,8 +55,13 @@ cdef extern from *:
int32_t x;
float y;
cdef struct ConditionalField:
int32_t field;
IF (PLATFORM_UNIX and X11):
void root(FooHandle a, C c);
IF (PLATFORM_WIN or M_32):
void root(BarHandle a, C c);
void cond(ConditionalField a);

@ -40,6 +40,15 @@ struct BarHandle {
y: f32,
}
// FIXME(#634): Support deriving methods for structs with conditional fields.
/// cbindgen:derive-eq=false
/// cbindgen:derive-neq=false
#[repr(C)]
struct ConditionalField {
#[cfg(x11)]
field: i32,
}
#[cfg(all(unix, x11))]
#[no_mangle]
pub extern "C" fn root(a: FooHandle, c: C)
@ -49,3 +58,7 @@ pub extern "C" fn root(a: FooHandle, c: C)
#[no_mangle]
pub extern "C" fn root(a: BarHandle, c: C)
{ }
#[no_mangle]
pub extern "C" fn cond(a: ConditionalField)
{ }