1use crate::Type;
7use crate::supporting_types::{AnonymousStructType, EnumType, NamedStructType, Signature};
8use std::fmt::{Display, Formatter};
9use std::rc::Rc;
10
11pub type TypeRef = Rc<Type>;
12
13#[derive(Debug, Eq, PartialEq, Hash, Clone)]
14pub enum TypeKind {
15 Byte,
17 Int,
18 Codepoint,
19 Float,
20 String(Rc<Type>, Rc<Type>), StringStorage(Rc<Type>, Rc<Type>, usize), Bool,
23 Unit, Tuple(Vec<Rc<Type>>),
27 NamedStruct(NamedStructType),
28 AnonymousStruct(AnonymousStructType),
29 Range(TypeRef),
30
31 Enum(EnumType),
32
33 Function(Signature),
34
35 Optional(Rc<Type>),
36
37 FixedCapacityAndLengthArray(Rc<Type>, usize),
39
40 SliceView(Rc<Type>),
42
43 VecStorage(Rc<Type>, usize),
45 DynamicLengthVecView(Rc<Type>),
46
47 StackStorage(Rc<Type>, usize),
48 StackView(Rc<Type>),
49
50 QueueStorage(Rc<Type>, usize),
51 QueueView(Rc<Type>),
52
53 MapStorage(Rc<Type>, Rc<Type>, usize),
54 DynamicLengthMapView(Rc<Type>, Rc<Type>),
55
56 SparseView(Rc<Type>),
57 SparseStorage(Rc<Type>, usize),
58
59 GridStorage(Rc<Type>, usize, usize),
60 GridView(Rc<Type>),
61}
62
63impl Display for TypeKind {
64 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
65 match self {
66 Self::Byte => write!(f, "byte"),
67 Self::Codepoint => write!(f, "codepoint"),
68 Self::Int => write!(f, "int"),
69 Self::Float => write!(f, "float"),
70 Self::String(..) => write!(f, "string"),
71 Self::StringStorage(_, size, _) => write!(f, "string[{size}]"),
72 Self::Bool => write!(f, "bool"),
73 Self::Unit => write!(f, "()"),
74 Self::Tuple(types) => write!(f, "({})", seq_fmt::comma(types)),
75 Self::NamedStruct(named_struct) => write!(f, "{named_struct}",),
76 Self::AnonymousStruct(anon_struct) => write!(f, "{anon_struct}"),
77 Self::Range(range) => {
78 if let Self::NamedStruct(named_struct) = &*range.kind {
79 if let Self::AnonymousStruct(anon_struct) = &*named_struct.anon_struct_type.kind
80 {
81 write!(f, "range{anon_struct}")
82 } else {
83 write!(f, "range<invalid>")
84 }
85 } else {
86 write!(f, "range<invalid>")
87 }
88 }
89 Self::Enum(enum_type) => write!(f, "{enum_type}"),
90 Self::Function(signature) => write!(f, "{signature}"),
91 Self::Optional(inner) => write!(f, "option<{inner}>"),
92 Self::FixedCapacityAndLengthArray(inner, size) => {
93 write!(f, "[{inner}; {size}]")
94 }
95 Self::SliceView(inner) => write!(f, "[{inner}]"),
96 Self::VecStorage(inner, capacity) => {
97 write!(f, "Vec<{inner}, {capacity}>")
98 }
99 Self::DynamicLengthVecView(inner) => write!(f, "Vec<{inner}>"),
100 Self::StackStorage(inner, capacity) => {
101 write!(f, "Stack<{inner}, {capacity}>")
102 }
103 Self::StackView(inner) => write!(f, "Stack<{inner}>"),
104 Self::QueueStorage(inner, capacity) => {
105 write!(f, "Queue<{inner}, {capacity}>")
106 }
107 Self::QueueView(inner) => write!(f, "Queue<{inner}>"),
108 Self::MapStorage(key, value, capacity) => {
109 write!(f, "Map<{key}, {value}, {capacity}>")
110 }
111 Self::DynamicLengthMapView(key, value) => {
112 write!(f, "Map<{key}, {value}>")
113 }
114 Self::SparseView(inner) => write!(f, "Sparse<{inner}>"),
115 Self::SparseStorage(inner, capacity) => {
116 write!(f, "Sparse<{inner}, {capacity}>")
117 }
118 Self::GridStorage(inner, width, height) => {
119 write!(f, "Grid<{inner}, {width}, {height}>")
120 }
121 Self::GridView(inner) => write!(f, "Grid<{inner}>"),
122 }
123 }
124}