Skip to content

Commit

Permalink
signature
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 11, 2024
1 parent f09cb7e commit d87d0d3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 46 deletions.
77 changes: 36 additions & 41 deletions crates/libs/bindgen/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,48 +266,43 @@ pub fn type_def_generics(def: TypeDef) -> Vec<Type> {
// TODO: namespace should not be required - it's a hack to accomodate Win32 metadata
// TODO: this is very Rust-specific and Win32-metadata specific with all of its translation. Replace with literal signature parser that just returns slice of types.
pub fn method_def_signature(namespace: &str, row: MethodDef, generics: &[Type]) -> Signature {
let reader = row.reader();
let mut blob = row.blob(4);
let call_flags = MethodCallAttributes(blob.read_usize() as u8);
let _param_count = blob.read_usize();
let mut return_type = reader.type_from_blob(&mut blob, None, generics);

let mut params: Vec<SignatureParam> = row
.params()
.filter_map(|param| {
let signature = row.signature(generics);

let mut return_type = signature.return_type.0;

if let Some(param) = signature.return_type.1 {
if param.has_attribute("ConstAttribute") {
return_type = return_type.clone().to_const_type();
}
}

let mut params: Vec<SignatureParam> = signature
.params
.into_iter()
.map(|(mut ty, param)| {
let param_is_const = param.has_attribute("ConstAttribute");
if param.sequence() == 0 {
if param_is_const {
return_type = return_type.clone().to_const_type();
}
None
} else {
let is_output = param.flags().contains(ParamAttributes::Out);
let mut ty = reader.type_from_blob(&mut blob, None, generics);

if let Some(name) = param_or_enum(param) {
let def = reader
.get_type_def(namespace, &name)
.next()
.expect("Enum not found");
ty = Type::PrimitiveOrEnum(
Box::new(ty),
Box::new(Type::TypeDef(def, Vec::new())),
);
}
let is_output = param.flags().contains(ParamAttributes::Out);

if param_is_const || !is_output {
ty = ty.to_const_type();
}
if !is_output {
ty = ty.to_const_ptr();
}
let kind = param_kind(param);
Some(SignatureParam {
def: param,
ty,
kind,
})
if let Some(name) = param_or_enum(param) {
let def = row
.reader()
.get_type_def(namespace, &name)
.next()
.expect("Enum not found");
ty = Type::PrimitiveOrEnum(Box::new(ty), Box::new(Type::TypeDef(def, Vec::new())));
}

if param_is_const || !is_output {
ty = ty.to_const_type();
}
if !is_output {
ty = ty.to_const_ptr();
}
let kind = param_kind(param);
SignatureParam {
def: param,
ty,
kind,
}
})
.collect();
Expand Down Expand Up @@ -395,7 +390,7 @@ pub fn method_def_signature(namespace: &str, row: MethodDef, generics: &[Type])
def: row,
params,
return_type,
call_flags,
call_flags: signature.call_flags,
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/libs/metadata/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ impl File {
Some(result)
}

pub fn usize(&self, row: usize, table: usize, column: usize) -> usize {
pub(crate) fn usize(&self, row: usize, table: usize, column: usize) -> usize {
let table = &self.tables[table];
let column = &table.columns[column];
let offset = table.offset + row * table.width + column.offset;
Expand All @@ -535,7 +535,7 @@ impl File {
}
}

pub fn str(&'static self, row: usize, table: usize, column: usize) -> &'static str {
pub(crate) fn str(&'static self, row: usize, table: usize, column: usize) -> &'static str {
let offset = self.strings + self.usize(row, table, column);
let bytes = &self.bytes[offset..];
let nul_pos = bytes
Expand All @@ -545,7 +545,7 @@ impl File {
std::str::from_utf8(&bytes[..nul_pos]).expect("expected valid utf-8 C-string")
}

pub fn blob(&'static self, row: usize, table: usize, column: usize) -> Blob {
pub(crate) fn blob(&'static self, row: usize, table: usize, column: usize) -> Blob {
let offset = self.blobs + self.usize(row, table, column);
let initial_byte = self.bytes[offset];

Expand Down
4 changes: 2 additions & 2 deletions crates/libs/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ mod r#type;
mod type_name;

pub use attributes::*;
pub use bindings::*;
pub use blob::*;
use bindings::*;
use blob::*;
pub use codes::*;
pub use file::*;
use filter::*;
Expand Down

0 comments on commit d87d0d3

Please sign in to comment.