Skip to content

Commit

Permalink
Metadata parsing improvements (#2592)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jul 31, 2023
1 parent a20dbba commit 53eed86
Show file tree
Hide file tree
Showing 36 changed files with 819 additions and 717 deletions.
46 changes: 46 additions & 0 deletions crates/libs/metadata/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,52 @@ impl HasAttribute {
}
}

impl From<MethodDef> for HasAttribute {
fn from(from: MethodDef) -> Self {
Self::MethodDef(from)
}
}
impl From<Field> for HasAttribute {
fn from(from: Field) -> Self {
Self::Field(from)
}
}
impl From<TypeRef> for HasAttribute {
fn from(from: TypeRef) -> Self {
Self::TypeRef(from)
}
}
impl From<TypeDef> for HasAttribute {
fn from(from: TypeDef) -> Self {
Self::TypeDef(from)
}
}
impl From<Param> for HasAttribute {
fn from(from: Param) -> Self {
Self::Param(from)
}
}
impl From<InterfaceImpl> for HasAttribute {
fn from(from: InterfaceImpl) -> Self {
Self::InterfaceImpl(from)
}
}
impl From<MemberRef> for HasAttribute {
fn from(from: MemberRef) -> Self {
Self::MemberRef(from)
}
}
impl From<TypeSpec> for HasAttribute {
fn from(from: TypeSpec) -> Self {
Self::TypeSpec(from)
}
}
impl From<GenericParam> for HasAttribute {
fn from(from: GenericParam) -> Self {
Self::GenericParam(from)
}
}

#[derive(Clone)]
pub enum HasConstant {
Field(Field),
Expand Down
40 changes: 12 additions & 28 deletions crates/libs/metadata/src/file/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ pub trait RowReader<'a> {
self.type_ref_name(row)
}

fn attributes<R: AsRow + Into<HasAttribute>>(&self, row: R) -> RowIterator<Attribute> {
self.row_equal_range(row, 0, row.into().encode())
}

fn find_attribute<R: AsRow + Into<HasAttribute>>(&self, row: R, name: &str) -> Option<Attribute> {
self.attributes(row).find(|attribute| self.attribute_name(*attribute) == name)
}

fn has_attribute<R: AsRow + Into<HasAttribute>>(&self, row: R, name: &str) -> bool {
self.find_attribute(row, name).is_some()
}

//
// Other
//
Expand Down Expand Up @@ -159,10 +171,6 @@ pub trait RowReader<'a> {
self.row_equal_range(row, 1, HasConstant::Field(row).encode()).next()
}

fn field_attributes(&self, row: Field) -> RowIterator<Attribute> {
self.row_equal_range(row, 0, HasAttribute::Field(row).encode())
}

//
// GenericParam
//
Expand All @@ -187,14 +195,6 @@ pub trait RowReader<'a> {
self.row_str(row, 2)
}

//
// InterfaceImpl
//

fn interface_impl_attributes(&self, row: InterfaceImpl) -> RowIterator<Attribute> {
self.row_equal_range(row, 0, HasAttribute::InterfaceImpl(row).encode())
}

//
// MemberRef
//
Expand Down Expand Up @@ -227,10 +227,6 @@ pub trait RowReader<'a> {
self.row_list(row, 5)
}

fn method_def_attributes(&self, row: MethodDef) -> RowIterator<Attribute> {
self.row_equal_range(row, 0, HasAttribute::MethodDef(row).encode())
}

fn method_def_impl_map(&self, row: MethodDef) -> Option<ImplMap> {
self.row_equal_range(row, 1, MemberForwarded::MethodDef(row).encode()).next()
}
Expand Down Expand Up @@ -280,10 +276,6 @@ pub trait RowReader<'a> {
self.row_str(row, 2)
}

fn param_attributes(&self, row: Param) -> RowIterator<Attribute> {
self.row_equal_range(row, 0, HasAttribute::Param(row).encode())
}

//
// TypeDef
//
Expand Down Expand Up @@ -315,10 +307,6 @@ pub trait RowReader<'a> {
self.row_list(row, 4)
}

fn type_def_attributes(&self, row: TypeDef) -> RowIterator<Attribute> {
self.row_equal_range(row, 0, HasAttribute::TypeDef(row).encode())
}

fn type_def_generics(&self, row: TypeDef) -> Vec<Type> {
self.row_equal_range(row, 2, TypeOrMethodDef::TypeDef(row).encode()).map(Type::GenericParam).collect()
}
Expand All @@ -335,10 +323,6 @@ pub trait RowReader<'a> {
self.row_equal_range(row, 2, row.0.row + 1).next()
}

fn type_def_is_scoped(&self, row: TypeDef) -> bool {
self.type_def_flags(row).contains(TypeAttributes::WindowsRuntime) || self.type_def_attributes(row).any(|attribute| self.attribute_name(attribute) == "ScopedEnumAttribute")
}

//
// TypeRef
//
Expand Down
Loading

0 comments on commit 53eed86

Please sign in to comment.