1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/// The size of a type.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum Size {
    U8,
    U16,
    U32,
    U64,
}

impl Size {
    /// Return the number of bits this `Size` represents.
    pub fn bits(self) -> u8 {
        use Size::*;

        match self {
            U8 => 8,
            U16 => 16,
            U32 => 32,
            U64 => 64,
        }
    }

    /// Return the number of bytes in a size.
    ///
    /// A byte is assumed to be 8 bits.
    pub fn bytes(self) -> u8 {
        use Size::*;

        match self {
            U8 => 1,
            U16 => 2,
            U32 => 4,
            U64 => 8,
        }
    }
}

/// The C data model used on a target.
///
/// See also https://en.cppreference.com/w/c/language/arithmetic_types
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "rust_1_40", non_exhaustive)]
pub enum CDataModel {
    /// The data model used most commonly on Win16. `long` and `pointer` are 32 bits.
    LP32,
    /// The data model used most commonly on Win32 and 32-bit Unix systems.
    ///
    /// `int`, `long`, and `pointer` are all 32 bits.
    ILP32,
    /// The data model used most commonly on Win64
    ///
    /// `long long`, and `pointer` are 64 bits.
    LLP64,
    /// The data model used most commonly on 64-bit Unix systems
    ///
    /// `long`, and `pointer` are 64 bits.
    LP64,
    /// A rare data model used on early 64-bit Unix systems
    ///
    /// `int`, `long`, and `pointer` are all 64 bits.
    ILP64,
}

impl CDataModel {
    /// The width of a pointer (in the default address space).
    pub fn pointer_width(self) -> Size {
        use CDataModel::*;

        match self {
            LP32 | ILP32 => Size::U32,
            LLP64 | LP64 | ILP64 => Size::U64,
        }
    }
    /// The size of a C `short`. This is required to be at least 16 bits.
    pub fn short_size(self) -> Size {
        use CDataModel::*;

        match self {
            LP32 | ILP32 | LLP64 | LP64 | ILP64 => Size::U16,
        }
    }
    /// The size of a C `int`. This is required to be at least 16 bits.
    pub fn int_size(self) -> Size {
        use CDataModel::*;

        match self {
            LP32 => Size::U16,
            ILP32 | LLP64 | LP64 | ILP64 => Size::U32,
        }
    }
    /// The size of a C `long`. This is required to be at least 32 bits.
    pub fn long_size(self) -> Size {
        use CDataModel::*;

        match self {
            LP32 | ILP32 | LLP64 | ILP64 => Size::U32,
            LP64 => Size::U64,
        }
    }
    /// The size of a C `long long`. This is required (in C99+) to be at least 64 bits.
    pub fn long_long_size(self) -> Size {
        use CDataModel::*;

        match self {
            LP32 | ILP32 | LLP64 | ILP64 | LP64 => Size::U64,
        }
    }
    /// The size of a C `float`.
    pub fn float_size(self) -> Size {
        // TODO: this is probably wrong on at least one architecture
        Size::U32
    }
    /// The size of a C `double`.
    pub fn double_size(self) -> Size {
        // TODO: this is probably wrong on at least one architecture
        Size::U64
    }
}