mago_codex/ttype/
combination.rs1use std::collections::BTreeMap;
2
3use foldhash::HashSet;
4use ordered_float::OrderedFloat;
5
6use mago_word::Word;
7use mago_word::WordMap;
8use mago_word::WordSet;
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_ALWAYS_FILLED: CombinationFlags = CombinationFlags(1 << 2);
24 pub const KEYED_ARRAY_SOMETIMES_FILLED: CombinationFlags = CombinationFlags(1 << 3);
25 pub const KEYED_ARRAY_ALWAYS_FILLED: CombinationFlags = CombinationFlags(1 << 4);
26 pub const HAS_EMPTY_ARRAY: CombinationFlags = CombinationFlags(1 << 5);
27 pub const HAS_KEYED_ARRAY: CombinationFlags = CombinationFlags(1 << 6);
28 pub const GENERIC_MIXED: CombinationFlags = CombinationFlags(1 << 7);
29 pub const HAS_MIXED: CombinationFlags = CombinationFlags(1 << 8);
30 pub const RESOURCE: CombinationFlags = CombinationFlags(1 << 9);
31 pub const OPEN_RESOURCE: CombinationFlags = CombinationFlags(1 << 10);
32 pub const CLOSED_RESOURCE: CombinationFlags = CombinationFlags(1 << 11);
33 const FALSY_MIXED_SET: CombinationFlags = CombinationFlags(1 << 12);
35 const FALSY_MIXED_VALUE: CombinationFlags = CombinationFlags(1 << 13);
36 const TRUTHY_MIXED_SET: CombinationFlags = CombinationFlags(1 << 14);
37 const TRUTHY_MIXED_VALUE: CombinationFlags = CombinationFlags(1 << 15);
38 const NONNULL_MIXED_SET: CombinationFlags = CombinationFlags(1 << 16);
39 const NONNULL_MIXED_VALUE: CombinationFlags = CombinationFlags(1 << 17);
40 const MIXED_FROM_LOOP_ISSET_SET: CombinationFlags = CombinationFlags(1 << 18);
41 const MIXED_FROM_LOOP_ISSET_VALUE: CombinationFlags = CombinationFlags(1 << 19);
42 pub const KEYED_ARRAY_KNOWN_NON_LIST: CombinationFlags = CombinationFlags(1 << 20);
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 #[must_use]
58 pub const fn contains(self, other: CombinationFlags) -> bool {
59 (self.0 & other.0) == other.0
60 }
61
62 #[inline]
63 #[must_use]
64 pub const fn intersects(self, other: CombinationFlags) -> bool {
65 (self.0 & other.0) != 0
66 }
67
68 #[inline]
70 #[must_use]
71 pub fn get_tristate(self, set_bit: CombinationFlags, value_bit: CombinationFlags) -> Option<bool> {
72 if self.contains(set_bit) { Some(self.contains(value_bit)) } else { None }
73 }
74
75 #[inline]
77 pub fn set_tristate(&mut self, set_bit: CombinationFlags, value_bit: CombinationFlags, value: Option<bool>) {
78 match value {
79 None => {
80 self.remove(set_bit);
81 self.remove(value_bit);
82 }
83 Some(false) => {
84 self.insert(set_bit);
85 self.remove(value_bit);
86 }
87 Some(true) => {
88 self.insert(set_bit);
89 self.insert(value_bit);
90 }
91 }
92 }
93
94 #[inline]
95 #[must_use]
96 pub fn falsy_mixed(self) -> Option<bool> {
97 self.get_tristate(Self::FALSY_MIXED_SET, Self::FALSY_MIXED_VALUE)
98 }
99
100 #[inline]
101 pub fn set_falsy_mixed(&mut self, value: Option<bool>) {
102 self.set_tristate(Self::FALSY_MIXED_SET, Self::FALSY_MIXED_VALUE, value);
103 }
104
105 #[inline]
106 #[must_use]
107 pub fn truthy_mixed(self) -> Option<bool> {
108 self.get_tristate(Self::TRUTHY_MIXED_SET, Self::TRUTHY_MIXED_VALUE)
109 }
110
111 #[inline]
112 pub fn set_truthy_mixed(&mut self, value: Option<bool>) {
113 self.set_tristate(Self::TRUTHY_MIXED_SET, Self::TRUTHY_MIXED_VALUE, value);
114 }
115
116 #[inline]
117 #[must_use]
118 pub fn nonnull_mixed(self) -> Option<bool> {
119 self.get_tristate(Self::NONNULL_MIXED_SET, Self::NONNULL_MIXED_VALUE)
120 }
121
122 #[inline]
123 pub fn set_nonnull_mixed(&mut self, value: Option<bool>) {
124 self.set_tristate(Self::NONNULL_MIXED_SET, Self::NONNULL_MIXED_VALUE, value);
125 }
126
127 #[inline]
128 #[must_use]
129 pub fn mixed_from_loop_isset(self) -> Option<bool> {
130 self.get_tristate(Self::MIXED_FROM_LOOP_ISSET_SET, Self::MIXED_FROM_LOOP_ISSET_VALUE)
131 }
132
133 #[inline]
134 pub fn set_mixed_from_loop_isset(&mut self, value: Option<bool>) {
135 self.set_tristate(Self::MIXED_FROM_LOOP_ISSET_SET, Self::MIXED_FROM_LOOP_ISSET_VALUE, value);
136 }
137}
138
139#[derive(Debug)]
140pub struct TypeCombination {
141 pub flags: CombinationFlags,
142 pub value_types: WordMap<TAtomic>,
143 pub callable_collisions: WordMap<Vec<TAtomic>>,
144 pub enum_names: HashSet<(Word, Option<Word>)>,
145 pub object_type_params: WordMap<(Word, Vec<TUnion>)>,
146 pub object_static: WordMap<bool>,
147 pub keyed_array_entries: BTreeMap<ArrayKey, (bool, TUnion)>,
148 pub list_array_entries: BTreeMap<usize, (bool, TUnion)>,
149 pub keyed_array_parameters: Option<(TUnion, TUnion)>,
150 pub list_array_parameter: Option<TUnion>,
151 pub sealed_arrays: Vec<TArray>,
152 pub sealed_keyed_budget_exhausted: bool,
153 pub integers: Vec<TInteger>,
154 pub literal_strings: WordSet,
155 pub literal_floats: Vec<OrderedFloat<f64>>,
156 pub derived_types: HashSet<TDerived>,
157}
158
159impl Default for TypeCombination {
160 fn default() -> Self {
161 Self::new()
162 }
163}
164
165impl TypeCombination {
166 #[must_use]
167 pub fn new() -> Self {
168 let flags = CombinationFlags::LIST_ARRAY_ALWAYS_FILLED | CombinationFlags::KEYED_ARRAY_ALWAYS_FILLED;
169
170 Self {
171 flags,
172 value_types: WordMap::default(),
173 callable_collisions: WordMap::default(),
174 object_type_params: WordMap::default(),
175 object_static: WordMap::default(),
176 keyed_array_entries: BTreeMap::new(),
177 list_array_entries: BTreeMap::new(),
178 keyed_array_parameters: None,
179 list_array_parameter: None,
180 sealed_arrays: Vec::new(),
181 sealed_keyed_budget_exhausted: false,
182 literal_strings: WordSet::default(),
183 integers: Vec::new(),
184 literal_floats: Vec::new(),
185 enum_names: HashSet::default(),
186 derived_types: HashSet::default(),
187 }
188 }
189
190 #[inline]
191 #[must_use]
192 pub fn is_simple(&self) -> bool {
193 if self.value_types.len() == 1
194 && self.callable_collisions.is_empty()
195 && self.sealed_arrays.is_empty()
196 && !self.flags.contains(CombinationFlags::HAS_KEYED_ARRAY)
197 && !self.flags.contains(CombinationFlags::HAS_EMPTY_ARRAY)
198 && !self.flags.intersects(
199 CombinationFlags::RESOURCE | CombinationFlags::OPEN_RESOURCE | CombinationFlags::CLOSED_RESOURCE,
200 )
201 && self.keyed_array_parameters.is_none()
202 && self.list_array_parameter.is_none()
203 {
204 return self.object_type_params.is_empty()
205 && self.enum_names.is_empty()
206 && self.literal_strings.is_empty()
207 && self.literal_floats.is_empty()
208 && self.integers.is_empty()
209 && self.derived_types.is_empty();
210 }
211
212 false
213 }
214}
215
216impl std::ops::BitOr for CombinationFlags {
217 type Output = Self;
218
219 #[inline]
220 fn bitor(self, rhs: Self) -> Self::Output {
221 CombinationFlags(self.0 | rhs.0)
222 }
223}