pdb-bindgen-codegen 0.1.0

Build-time discovery, PDB acquisition, and binding generation for pdb-bindgen.
Documentation
use crate::pdb::ir::TypeId;
use std::error::Error as StdError;
use std::fmt;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ObjectKind {
    Absent,
    Void,
    Procedure,
    MemberFunction,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum Error {
    MissingType {
        type_id: TypeId,
    },
    MissingRecord {
        type_id: TypeId,
    },
    InvalidObjectType {
        type_id: TypeId,
        kind: ObjectKind,
    },
    BitfieldIndirectUse {
        type_id: TypeId,
    },
    BitfieldStorageMissing {
        type_id: TypeId,
        storage: TypeId,
    },
    BitfieldStorage {
        type_id: TypeId,
        storage: TypeId,
    },
    BitfieldZeroStorage {
        type_id: TypeId,
        storage: TypeId,
    },
    BitfieldRange {
        type_id: TypeId,
        storage: TypeId,
        position: u8,
        length: u8,
        storage_size: u64,
    },
    BitfieldOutOfBounds {
        record: TypeId,
        field: String,
        type_id: TypeId,
        storage: TypeId,
    },
    BitfieldRangeOverlap {
        record: TypeId,
        field: String,
        storage: TypeId,
    },
    BitfieldStorageConflict {
        record: TypeId,
        field: String,
        storage: TypeId,
        prior_storage: TypeId,
    },
    BitfieldPhysicalOverlap {
        record: TypeId,
        field: String,
        storage: TypeId,
    },
    Cycle {
        type_id: TypeId,
    },
    InvalidAlignment {
        type_id: TypeId,
        alignment: u64,
    },
    RecordSizeAlignment {
        record: TypeId,
        size: u64,
        alignment: u64,
    },
    RecordAlignmentTooSmall {
        record: TypeId,
        alignment: u64,
        natural_alignment: u64,
    },
    PackedAlignmentUnavailable {
        record: TypeId,
    },
    PackedLayoutMismatch {
        record: TypeId,
        size: u64,
    },
    PackedAlignmentAmbiguous {
        record: TypeId,
        alignments: Vec<u64>,
    },
    MalformedArray {
        type_id: TypeId,
        size: u64,
        element_size: u64,
    },
    Overflow {
        type_id: TypeId,
        operation: &'static str,
    },
    FieldOutOfBounds {
        record: TypeId,
        field: String,
    },
    FieldsOutOfOrder {
        record: TypeId,
        field: String,
    },
    FieldsOverlap {
        record: TypeId,
        field: String,
    },
    MisalignedField {
        record: TypeId,
        field: String,
        alignment: u64,
    },
    UnionFieldOffset {
        record: TypeId,
        field: String,
        offset: u64,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MissingType { type_id } => write!(f, "missing type 0x{:x}", type_id.0),
            Self::MissingRecord { type_id } => write!(f, "missing record 0x{:x}", type_id.0),
            Self::InvalidObjectType { type_id, kind } => {
                write!(f, "{kind:?} type 0x{:x} is not an object", type_id.0)
            }
            Self::BitfieldIndirectUse { type_id } => {
                write!(
                    f,
                    "bitfield type 0x{:x} is not a direct record field",
                    type_id.0
                )
            }
            Self::BitfieldStorageMissing { type_id, storage } => write!(
                f,
                "bitfield type 0x{:x} has missing storage type 0x{:x}",
                type_id.0, storage.0
            ),
            Self::BitfieldStorage { type_id, storage } => write!(
                f,
                "bitfield type 0x{:x} has non-primitive storage type 0x{:x}",
                type_id.0, storage.0
            ),
            Self::BitfieldZeroStorage { type_id, storage } => write!(
                f,
                "bitfield type 0x{:x} has zero-sized storage type 0x{:x}",
                type_id.0, storage.0
            ),
            Self::BitfieldRange {
                type_id,
                storage,
                position,
                length,
                storage_size,
            } => write!(
                f,
                "bitfield type 0x{:x} has invalid range {position}+{length} for {storage_size}-byte storage type 0x{:x}",
                type_id.0, storage.0
            ),
            Self::BitfieldOutOfBounds {
                record,
                field,
                type_id,
                storage,
            } => write!(
                f,
                "bitfield {field} type 0x{:x} with storage type 0x{:x} exceeds record 0x{:x}",
                type_id.0, storage.0, record.0
            ),
            Self::BitfieldRangeOverlap {
                record,
                field,
                storage,
            } => write!(
                f,
                "bitfield {field} overlaps a prior range in storage type 0x{:x} in record 0x{:x}",
                storage.0, record.0
            ),
            Self::BitfieldStorageConflict {
                record,
                field,
                storage,
                prior_storage,
            } => write!(
                f,
                "bitfield {field} storage type 0x{:x} conflicts with prior storage type 0x{:x} in record 0x{:x}",
                storage.0, prior_storage.0, record.0
            ),
            Self::BitfieldPhysicalOverlap {
                record,
                field,
                storage,
            } => write!(
                f,
                "bitfield {field} storage type 0x{:x} partially overlaps a prior field in record 0x{:x}",
                storage.0, record.0
            ),
            Self::Cycle { type_id } => write!(f, "by-value type cycle at 0x{:x}", type_id.0),
            Self::InvalidAlignment { type_id, alignment } => {
                write!(
                    f,
                    "invalid alignment {alignment} for type 0x{:x}",
                    type_id.0
                )
            }
            Self::RecordSizeAlignment {
                record,
                size,
                alignment,
            } => write!(
                f,
                "record 0x{:x} has size {size} not divisible by alignment {alignment}",
                record.0
            ),
            Self::RecordAlignmentTooSmall {
                record,
                alignment,
                natural_alignment,
            } => write!(
                f,
                "record 0x{:x} alignment {alignment} is smaller than natural alignment {natural_alignment}",
                record.0
            ),
            Self::PackedAlignmentUnavailable { record } => write!(
                f,
                "packed record 0x{:x} has unavailable member or natural alignment",
                record.0
            ),
            Self::PackedLayoutMismatch { record, size } => write!(
                f,
                "packed record 0x{:x} size {size} matches no supported MSVC packed layout",
                record.0
            ),
            Self::PackedAlignmentAmbiguous { record, alignments } => write!(
                f,
                "packed record 0x{:x} has ambiguous effective alignments {alignments:?}",
                record.0
            ),
            Self::MalformedArray {
                type_id,
                size,
                element_size,
            } => write!(
                f,
                "array type 0x{:x} has size {size} incompatible with element size {element_size}",
                type_id.0
            ),
            Self::Overflow { type_id, operation } => {
                write!(f, "overflow while {operation} for type 0x{:x}", type_id.0)
            }
            Self::FieldOutOfBounds { record, field } => {
                write!(f, "field {field} exceeds record 0x{:x}", record.0)
            }
            Self::FieldsOutOfOrder { record, field } => {
                write!(
                    f,
                    "field {field} is out of order in record 0x{:x}",
                    record.0
                )
            }
            Self::FieldsOverlap { record, field } => {
                write!(
                    f,
                    "field {field} overlaps a prior field in record 0x{:x}",
                    record.0
                )
            }
            Self::MisalignedField {
                record,
                field,
                alignment,
            } => write!(
                f,
                "field {field} is not aligned to {alignment} in record 0x{:x}",
                record.0
            ),
            Self::UnionFieldOffset {
                record,
                field,
                offset,
            } => write!(
                f,
                "union field {field} starts at {offset}, not zero, in record 0x{:x}",
                record.0
            ),
        }
    }
}

impl StdError for Error {}