isr_core/
types.rs

1//! Types module.
2//!
3//! This module contains the types used to represent the data structures of the
4//! profile and symbols files.
5
6use std::borrow::Cow;
7
8use indexmap::IndexMap;
9use serde::{Deserialize, Serialize};
10use smallvec::SmallVec;
11
12#[derive(Debug, Default, Serialize, Deserialize)]
13pub struct Types<'a> {
14    #[serde(borrow)]
15    pub enums: IndexMap<Cow<'a, str>, Enum<'a>>,
16    #[serde(borrow)]
17    pub structs: IndexMap<Cow<'a, str>, Struct<'a>>,
18}
19
20//
21// Enum
22//
23
24/// Enum type.
25#[derive(Debug, Serialize, Deserialize)]
26pub struct Enum<'a> {
27    #[serde(borrow)]
28    pub subtype: Type<'a>,
29    #[serde(borrow)]
30    pub fields: IndexMap<Cow<'a, str>, Variant>,
31}
32
33/// Enum variant.
34#[derive(Debug, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum Variant {
37    U8(u8),
38    U16(u16),
39    U32(u32),
40    U64(u64),
41    U128(u128),
42    I8(i8),
43    I16(i16),
44    I32(i32),
45    I64(i64),
46    I128(i128),
47}
48
49//
50// Struct
51//
52
53/// Struct type.
54#[derive(Debug, Serialize, Deserialize)]
55pub struct Struct<'a> {
56    pub kind: StructKind,
57    pub size: u64,
58    #[serde(borrow)]
59    pub fields: IndexMap<Cow<'a, str>, Field<'a>>,
60}
61
62/// Struct kind.
63#[derive(Debug, Serialize, Deserialize)]
64#[serde(rename_all = "snake_case")]
65pub enum StructKind {
66    /// A `struct`.
67    Struct,
68
69    /// A `class`.
70    Class,
71
72    /// A `union`.
73    Union,
74
75    /// An `interface`.
76    Interface,
77}
78
79/// Struct field.
80#[derive(Debug, Serialize, Deserialize)]
81pub struct Field<'a> {
82    /// Field offset (in bytes).
83    pub offset: u64,
84
85    /// Field type.
86    #[serde(borrow, rename = "type")]
87    pub type_: Type<'a>,
88}
89
90//
91// Type
92//
93
94/// Type.
95#[derive(Debug, Serialize, Deserialize)]
96#[serde(rename_all = "snake_case", tag = "kind")]
97pub enum Type<'a> {
98    /// Base type.
99    Base(BaseRef),
100
101    /// Enum type.
102    Enum(#[serde(borrow)] EnumRef<'a>),
103
104    /// Struct type.
105    Struct(#[serde(borrow)] StructRef<'a>),
106
107    /// Array type.
108    Array(#[serde(borrow)] ArrayRef<'a>),
109
110    /// Pointer type.
111    Pointer(#[serde(borrow)] PointerRef<'a>),
112
113    /// Bitfield type.
114    Bitfield(#[serde(borrow)] BitfieldRef<'a>),
115
116    /// Function type.
117    Function,
118}
119
120/// Base type reference.
121#[derive(Debug, Serialize, Deserialize)]
122#[serde(rename_all = "snake_case", tag = "subkind")]
123pub enum BaseRef {
124    /// Void type.
125    Void,
126
127    /// Boolean type.
128    Bool,
129
130    /// Character type.
131    Char,
132
133    /// Wide character type.
134    Wchar,
135
136    /// Signed integer types.
137    I8,
138    I16,
139    I32,
140    I64,
141    I128,
142
143    /// Unsigned integer types.
144    U8,
145    U16,
146    U32,
147    U64,
148    U128,
149
150    /// Floating-point types.
151    F8,
152    F16,
153    F32,
154    F64,
155    F128,
156}
157
158/// Enum reference.
159#[derive(Debug, Serialize, Deserialize)]
160pub struct EnumRef<'a> {
161    /// Name of the referenced enum.
162    #[serde(borrow)]
163    pub name: Cow<'a, str>,
164}
165
166/// Struct reference.
167#[derive(Debug, Serialize, Deserialize)]
168pub struct StructRef<'a> {
169    /// Name of the referenced struct.
170    #[serde(borrow)]
171    pub name: Cow<'a, str>,
172}
173
174/// Array reference.
175#[derive(Debug, Serialize, Deserialize)]
176pub struct ArrayRef<'a> {
177    /// Element type.
178    #[serde(borrow)]
179    pub subtype: Box<Type<'a>>,
180
181    /// Array dimensions.
182    pub dims: SmallVec<[u64; 4]>,
183
184    /// Total number of elements.
185    pub size: u64,
186}
187
188/// Bitfield reference.
189#[derive(Debug, Serialize, Deserialize)]
190pub struct BitfieldRef<'a> {
191    /// Bitfield subtype.
192    #[serde(borrow)]
193    pub subtype: Box<Type<'a>>,
194
195    /// Bit length.
196    pub bit_length: u64,
197
198    /// Bit position.
199    pub bit_position: u64,
200}
201
202/// Pointer reference.
203#[derive(Debug, Serialize, Deserialize)]
204pub struct PointerRef<'a> {
205    /// Type of the pointed value.
206    #[serde(borrow)]
207    pub subtype: Box<Type<'a>>,
208}