Skip to content

Commit

Permalink
simpler static reader
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jan 8, 2025
1 parent 05f6b89 commit 9b0ea24
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 64 deletions.
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/tables/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Attribute {
let ret_type = sig.read_usize();
std::debug_assert_eq!(ret_type, 1);
let mut args = Vec::with_capacity(fixed_arg_count);
let reader = self.reader();
let reader = reader();

for _ in 0..fixed_arg_count {
let arg = match Type::from_blob(&mut sig, None, &[]) {
Expand Down
3 changes: 2 additions & 1 deletion crates/libs/bindgen/src/tables/method_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl MethodDef {
let _param_count = blob.read_usize();
let mut return_type = Type::from_blob(&mut blob, None, generics);
let mut return_param = None;
let reader = reader();

let mut params = vec![];

Expand All @@ -61,7 +62,7 @@ impl MethodDef {
if !param_is_output {
if let Some(attribute) = param.find_attribute("AssociatedEnumAttribute") {
if let Some((_, Value::Str(name))) = attribute.args().first() {
let overload = param.reader().unwrap_full_name(namespace, name);
let overload = reader.unwrap_full_name(namespace, name);

ty = Type::PrimitiveOrEnum(Box::new(ty), Box::new(overload));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/tables/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl TypeDef {
if let Some(attribute) = self.find_attribute("RAIIFreeAttribute") {
if let Some((_, Value::Str(name))) = attribute.args().first() {
if let Some(Type::CppFn(ty)) =
self.reader().with_full_name(self.namespace(), name).next()
reader().with_full_name(self.namespace(), name).next()
{
return Some(ty);
}
Expand Down
10 changes: 5 additions & 5 deletions crates/libs/bindgen/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl Class {
fn bases(&self) -> Vec<Self> {
let mut bases = Vec::new();
let mut def = self.def;
let reader = def.reader();
let reader = reader();

loop {
let extends = def.extends().unwrap();
Expand Down Expand Up @@ -309,6 +309,8 @@ impl Class {
walk(base.def, &[], true, &mut set);
}

let reader = reader();

for attribute in self.def.attributes() {
let kind = match attribute.name() {
"StaticAttribute" | "ActivatableAttribute" => InterfaceKind::Static,
Expand All @@ -318,10 +320,8 @@ impl Class {

for (_, arg) in attribute.args() {
if let Value::TypeName(tn) = arg {
let Type::Interface(mut interface) = self
.def
.reader()
.unwrap_full_name(tn.namespace(), tn.name())
let Type::Interface(mut interface) =
reader.unwrap_full_name(tn.namespace(), tn.name())
else {
panic!("type not found: {tn}");
};
Expand Down
3 changes: 1 addition & 2 deletions crates/libs/bindgen/src/types/cpp_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ impl CppEnum {
pub fn dependencies(&self, dependencies: &mut TypeMap) {
if let Some(attribute) = self.def.find_attribute("AlsoUsableForAttribute") {
if let Some((_, Value::Str(type_name))) = attribute.args().first() {
self.def
.reader()
reader()
.unwrap_full_name(self.def.namespace(), type_name)
.dependencies(dependencies);
}
Expand Down
3 changes: 1 addition & 2 deletions crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ impl CppFn {
};

if let Some(dependency) = dependency {
self.method
.reader()
reader()
.unwrap_full_name(self.namespace, dependency)
.dependencies(dependencies);
}
Expand Down
3 changes: 1 addition & 2 deletions crates/libs/bindgen/src/types/cpp_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ impl CppStruct {

if let Some(attribute) = self.def.find_attribute("AlsoUsableForAttribute") {
if let Some((_, Value::Str(type_name))) = attribute.args().first() {
self.def
.reader()
reader()
.unwrap_full_name(self.def.namespace(), type_name)
.dependencies(dependencies);
}
Expand Down
29 changes: 6 additions & 23 deletions crates/libs/bindgen/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ impl Type {
}
}

code.reader()
.unwrap_full_name(code_name.namespace(), code_name.name())
reader().unwrap_full_name(code_name.namespace(), code_name.name())
}

#[track_caller]
Expand Down Expand Up @@ -298,9 +297,7 @@ impl Type {
let code = blob.decode::<TypeDefOrRef>();
let code_name = code.type_name();

let mut ty = blob
.reader()
.unwrap_full_name(code_name.namespace(), code_name.name());
let mut ty = reader().unwrap_full_name(code_name.namespace(), code_name.name());

let mut item_generics = vec![];

Expand Down Expand Up @@ -573,17 +570,11 @@ impl Type {
pub fn split_generic(&self) -> (Type, Vec<Type>) {
match self {
Self::Interface(ty) if !ty.generics.is_empty() => {
let base = ty
.def
.reader()
.unwrap_full_name(ty.def.namespace(), ty.def.name());
let base = reader().unwrap_full_name(ty.def.namespace(), ty.def.name());
(base, ty.generics.clone())
}
Self::Delegate(ty) if !ty.generics.is_empty() => {
let base = ty
.def
.reader()
.unwrap_full_name(ty.def.namespace(), ty.def.name());
let base = reader().unwrap_full_name(ty.def.namespace(), ty.def.name());
(base, ty.generics.clone())
}
_ => (self.clone(), vec![]),
Expand Down Expand Up @@ -630,16 +621,8 @@ impl Type {
}

if let Some(multi) = match &ty {
Self::CppStruct(ty) => Some(
ty.def
.reader()
.with_full_name(ty.def.namespace(), ty.def.name()),
),
Self::CppFn(ty) => Some(
ty.method
.reader()
.with_full_name(ty.namespace, ty.method.name()),
),
Self::CppStruct(ty) => Some(reader().with_full_name(ty.def.namespace(), ty.def.name())),
Self::CppFn(ty) => Some(reader().with_full_name(ty.namespace, ty.method.name())),
_ => None,
} {
multi.for_each(|multi| {
Expand Down
4 changes: 0 additions & 4 deletions crates/libs/bindgen/src/winmd/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ impl Blob {
Self { file, slice }
}

pub fn reader(&self) -> &'static Reader {
self.file.reader()
}

fn peek(&self) -> (usize, usize) {
if self[0] & 0x80 == 0 {
(self[0] as usize, 1)
Expand Down
8 changes: 0 additions & 8 deletions crates/libs/bindgen/src/winmd/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,4 @@ impl TypeDefOrRef {
rest => panic!("{rest:?}"),
}
}

pub fn reader(&self) -> &'static Reader {
match self {
Self::TypeDef(row) => row.reader(),
Self::TypeRef(row) => row.reader(),
Self::TypeSpec(row) => row.reader(),
}
}
}
7 changes: 0 additions & 7 deletions crates/libs/bindgen/src/winmd/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;

pub struct File {
pub(crate) reader: *const Reader,
bytes: Vec<u8>,
strings: usize,
blobs: usize,
Expand Down Expand Up @@ -46,7 +45,6 @@ impl File {
pub fn new(bytes: Vec<u8>) -> Option<Self> {
let mut result = File {
bytes,
reader: std::ptr::null(),
strings: 0,
blobs: 0,
tables: Default::default(),
Expand Down Expand Up @@ -655,11 +653,6 @@ impl File {
pub(crate) fn table<R: AsRow>(&'static self) -> RowIterator<R> {
RowIterator::new(self, 0..self.tables[R::TABLE].len)
}

pub(crate) fn reader(&self) -> &'static Reader {
// Safety: At this point the File is already pointing to a valid Reader.
unsafe { &*self.reader }
}
}

fn section_from_rva(sections: &[IMAGE_SECTION_HEADER], rva: u32) -> Option<&IMAGE_SECTION_HEADER> {
Expand Down
17 changes: 15 additions & 2 deletions crates/libs/bindgen/src/winmd/reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
use super::*;
use std::sync::atomic::{AtomicPtr, Ordering};

static READER: AtomicPtr<Reader> = AtomicPtr::new(std::ptr::null_mut());

pub fn reader() -> &'static Reader {
let ptr = READER.load(Ordering::Relaxed);

if ptr.is_null() {
panic!();
} else {
unsafe { &*ptr }
}
}

fn insert(types: &mut HashMap<&'static str, Vec<Type>>, name: &'static str, ty: Type) {
types.entry(name).or_default().push(ty);
Expand All @@ -18,8 +31,7 @@ impl Reader {
pub fn new(files: Vec<File>) -> &'static Self {
let reader = Box::leak(Box::new(Self(HashMap::new())));

for mut file in files {
file.reader = reader;
for file in files {
let file = Box::leak(Box::new(file));
let mut nested = HashMap::<TypeDef, Vec<TypeDef>>::new();

Expand Down Expand Up @@ -167,6 +179,7 @@ impl Reader {
}
}

READER.store(reader, Ordering::Relaxed);
reader
}

Expand Down
5 changes: 0 additions & 5 deletions crates/libs/bindgen/src/winmd/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ pub trait AsRow: Copy {
self.to_row().file
}

fn reader(&self) -> &'static Reader {
// Safety: At this point the File is already pointing to a valid Reader.
unsafe { &*self.file().reader }
}

fn index(&self) -> usize {
self.to_row().index
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/writer/cpp_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Writer {

if let Some(attribute) = def.find_attribute("AlsoUsableForAttribute") {
if let Some((_, Value::Str(type_name))) = attribute.args().first() {
let ty = def.reader().unwrap_full_name(def.namespace(), type_name);
let ty = reader().unwrap_full_name(def.namespace(), type_name);

let ty = ty.write_name(self);

Expand Down

0 comments on commit 9b0ea24

Please sign in to comment.