Skip to content

Commit

Permalink
type_def_interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Oct 20, 2023
1 parent 16420a5 commit 9dd482e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
13 changes: 9 additions & 4 deletions crates/libs/bindgen/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,7 @@ pub fn type_interfaces(ty: &Type) -> Vec<Interface> {
// This will both sort the results and should make finding dupes faster
fn walk(result: &mut Vec<Interface>, parent: &Type, is_base: bool) {
if let Type::TypeDef(row, generics) = parent {
for imp in row.interface_impls() {
let mut child = Interface { ty: imp.ty(generics), kind: if imp.has_attribute("DefaultAttribute") { InterfaceKind::Default } else { InterfaceKind::None } };

for mut child in type_def_interfaces(*row, generics) {
child.kind = if !is_base && child.kind == InterfaceKind::Default {
InterfaceKind::Default
} else if child.kind == InterfaceKind::Overridable {
Expand Down Expand Up @@ -656,8 +654,15 @@ pub fn type_def_has_packing(row: TypeDef) -> bool {
}
}

pub fn type_def_interfaces(def: TypeDef, generics: &[Type]) -> impl Iterator<Item = Interface> + '_ {
def.interface_impls().map(|imp| {
let kind = if imp.has_attribute("DefaultAttribute") { InterfaceKind::Default } else { InterfaceKind::None };
Interface { kind, ty: imp.ty(generics) }
})
}

pub fn type_def_default_interface(row: TypeDef) -> Option<Type> {
row.interface_impls().find_map(move |imp| if imp.has_attribute("DefaultAttribute") { Some(imp.ty(&[])) } else { None })
type_def_interfaces(row, &[]).find_map(move |interface| if interface.kind == InterfaceKind::Default { Some(interface.ty) } else { None })
}

fn type_signature(ty: &Type) -> String {
Expand Down
9 changes: 4 additions & 5 deletions crates/libs/bindgen/src/rdl/from_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,11 @@ impl Writer {
// TODO: then list default interface first
// Then everything else

for imp in def.interface_impls() {
let ty = imp.ty(generics);
if imp.has_attribute("DefaultAttribute") {
types.insert(0, self.ty(&ty));
for interface in type_def_interfaces(def, generics) {
if interface.kind == InterfaceKind::Default {
types.insert(0, self.ty(&interface.ty));
} else {
types.push(self.ty(&ty));
types.push(self.ty(&interface.ty));
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/libs/bindgen/src/winmd/from_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ pub fn from_reader(reader: &metadata::Reader, config: std::collections::BTreeMap
});
}

for imp in def.interface_impls() {
let ty = imp.ty(generics);
let ty = winmd_type(&ty);
for interface in metadata::type_def_interfaces(def, generics) {
let ty = winmd_type(&interface.ty);

let reference = match &ty {
winmd::Type::TypeRef(type_name) if type_name.generics.is_empty() => writer.insert_type_ref(&type_name.namespace, &type_name.name),
Expand Down

0 comments on commit 9dd482e

Please sign in to comment.