Skip to content

Commit

Permalink
metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Oct 17, 2023
1 parent 41ad38d commit 1a81f41
Show file tree
Hide file tree
Showing 18 changed files with 1,078 additions and 1,195 deletions.
2 changes: 1 addition & 1 deletion crates/libs/metadata/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

macro_rules! flags {
($name:ident, $size:ty) => {
#[derive(Default, Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Default, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
pub struct $name(pub $size);
impl $name {
pub fn contains(&self, contains: Self) -> bool {
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions crates/libs/metadata/src/blob.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use super::*;

pub struct Blob<'a> {
pub file: usize,
pub slice: &'a [u8],
pub struct Blob {
pub file: &'static File,
pub slice: &'static [u8],
}

impl<'a> std::ops::Deref for Blob<'a> {
impl std::ops::Deref for Blob {
type Target = [u8];

fn deref(&self) -> &[u8] {
self.slice
}
}

impl<'a> Blob<'a> {
pub fn new(file: usize, slice: &'a [u8]) -> Self {
impl Blob {
pub fn new(file: &'static File, slice: &'static [u8]) -> Self {
Self { file, slice }
}

Expand Down
216 changes: 62 additions & 154 deletions crates/libs/metadata/src/codes.rs
Original file line number Diff line number Diff line change
@@ -1,183 +1,91 @@
use super::*;

pub trait Decode {
fn decode(file: usize, code: usize) -> Self;
fn decode(file: &'static File, code: usize) -> Self;
}

pub enum AttributeType {
MemberRef(MemberRef),
}

impl Decode for AttributeType {
fn decode(file: usize, code: usize) -> Self {
let (kind, row) = (code & ((1 << 3) - 1), (code >> 3) - 1);
match kind {
3 => Self::MemberRef(MemberRef(Row::new(row, file))),
rest => unimplemented!("{rest:?}"),
macro_rules! code {
($name:ident($size:literal) $(($table:ident, $code:literal))+) => {
#[derive(Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub enum $name {
$($table($table),)*
}
}
}

pub enum HasAttribute {
MethodDef(MethodDef),
Field(Field),
TypeRef(TypeRef),
TypeDef(TypeDef),
Param(Param),
InterfaceImpl(InterfaceImpl),
MemberRef(MemberRef),
TypeSpec(TypeSpec),
GenericParam(GenericParam),
}

impl HasAttribute {
pub fn encode(&self) -> usize {
match self {
Self::MethodDef(row) => (row.0.row + 1) << 5,
Self::Field(row) => ((row.0.row + 1) << 5) | 1,
Self::TypeRef(row) => ((row.0.row + 1) << 5) | 2,
Self::TypeDef(row) => ((row.0.row + 1) << 5) | 3,
Self::Param(row) => ((row.0.row + 1) << 5) | 4,
Self::InterfaceImpl(row) => ((row.0.row + 1) << 5) | 5,
Self::MemberRef(row) => ((row.0.row + 1) << 5) | 6,
Self::TypeSpec(row) => ((row.0.row + 1) << 5) | 13,
Self::GenericParam(row) => ((row.0.row + 1) << 5) | 19,
impl Decode for $name {
fn decode(file: &'static File, code: usize) -> Self {
let (kind, row) = (code & ((1 << $size) - 1), (code >> $size) - 1);
match kind {
$($code => Self::$table($table(Row::new(file, row))),)*
rest => unimplemented!("{rest:?}"),
}
}
}
}
}

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),
}

impl HasConstant {
pub fn encode(&self) -> usize {
match self {
Self::Field(row) => (row.0.row + 1) << 2,
impl $name {
pub fn encode(&self) -> usize {
match self {
$(Self::$table(row) => (row.index() + 1) << $size | $code,)*
}
}
}
}
$(
impl From<$table> for $name {
fn from(from: $table) -> Self {
Self::$table(from)
}
}
)*
};
}

#[derive(Clone)]
pub enum MemberForwarded {
MethodDef(MethodDef),
code! { AttributeType(3)
(MemberRef, 3)
}

impl MemberForwarded {
pub fn encode(&self) -> usize {
match self {
Self::MethodDef(value) => ((value.0.row + 1) << 1) | 1,
}
}
code! { HasAttribute(5)
(MethodDef, 0)
(Field, 1)
(TypeRef, 2)
(TypeDef, 3)
(Param, 4)
(InterfaceImpl, 5)
(MemberRef, 6)
(TypeSpec, 13)
(GenericParam, 19)
}

pub enum MemberRefParent {
TypeRef(TypeRef),
code! { HasConstant(2)
(Field, 0)
}

impl Decode for MemberRefParent {
fn decode(file: usize, code: usize) -> Self {
let (kind, row) = (code & ((1 << 3) - 1), (code >> 3) - 1);
match kind {
1 => Self::TypeRef(TypeRef(Row::new(row, file))),
rest => unimplemented!("{rest:?}"),
}
}
code! { MemberForwarded(1)
(MethodDef, 1)
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum TypeDefOrRef {
TypeDef(TypeDef),
TypeRef(TypeRef),
TypeSpec(TypeSpec),
}

impl Decode for TypeDefOrRef {
fn decode(file: usize, code: usize) -> Self {
let (kind, row) = (code & ((1 << 2) - 1), (code >> 2) - 1);
match kind {
0 => Self::TypeDef(TypeDef(Row::new(row, file))),
1 => Self::TypeRef(TypeRef(Row::new(row, file))),
2 => Self::TypeSpec(TypeSpec(Row::new(row, file))),
rest => unimplemented!("{rest:?}"),
}
}
code! { MemberRefParent(3)
(TypeRef, 1)
}

pub enum TypeOrMethodDef {
TypeDef(TypeDef),
code! { TypeDefOrRef(2)
(TypeDef, 0)
(TypeRef, 1)
(TypeSpec, 2)
}

impl TypeOrMethodDef {
pub fn encode(&self) -> usize {
match self {
Self::TypeDef(value) => (value.0.row + 1) << 1,
}
}
code! { TypeOrMethodDef(1)
(TypeDef, 0)
}

pub enum ResolutionScope {
Module(Module),
ModuleRef(ModuleRef),
AssemblyRef(AssemblyRef),
TypeRef(TypeRef),
code! { ResolutionScope(2)
(Module, 0)
(ModuleRef, 1)
(AssemblyRef, 2)
(TypeRef, 3)
}

impl Decode for ResolutionScope {
fn decode(file: usize, code: usize) -> Self {
let (kind, row) = (code & ((1 << 2) - 1), (code >> 2) - 1);
match kind {
0 => Self::Module(Module(Row::new(row, file))),
1 => Self::ModuleRef(ModuleRef(Row::new(row, file))),
2 => Self::AssemblyRef(AssemblyRef(Row::new(row, file))),
3 => Self::TypeRef(TypeRef(Row::new(row, file))),
impl TypeDefOrRef {
pub fn type_name(&self) -> TypeName {
match self {
Self::TypeDef(row) => row.type_name(),
Self::TypeRef(row) => row.type_name(),
rest => unimplemented!("{rest:?}"),
}
}
Expand Down
11 changes: 11 additions & 0 deletions crates/libs/metadata/src/column.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[derive(Default)]
pub struct Column {
pub offset: usize,
pub width: usize,
}

impl Column {
pub fn new(offset: usize, width: usize) -> Self {
Self { offset, width }
}
}
Loading

0 comments on commit 1a81f41

Please sign in to comment.