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
335
336
337
338
339
340
use super::*;

use crate::sabi_types::Constructor;

/// The layout of a field.
#[repr(C)]
#[derive(Debug, Copy, Clone, StableAbi)]
#[sabi(unsafe_sabi_opaque_fields)]
pub struct TLField {
    /// The field's name.
    name: RStr<'static>,
    /// Which lifetimes in the struct are referenced in the field type.
    lifetime_indices: LifetimeArrayOrSlice<'static>,
    /// The layout of the field's type.
    ///
    /// This is a function pointer to avoid infinite recursion,
    /// if you have a `&'static TypeLayout`s with the same address as one of its parent type,
    /// you've encountered a cycle.
    layout: Constructor<&'static TypeLayout>,

    /// The function pointer types within the field.
    function_range: TLFunctionSlice,

    /// Whether this field is only a function pointer.
    is_function: bool,

    /// How this field is accessed.
    field_accessor: FieldAccessor,
}

///////////////////////////

impl TLField {
    /// Constructs a field which does not contain function pointers,or lifetime indices.
    pub const fn new(
        name: RStr<'static>,
        layout: extern "C" fn() -> &'static TypeLayout,
        vars: &'static SharedVars,
    ) -> Self {
        Self {
            name,
            lifetime_indices: LifetimeArrayOrSlice::EMPTY,
            layout: Constructor(layout),
            function_range: TLFunctionSlice::empty(vars),
            is_function: false,
            field_accessor: FieldAccessor::Direct,
        }
    }

    /// Gets a printable version of the field type.
    pub fn full_type(&self) -> FmtFullType {
        self.layout.get().full_type()
    }

    /// Gets the name of the field
    pub fn name(&self) -> &'static str {
        self.name.as_str()
    }

    /// Gets the lifetimes that the field references.
    pub const fn lifetime_indices(&self) -> LifetimeArrayOrSlice<'static> {
        self.lifetime_indices
    }
    /// Gets the layout of the field type
    pub fn layout(&self) -> &'static TypeLayout {
        self.layout.get()
    }
    /// Gets all the function pointer types in the field.
    pub const fn function_range(&self) -> TLFunctionSlice {
        self.function_range
    }
    /// Gets whether the field is itself a function pointer.
    pub const fn is_function(&self) -> bool {
        self.is_function
    }
    /// Gets the `FieldAccessor` for the type,
    /// which describes whether a field is accessible,and how it is accessed.
    pub const fn field_accessor(&self) -> FieldAccessor {
        self.field_accessor
    }

    /// Used for calling recursive methods,
    /// so as to avoid infinite recursion in types that reference themselves(even indirectly).
    fn recursive<F, U>(self, f: F) -> U
    where
        F: FnOnce(usize, TLFieldShallow) -> U,
    {
        let mut already_recursed = false;
        let mut recursion_depth = !0;
        let mut visited_nodes = !0;

        ALREADY_RECURSED.with(|state| {
            let mut state = state.borrow_mut();
            recursion_depth = state.recursion_depth;
            visited_nodes = state.visited_nodes;
            state.recursion_depth += 1;
            state.visited_nodes += 1;
            already_recursed = state.visited.replace(self.layout.get()).is_some();
        });

        let _guard = if visited_nodes == 0 {
            Some(ResetRecursion)
        } else {
            None
        };

        let field = TLFieldShallow::new(self, !already_recursed);
        let res = f(recursion_depth, field);

        ALREADY_RECURSED.with(|state| {
            let mut state = state.borrow_mut();
            state.recursion_depth -= 1;
        });

        res
    }
}

impl Eq for TLField {}

impl PartialEq for TLField {
    fn eq(&self, other: &Self) -> bool {
        self.recursive(|_, this| {
            let r = TLFieldShallow::new(*other, this.layout.is_some());
            this == r
        })
    }
}

impl Display for TLField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let layout = self.layout.get();
        let (package, version) = layout.item_info().package_and_version();
        writeln!(
            f,
            "field_name:{name}\n\
             type:{ty}\n\
             size:{size} align:{align}\n\
             package:'{package}' version:'{version}'",
            name = self.name,
            ty = layout.full_type(),
            size = layout.size(),
            align = layout.alignment(),
            package = package,
            version = version,
        )?;

        if !self.function_range.is_empty() {
            writeln!(f, "fn pointer(s):")?;
            for func in self.function_range.iter() {
                writeln!(f, "{}", func.to_string().left_padder(4))?;
            }
        }

        if !self.lifetime_indices.is_empty() {
            writeln!(f, "lifetime indices:{:?}", self.lifetime_indices)?;
        }

        Ok(())
    }
}

///////////////////////////

struct ResetRecursion;

impl Drop for ResetRecursion {
    fn drop(&mut self) {
        ALREADY_RECURSED.with(|state| {
            let mut state = state.borrow_mut();
            state.recursion_depth = 0;
            state.visited_nodes = 0;
            state.visited.clear();
        });
    }
}

struct RecursionState {
    recursion_depth: usize,
    visited_nodes: u64,
    visited: HashSet<*const TypeLayout>,
}

thread_local! {
    static ALREADY_RECURSED: RefCell<RecursionState> = RefCell::new(RecursionState{
        recursion_depth:0,
        visited_nodes:0,
        visited: HashSet::default(),
    });
}

////////////////////////////////////

#[derive(Debug, Copy, Clone, PartialEq)]
struct TLFieldShallow {
    name: RStr<'static>,

    full_type: FmtFullType,

    lifetime_indices: LifetimeArrayOrSlice<'static>,

    /// This is None if it already printed that TypeLayout
    layout: Option<&'static TypeLayout>,

    function_range: TLFunctionSlice,

    is_function: bool,

    field_accessor: FieldAccessor,
}

impl TLFieldShallow {
    fn new(field: TLField, include_type_layout: bool) -> Self {
        let layout = field.layout.get();
        TLFieldShallow {
            name: field.name,
            lifetime_indices: field.lifetime_indices,
            layout: if include_type_layout {
                Some(layout)
            } else {
                None
            },
            full_type: layout.full_type(),

            function_range: field.function_range,
            is_function: field.is_function,
            field_accessor: field.field_accessor,
        }
    }
}

////////////////////////////////////

abi_stable_shared::declare_comp_tl_field! {
    attrs=[
        derive(StableAbi),
        sabi(unsafe_sabi_opaque_fields),
    ]
}

impl CompTLField {
    /// Gets the name of the field from `SharedVars`'s string slice.
    pub fn name(&self, strings: &'static str) -> &'static str {
        &strings[self.name_start_len().to_range()]
    }

    /// Gets the name of the field from `SharedVars`'s slice of lifetime indices.
    pub fn lifetime_indices(
        &self,
        indices: &'static [LifetimeIndexPair],
    ) -> LifetimeArrayOrSlice<'static> {
        let comp = LifetimeRange::from_u21(self.lifetime_indices_bits());
        comp.slicing(indices)
    }

    /// Gets the `FieldAccessor` for the type from `SharedVars`'s string slice,
    /// which describes whether a field is accessible,and how it is accessed..
    pub fn field_accessor(&self, strings: &'static str) -> FieldAccessor {
        let name_end = self.name_start_len().end();
        let comp = CompFieldAccessor::from_u3((self.bits0 >> Self::FIELD_ACCESSOR_OFFSET) as u8);
        let accessor_payload = if comp.requires_payload() {
            strings[name_end..].split(';').next().unwrap_or("")
        } else {
            ""
        };
        comp.expand(accessor_payload)
            .unwrap_or(FieldAccessor::Opaque)
    }

    /// Gets the name of the field from `SharedVars`'s slice of type layouts.
    pub const fn type_layout(
        &self,
        type_layouts: &'static [extern "C" fn() -> &'static TypeLayout],
    ) -> extern "C" fn() -> &'static TypeLayout {
        type_layouts[self.type_layout_index()]
    }

    /// Expands this CompTLField into a TLField.
    pub fn expand(
        &self,
        field_index: usize,
        functions: Option<&'static TLFunctions>,
        vars: &'static SharedVars,
    ) -> TLField {
        let strings = vars.strings();
        let function_range = TLFunctionSlice::for_field(field_index, functions, vars);

        TLField {
            name: self.name(strings).into(),
            lifetime_indices: self.lifetime_indices(vars.lifetime_indices()),
            layout: Constructor(self.type_layout(vars.type_layouts())),
            function_range,
            is_function: self.is_function(),
            field_accessor: self.field_accessor(strings),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{abi_stability::stable_abi_trait::get_type_layout, std_types::RString};

    #[test]
    fn roundtrip() {
        const UNIT_CTOR: extern "C" fn() -> &'static TypeLayout = get_type_layout::<()>;
        const U32_CTOR: extern "C" fn() -> &'static TypeLayout = get_type_layout::<u32>;
        const RSTRING_CTOR: extern "C" fn() -> &'static TypeLayout = get_type_layout::<RString>;

        const MONO_VARS: &MonoSharedVars = &MonoSharedVars::new(rstr!("foo;bar; baz; "), rslice![]);

        const VARS: &SharedVars = &SharedVars::new(
            MONO_VARS,
            rslice![UNIT_CTOR, U32_CTOR, RSTRING_CTOR],
            rslice![],
        );

        let vars = VARS;

        let mut arr = [LifetimeIndex::NONE; 5];
        arr[0] = LifetimeIndex::STATIC;
        let lifetime_range = LifetimeRange::from_array(arr);

        let field = CompTLField::new(
            StartLen::new(9, 3),
            lifetime_range,
            CompFieldAccessor::DIRECT,
            TypeLayoutIndex::from_u10(2),
            false,
        );

        assert_eq!(field.name(vars.strings()), "baz",);
        assert_eq!(
            field.lifetime_indices(vars.lifetime_indices()),
            lifetime_range.slicing(vars.lifetime_indices()),
        );
        assert_eq!(field.type_layout(vars.type_layouts()), RSTRING_CTOR,);
        assert_eq!(field.field_accessor(vars.strings()), FieldAccessor::Direct,);
    }
}