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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use crate::{
    ast::{expr::ConstExpr, generic::GenericParams, ty::TyKind},
    common::{FieldId, SpanId, SymbolId, VariantId},
    context::with_cx,
    ffi::{FfiOption, FfiSlice},
    span::{HasSpan, Span},
};

use super::{CommonItemData, Visibility};

/// A union item like:
///
/// ```
/// pub union Foo {
///     a: i32,
///     b: f32,
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct UnionItem<'ast> {
    data: CommonItemData<'ast>,
    generics: GenericParams<'ast>,
    fields: FfiSlice<'ast, ItemField<'ast>>,
}

super::impl_item_data!(UnionItem, Union);

impl<'ast> UnionItem<'ast> {
    pub fn generics(&self) -> &GenericParams<'ast> {
        &self.generics
    }

    pub fn fields(&self) -> &[ItemField<'ast>] {
        self.fields.get()
    }
}

#[cfg(feature = "driver-api")]
impl<'ast> UnionItem<'ast> {
    pub fn new(data: CommonItemData<'ast>, generics: GenericParams<'ast>, fields: &'ast [ItemField<'ast>]) -> Self {
        Self {
            data,
            generics,
            fields: fields.into(),
        }
    }
}

/// An enum item like:
///
/// ```
/// #[repr(u32)]
/// pub enum Foo {
///     Elem1,
///     Elem2 = 1,
///     Elem3(u32),
///     Elem4 {
///         field_1: u32,
///         field_2: u32,
///     }
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct EnumItem<'ast> {
    data: CommonItemData<'ast>,
    generics: GenericParams<'ast>,
    variants: FfiSlice<'ast, EnumVariant<'ast>>,
}

super::impl_item_data!(EnumItem, Enum);

impl<'ast> EnumItem<'ast> {
    pub fn generics(&self) -> &GenericParams<'ast> {
        &self.generics
    }

    pub fn variants(&self) -> &[EnumVariant<'ast>] {
        self.variants.get()
    }
}

#[cfg(feature = "driver-api")]
impl<'ast> EnumItem<'ast> {
    pub fn new(data: CommonItemData<'ast>, generics: GenericParams<'ast>, variants: &'ast [EnumVariant<'ast>]) -> Self {
        Self {
            data,
            generics,
            variants: variants.into(),
        }
    }
}

#[repr(C)]
#[derive(Debug)]
pub struct EnumVariant<'ast> {
    id: VariantId,
    ident: SymbolId,
    span: SpanId,
    kind: AdtKind<'ast>,
    discriminant: FfiOption<ConstExpr<'ast>>,
}

impl<'ast> EnumVariant<'ast> {
    pub fn id(&self) -> VariantId {
        self.id
    }

    pub fn ident(&self) -> &str {
        with_cx(self, |cx| cx.symbol_str(self.ident))
    }

    // FIXME(xFrednet): Add `fn attrs() -> ??? {}`, see rust-marker/marker#51

    /// Returns `true` if this is a unit variant like:
    ///
    /// ```
    /// pub enum Foo {
    ///     Bar,
    /// }
    /// ```
    pub fn is_unit_variant(&self) -> bool {
        matches!(self.kind, AdtKind::Unit)
    }

    /// Returns `true` if this is a tuple variant like:
    ///
    /// ```
    /// pub enum Foo {
    ///     Bar(u32, u32)
    /// }
    /// ```
    pub fn is_tuple_variant(&self) -> bool {
        matches!(self.kind, AdtKind::Tuple(..))
    }

    /// Returns `true` if this is an variant with fields like:
    ///
    /// ```
    /// pub enum Foo {
    ///    Bar {
    ///        data: i32,
    ///        buffer: u32,
    ///    }
    /// }
    /// ```
    pub fn is_field_variant(&self) -> bool {
        matches!(self.kind, AdtKind::Field(..))
    }

    pub fn fields(&self) -> &[ItemField<'ast>] {
        match &self.kind {
            AdtKind::Unit => &[],
            AdtKind::Tuple(fields) | AdtKind::Field(fields) => fields.get(),
        }
    }

    /// The discriminant of this variant, if one has been defined
    pub fn discriminant(&self) -> Option<&ConstExpr<'ast>> {
        self.discriminant.get()
    }
}

impl<'ast> HasSpan<'ast> for EnumVariant<'ast> {
    fn span(&self) -> &Span<'ast> {
        with_cx(self, |cx| cx.span(self.span))
    }
}

crate::common::impl_identifiable_for!(EnumVariant<'ast>);

#[cfg(feature = "driver-api")]
impl<'ast> EnumVariant<'ast> {
    pub fn new(
        id: VariantId,
        ident: SymbolId,
        span: SpanId,
        kind: AdtKind<'ast>,
        discriminant: Option<ConstExpr<'ast>>,
    ) -> Self {
        Self {
            id,
            ident,
            span,
            kind,
            discriminant: discriminant.into(),
        }
    }
}

/// A struct item like:
///
/// ```
/// pub struct Foo;
/// pub struct Bar(u32, u32);
/// pub struct Baz {
///     field_1: u32,
///     field_2: u32,
/// }
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct StructItem<'ast> {
    data: CommonItemData<'ast>,
    generics: GenericParams<'ast>,
    kind: AdtKind<'ast>,
}

super::impl_item_data!(StructItem, Struct);

impl<'ast> StructItem<'ast> {
    pub fn generics(&self) -> &GenericParams<'ast> {
        &self.generics
    }

    /// Returns `true` if this is a unit struct like:
    ///
    /// ```
    /// struct Name1;
    /// struct Name2 {};
    /// ```
    pub fn is_unit_struct(&self) -> bool {
        matches!(self.kind, AdtKind::Unit)
    }

    /// Returns `true` if this is a tuple struct like:
    ///
    /// ```
    /// struct Name(u32, u64);
    /// ```
    pub fn is_tuple_struct(&self) -> bool {
        matches!(self.kind, AdtKind::Tuple(..))
    }

    /// Returns `true` if this is a field struct like:
    ///
    /// ```
    /// struct Name {
    ///     field: u32,
    /// };
    /// ```
    pub fn is_field_struct(&self) -> bool {
        matches!(self.kind, AdtKind::Field(..))
    }

    pub fn fields(&self) -> &[ItemField<'ast>] {
        match &self.kind {
            AdtKind::Unit => &[],
            AdtKind::Tuple(fields) | AdtKind::Field(fields) => fields.get(),
        }
    }
}

#[cfg(feature = "driver-api")]
impl<'ast> StructItem<'ast> {
    pub fn new(data: CommonItemData<'ast>, generics: GenericParams<'ast>, kind: AdtKind<'ast>) -> Self {
        Self { data, generics, kind }
    }
}

#[derive(Debug)]
#[allow(clippy::exhaustive_enums)]
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
enum AdtKind<'ast> {
    Unit,
    Tuple(FfiSlice<'ast, ItemField<'ast>>),
    Field(FfiSlice<'ast, ItemField<'ast>>),
}

impl<'ast> AdtKind<'ast> {
    // The slice lifetime here is explicitly denoted, as this is used by the
    // driver for convenience and is not part of the public API
    pub fn fields(self) -> &'ast [ItemField<'ast>] {
        match self {
            AdtKind::Tuple(fields) | AdtKind::Field(fields) => fields.get(),
            AdtKind::Unit => &[],
        }
    }
}

/// A single field inside a [`StructItem`] or [`UnionItem`] with an identifier
/// type and span.
#[repr(C)]
#[derive(Debug)]
pub struct ItemField<'ast> {
    id: FieldId,
    vis: Visibility<'ast>,
    ident: SymbolId,
    ty: TyKind<'ast>,
    span: SpanId,
}

impl<'ast> ItemField<'ast> {
    pub fn id(&self) -> FieldId {
        self.id
    }

    /// The [`Visibility`] of this item.
    pub fn visibility(&self) -> &Visibility<'ast> {
        &self.vis
    }

    pub fn ident(&self) -> &str {
        with_cx(self, |cx| cx.symbol_str(self.ident))
    }

    pub fn ty(&self) -> TyKind<'ast> {
        self.ty
    }

    // FIXME(xFrednet): Add `fn attrs() -> ??? {}`, see rust-marker/marker#51
}

impl<'ast> HasSpan<'ast> for ItemField<'ast> {
    fn span(&self) -> &Span<'ast> {
        with_cx(self, |cx| cx.span(self.span))
    }
}

crate::common::impl_identifiable_for!(ItemField<'ast>);

#[cfg(feature = "driver-api")]
impl<'ast> ItemField<'ast> {
    pub fn new(id: FieldId, vis: Visibility<'ast>, ident: SymbolId, ty: TyKind<'ast>, span: SpanId) -> Self {
        Self {
            id,
            vis,
            ident,
            ty,
            span,
        }
    }
}