Skip to content

Commit

Permalink
refactor: remove StructInfo name
Browse files Browse the repository at this point in the history
This prevents duplicate storage as the struct's name is already stored
in the TypeInfo
  • Loading branch information
Wodann committed Mar 31, 2020
1 parent 7da1811 commit 71d80c9
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 96 deletions.
2 changes: 1 addition & 1 deletion crates/mun_abi/c
Submodule c updated 1 files
+0 −2 include/mun_abi.h
24 changes: 6 additions & 18 deletions crates/mun_abi/src/autogen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ fn bindgen_test_layout_FunctionInfo() {
#[repr(C)]
#[derive(Clone, Debug)]
pub struct StructInfo {
#[doc = " Struct name"]
pub name: *const ::std::os::raw::c_char,
#[doc = " Struct fields' names"]
pub field_names: *const *const ::std::os::raw::c_char,
#[doc = " Struct fields' information"]
Expand All @@ -265,27 +263,17 @@ pub struct StructInfo {
fn bindgen_test_layout_StructInfo() {
assert_eq!(
::std::mem::size_of::<StructInfo>(),
40usize,
32usize,
concat!("Size of: ", stringify!(StructInfo))
);
assert_eq!(
::std::mem::align_of::<StructInfo>(),
8usize,
concat!("Alignment of ", stringify!(StructInfo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<StructInfo>())).name as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(StructInfo),
"::",
stringify!(name)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<StructInfo>())).field_names as *const _ as usize },
8usize,
0usize,
concat!(
"Offset of field: ",
stringify!(StructInfo),
Expand All @@ -295,7 +283,7 @@ fn bindgen_test_layout_StructInfo() {
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<StructInfo>())).field_types as *const _ as usize },
16usize,
8usize,
concat!(
"Offset of field: ",
stringify!(StructInfo),
Expand All @@ -305,7 +293,7 @@ fn bindgen_test_layout_StructInfo() {
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<StructInfo>())).field_offsets as *const _ as usize },
24usize,
16usize,
concat!(
"Offset of field: ",
stringify!(StructInfo),
Expand All @@ -315,7 +303,7 @@ fn bindgen_test_layout_StructInfo() {
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<StructInfo>())).num_fields as *const _ as usize },
32usize,
24usize,
concat!(
"Offset of field: ",
stringify!(StructInfo),
Expand All @@ -325,7 +313,7 @@ fn bindgen_test_layout_StructInfo() {
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<StructInfo>())).memory_kind as *const _ as usize },
34usize,
26usize,
concat!(
"Offset of field: ",
stringify!(StructInfo),
Expand Down
53 changes: 8 additions & 45 deletions crates/mun_abi/src/autogen_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ unsafe impl Send for FunctionInfo {}
unsafe impl Sync for FunctionInfo {}

impl StructInfo {
/// Returns the struct's name.
pub fn name(&self) -> &str {
unsafe { str::from_utf8_unchecked(CStr::from_ptr(self.name).to_bytes()) }
}

/// Returns the struct's field names.
pub fn field_names(&self) -> impl Iterator<Item = &str> {
let field_names = if self.num_fields == 0 {
Expand Down Expand Up @@ -180,12 +175,6 @@ impl StructInfo {
}
}

impl fmt::Display for StructInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}

impl ModuleInfo {
/// Returns the module's full path.
pub fn path(&self) -> &str {
Expand Down Expand Up @@ -496,7 +485,6 @@ mod tests {
}

fn fake_struct_info(
name: &CStr,
field_names: &[*const c_char],
field_types: &[&TypeInfo],
field_offsets: &[u16],
Expand All @@ -506,7 +494,6 @@ mod tests {
assert!(field_types.len() == field_offsets.len());

StructInfo {
name: name.as_ptr(),
field_names: field_names.as_ptr(),
field_types: field_types.as_ptr().cast::<*const TypeInfo>(),
field_offsets: field_offsets.as_ptr(),
Expand All @@ -515,29 +502,13 @@ mod tests {
}
}

const FAKE_STRUCT_NAME: &str = "struct-name";

#[test]
fn test_struct_info_name() {
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_info = fake_struct_info(&struct_name, &[], &[], &[], Default::default());

assert_eq!(struct_info.name(), FAKE_STRUCT_NAME);
}

#[test]
fn test_struct_info_fields_none() {
let field_names = &[];
let field_types = &[];
let field_offsets = &[];
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_info = fake_struct_info(
&struct_name,
field_names,
field_types,
field_offsets,
Default::default(),
);
let struct_info =
fake_struct_info(field_names, field_types, field_offsets, Default::default());

assert_eq!(struct_info.field_names().count(), 0);
assert_eq!(struct_info.field_types(), field_types);
Expand All @@ -553,14 +524,8 @@ mod tests {
let field_names = &[field_name.as_ptr()];
let field_types = &[&type_info];
let field_offsets = &[1];
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_info = fake_struct_info(
&struct_name,
field_names,
field_types,
field_offsets,
Default::default(),
);
let struct_info =
fake_struct_info(field_names, field_types, field_offsets, Default::default());

for (lhs, rhs) in struct_info.field_names().zip([FAKE_FIELD_NAME].iter()) {
assert_eq!(lhs, *rhs)
Expand All @@ -571,18 +536,16 @@ mod tests {

#[test]
fn test_struct_info_memory_kind_gc() {
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_memory_kind = StructMemoryKind::GC;
let struct_info = fake_struct_info(&struct_name, &[], &[], &[], struct_memory_kind.clone());
let struct_info = fake_struct_info(&[], &[], &[], struct_memory_kind.clone());

assert_eq!(struct_info.memory_kind, struct_memory_kind);
}

#[test]
fn test_struct_info_memory_kind_value() {
let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name.");
let struct_memory_kind = StructMemoryKind::Value;
let struct_info = fake_struct_info(&struct_name, &[], &[], &[], struct_memory_kind.clone());
let struct_info = fake_struct_info(&[], &[], &[], struct_memory_kind.clone());

assert_eq!(struct_info.memory_kind, struct_memory_kind);
}
Expand All @@ -602,6 +565,7 @@ mod tests {
}

const FAKE_MODULE_PATH: &str = "path::to::module";
const FAKE_STRUCT_NAME: &str = "StructName";

#[test]
fn test_module_info_path() {
Expand Down Expand Up @@ -638,7 +602,7 @@ mod tests {
let functions = &[fn_info];

let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name");
let struct_info = fake_struct_info(&struct_name, &[], &[], &[], Default::default());
let struct_info = fake_struct_info(&[], &[], &[], Default::default());
let struct_type_info = fake_struct_type_info(&struct_name, struct_info, 1, 1);
let types = &[unsafe { mem::transmute(&struct_type_info) }];

Expand All @@ -664,7 +628,6 @@ mod tests {
if lhs.group == TypeGroup::StructTypes {
let lhs_struct = lhs.as_struct().unwrap();
let rhs_struct = rhs.as_struct().unwrap();
assert_eq!(lhs_struct.name(), rhs_struct.name());
assert_eq!(lhs_struct.field_types(), rhs_struct.field_types());
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/mun_codegen/src/ir/abi_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ pub(crate) fn gen_abi_types(context: &Context) -> AbiTypes {
let struct_info_type = context.opaque_struct_type("struct.MunStructInfo");
struct_info_type.set_body(
&[
str_type.into(), // name
str_type.ptr_type(AddressSpace::Const).into(), // field_names
str_type.ptr_type(AddressSpace::Const).into(), // field_names
type_info_ptr_type.ptr_type(AddressSpace::Const).into(), // field_types
context.i16_type().ptr_type(AddressSpace::Const).into(), // field_offsets
context.i16_type().into(), // num_fields
context.i8_type().into(), // memory_kind
context.i16_type().into(), // num_fields
context.i8_type().into(), // memory_kind
],
false,
);
Expand Down
8 changes: 1 addition & 7 deletions crates/mun_codegen/src/ir/type_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,9 @@ impl<'a, D: IrDatabase> TypeTableBuilder<'a, D> {
hir_struct: hir::Struct,
) -> StructValue {
let struct_ir = self.db.struct_ty(hir_struct);
let name = hir_struct.name(self.db).to_string();
let fields = hir_struct.fields(self.db);

let name = hir_struct.name(self.db).to_string();
let name_str = intern_string(
&self.module,
&name,
&format!("struct_info::<{}>::name", name),
);
let field_names = gen_string_array(
self.module,
fields.iter().map(|field| field.name(self.db).to_string()),
Expand Down Expand Up @@ -266,7 +261,6 @@ impl<'a, D: IrDatabase> TypeTableBuilder<'a, D> {
);

self.abi_types.struct_info_type.const_named_struct(&[
name_str.into(),
field_names.into(),
field_types.into(),
field_offsets.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ expression: "struct(gc) Foo { a: int };\n\nfn main(c:int):int {\n let b = Foo
source_filename = "group_name"

%struct.MunTypeInfo = type { [16 x i8], i8 addrspace(4)*, i32, i8, i8 }
%struct.MunStructInfo = type { i8 addrspace(4)*, i8 addrspace(4)* addrspace(4)*, %struct.MunTypeInfo addrspace(4)* addrspace(4)*, i16 addrspace(4)*, i16, i8 }
%struct.MunStructInfo = type { i8 addrspace(4)* addrspace(4)*, %struct.MunTypeInfo addrspace(4)* addrspace(4)*, i16 addrspace(4)*, i16, i8 }
%DispatchTable = type { i8* addrspace(4)* (i8 addrspace(4)*, i8*)* }

@"type_info::<Foo>::name" = private unnamed_addr constant [4 x i8] c"Foo\00"
@"struct_info::<Foo>::name" = private unnamed_addr constant [4 x i8] c"Foo\00"
@"struct_info::<Foo>::field_names" = private unnamed_addr constant [2 x i8] c"a\00"
@0 = private unnamed_addr constant [1 x i8 addrspace(4)*] [i8 addrspace(4)* @"struct_info::<Foo>::field_names"]
@"type_info::<core::i64>::name" = private unnamed_addr constant [10 x i8] c"core::i64\00"
@"type_info::<core::i64>" = private unnamed_addr constant %struct.MunTypeInfo { [16 x i8] c"G\13;t\97j8\18\D7M\83`\1D\C8\19%", [10 x i8]* @"type_info::<core::i64>::name", i32 64, i8 8, i8 0 }
@"struct_info::<Foo>::field_types" = private unnamed_addr constant [1 x %struct.MunTypeInfo addrspace(4)*] [%struct.MunTypeInfo addrspace(4)* @"type_info::<core::i64>"]
@"struct_info::<Foo>::field_offsets" = private unnamed_addr constant [1 x i16] zeroinitializer
@"type_info::<Foo>" = private unnamed_addr constant { %struct.MunTypeInfo, %struct.MunStructInfo } { %struct.MunTypeInfo { [16 x i8] c"\13V\C6}z\D1c\8D\81k\FB\82-\D2\C2]", [4 x i8]* @"type_info::<Foo>::name", i32 64, i8 8, i8 1 }, %struct.MunStructInfo { [4 x i8]* @"struct_info::<Foo>::name", [1 x i8 addrspace(4)*]* @0, [1 x %struct.MunTypeInfo addrspace(4)*]* @"struct_info::<Foo>::field_types", [1 x i16]* @"struct_info::<Foo>::field_offsets", i16 1, i8 0 } }
@"type_info::<Foo>" = private unnamed_addr constant { %struct.MunTypeInfo, %struct.MunStructInfo } { %struct.MunTypeInfo { [16 x i8] c"\13V\C6}z\D1c\8D\81k\FB\82-\D2\C2]", [4 x i8]* @"type_info::<Foo>::name", i32 64, i8 8, i8 1 }, %struct.MunStructInfo { [1 x i8 addrspace(4)*]* @0, [1 x %struct.MunTypeInfo addrspace(4)*]* @"struct_info::<Foo>::field_types", [1 x i16]* @"struct_info::<Foo>::field_offsets", i16 1, i8 0 } }
@"type_info::<*const TypeInfo>::name" = private unnamed_addr constant [16 x i8] c"*const TypeInfo\00"
@"type_info::<*const TypeInfo>" = private unnamed_addr constant %struct.MunTypeInfo { [16 x i8] c"=\A1-\1F\C2\A7\88`d\90\F4\B5\BEE}x", [16 x i8]* @"type_info::<*const TypeInfo>::name", i32 64, i8 8, i8 0 }
@"type_info::<*const *mut core::void>::name" = private unnamed_addr constant [23 x i8] c"*const *mut core::void\00"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@ expression: "struct(value) Bar(float, Foo);\nstruct(value) Foo { a: int };\n\nfn
source_filename = "group_name"

%struct.MunTypeInfo = type { [16 x i8], i8 addrspace(4)*, i32, i8, i8 }
%struct.MunStructInfo = type { i8 addrspace(4)*, i8 addrspace(4)* addrspace(4)*, %struct.MunTypeInfo addrspace(4)* addrspace(4)*, i16 addrspace(4)*, i16, i8 }
%struct.MunStructInfo = type { i8 addrspace(4)* addrspace(4)*, %struct.MunTypeInfo addrspace(4)* addrspace(4)*, i16 addrspace(4)*, i16, i8 }
%DispatchTable = type { i8* addrspace(4)* (i8 addrspace(4)*, i8*)*, i64 (%Foo)*, %Foo (%Bar)* }
%Foo = type { i64 }
%Bar = type { double, %Foo }

@"type_info::<Foo>::name" = private unnamed_addr constant [4 x i8] c"Foo\00"
@"struct_info::<Foo>::name" = private unnamed_addr constant [4 x i8] c"Foo\00"
@"struct_info::<Foo>::field_names" = private unnamed_addr constant [2 x i8] c"a\00"
@0 = private unnamed_addr constant [1 x i8 addrspace(4)*] [i8 addrspace(4)* @"struct_info::<Foo>::field_names"]
@"type_info::<core::i64>::name" = private unnamed_addr constant [10 x i8] c"core::i64\00"
@"type_info::<core::i64>" = private unnamed_addr constant %struct.MunTypeInfo { [16 x i8] c"G\13;t\97j8\18\D7M\83`\1D\C8\19%", [10 x i8]* @"type_info::<core::i64>::name", i32 64, i8 8, i8 0 }
@"struct_info::<Foo>::field_types" = private unnamed_addr constant [1 x %struct.MunTypeInfo addrspace(4)*] [%struct.MunTypeInfo addrspace(4)* @"type_info::<core::i64>"]
@"struct_info::<Foo>::field_offsets" = private unnamed_addr constant [1 x i16] zeroinitializer
@"type_info::<Foo>" = private unnamed_addr constant { %struct.MunTypeInfo, %struct.MunStructInfo } { %struct.MunTypeInfo { [16 x i8] c"\13V\C6}z\D1c\8D\81k\FB\82-\D2\C2]", [4 x i8]* @"type_info::<Foo>::name", i32 64, i8 8, i8 1 }, %struct.MunStructInfo { [4 x i8]* @"struct_info::<Foo>::name", [1 x i8 addrspace(4)*]* @0, [1 x %struct.MunTypeInfo addrspace(4)*]* @"struct_info::<Foo>::field_types", [1 x i16]* @"struct_info::<Foo>::field_offsets", i16 1, i8 1 } }
@"type_info::<Foo>" = private unnamed_addr constant { %struct.MunTypeInfo, %struct.MunStructInfo } { %struct.MunTypeInfo { [16 x i8] c"\13V\C6}z\D1c\8D\81k\FB\82-\D2\C2]", [4 x i8]* @"type_info::<Foo>::name", i32 64, i8 8, i8 1 }, %struct.MunStructInfo { [1 x i8 addrspace(4)*]* @0, [1 x %struct.MunTypeInfo addrspace(4)*]* @"struct_info::<Foo>::field_types", [1 x i16]* @"struct_info::<Foo>::field_offsets", i16 1, i8 1 } }
@"type_info::<*const TypeInfo>::name" = private unnamed_addr constant [16 x i8] c"*const TypeInfo\00"
@"type_info::<*const TypeInfo>" = private unnamed_addr constant %struct.MunTypeInfo { [16 x i8] c"=\A1-\1F\C2\A7\88`d\90\F4\B5\BEE}x", [16 x i8]* @"type_info::<*const TypeInfo>::name", i32 64, i8 8, i8 0 }
@"type_info::<core::f64>::name" = private unnamed_addr constant [10 x i8] c"core::f64\00"
@"type_info::<core::f64>" = private unnamed_addr constant %struct.MunTypeInfo { [16 x i8] c"`\DBF\9C?YJ%G\AD4\9F\D5\92%A", [10 x i8]* @"type_info::<core::f64>::name", i32 64, i8 8, i8 0 }
@"type_info::<*const *mut core::void>::name" = private unnamed_addr constant [23 x i8] c"*const *mut core::void\00"
@"type_info::<*const *mut core::void>" = private unnamed_addr constant %struct.MunTypeInfo { [16 x i8] c"\C5fO\BD\84\DF\06\BFd+\B1\9Abv\CE\00", [23 x i8]* @"type_info::<*const *mut core::void>::name", i32 64, i8 8, i8 0 }
@"type_info::<Bar>::name" = private unnamed_addr constant [4 x i8] c"Bar\00"
@"struct_info::<Bar>::name" = private unnamed_addr constant [4 x i8] c"Bar\00"
@"struct_info::<Bar>::field_names" = private unnamed_addr constant [2 x i8] c"0\00"
@"struct_info::<Bar>::field_names.1" = private unnamed_addr constant [2 x i8] c"1\00"
@1 = private unnamed_addr constant [2 x i8 addrspace(4)*] [i8 addrspace(4)* @"struct_info::<Bar>::field_names", i8 addrspace(4)* @"struct_info::<Bar>::field_names.1"]
@"struct_info::<Bar>::field_types" = private unnamed_addr constant [2 x %struct.MunTypeInfo addrspace(4)*] [%struct.MunTypeInfo addrspace(4)* @"type_info::<core::f64>", %struct.MunTypeInfo addrspace(4)* @"type_info::<Foo>"]
@"struct_info::<Bar>::field_offsets" = private unnamed_addr constant [2 x i16] [i16 0, i16 8]
@"type_info::<Bar>" = private unnamed_addr constant { %struct.MunTypeInfo, %struct.MunStructInfo } { %struct.MunTypeInfo { [16 x i8] c"\DD\C3_\88\FAq\B6\EF\14*\E6\1F56FS", [4 x i8]* @"type_info::<Bar>::name", i32 128, i8 8, i8 1 }, %struct.MunStructInfo { [4 x i8]* @"struct_info::<Bar>::name", [2 x i8 addrspace(4)*]* @1, [2 x %struct.MunTypeInfo addrspace(4)*]* @"struct_info::<Bar>::field_types", [2 x i16]* @"struct_info::<Bar>::field_offsets", i16 2, i8 1 } }
@"type_info::<Bar>" = private unnamed_addr constant { %struct.MunTypeInfo, %struct.MunStructInfo } { %struct.MunTypeInfo { [16 x i8] c"\DD\C3_\88\FAq\B6\EF\14*\E6\1F56FS", [4 x i8]* @"type_info::<Bar>::name", i32 128, i8 8, i8 1 }, %struct.MunStructInfo { [2 x i8 addrspace(4)*]* @1, [2 x %struct.MunTypeInfo addrspace(4)*]* @"struct_info::<Bar>::field_types", [2 x i16]* @"struct_info::<Bar>::field_offsets", i16 2, i8 1 } }
@"type_info::<*mut core::void>::name" = private unnamed_addr constant [16 x i8] c"*mut core::void\00"
@"type_info::<*mut core::void>" = private unnamed_addr constant %struct.MunTypeInfo { [16 x i8] c"\F0Y\22\FC\95\9E\7F\CE\08T\B1\A2\CD\A7\FAz", [16 x i8]* @"type_info::<*mut core::void>::name", i32 64, i8 8, i8 0 }
@global_type_table = global [7 x %struct.MunTypeInfo addrspace(4)*] [%struct.MunTypeInfo addrspace(4)* @"type_info::<Foo>", %struct.MunTypeInfo addrspace(4)* @"type_info::<*const TypeInfo>", %struct.MunTypeInfo addrspace(4)* @"type_info::<core::i64>", %struct.MunTypeInfo addrspace(4)* @"type_info::<core::f64>", %struct.MunTypeInfo addrspace(4)* @"type_info::<*const *mut core::void>", %struct.MunTypeInfo addrspace(4)* @"type_info::<Bar>", %struct.MunTypeInfo addrspace(4)* @"type_info::<*mut core::void>"]
Expand Down
Loading

0 comments on commit 71d80c9

Please sign in to comment.