Skip to main content

mago_codex/ttype/
combination.rs

1use std::collections::BTreeMap;
2
3use foldhash::HashSet;
4use ordered_float::OrderedFloat;
5
6use mago_atom::Atom;
7use mago_atom::AtomMap;
8use mago_atom::AtomSet;
9
10use crate::ttype::atomic::TAtomic;
11use crate::ttype::atomic::array::TArray;
12use crate::ttype::atomic::array::key::ArrayKey;
13use crate::ttype::atomic::derived::TDerived;
14use crate::ttype::atomic::scalar::int::TInteger;
15use crate::ttype::union::TUnion;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
18#[repr(transparent)]
19pub struct CombinationFlags(u32);
20
21impl CombinationFlags {
22    pub const HAS_OBJECT_TOP_TYPE: CombinationFlags = CombinationFlags(1 << 0);
23    pub const LIST_ARRAY_SOMETIMES_FILLED: CombinationFlags = CombinationFlags(1 << 1);
24    pub const LIST_ARRAY_ALWAYS_FILLED: CombinationFlags = CombinationFlags(1 << 2);
25    pub const KEYED_ARRAY_SOMETIMES_FILLED: CombinationFlags = CombinationFlags(1 << 3);
26    pub const KEYED_ARRAY_ALWAYS_FILLED: CombinationFlags = CombinationFlags(1 << 4);
27    pub const HAS_EMPTY_ARRAY: CombinationFlags = CombinationFlags(1 << 5);
28    pub const HAS_KEYED_ARRAY: CombinationFlags = CombinationFlags(1 << 6);
29    pub const GENERIC_MIXED: CombinationFlags = CombinationFlags(1 << 7);
30    pub const HAS_MIXED: CombinationFlags = CombinationFlags(1 << 8);
31    pub const RESOURCE: CombinationFlags = CombinationFlags(1 << 9);
32    pub const OPEN_RESOURCE: CombinationFlags = CombinationFlags(1 << 10);
33    pub const CLOSED_RESOURCE: CombinationFlags = CombinationFlags(1 << 11);
34    // Tristate encoding: 2 bits each (None=neither set, Some(false)=SET only, Some(true)=SET+VALUE)
35    const FALSY_MIXED_SET: CombinationFlags = CombinationFlags(1 << 12);
36    const FALSY_MIXED_VALUE: CombinationFlags = CombinationFlags(1 << 13);
37    const TRUTHY_MIXED_SET: CombinationFlags = CombinationFlags(1 << 14);
38    const TRUTHY_MIXED_VALUE: CombinationFlags = CombinationFlags(1 << 15);
39    const NONNULL_MIXED_SET: CombinationFlags = CombinationFlags(1 << 16);
40    const NONNULL_MIXED_VALUE: CombinationFlags = CombinationFlags(1 << 17);
41    const MIXED_FROM_LOOP_ISSET_SET: CombinationFlags = CombinationFlags(1 << 18);
42    const MIXED_FROM_LOOP_ISSET_VALUE: CombinationFlags = CombinationFlags(1 << 19);
43}
44
45impl CombinationFlags {
46    #[inline]
47    pub const fn insert(&mut self, other: CombinationFlags) {
48        self.0 |= other.0;
49    }
50
51    #[inline]
52    pub const fn remove(&mut self, other: CombinationFlags) {
53        self.0 &= !other.0;
54    }
55
56    #[inline]
57    pub const fn contains(self, other: CombinationFlags) -> bool {
58        (self.0 & other.0) == other.0
59    }
60
61    #[inline]
62    pub const fn intersects(self, other: CombinationFlags) -> bool {
63        (self.0 & other.0) != 0
64    }
65
66    /// Get a tristate value (Option<bool>) from two bits.
67    #[inline]
68    #[must_use]
69    pub fn get_tristate(self, set_bit: CombinationFlags, value_bit: CombinationFlags) -> Option<bool> {
70        if self.contains(set_bit) { Some(self.contains(value_bit)) } else { None }
71    }
72
73    /// Set a tristate value (Option<bool>) using two bits.
74    #[inline]
75    pub fn set_tristate(&mut self, set_bit: CombinationFlags, value_bit: CombinationFlags, value: Option<bool>) {
76        match value {
77            None => {
78                self.remove(set_bit);
79                self.remove(value_bit);
80            }
81            Some(false) => {
82                self.insert(set_bit);
83                self.remove(value_bit);
84            }
85            Some(true) => {
86                self.insert(set_bit);
87                self.insert(value_bit);
88            }
89        }
90    }
91
92    #[inline]
93    #[must_use]
94    pub fn falsy_mixed(self) -> Option<bool> {
95        self.get_tristate(Self::FALSY_MIXED_SET, Self::FALSY_MIXED_VALUE)
96    }
97
98    #[inline]
99    pub fn set_falsy_mixed(&mut self, value: Option<bool>) {
100        self.set_tristate(Self::FALSY_MIXED_SET, Self::FALSY_MIXED_VALUE, value);
101    }
102
103    #[inline]
104    #[must_use]
105    pub fn truthy_mixed(self) -> Option<bool> {
106        self.get_tristate(Self::TRUTHY_MIXED_SET, Self::TRUTHY_MIXED_VALUE)
107    }
108
109    #[inline]
110    pub fn set_truthy_mixed(&mut self, value: Option<bool>) {
111        self.set_tristate(Self::TRUTHY_MIXED_SET, Self::TRUTHY_MIXED_VALUE, value);
112    }
113
114    #[inline]
115    #[must_use]
116    pub fn nonnull_mixed(self) -> Option<bool> {
117        self.get_tristate(Self::NONNULL_MIXED_SET, Self::NONNULL_MIXED_VALUE)
118    }
119
120    #[inline]
121    pub fn set_nonnull_mixed(&mut self, value: Option<bool>) {
122        self.set_tristate(Self::NONNULL_MIXED_SET, Self::NONNULL_MIXED_VALUE, value);
123    }
124
125    #[inline]
126    #[must_use]
127    pub fn mixed_from_loop_isset(self) -> Option<bool> {
128        self.get_tristate(Self::MIXED_FROM_LOOP_ISSET_SET, Self::MIXED_FROM_LOOP_ISSET_VALUE)
129    }
130
131    #[inline]
132    pub fn set_mixed_from_loop_isset(&mut self, value: Option<bool>) {
133        self.set_tristate(Self::MIXED_FROM_LOOP_ISSET_SET, Self::MIXED_FROM_LOOP_ISSET_VALUE, value);
134    }
135}
136
137#[derive(Debug)]
138pub struct TypeCombination {
139    pub flags: CombinationFlags,
140    pub value_types: AtomMap<TAtomic>,
141    pub enum_names: HashSet<(Atom, Option<Atom>)>,
142    pub object_type_params: AtomMap<(Atom, Vec<TUnion>)>,
143    pub object_static: AtomMap<bool>,
144    pub list_array_counts: Option<HashSet<usize>>,
145    pub keyed_array_entries: BTreeMap<ArrayKey, (bool, TUnion)>,
146    pub list_array_entries: BTreeMap<usize, (bool, TUnion)>,
147    pub keyed_array_parameters: Option<(TUnion, TUnion)>,
148    pub list_array_parameter: Option<TUnion>,
149    pub sealed_arrays: Vec<TArray>,
150    pub integers: Vec<TInteger>,
151    pub literal_strings: AtomSet,
152    pub literal_floats: Vec<OrderedFloat<f64>>,
153    pub class_string_types: AtomMap<TAtomic>,
154    pub derived_types: HashSet<TDerived>,
155}
156
157impl Default for TypeCombination {
158    fn default() -> Self {
159        Self::new()
160    }
161}
162
163impl TypeCombination {
164    #[must_use]
165    pub fn new() -> Self {
166        let flags = CombinationFlags::LIST_ARRAY_ALWAYS_FILLED | CombinationFlags::KEYED_ARRAY_ALWAYS_FILLED;
167
168        Self {
169            flags,
170            value_types: AtomMap::default(),
171            object_type_params: AtomMap::default(),
172            object_static: AtomMap::default(),
173            list_array_counts: Some(HashSet::default()),
174            keyed_array_entries: BTreeMap::new(),
175            list_array_entries: BTreeMap::new(),
176            keyed_array_parameters: None,
177            list_array_parameter: None,
178            sealed_arrays: Vec::new(),
179            literal_strings: AtomSet::default(),
180            integers: Vec::new(),
181            literal_floats: Vec::new(),
182            class_string_types: AtomMap::default(),
183            enum_names: HashSet::default(),
184            derived_types: HashSet::default(),
185        }
186    }
187
188    #[inline]
189    #[must_use]
190    pub fn is_simple(&self) -> bool {
191        if self.value_types.len() == 1
192            && self.sealed_arrays.is_empty()
193            && !self.flags.contains(CombinationFlags::HAS_KEYED_ARRAY)
194            && !self.flags.contains(CombinationFlags::HAS_EMPTY_ARRAY)
195            && !self.flags.intersects(
196                CombinationFlags::RESOURCE | CombinationFlags::OPEN_RESOURCE | CombinationFlags::CLOSED_RESOURCE,
197            )
198            && self.keyed_array_parameters.is_none()
199            && self.list_array_parameter.is_none()
200        {
201            return self.object_type_params.is_empty()
202                && self.enum_names.is_empty()
203                && self.literal_strings.is_empty()
204                && self.literal_floats.is_empty()
205                && self.class_string_types.is_empty()
206                && self.integers.is_empty()
207                && self.derived_types.is_empty();
208        }
209
210        false
211    }
212}
213
214impl std::ops::BitOr for CombinationFlags {
215    type Output = Self;
216
217    #[inline]
218    fn bitor(self, rhs: Self) -> Self::Output {
219        CombinationFlags(self.0 | rhs.0)
220    }
221}
222
223impl std::ops::BitAnd for CombinationFlags {
224    type Output = Self;
225
226    #[inline]
227    fn bitand(self, rhs: Self) -> Self::Output {
228        CombinationFlags(self.0 & rhs.0)
229    }
230}
231
232impl std::ops::BitXor for CombinationFlags {
233    type Output = Self;
234
235    #[inline]
236    fn bitxor(self, rhs: Self) -> Self::Output {
237        CombinationFlags(self.0 ^ rhs.0)
238    }
239}
240
241impl std::ops::Not for CombinationFlags {
242    type Output = Self;
243
244    #[inline]
245    fn not(self) -> Self::Output {
246        CombinationFlags(!self.0)
247    }
248}