1use crate::ir::Type;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum TypeConversionStatus {
5 Exact,
6 Unresolved,
7}
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum LoweringExactness {
10 Exact,
11}
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct TypeConversionRecipe {
14 pub id: String,
15 pub from_kind: String,
16 pub to_kind: String,
17 pub target: String,
18 pub guard: Vec<String>,
19 pub preservation_class: LoweringExactness,
20 pub status: TypeConversionStatus,
21}
22fn kind(t: &Type) -> &'static str {
23 match t {
24 Type::Unit => "unit",
25 Type::Bool => "boolean",
26 Type::String => "string",
27 Type::Int
28 | Type::Int8
29 | Type::Int16
30 | Type::Int32
31 | Type::Int64
32 | Type::UInt
33 | Type::UInt8
34 | Type::UInt16
35 | Type::UInt32
36 | Type::UInt64 => "integer",
37 Type::Float32 | Type::Float64 => "float",
38 Type::Slice(_) => "list",
39 Type::Map(_, _) => "map",
40 Type::Tuple(_) => "tuple",
41 Type::Named(_) => "named",
42 }
43}
44fn integer_shape(t: &Type) -> Option<(usize, bool)> {
45 Some(match t {
46 Type::Int => (usize::BITS as usize, true),
47 Type::Int8 => (8, true),
48 Type::Int16 => (16, true),
49 Type::Int32 => (32, true),
50 Type::Int64 => (64, true),
51 Type::UInt => (usize::BITS as usize, false),
52 Type::UInt8 => (8, false),
53 Type::UInt16 => (16, false),
54 Type::UInt32 => (32, false),
55 Type::UInt64 => (64, false),
56 _ => return None,
57 })
58}
59fn float_bits(t: &Type) -> Option<usize> {
60 match t {
61 Type::Float32 => Some(32),
62 Type::Float64 => Some(64),
63 _ => None,
64 }
65}
66
67pub fn universal_type_conversion_registry() -> Vec<TypeConversionRecipe> {
68 vec![
69 TypeConversionRecipe {
70 id: "type.identity".into(),
71 from_kind: "*".into(),
72 to_kind: "*".into(),
73 target: "*".into(),
74 guard: vec![],
75 preservation_class: LoweringExactness::Exact,
76 status: TypeConversionStatus::Exact,
77 },
78 TypeConversionRecipe {
79 id: "type.integer.same_width".into(),
80 from_kind: "integer".into(),
81 to_kind: "integer".into(),
82 target: "*".into(),
83 guard: vec![
84 "bits_equal".into(),
85 "signedness_equal".into(),
86 "overflow_equal".into(),
87 ],
88 preservation_class: LoweringExactness::Exact,
89 status: TypeConversionStatus::Exact,
90 },
91 TypeConversionRecipe {
92 id: "type.float.same_precision".into(),
93 from_kind: "float".into(),
94 to_kind: "float".into(),
95 target: "*".into(),
96 guard: vec![
97 "precision_equal".into(),
98 "rounding_equal".into(),
99 "nan_equal".into(),
100 ],
101 preservation_class: LoweringExactness::Exact,
102 status: TypeConversionStatus::Exact,
103 },
104 TypeConversionRecipe {
105 id: "type.list.elementwise".into(),
106 from_kind: "list".into(),
107 to_kind: "list".into(),
108 target: "*".into(),
109 guard: vec![
110 "element_conversion_exact".into(),
111 "order_equal".into(),
112 "nullability_equal".into(),
113 "mutability_equal".into(),
114 ],
115 preservation_class: LoweringExactness::Exact,
116 status: TypeConversionStatus::Exact,
117 },
118 ]
119}
120
121pub fn resolve_universal_type_conversion(
122 from: &Type,
123 to: &Type,
124 target: &str,
125) -> Result<TypeConversionRecipe, String> {
126 if target.is_empty() {
127 return Err("TYPE_CONVERSION_UNRESOLVED: missing target".into());
128 }
129 if from == to {
130 return Ok(TypeConversionRecipe {
131 id: "type.identity".into(),
132 from_kind: kind(from).into(),
133 to_kind: kind(to).into(),
134 target: target.into(),
135 guard: vec![],
136 preservation_class: LoweringExactness::Exact,
137 status: TypeConversionStatus::Exact,
138 });
139 }
140 if let (Some(a), Some(b)) = (integer_shape(from), integer_shape(to)) {
141 if a != b {
142 return Err("TYPE_CONVERSION_UNRESOLVED: integer width/signedness".into());
143 }
144 return Ok(TypeConversionRecipe {
145 id: "type.integer.same_width".into(),
146 from_kind: "integer".into(),
147 to_kind: "integer".into(),
148 target: target.into(),
149 guard: vec![
150 "bits_equal".into(),
151 "signedness_equal".into(),
152 "overflow_equal".into(),
153 ],
154 preservation_class: LoweringExactness::Exact,
155 status: TypeConversionStatus::Exact,
156 });
157 }
158 if let (Some(a), Some(b)) = (float_bits(from), float_bits(to)) {
159 if a != b {
160 return Err("TYPE_CONVERSION_UNRESOLVED: float precision".into());
161 }
162 return Ok(TypeConversionRecipe {
163 id: "type.float.same_precision".into(),
164 from_kind: "float".into(),
165 to_kind: "float".into(),
166 target: target.into(),
167 guard: vec![
168 "precision_equal".into(),
169 "rounding_equal".into(),
170 "nan_equal".into(),
171 ],
172 preservation_class: LoweringExactness::Exact,
173 status: TypeConversionStatus::Exact,
174 });
175 }
176 if let (Type::Slice(a), Type::Slice(b)) = (from, to) {
177 resolve_universal_type_conversion(a, b, target)?;
178 return Ok(TypeConversionRecipe {
179 id: "type.list.elementwise".into(),
180 from_kind: "list".into(),
181 to_kind: "list".into(),
182 target: target.into(),
183 guard: vec![
184 "element_conversion_exact".into(),
185 "order_equal".into(),
186 "nullability_equal".into(),
187 "mutability_equal".into(),
188 ],
189 preservation_class: LoweringExactness::Exact,
190 status: TypeConversionStatus::Exact,
191 });
192 }
193 Err(format!(
194 "TYPE_CONVERSION_UNRESOLVED: {} -> {}",
195 kind(from),
196 kind(to)
197 ))
198}