summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/clang.rs1
-rw-r--r--src/gen.rs17
-rw-r--r--src/parser.rs10
-rw-r--r--src/types.rs5
4 files changed, 22 insertions, 11 deletions
diff --git a/src/clang.rs b/src/clang.rs
index da4b9dae..2cf0905e 100644
--- a/src/clang.rs
+++ b/src/clang.rs
@@ -663,6 +663,7 @@ pub fn kind_to_str(x: Enum_CXCursorKind) -> &'static str {
CXCursor_InclusionDirective => "InclusionDirective",
//CXCursor_FirstPreprocessing => "FirstPreprocessing",
//CXCursor_LastPreprocessing => "LastPreprocessing",
+ CXCursor_PackedAttr => "PackedAttr",
_ => "?",
}
diff --git a/src/gen.rs b/src/gen.rs
index eed36477..fc90e5fa 100644
--- a/src/gen.rs
+++ b/src/gen.rs
@@ -488,12 +488,13 @@ fn ctypedef_to_rs(ctx: &mut GenCtx, name: String, ty: &Type) -> Vec<P<ast::Item>
fn comp_to_rs(ctx: &mut GenCtx, kind: CompKind, name: String,
layout: Layout, members: Vec<CompMember>) -> Vec<P<ast::Item>> {
match kind {
- CompKind::Struct => cstruct_to_rs(ctx, name, members),
+ CompKind::Struct => cstruct_to_rs(ctx, name, layout, members),
CompKind::Union => cunion_to_rs(ctx, name, layout, members),
}
}
-fn cstruct_to_rs(ctx: &mut GenCtx, name: String, members: Vec<CompMember>) -> Vec<P<ast::Item>> {
+fn cstruct_to_rs(ctx: &mut GenCtx, name: String,
+ layout: Layout, members: Vec<CompMember>) -> Vec<P<ast::Item>> {
let mut fields = vec!();
let mut methods = vec!();
// Nested composites may need to emit declarations and implementations as
@@ -557,7 +558,7 @@ fn cstruct_to_rs(ctx: &mut GenCtx, name: String, members: Vec<CompMember>) -> Ve
let id = rust_type_id(ctx, name.clone());
let struct_def = P(ast::Item { ident: ctx.ext_cx.ident_of(&id[..]),
- attrs: vec!(mk_repr_attr(ctx), mk_deriving_copy_attr(ctx)),
+ attrs: vec!(mk_repr_attr(ctx, layout), mk_deriving_copy_attr(ctx)),
id: ast::DUMMY_NODE_ID,
node: def,
vis: ast::Public,
@@ -640,7 +641,7 @@ fn cunion_to_rs(ctx: &mut GenCtx, name: String, layout: Layout, members: Vec<Com
empty_generics()
);
let union_id = rust_type_id(ctx, name.clone());
- let union_attrs = vec!(mk_repr_attr(ctx), mk_deriving_copy_attr(ctx));
+ let union_attrs = vec!(mk_repr_attr(ctx, layout), mk_deriving_copy_attr(ctx));
let union_def = mk_item(ctx, union_id, def, ast::Public, union_attrs);
let union_impl = ast::ItemImpl(
@@ -833,10 +834,14 @@ fn mk_link_name_attr(ctx: &mut GenCtx, name: String) -> ast::Attribute {
respan(ctx.span, attr)
}
-fn mk_repr_attr(ctx: &mut GenCtx) -> ast::Attribute {
+fn mk_repr_attr(ctx: &mut GenCtx, layout: Layout) -> ast::Attribute {
+ let mut values = vec!(P(respan(ctx.span, ast::MetaWord(to_intern_str(ctx, "C".to_string())))));
+ if layout.packed {
+ values.push(P(respan(ctx.span, ast::MetaWord(to_intern_str(ctx, "packed".to_string())))));
+ }
let attr_val = P(respan(ctx.span, ast::MetaList(
to_intern_str(ctx, "repr".to_string()),
- vec!(P(respan(ctx.span, ast::MetaWord(to_intern_str(ctx, "C".to_string())))))
+ values
)));
respan(ctx.span, ast::Attribute_ {
diff --git a/src/parser.rs b/src/parser.rs
index b8e4e72a..bfb8065d 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -312,7 +312,8 @@ fn opaque_ty(ctx: &mut ClangParserCtx, ty: &cx::Type) {
/// nested composites that make up the visited composite.
fn visit_composite(cursor: &Cursor, parent: &Cursor,
ctx: &mut ClangParserCtx,
- members: &mut Vec<CompMember>) -> Enum_CXVisitorResult {
+ compinfo: &mut CompInfo) -> Enum_CXVisitorResult {
+ let ref mut members = compinfo.members;
fn is_bitfield_continuation(field: &il::FieldInfo, ty: &il::Type, width: u32) -> bool {
match (&field.bitfields, ty) {
@@ -421,11 +422,14 @@ fn visit_composite(cursor: &Cursor, parent: &Cursor,
let ci = decl.compinfo();
cursor.visit(|c, p| {
let mut ci_ = ci.borrow_mut();
- visit_composite(c, p, ctx_, &mut ci_.members)
+ visit_composite(c, p, ctx_, &mut ci_)
});
members.push(CompMember::Comp(decl.compinfo()));
});
}
+ CXCursor_PackedAttr => {
+ compinfo.layout.packed = true;
+ }
_ => {
// XXX: Some kind of warning would be nice, but this produces far
// too many.
@@ -493,7 +497,7 @@ fn visit_top<'r>(cursor: &Cursor,
let ci = decl.compinfo();
cursor.visit(|c, p| {
let mut ci_ = ci.borrow_mut();
- visit_composite(c, p, ctx_, &mut ci_.members)
+ visit_composite(c, p, ctx_, &mut ci_)
});
ctx_.globals.push(GComp(ci));
});
diff --git a/src/types.rs b/src/types.rs
index 27cc0b03..5af176bf 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -128,15 +128,16 @@ impl Type {
pub struct Layout {
pub size: usize,
pub align: usize,
+ pub packed: bool,
}
impl Layout {
pub fn new(size: usize, align: usize) -> Layout {
- Layout { size: size, align: align }
+ Layout { size: size, align: align, packed: false }
}
pub fn zero() -> Layout {
- Layout { size: 0, align: 0 }
+ Layout { size: 0, align: 0, packed: false }
}
}