1use std::hash::Hash;
2use std::sync::Arc;
3
4use indexmap::IndexMap;
5use serde::{Deserialize, Serialize};
6
7use crate::symbol::Name;
8use crate::Type;
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct FnParam {
16 pub name: Name,
17 pub ty: Option<crate::compact::SimpleType>,
20 #[serde(default)]
25 pub out_ty: Option<crate::compact::SimpleType>,
26 pub default: Option<crate::compact::SimpleType>,
28 pub is_variadic: bool,
29 pub is_byref: bool,
30 pub is_optional: bool,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
38pub enum Variance {
39 #[default]
41 Invariant,
42 Covariant,
44 Contravariant,
46}
47
48#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
53pub struct TemplateParam {
54 pub name: Name,
55 pub bound: Option<Type>,
56 pub variance: Variance,
57}
58
59#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
64pub enum ArrayKey {
65 String(Arc<str>),
66 Int(i64),
67}
68
69impl PartialOrd for ArrayKey {
70 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
71 Some(self.cmp(other))
72 }
73}
74
75impl Ord for ArrayKey {
76 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
77 match (self, other) {
78 (ArrayKey::Int(a), ArrayKey::Int(b)) => a.cmp(b),
79 (ArrayKey::String(a), ArrayKey::String(b)) => a.cmp(b),
80 (ArrayKey::Int(_), ArrayKey::String(_)) => std::cmp::Ordering::Less,
82 (ArrayKey::String(_), ArrayKey::Int(_)) => std::cmp::Ordering::Greater,
83 }
84 }
85}
86
87#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
88pub struct KeyedProperty {
89 pub ty: Type,
90 pub optional: bool,
91}
92
93type KeyedMap = IndexMap<ArrayKey, KeyedProperty, rustc_hash::FxBuildHasher>;
94
95#[derive(Default, Serialize, Deserialize)]
99#[serde(transparent)]
100pub struct KeyedProperties {
101 map: KeyedMap,
102 #[serde(skip)]
103 hash: std::sync::OnceLock<u64>,
104}
105
106impl KeyedProperties {
107 pub fn with_capacity_and_hasher(n: usize, hasher: rustc_hash::FxBuildHasher) -> Self {
108 KeyedMap::with_capacity_and_hasher(n, hasher).into()
109 }
110
111 pub fn entries_hash(&self) -> u64 {
113 *self.hash.get_or_init(|| {
114 self.map.iter().fold(0u64, |combined, (k, v)| {
115 let mut entry_hasher = rustc_hash::FxHasher::default();
116 k.hash(&mut entry_hasher);
117 v.hash(&mut entry_hasher);
118 combined.wrapping_add(std::hash::Hasher::finish(&entry_hasher))
119 })
120 })
121 }
122}
123
124impl From<KeyedMap> for KeyedProperties {
125 fn from(map: KeyedMap) -> Self {
126 KeyedProperties {
127 map,
128 hash: std::sync::OnceLock::new(),
129 }
130 }
131}
132
133impl Clone for KeyedProperties {
134 fn clone(&self) -> Self {
135 KeyedProperties {
136 map: self.map.clone(),
137 hash: self.hash.clone(),
138 }
139 }
140}
141
142impl std::fmt::Debug for KeyedProperties {
143 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144 self.map.fmt(f)
145 }
146}
147
148impl PartialEq for KeyedProperties {
149 fn eq(&self, other: &Self) -> bool {
150 if let (Some(a), Some(b)) = (self.hash.get(), other.hash.get()) {
152 if a != b {
153 return false;
154 }
155 }
156 self.map == other.map
157 }
158}
159
160impl Eq for KeyedProperties {}
161
162impl std::ops::Deref for KeyedProperties {
163 type Target = KeyedMap;
164 fn deref(&self) -> &KeyedMap {
165 &self.map
166 }
167}
168
169impl std::ops::DerefMut for KeyedProperties {
170 fn deref_mut(&mut self) -> &mut KeyedMap {
171 self.hash = std::sync::OnceLock::new();
172 &mut self.map
173 }
174}
175
176impl FromIterator<(ArrayKey, KeyedProperty)> for KeyedProperties {
177 fn from_iter<I: IntoIterator<Item = (ArrayKey, KeyedProperty)>>(iter: I) -> Self {
178 KeyedMap::from_iter(iter).into()
179 }
180}
181
182impl Extend<(ArrayKey, KeyedProperty)> for KeyedProperties {
183 fn extend<I: IntoIterator<Item = (ArrayKey, KeyedProperty)>>(&mut self, iter: I) {
184 std::ops::DerefMut::deref_mut(self).extend(iter);
185 }
186}
187
188impl IntoIterator for KeyedProperties {
189 type Item = (ArrayKey, KeyedProperty);
190 type IntoIter = indexmap::map::IntoIter<ArrayKey, KeyedProperty>;
191 fn into_iter(self) -> Self::IntoIter {
192 self.map.into_iter()
193 }
194}
195
196impl<'a> IntoIterator for &'a KeyedProperties {
197 type Item = (&'a ArrayKey, &'a KeyedProperty);
198 type IntoIter = indexmap::map::Iter<'a, ArrayKey, KeyedProperty>;
199 fn into_iter(self) -> Self::IntoIter {
200 self.map.iter()
201 }
202}
203
204impl<'a> IntoIterator for &'a mut KeyedProperties {
205 type Item = (&'a ArrayKey, &'a mut KeyedProperty);
206 type IntoIter = indexmap::map::IterMut<'a, ArrayKey, KeyedProperty>;
207 fn into_iter(self) -> Self::IntoIter {
208 std::ops::DerefMut::deref_mut(self).iter_mut()
209 }
210}
211
212#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
218pub struct ClosureData {
219 pub params: Box<[FnParam]>,
220 pub return_type: Type,
221 pub this_type: Option<Type>,
222 pub is_pure: bool,
224 pub is_static: bool,
226 #[serde(default)]
228 pub rejects_surplus_args: bool,
229}
230
231#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
233pub struct ConditionalData {
234 pub param_name: Option<Name>,
237 pub subject: Type,
238 pub if_true: Type,
239 pub if_false: Type,
240}
241
242#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
247pub enum Atomic {
248 TString,
251 TLiteralString(Arc<str>),
253 TCallableString,
255 TClassString(Option<Name>),
257 TNonEmptyString,
259 TNumericString,
261
262 TInt,
264 TLiteralInt(i64),
266 TIntRange { min: Option<i64>, max: Option<i64> },
268 TPositiveInt,
270 TNegativeInt,
272 TNonNegativeInt,
274
275 TFloat,
277 TIntegralFloat,
281 TLiteralFloat(i64, i64), TBool,
286 TTrue,
288 TFalse,
290
291 TNull,
293
294 TVoid,
297 TNever,
299 TMixed,
301 TScalar,
303 TNumeric,
305
306 TObject,
309 TNamedObject {
311 fqcn: Name,
312 type_params: Arc<[Type]>,
314 },
315 TStaticObject { fqcn: Name },
317 TSelf { fqcn: Name },
319 TParent { fqcn: Name },
321
322 TCallable {
325 params: Option<Box<Vec<FnParam>>>,
329 return_type: Option<Box<Type>>,
330 is_pure: bool,
332 },
333 TClosure { data: Box<ClosureData> },
336
337 TArray { key: Box<Type>, value: Box<Type> },
340 TList { value: Box<Type> },
342 TNonEmptyArray { key: Box<Type>, value: Box<Type> },
344 TNonEmptyList { value: Box<Type> },
346 TKeyedArray {
348 properties: Arc<KeyedProperties>,
350 is_open: bool,
352 is_list: bool,
354 },
355
356 TTemplateParam {
359 name: Name,
360 as_type: Box<Type>,
361 defining_entity: Name,
363 },
364 TKeyOf { target: Box<Type> },
366 TValueOf { target: Box<Type> },
368 TConditional { data: Box<ConditionalData> },
371
372 TInterfaceString(Option<Name>),
375 TEnumString,
377 TTraitString,
379
380 TLiteralEnumCase { enum_fqcn: Name, case_name: Name },
383
384 TIntersection { parts: Arc<[Type]> },
387
388 TObjectShape { properties: Arc<KeyedProperties> },
390
391 TResource,
393}
394
395impl Atomic {
396 pub fn can_be_falsy(&self) -> bool {
398 match self {
399 Atomic::TNull
400 | Atomic::TFalse
401 | Atomic::TBool
402 | Atomic::TNever
403 | Atomic::TLiteralInt(0)
404 | Atomic::TLiteralFloat(0, 0)
405 | Atomic::TInt
406 | Atomic::TFloat
407 | Atomic::TNumeric
408 | Atomic::TScalar
409 | Atomic::TMixed
410 | Atomic::TString
411 | Atomic::TNonEmptyString | Atomic::TNumericString | Atomic::TKeyOf { .. }
414 | Atomic::TValueOf { .. }
415 | Atomic::TArray { .. }
416 | Atomic::TList { .. } => true,
417 Atomic::TLiteralString(s) => s.as_ref().is_empty() || s.as_ref() == "0",
421
422 Atomic::TKeyedArray { properties, .. } => properties.is_empty(),
423
424 Atomic::TIntRange { min, max } => {
426 min.is_none_or(|lo| lo <= 0) && max.is_none_or(|hi| hi >= 0)
427 }
428 Atomic::TNonNegativeInt => true,
430
431 _ => false,
432 }
433 }
434
435 pub fn can_be_truthy(&self) -> bool {
437 match self {
438 Atomic::TNever
439 | Atomic::TVoid
440 | Atomic::TNull
441 | Atomic::TFalse
442 | Atomic::TLiteralInt(0)
443 | Atomic::TLiteralFloat(0, 0) => false,
444 Atomic::TLiteralString(s) if s.as_ref() == "" || s.as_ref() == "0" => false,
445 Atomic::TIntRange {
447 min: Some(0),
448 max: Some(0),
449 } => false,
450 Atomic::TKeyedArray {
452 properties,
453 is_open,
454 ..
455 } if !is_open && properties.is_empty() => false,
456 _ => true,
457 }
458 }
459
460 pub fn is_numeric(&self) -> bool {
462 matches!(
463 self,
464 Atomic::TInt
465 | Atomic::TLiteralInt(_)
466 | Atomic::TIntRange { .. }
467 | Atomic::TPositiveInt
468 | Atomic::TNegativeInt
469 | Atomic::TNonNegativeInt
470 | Atomic::TFloat
471 | Atomic::TIntegralFloat
472 | Atomic::TLiteralFloat(..)
473 | Atomic::TNumeric
474 | Atomic::TNumericString
475 )
476 }
477
478 pub fn is_int(&self) -> bool {
480 matches!(
481 self,
482 Atomic::TInt
483 | Atomic::TLiteralInt(_)
484 | Atomic::TIntRange { .. }
485 | Atomic::TPositiveInt
486 | Atomic::TNegativeInt
487 | Atomic::TNonNegativeInt
488 )
489 }
490
491 pub fn is_string(&self) -> bool {
493 matches!(
494 self,
495 Atomic::TString
496 | Atomic::TLiteralString(_)
497 | Atomic::TCallableString
498 | Atomic::TClassString(_)
499 | Atomic::TNonEmptyString
500 | Atomic::TNumericString
501 | Atomic::TInterfaceString(_)
502 | Atomic::TEnumString
503 | Atomic::TTraitString
504 )
505 }
506
507 pub fn is_array(&self) -> bool {
509 matches!(
510 self,
511 Atomic::TArray { .. }
512 | Atomic::TList { .. }
513 | Atomic::TNonEmptyArray { .. }
514 | Atomic::TNonEmptyList { .. }
515 | Atomic::TKeyedArray { .. }
516 )
517 }
518
519 pub fn is_object(&self) -> bool {
521 matches!(
522 self,
523 Atomic::TObject
524 | Atomic::TObjectShape { .. }
525 | Atomic::TNamedObject { .. }
526 | Atomic::TStaticObject { .. }
527 | Atomic::TSelf { .. }
528 | Atomic::TParent { .. }
529 | Atomic::TIntersection { .. }
530 )
531 }
532
533 pub fn is_definitely_non_object(&self) -> bool {
538 matches!(
539 self,
540 Atomic::TString
541 | Atomic::TLiteralString(_)
542 | Atomic::TCallableString
543 | Atomic::TClassString(_)
544 | Atomic::TNonEmptyString
545 | Atomic::TNumericString
546 | Atomic::TInterfaceString(_)
547 | Atomic::TEnumString
548 | Atomic::TTraitString
549 | Atomic::TInt
550 | Atomic::TLiteralInt(_)
551 | Atomic::TIntRange { .. }
552 | Atomic::TPositiveInt
553 | Atomic::TNegativeInt
554 | Atomic::TNonNegativeInt
555 | Atomic::TFloat
556 | Atomic::TIntegralFloat
557 | Atomic::TLiteralFloat(..)
558 | Atomic::TBool
559 | Atomic::TTrue
560 | Atomic::TFalse
561 | Atomic::TNull
562 | Atomic::TVoid
563 | Atomic::TArray { .. }
564 | Atomic::TList { .. }
565 | Atomic::TNonEmptyArray { .. }
566 | Atomic::TNonEmptyList { .. }
567 | Atomic::TKeyedArray { .. }
568 | Atomic::TScalar
569 | Atomic::TNumeric
570 )
571 }
572
573 pub fn is_callable(&self) -> bool {
578 matches!(
579 self,
580 Atomic::TCallable { .. } | Atomic::TClosure { .. } | Atomic::TCallableString
581 )
582 }
583
584 pub fn named_object_fqcn(&self) -> Option<&str> {
586 match self {
587 Atomic::TNamedObject { fqcn, .. }
588 | Atomic::TStaticObject { fqcn }
589 | Atomic::TSelf { fqcn }
590 | Atomic::TParent { fqcn } => Some(fqcn.as_ref()),
591 _ => None,
592 }
593 }
594
595 pub fn type_name(&self) -> &'static str {
597 match self {
598 Atomic::TString
599 | Atomic::TLiteralString(_)
600 | Atomic::TNonEmptyString
601 | Atomic::TNumericString => "string",
602 Atomic::TCallableString => "callable-string",
603 Atomic::TClassString(_) => "class-string",
604 Atomic::TInt | Atomic::TLiteralInt(_) | Atomic::TIntRange { .. } => "int",
605 Atomic::TPositiveInt => "positive-int",
606 Atomic::TNegativeInt => "negative-int",
607 Atomic::TNonNegativeInt => "non-negative-int",
608 Atomic::TFloat | Atomic::TIntegralFloat | Atomic::TLiteralFloat(..) => "float",
609 Atomic::TBool => "bool",
610 Atomic::TTrue => "true",
611 Atomic::TFalse => "false",
612 Atomic::TNull => "null",
613 Atomic::TVoid => "void",
614 Atomic::TNever => "never",
615 Atomic::TMixed => "mixed",
616 Atomic::TScalar => "scalar",
617 Atomic::TNumeric => "numeric",
618 Atomic::TObject => "object",
619 Atomic::TResource => "resource",
620 Atomic::TNamedObject { .. } => "object",
621 Atomic::TStaticObject { .. } => "static",
622 Atomic::TSelf { .. } => "self",
623 Atomic::TParent { .. } => "parent",
624 Atomic::TCallable { .. } => "callable",
625 Atomic::TClosure { .. } => "Closure",
626 Atomic::TArray { .. } => "array",
627 Atomic::TList { .. } => "list",
628 Atomic::TNonEmptyArray { .. } => "non-empty-array",
629 Atomic::TNonEmptyList { .. } => "non-empty-list",
630 Atomic::TKeyedArray { .. } => "array",
631 Atomic::TTemplateParam { .. } => "template-param",
632 Atomic::TKeyOf { .. } => "key-of",
633 Atomic::TValueOf { .. } => "value-of",
634 Atomic::TConditional { .. } => "conditional-type",
635 Atomic::TInterfaceString(_) => "interface-string",
636 Atomic::TEnumString => "enum-string",
637 Atomic::TTraitString => "trait-string",
638 Atomic::TLiteralEnumCase { .. } => "enum-case",
639 Atomic::TIntersection { .. } => "intersection",
640 Atomic::TObjectShape { .. } => "object",
641 }
642 }
643}
644
645impl Hash for FnParam {
650 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
651 self.name.hash(state);
652 self.ty.hash(state);
653 self.out_ty.hash(state);
654 self.default.hash(state);
655 self.is_variadic.hash(state);
656 self.is_byref.hash(state);
657 self.is_optional.hash(state);
658 }
659}
660
661#[allow(non_camel_case_types, clippy::enum_variant_names)]
663#[repr(u8)]
664enum AtomicTag {
665 TString = 0,
666 TLiteralString,
667 TCallableString,
668 TClassString,
669 TNonEmptyString,
670 TNumericString,
671 TInt,
672 TLiteralInt,
673 TIntRange,
674 TPositiveInt,
675 TNegativeInt,
676 TNonNegativeInt,
677 TFloat,
678 TIntegralFloat,
679 TLiteralFloat,
680 TBool,
681 TTrue,
682 TFalse,
683 TNull,
684 TVoid,
685 TNever,
686 TMixed,
687 TScalar,
688 TNumeric,
689 TObject,
690 TNamedObject,
691 TStaticObject,
692 TSelf,
693 TParent,
694 TCallable,
695 TClosure,
696 TArray,
697 TList,
698 TNonEmptyArray,
699 TNonEmptyList,
700 TKeyedArray,
701 TTemplateParam,
702 TKeyOf,
703 TValueOf,
704 TConditional,
705 TInterfaceString,
706 TEnumString,
707 TTraitString,
708 TLiteralEnumCase,
709 TIntersection,
710 TObjectShape,
711 TResource,
712}
713
714impl Hash for Atomic {
715 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
716 use AtomicTag as T;
717 match self {
718 Atomic::TString => (T::TString as u8).hash(state),
720 Atomic::TCallableString => (T::TCallableString as u8).hash(state),
721 Atomic::TNonEmptyString => (T::TNonEmptyString as u8).hash(state),
722 Atomic::TNumericString => (T::TNumericString as u8).hash(state),
723 Atomic::TInt => (T::TInt as u8).hash(state),
724 Atomic::TPositiveInt => (T::TPositiveInt as u8).hash(state),
725 Atomic::TNegativeInt => (T::TNegativeInt as u8).hash(state),
726 Atomic::TNonNegativeInt => (T::TNonNegativeInt as u8).hash(state),
727 Atomic::TFloat => (T::TFloat as u8).hash(state),
728 Atomic::TIntegralFloat => (T::TIntegralFloat as u8).hash(state),
729 Atomic::TBool => (T::TBool as u8).hash(state),
730 Atomic::TTrue => (T::TTrue as u8).hash(state),
731 Atomic::TFalse => (T::TFalse as u8).hash(state),
732 Atomic::TNull => (T::TNull as u8).hash(state),
733 Atomic::TVoid => (T::TVoid as u8).hash(state),
734 Atomic::TNever => (T::TNever as u8).hash(state),
735 Atomic::TMixed => (T::TMixed as u8).hash(state),
736 Atomic::TScalar => (T::TScalar as u8).hash(state),
737 Atomic::TNumeric => (T::TNumeric as u8).hash(state),
738 Atomic::TObject => (T::TObject as u8).hash(state),
739 Atomic::TEnumString => (T::TEnumString as u8).hash(state),
740 Atomic::TTraitString => (T::TTraitString as u8).hash(state),
741 Atomic::TResource => (T::TResource as u8).hash(state),
742
743 Atomic::TLiteralString(s) => {
745 (T::TLiteralString as u8).hash(state);
746 s.hash(state);
747 }
748 Atomic::TClassString(opt) => {
749 (T::TClassString as u8).hash(state);
750 opt.hash(state);
751 }
752 Atomic::TInterfaceString(opt) => {
753 (T::TInterfaceString as u8).hash(state);
754 opt.hash(state);
755 }
756 Atomic::TLiteralInt(n) => {
757 (T::TLiteralInt as u8).hash(state);
758 n.hash(state);
759 }
760 Atomic::TIntRange { min, max } => {
761 (T::TIntRange as u8).hash(state);
762 min.hash(state);
763 max.hash(state);
764 }
765 Atomic::TLiteralFloat(int_bits, frac_bits) => {
766 (T::TLiteralFloat as u8).hash(state);
767 int_bits.hash(state);
768 frac_bits.hash(state);
769 }
770 Atomic::TNamedObject { fqcn, type_params } => {
771 (T::TNamedObject as u8).hash(state);
772 fqcn.hash(state);
773 type_params.hash(state);
774 }
775 Atomic::TStaticObject { fqcn } => {
776 (T::TStaticObject as u8).hash(state);
777 fqcn.hash(state);
778 }
779 Atomic::TSelf { fqcn } => {
780 (T::TSelf as u8).hash(state);
781 fqcn.hash(state);
782 }
783 Atomic::TParent { fqcn } => {
784 (T::TParent as u8).hash(state);
785 fqcn.hash(state);
786 }
787 Atomic::TCallable {
788 params,
789 return_type,
790 is_pure,
791 } => {
792 (T::TCallable as u8).hash(state);
793 params.hash(state);
794 return_type.hash(state);
795 is_pure.hash(state);
796 }
797 Atomic::TClosure { data } => {
798 (T::TClosure as u8).hash(state);
799 data.hash(state);
800 }
801 Atomic::TArray { key, value } => {
802 (T::TArray as u8).hash(state);
803 key.hash(state);
804 value.hash(state);
805 }
806 Atomic::TList { value } => {
807 (T::TList as u8).hash(state);
808 value.hash(state);
809 }
810 Atomic::TNonEmptyArray { key, value } => {
811 (T::TNonEmptyArray as u8).hash(state);
812 key.hash(state);
813 value.hash(state);
814 }
815 Atomic::TNonEmptyList { value } => {
816 (T::TNonEmptyList as u8).hash(state);
817 value.hash(state);
818 }
819 Atomic::TObjectShape { properties } => {
820 (T::TObjectShape as u8).hash(state);
821 properties.entries_hash().hash(state);
822 properties.len().hash(state);
823 }
824 Atomic::TKeyedArray {
825 properties,
826 is_open,
827 is_list,
828 } => {
829 (T::TKeyedArray as u8).hash(state);
830 properties.entries_hash().hash(state);
831 properties.len().hash(state);
832 is_open.hash(state);
833 is_list.hash(state);
834 }
835 Atomic::TTemplateParam {
836 name,
837 as_type,
838 defining_entity,
839 } => {
840 (T::TTemplateParam as u8).hash(state);
841 name.hash(state);
842 as_type.hash(state);
843 defining_entity.hash(state);
844 }
845 Atomic::TKeyOf { target } => {
846 (T::TKeyOf as u8).hash(state);
847 target.hash(state);
848 }
849 Atomic::TValueOf { target } => {
850 (T::TValueOf as u8).hash(state);
851 target.hash(state);
852 }
853 Atomic::TConditional { data } => {
854 (T::TConditional as u8).hash(state);
855 data.hash(state);
856 }
857 Atomic::TLiteralEnumCase {
858 enum_fqcn,
859 case_name,
860 } => {
861 (T::TLiteralEnumCase as u8).hash(state);
862 enum_fqcn.hash(state);
863 case_name.hash(state);
864 }
865 Atomic::TIntersection { parts } => {
866 (T::TIntersection as u8).hash(state);
867 parts.hash(state);
868 }
869 }
870 }
871}