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
use std::cell::OnceCell;
use std::{fmt, hash::BuildHasherDefault, rc::Rc};

use debris_common::Ident;
use indexmap::IndexMap;
use itertools::Itertools;
use rustc_hash::FxHasher;

use crate::{
    class::{Class, ClassKind, ClassRef},
    impl_class,
    memory::MemoryLayout,
    type_context::TypeContext,
    ObjectPayload, ObjectProperties, Type,
};

pub type StructRef = Rc<Struct>;

#[derive(PartialEq, Eq)]
pub struct Struct {
    pub ident: Ident,
    /// The fields are stored in an indexmap so that the user defined
    /// order is preserved. Uses the fast [FxHasher]
    pub fields: IndexMap<Ident, ClassRef, BuildHasherDefault<FxHasher>>,
    /// Namespace is in a once cell, because the struct must be created before
    /// its members can be added
    pub namespace: OnceCell<ObjectProperties>,
}

impl Struct {
    pub fn diverges(&self) -> bool {
        self.fields.values().any(|value| value.diverges())
    }

    pub fn runtime_encodable(&self) -> bool {
        self.fields.values().all(|value| {
            value
                .kind
                .as_value()
                .expect("Field classes of a struct can never be values")
                .runtime_encodable()
        })
    }

    pub fn comptime_encodable(&self) -> bool {
        self.fields
            .values()
            .any(|value| value.kind.comptime_encodable())
    }
}

impl fmt::Debug for Struct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Struct")
            .field("ident", &self.ident)
            .field("fields", &self.fields)
            .field(
                "namespace",
                &self
                    .namespace
                    .get()
                    .map(|namespace| namespace.keys().collect_vec()),
            )
            .finish()
    }
}

/// Stores a user defined struct
#[derive(Debug, PartialEq, Eq)]
pub struct ObjStruct {
    pub struct_ref: StructRef,
}

impl_class! {ObjStruct, Type::Struct, {}}

impl ObjStruct {
    pub fn new(strukt: StructRef) -> Self {
        ObjStruct { struct_ref: strukt }
    }
}

impl ObjectPayload for ObjStruct {
    fn memory_layout(&self) -> &MemoryLayout {
        &MemoryLayout::Unsized
    }

    fn create_class(&self, _: &TypeContext) -> ClassRef {
        let kind = ClassKind::Struct(self.struct_ref.clone());
        let class = Class::new_empty(kind);
        ClassRef::new(class)
    }

    fn get_property(&self, _ctx: &TypeContext, ident: &Ident) -> Option<crate::ObjectRef> {
        self.namespace
            .get()
            .expect("Evaluated struct property during struct initialization")
            .get(ident)
            .cloned()
    }
}

impl std::ops::Deref for ObjStruct {
    type Target = StructRef;

    fn deref(&self) -> &Self::Target {
        &self.struct_ref
    }
}

impl fmt::Display for Struct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "<struct {}>", self.ident)
    }
}

impl fmt::Display for ObjStruct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.struct_ref, f)
    }
}