Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Feb 13, 2024
1 parent 6929dd4 commit e000d30
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 59 deletions.
39 changes: 14 additions & 25 deletions crates/libs/bindgen/src/winmd/from_reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use metadata::{HasAttributes, AsRow};
use metadata::{AsRow, HasAttributes};

pub fn from_reader(reader: &metadata::Reader, config: std::collections::BTreeMap<&str, &str>, output: &str) -> Result<()> {
let mut writer = Writer::new(output);
Expand Down Expand Up @@ -68,11 +68,7 @@ pub fn from_reader(reader: &metadata::Reader, config: std::collections::BTreeMap
writer.tables.Field.push(Field { Flags: field.flags().0, Name: writer.strings.insert(field.name()), Signature: signature });

if let Some(constant) = field.constant() {
writer.tables.Constant.push(Constant {
Type: constant.usize(0) as u16,
Parent: HasConstant::Field(writer.tables.Field.len() as u32 -1),
Value: writer.blobs.insert(&constant.blob(2)),
})
writer.tables.Constant.push(Constant { Type: constant.usize(0) as u16, Parent: HasConstant::Field(writer.tables.Field.len() as u32 - 1), Value: writer.blobs.insert(&constant.blob(2)) })
}
}

Expand Down Expand Up @@ -102,22 +98,14 @@ pub fn from_reader(reader: &metadata::Reader, config: std::collections::BTreeMap
assert_eq!(attribute_ctor.name(), ".ctor");
let metadata::MemberRefParent::TypeRef(attribute_type) = attribute_ctor.parent();

let attribute_type_ref = if let TypeDefOrRef::TypeRef(type_ref) = writer.insert_type_ref(attribute_type.namespace(), attribute_type.name()) {
MemberRefParent::TypeRef(type_ref)
} else {
panic!()
};
let attribute_type_ref = if let TypeDefOrRef::TypeRef(type_ref) = writer.insert_type_ref(attribute_type.namespace(), attribute_type.name()) { MemberRefParent::TypeRef(type_ref) } else { panic!() };

let signature = attribute_ctor.signature();
let return_type = winmd_type(&signature.return_type);
let param_types: Vec<Type> = signature.params.iter().map(winmd_type).collect();
let signature = writer.insert_method_sig(signature.call_flags, &return_type, &param_types);

writer.tables.MemberRef.push(MemberRef {
Class: attribute_type_ref,
Name: writer.strings.insert(".ctor"),
Signature: signature,
});

writer.tables.MemberRef.push(MemberRef { Class: attribute_type_ref, Name: writer.strings.insert(".ctor"), Signature: signature });

let mut values = 1u16.to_le_bytes().to_vec(); // prolog
let args = attribute.args();
Expand All @@ -140,11 +128,7 @@ pub fn from_reader(reader: &metadata::Reader, config: std::collections::BTreeMap

let values = writer.blobs.insert(&values);

writer.tables.CustomAttribute.push(CustomAttribute {
Parent: HasAttribute::TypeDef(def_ref),
Type: AttributeType::MemberRef(writer.tables.MemberRef.len() as u32 - 1),
Value: values
});
writer.tables.CustomAttribute.push(CustomAttribute { Parent: HasAttribute::TypeDef(def_ref), Type: AttributeType::MemberRef(writer.tables.MemberRef.len() as u32 - 1), Value: values });
}
}

Expand All @@ -156,13 +140,19 @@ pub fn from_reader(reader: &metadata::Reader, config: std::collections::BTreeMap
// TODO: need a Blob type for writing
fn value_blob(value: &metadata::Value, blob: &mut Vec<u8>) {
match value {
metadata::Value::Bool(value) => if *value { blob.push(1) } else { blob.push(0 )},
metadata::Value::Bool(value) => {
if *value {
blob.push(1)
} else {
blob.push(0)
}
}
metadata::Value::U32(value) => blob.extend_from_slice(&value.to_le_bytes()),
metadata::Value::I32(value) => blob.extend_from_slice(&value.to_le_bytes()),
metadata::Value::U16(value) => blob.extend_from_slice(&value.to_le_bytes()),
metadata::Value::U8(value) => blob.extend_from_slice(&value.to_le_bytes()),
metadata::Value::EnumDef(_def, value) => value_blob(value, blob),

metadata::Value::TypeName(value) => {
let value = value.to_string();
usize_blob(value.len(), blob);
Expand All @@ -174,7 +164,6 @@ fn value_blob(value: &metadata::Value, blob: &mut Vec<u8>) {
}
rest => unimplemented!("{rest:?}"),
}

}

// TODO: keep the basic type conversion
Expand Down
3 changes: 1 addition & 2 deletions crates/libs/bindgen/src/winmd/writer/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ macro_rules! code {
};
}


code! { AttributeType(3)
(MemberRef, 3)
}
Expand Down Expand Up @@ -67,4 +66,4 @@ code! { ResolutionScope(2)
(ModuleRef, 1)
(AssemblyRef, 2)
(TypeRef, 3)
}
}
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/winmd/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Writer {
type_specs: Default::default(),
};

writer.tables.TypeDef.push(TypeDef { TypeName: writer.strings.insert("<Module>"), Flags: 0, TypeNamespace: 0, Extends: TypeDefOrRef::none(), FieldList: 0, MethodList: 0 });
writer.tables.TypeDef.push(TypeDef { TypeName: writer.strings.insert("<Module>"), Flags: 0, TypeNamespace: 0, Extends: TypeDefOrRef::none(), FieldList: 0, MethodList: 0 });

let name = name.rsplit_once(&['/', '\\']).map_or(name, |(_, name)| name);

Expand Down
28 changes: 1 addition & 27 deletions crates/libs/bindgen/src/winmd/writer/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
use super::Write;
use super::*;

// TODO: make these columns strongly types


#[derive(Default)]
pub struct Tables {
// TODO: use BTreeSet for tables that have a primary key, unless they are naturally sorted.
Expand Down Expand Up @@ -184,30 +181,7 @@ impl Tables {
let type_or_method_def = metadata::coded_index_size(&[self.TypeDef.len(), self.MethodDef.len()]);
let member_ref_parent = metadata::coded_index_size(&[self.TypeDef.len(), self.TypeRef.len(), self.ModuleRef.len(), self.MethodDef.len(), self.TypeSpec.len()]);
let custom_attribute_type = metadata::coded_index_size(&[self.MethodDef.len(), self.MemberRef.len(), 0, 0, 0]);

let has_custom_attribute = metadata::coded_index_size(&[
self.MethodDef.len(),
self.Field.len(),
self.TypeRef.len(),
self.TypeDef.len(),
self.Param.len(),
self.InterfaceImpl.len(),
self.MemberRef.len(),
self.Module.len(),
0,
0,
0,
self.ModuleRef.len(),
self.TypeSpec.len(),
0,
self.AssemblyRef.len(),
0,
0,
0,
self.GenericParam.len(),
0,
0,
]);
let has_custom_attribute = metadata::coded_index_size(&[self.MethodDef.len(), self.Field.len(), self.TypeRef.len(), self.TypeDef.len(), self.Param.len(), self.InterfaceImpl.len(), self.MemberRef.len(), self.Module.len(), 0, 0, 0, self.ModuleRef.len(), self.TypeSpec.len(), 0, self.AssemblyRef.len(), 0, 0, 0, self.GenericParam.len(), 0, 0]);

let valid_tables: u64 = 1 << 0 | // Module
1 << 0x01 | // TypeRef
Expand Down
4 changes: 0 additions & 4 deletions crates/libs/metadata/src/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ impl TypeName {
let index = full_name.rfind('.').expect("Expected full name separated with `.`");
Self::new(&full_name[0..index], &full_name[index + 1..])
}

pub fn to_string(&self) -> String {
format!("{}.{}", self.namespace, self.name)
}
}

impl std::fmt::Display for TypeName {
Expand Down

0 comments on commit e000d30

Please sign in to comment.