use crate::error::Result;
use crate::tables::coded::CodedIndex;
use crate::tables::{ColumnKind, RowCursor, TableRow};
use crate::token::{BlobIndex, GuidIndex, Rid, StringIndex, TableId, Token, marker};
macro_rules! col_kind {
([u16]) => {
ColumnKind::U16
};
([u32]) => {
ColumnKind::U32
};
([str]) => {
ColumnKind::String
};
([blob]) => {
ColumnKind::Blob
};
([guid]) => {
ColumnKind::Guid
};
([rid $t:ident]) => {
ColumnKind::Rid(TableId::$t)
};
([coded $k:ident]) => {
ColumnKind::Coded(CodedIndex::$k)
};
}
macro_rules! col_ty {
([u16]) => { u16 };
([u32]) => { u32 };
([str]) => { StringIndex };
([blob]) => { BlobIndex };
([guid]) => { GuidIndex };
([rid $t:ident]) => { Rid<marker::$t> };
([coded $k:ident]) => { Token };
}
macro_rules! col_read {
([u16], $c:expr) => {
$c.uint().map(|v| v as u16)
};
([u32], $c:expr) => {
$c.uint()
};
([str], $c:expr) => {
$c.uint().map(StringIndex)
};
([blob], $c:expr) => {
$c.uint().map(BlobIndex)
};
([guid], $c:expr) => {
$c.uint().map(GuidIndex)
};
([rid $t:ident], $c:expr) => {
$c.uint().map(Rid::<marker::$t>::new)
};
([coded $k:ident], $c:expr) => {
$c.coded(CodedIndex::$k)
};
}
macro_rules! define_rows {
($(
$(#[$attr:meta])*
$table:ident as $one:ident / $many:ident {
$( $(#[$fattr:meta])* $field:ident : $kind:tt ),* $(,)?
}
)*) => {
$(
$(#[$attr])*
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct $table {
$( $(#[$fattr])* pub $field : col_ty!($kind), )*
}
impl TableRow for $table {
const TABLE: TableId = TableId::$one;
const COLUMNS: &'static [ColumnKind] = &[ $( col_kind!($kind) ),* ];
fn decode(cursor: &mut RowCursor<'_>) -> Result<Self> {
$( let $field = col_read!($kind, cursor)?; )*
Ok($table { $( $field ),* })
}
}
)*
impl<'a> crate::tables::Tables<'a> {
$(
#[doc = concat!("Reads one `", stringify!($one), "` row by 1-based RID, following the `*Ptr` indirection when the image has one.")]
pub fn $many(&self, rid: u32) -> Result<$table> {
self.row_indirect::<$table>(rid)
}
)*
}
};
}
define_rows! {
ModuleRow as Module / module {
generation: [u16],
name: [str],
mvid: [guid],
enc_id: [guid],
enc_base_id: [guid],
}
TypeRefRow as TypeRef / type_ref {
resolution_scope: [coded ResolutionScope],
type_name: [str],
type_namespace: [str],
}
TypeDefRow as TypeDef / type_def {
flags: [u32],
type_name: [str],
type_namespace: [str],
extends: [coded TypeDefOrRef],
field_list: [rid Field],
method_list: [rid MethodDef],
}
FieldPtrRow as FieldPtr / field_ptr {
field: [rid Field],
}
FieldRow as Field / field {
flags: [u16],
name: [str],
signature: [blob],
}
MethodPtrRow as MethodPtr / method_ptr {
method: [rid MethodDef],
}
MethodDefRow as MethodDef / method_def {
rva: [u32],
impl_flags: [u16],
flags: [u16],
name: [str],
signature: [blob],
param_list: [rid Param],
}
ParamPtrRow as ParamPtr / param_ptr {
param: [rid Param],
}
ParamRow as Param / param {
flags: [u16],
sequence: [u16],
name: [str],
}
InterfaceImplRow as InterfaceImpl / interface_impl {
class: [rid TypeDef],
interface: [coded TypeDefOrRef],
}
MemberRefRow as MemberRef / member_ref {
class: [coded MemberRefParent],
name: [str],
signature: [blob],
}
ConstantRow as Constant / constant {
type_raw: [u16],
parent: [coded HasConstant],
value: [blob],
}
CustomAttributeRow as CustomAttribute / custom_attribute {
parent: [coded HasCustomAttribute],
constructor: [coded CustomAttributeType],
value: [blob],
}
FieldMarshalRow as FieldMarshal / field_marshal {
parent: [coded HasFieldMarshal],
native_type: [blob],
}
DeclSecurityRow as DeclSecurity / decl_security {
action: [u16],
parent: [coded HasDeclSecurity],
permission_set: [blob],
}
ClassLayoutRow as ClassLayout / class_layout {
packing_size: [u16],
class_size: [u32],
parent: [rid TypeDef],
}
FieldLayoutRow as FieldLayout / field_layout {
offset: [u32],
field: [rid Field],
}
StandAloneSigRow as StandAloneSig / stand_alone_sig {
signature: [blob],
}
EventMapRow as EventMap / event_map {
parent: [rid TypeDef],
event_list: [rid Event],
}
EventPtrRow as EventPtr / event_ptr {
event: [rid Event],
}
EventRow as Event / event {
event_flags: [u16],
name: [str],
event_type: [coded TypeDefOrRef],
}
PropertyMapRow as PropertyMap / property_map {
parent: [rid TypeDef],
property_list: [rid Property],
}
PropertyPtrRow as PropertyPtr / property_ptr {
property: [rid Property],
}
PropertyRow as Property / property {
flags: [u16],
name: [str],
type_signature: [blob],
}
MethodSemanticsRow as MethodSemantics / method_semantics {
semantics: [u16],
method: [rid MethodDef],
association: [coded HasSemantics],
}
MethodImplRow as MethodImpl / method_impl {
class: [rid TypeDef],
method_body: [coded MethodDefOrRef],
method_declaration: [coded MethodDefOrRef],
}
ModuleRefRow as ModuleRef / module_ref {
name: [str],
}
TypeSpecRow as TypeSpec / type_spec {
signature: [blob],
}
ImplMapRow as ImplMap / impl_map {
mapping_flags: [u16],
member_forwarded: [coded MemberForwarded],
import_name: [str],
import_scope: [rid ModuleRef],
}
FieldRvaRow as FieldRva / field_rva {
rva: [u32],
field: [rid Field],
}
EncLogRow as EncLog / enc_log {
token: [u32],
func_code: [u32],
}
EncMapRow as EncMap / enc_map {
token: [u32],
}
AssemblyRow as Assembly / assembly {
hash_alg_id: [u32],
major_version: [u16],
minor_version: [u16],
build_number: [u16],
revision_number: [u16],
flags: [u32],
public_key: [blob],
name: [str],
culture: [str],
}
AssemblyProcessorRow as AssemblyProcessor / assembly_processor {
processor: [u32],
}
AssemblyOsRow as AssemblyOs / assembly_os {
os_platform_id: [u32],
os_major_version: [u32],
os_minor_version: [u32],
}
AssemblyRefRow as AssemblyRef / assembly_ref {
major_version: [u16],
minor_version: [u16],
build_number: [u16],
revision_number: [u16],
flags: [u32],
public_key_or_token: [blob],
name: [str],
culture: [str],
hash_value: [blob],
}
AssemblyRefProcessorRow as AssemblyRefProcessor / assembly_ref_processor {
processor: [u32],
assembly_ref: [rid AssemblyRef],
}
AssemblyRefOsRow as AssemblyRefOs / assembly_ref_os {
os_platform_id: [u32],
os_major_version: [u32],
os_minor_version: [u32],
assembly_ref: [rid AssemblyRef],
}
FileRow as File / file {
flags: [u32],
name: [str],
hash_value: [blob],
}
ExportedTypeRow as ExportedType / exported_type {
flags: [u32],
type_def_id: [u32],
type_name: [str],
type_namespace: [str],
implementation: [coded Implementation],
}
ManifestResourceRow as ManifestResource / manifest_resource {
offset: [u32],
flags: [u32],
name: [str],
implementation: [coded Implementation],
}
NestedClassRow as NestedClass / nested_class {
nested_class: [rid TypeDef],
enclosing_class: [rid TypeDef],
}
GenericParamRow as GenericParam / generic_param {
number: [u16],
flags: [u16],
owner: [coded TypeOrMethodDef],
name: [str],
}
MethodSpecRow as MethodSpec / method_spec {
method: [coded MethodDefOrRef],
instantiation: [blob],
}
GenericParamConstraintRow as GenericParamConstraint / generic_param_constraint {
owner: [rid GenericParam],
constraint: [coded TypeDefOrRef],
}
DocumentRow as Document / document {
name: [blob],
hash_algorithm: [guid],
hash: [blob],
language: [guid],
}
MethodDebugInformationRow as MethodDebugInformation / method_debug_information {
document: [rid Document],
sequence_points: [blob],
}
LocalScopeRow as LocalScope / local_scope {
method: [rid MethodDef],
import_scope: [rid ImportScope],
variable_list: [rid LocalVariable],
constant_list: [rid LocalConstant],
start_offset: [u32],
length: [u32],
}
LocalVariableRow as LocalVariable / local_variable {
attributes: [u16],
index: [u16],
name: [str],
}
LocalConstantRow as LocalConstant / local_constant {
name: [str],
signature: [blob],
}
ImportScopeRow as ImportScope / import_scope {
parent: [rid ImportScope],
imports: [blob],
}
StateMachineMethodRow as StateMachineMethod / state_machine_method {
move_next_method: [rid MethodDef],
kickoff_method: [rid MethodDef],
}
CustomDebugInformationRow as CustomDebugInformation / custom_debug_information {
parent: [coded HasCustomDebugInformation],
kind: [guid],
value: [blob],
}
}
impl ConstantRow {
pub const fn element_type(self) -> u8 {
self.type_raw as u8
}
pub const fn padding(self) -> u8 {
(self.type_raw >> 8) as u8
}
}
impl MethodDefRow {
pub const fn is_il(self) -> bool {
self.impl_flags & 0x0003 == 0x0000
}
pub const fn is_native(self) -> bool {
self.impl_flags & 0x0003 == 0x0001
}
pub const fn is_runtime(self) -> bool {
self.impl_flags & 0x0003 == 0x0003
}
pub const fn is_static(self) -> bool {
self.flags & 0x0010 != 0
}
pub const fn is_abstract(self) -> bool {
self.flags & 0x0400 != 0
}
pub const fn is_pinvoke(self) -> bool {
self.flags & 0x2000 != 0
}
pub const fn has_no_body(self) -> bool {
self.rva == 0 || !self.is_il()
}
}
impl TypeDefRow {
pub const fn layout_kind(self) -> u8 {
(self.flags & 0x0000_0018) as u8 >> 3
}
pub const fn is_interface(self) -> bool {
self.flags & 0x0000_0020 != 0
}
pub const fn is_nested(self) -> bool {
let vis = self.flags & 0x0000_0007;
vis >= 2
}
}
impl FieldRow {
pub const fn is_static(self) -> bool {
self.flags & 0x0010 != 0
}
pub const fn has_rva(self) -> bool {
self.flags & 0x0100 != 0
}
pub const fn is_literal(self) -> bool {
self.flags & 0x0040 != 0
}
}