Skip to content

Commit 754f6e6

Browse files
committed
extract offset of field
1 parent 268d40d commit 754f6e6

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/clang.rs

+11
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,17 @@ impl Cursor {
475475
unsafe { clang_CXXField_isMutable(self.x) != 0 }
476476
}
477477

478+
/// Get the offset of the field represented by the Cursor.
479+
pub fn offset_of_field(&self) -> Result<usize, LayoutError> {
480+
let offset = unsafe { clang_Cursor_getOffsetOfField(self.x) };
481+
482+
if offset < 0 {
483+
Err(LayoutError::from(offset as i32))
484+
} else {
485+
Ok(offset as usize)
486+
}
487+
}
488+
478489
/// Is this cursor's referent a member function that is declared `static`?
479490
pub fn method_is_static(&self) -> bool {
480491
unsafe { clang_CXXMethod_isStatic(self.x) != 0 }

src/ir/comp.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ pub struct Field {
102102
bitfield: Option<u32>,
103103
/// If the C++ field is marked as `mutable`
104104
mutable: bool,
105+
/// The offset of the field (in bits)
106+
offset: usize,
105107
}
106108

107109
impl Field {
@@ -111,7 +113,8 @@ impl Field {
111113
comment: Option<String>,
112114
annotations: Option<Annotations>,
113115
bitfield: Option<u32>,
114-
mutable: bool)
116+
mutable: bool,
117+
offset: usize)
115118
-> Field {
116119
Field {
117120
name: name,
@@ -120,6 +123,7 @@ impl Field {
120123
annotations: annotations.unwrap_or_default(),
121124
bitfield: bitfield,
122125
mutable: mutable,
126+
offset: offset,
123127
}
124128
}
125129

@@ -152,6 +156,11 @@ impl Field {
152156
pub fn annotations(&self) -> &Annotations {
153157
&self.annotations
154158
}
159+
160+
/// The offset of the field (in bits)
161+
pub fn offset(&self) -> usize {
162+
self.offset
163+
}
155164
}
156165

157166
impl CanDeriveDebug for Field {
@@ -532,7 +541,7 @@ impl CompInfo {
532541
cursor.visit(|cur| {
533542
if cur.kind() != CXCursor_FieldDecl {
534543
if let Some((ty, _)) = maybe_anonymous_struct_field {
535-
let field = Field::new(None, ty, None, None, None, false);
544+
let field = Field::new(None, ty, None, None, None, false, 0);
536545
ci.fields.push(field);
537546
}
538547
maybe_anonymous_struct_field = None;
@@ -555,7 +564,8 @@ impl CompInfo {
555564
None,
556565
None,
557566
None,
558-
false);
567+
false,
568+
0);
559569
ci.fields.push(field);
560570
}
561571
}
@@ -572,6 +582,7 @@ impl CompInfo {
572582
let annotations = Annotations::new(&cur);
573583
let name = cur.spelling();
574584
let is_mutable = cursor.is_mutable_field();
585+
let offset = cur.offset_of_field().unwrap(); // TODO
575586

576587
// Name can be empty if there are bitfields, for example,
577588
// see tests/headers/struct_with_bitfields.h
@@ -585,7 +596,8 @@ impl CompInfo {
585596
comment,
586597
annotations,
587598
bit_width,
588-
is_mutable);
599+
is_mutable,
600+
offset);
589601
ci.fields.push(field);
590602

591603
// No we look for things like attributes and stuff.
@@ -748,7 +760,7 @@ impl CompInfo {
748760
});
749761

750762
if let Some((ty, _)) = maybe_anonymous_struct_field {
751-
let field = Field::new(None, ty, None, None, None, false);
763+
let field = Field::new(None, ty, None, None, None, false, 0);
752764
ci.fields.push(field);
753765
}
754766

0 commit comments

Comments
 (0)