1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use arret_runtime::abitype;
use arret_runtime::callback;
use crate::mir::ops;
use crate::ty;
use crate::ty::Ty;
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct PolymorphAbi {
pub call_conv: ops::CallConv,
pub has_captures: bool,
pub fixed_params: Box<[abitype::AbiType]>,
pub rest_param: Option<abitype::AbiType>,
pub ret: abitype::RetAbiType,
}
impl PolymorphAbi {
pub fn thunk_abi() -> PolymorphAbi {
PolymorphAbi {
call_conv: ops::CallConv::Ccc,
has_captures: true,
fixed_params: Box::new([]),
rest_param: Some(abitype::TOP_LIST_BOXED_ABI_TYPE.into()),
ret: abitype::BoxedAbiType::Any.into(),
}
}
pub fn param_ty_ref<M: ty::Pm>(&self) -> ty::List<M> {
use crate::ty::conv_abi::ConvertableAbiType;
let fixed_refs = self
.fixed_params
.iter()
.map(ConvertableAbiType::to_ty_ref)
.collect();
let rest_ref = match &self.rest_param {
Some(abitype::AbiType::Boxed(abitype::BoxedAbiType::List(member_abi_type))) => {
member_abi_type.to_ty_ref()
}
Some(other) => {
panic!("cannot determine member type for ABI rest list {:?}", other);
}
None => Ty::never().into(),
};
ty::List::new(fixed_refs, rest_ref)
}
}
impl From<callback::EntryPointAbiType> for PolymorphAbi {
fn from(abi_type: callback::EntryPointAbiType) -> Self {
PolymorphAbi {
call_conv: ops::CallConv::Ccc,
has_captures: true,
fixed_params: abi_type.params.iter().cloned().collect(),
rest_param: None,
ret: abi_type.ret,
}
}
}
impl From<PolymorphAbi> for ops::OpsAbi {
fn from(polymorph_abi: PolymorphAbi) -> Self {
ops::OpsAbi {
params: Some(abitype::BoxedAbiType::Any.into())
.filter(|_| polymorph_abi.has_captures)
.into_iter()
.chain(polymorph_abi.fixed_params.iter().cloned())
.chain(polymorph_abi.rest_param.iter().cloned())
.collect(),
call_conv: polymorph_abi.call_conv,
ret: polymorph_abi.ret,
}
}
}
pub fn polymorph_abi_for_list_ty<M: ty::Pm>(
has_captures: bool,
list_ty: &ty::List<M>,
ret_ty: &ty::Ref<M>,
) -> PolymorphAbi {
use crate::mir::specific_abi_type::*;
PolymorphAbi {
call_conv: ops::CallConv::FastCc,
has_captures,
fixed_params: list_ty
.fixed()
.iter()
.map(specific_abi_type_for_ty_ref)
.collect(),
rest_param: Some(
abitype::BoxedAbiType::List(specific_boxed_abi_type_for_ty_ref(list_ty.rest())).into(),
)
.filter(|_| list_ty.has_rest()),
ret: specific_ret_abi_type_for_ty_ref(ret_ty),
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::hir;
#[test]
fn polymorph_abi_param_ty_ref() {
use arret_runtime::abitype::EncodeBoxedAbiType;
use arret_runtime::boxed;
let thunk_param_poly = PolymorphAbi::thunk_abi().param_ty_ref();
let expected_poly = hir::poly_for_str("(List & Any)");
assert_eq!(expected_poly, thunk_param_poly.into());
let mul_param_poly = PolymorphAbi {
call_conv: ops::CallConv::FastCc,
has_captures: false,
fixed_params: Box::new([boxed::Num::BOXED_ABI_TYPE.into()]),
rest_param: Some(abitype::BoxedAbiType::List(&boxed::Num::BOXED_ABI_TYPE).into()),
ret: boxed::Num::BOXED_ABI_TYPE.into(),
}
.param_ty_ref();
let expected_poly = hir::poly_for_str("(List Num & Num)");
assert_eq!(expected_poly, mul_param_poly.into());
}
}