Skip to content

Commit 0fff786

Browse files
committed
Add support for packed struct
1 parent 5a1c226 commit 0fff786

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/type_.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,18 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
114114
self.context.new_function_pointer_type(None, return_type, params, false)
115115
}
116116

117-
fn type_struct(&self, fields: &[Type<'gcc>], _packed: bool) -> Type<'gcc> {
117+
fn type_struct(&self, fields: &[Type<'gcc>], packed: bool) -> Type<'gcc> {
118118
let types = fields.to_vec();
119119
if let Some(typ) = self.struct_types.borrow().get(fields) {
120120
return typ.clone();
121121
}
122122
let fields: Vec<_> = fields.iter().enumerate()
123123
.map(|(index, field)| self.context.new_field(None, *field, &format!("field{}_TODO", index)))
124124
.collect();
125-
// TODO(antoyo): use packed.
126125
let typ = self.context.new_struct_type(None, "struct", &fields).as_type();
126+
if packed {
127+
typ.set_packed();
128+
}
127129
self.struct_types.borrow_mut().insert(types, typ);
128130
typ
129131
}
@@ -209,12 +211,14 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
209211
self.type_array(self.type_from_integer(unit), size / unit_size)
210212
}
211213

212-
pub fn set_struct_body(&self, typ: Struct<'gcc>, fields: &[Type<'gcc>], _packed: bool) {
213-
// TODO(antoyo): use packed.
214+
pub fn set_struct_body(&self, typ: Struct<'gcc>, fields: &[Type<'gcc>], packed: bool) {
214215
let fields: Vec<_> = fields.iter().enumerate()
215216
.map(|(index, field)| self.context.new_field(None, *field, &format!("field_{}", index)))
216217
.collect();
217218
typ.set_fields(None, &fields);
219+
if packed {
220+
typ.as_type().set_packed();
221+
}
218222
}
219223

220224
pub fn type_named_struct(&self, name: &str) -> Struct<'gcc> {

0 commit comments

Comments
 (0)