-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathshared_enum.rs
More file actions
251 lines (224 loc) · 9.29 KB
/
shared_enum.rs
File metadata and controls
251 lines (224 loc) · 9.29 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! More tests can be found in
//! crates/swift-bridge-ir/src/codegen/codegen_tests/shared_enum_codegen_tests.rs
use crate::bridged_type::{BridgedType, SharedEnum, StructFields};
use crate::codegen::generate_rust_tokens::vec::vec_of_transparent_enum::generate_vec_of_transparent_enum_functions;
use crate::parse::TypeDeclarations;
use crate::{SwiftBridgeModule, SWIFT_BRIDGE_PREFIX};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::Ident;
impl SwiftBridgeModule {
/// Generate the tokens for a shared enum.
pub(super) fn generate_shared_enum_tokens(
&self,
shared_enum: &SharedEnum,
types: &TypeDeclarations,
) -> Option<TokenStream> {
if shared_enum.already_declared {
return None;
}
let enum_name = &shared_enum.name;
let swift_bridge_path = &self.swift_bridge_path;
let enum_ffi_name = format!("{}{}", SWIFT_BRIDGE_PREFIX, enum_name);
let enum_ffi_name = Ident::new(&enum_ffi_name, enum_name.span());
let option_enum = shared_enum.ffi_option_name_tokens();
let mut enum_variants = vec![];
let mut enum_ffi_variants = vec![];
for variant in shared_enum.variants.iter() {
let variant_name = &variant.name;
let enum_variant = match &variant.fields {
StructFields::Named(named_fields) => {
let mut names = vec![];
for named_field in named_fields {
let field_name = &named_field.name;
let ty = BridgedType::new_with_type(&named_field.ty, &self.types)
.unwrap()
.to_rust_type_path(types);
let field = quote! {#field_name : #ty};
names.push(field);
}
quote! {
#variant_name {#(#names),*}
}
}
StructFields::Unnamed(unamed_fields) => {
let mut names = vec![];
for unnamed_field in unamed_fields {
let ty =
BridgedType::new_with_type(&unnamed_field.ty, &self.types).unwrap();
names.push(ty.to_rust_type_path(types));
}
quote! {
#variant_name (#(#names),*)
}
}
StructFields::Unit => {
quote! {
#variant_name
}
}
};
enum_variants.push(enum_variant);
}
for variant in shared_enum.variants.iter() {
let variant_name = &variant.name;
let enum_ffi_variant = match &variant.fields {
StructFields::Named(named_fields) => {
let mut names = vec![];
for named_field in named_fields {
let field_name = &named_field.name;
let ty = BridgedType::new_with_type(&named_field.ty, &self.types)
.unwrap()
.to_ffi_compatible_rust_type(&self.swift_bridge_path, &self.types);
let field = quote! {#field_name : #ty};
names.push(field);
}
quote! {
#variant_name {#(#names),*}
}
}
StructFields::Unnamed(unamed_fields) => {
let mut names = vec![];
for unnamed_field in unamed_fields {
let ty =
BridgedType::new_with_type(&unnamed_field.ty, &self.types).unwrap();
names.push(
ty.to_ffi_compatible_rust_type(&self.swift_bridge_path, &self.types),
);
}
quote! {
#variant_name (#(#names),*)
}
}
StructFields::Unit => {
quote! {
#variant_name
}
}
};
enum_ffi_variants.push(enum_ffi_variant);
}
let mut convert_rust_variants_to_ffi = vec![];
let mut convert_ffi_variants_to_rust = vec![];
for variant in shared_enum.variants.iter() {
let convert_rust_variant_to_ffi = variant.convert_rust_expression_to_ffi_repr(
&self.types,
&self.swift_bridge_path,
&format_ident!("{}", enum_name),
&format_ident!("{}", enum_ffi_name),
);
convert_rust_variants_to_ffi.push(convert_rust_variant_to_ffi);
}
for variant in shared_enum.variants.iter() {
let convert_ffi_variant_to_rust = variant.convert_ffi_repr_to_rust(
&self.swift_bridge_path,
&self.types,
&format_ident!("{}", enum_name),
&format_ident!("{}", enum_ffi_name),
);
convert_ffi_variants_to_rust.push(convert_ffi_variant_to_rust);
}
// Auto derives
let mut derives = if shared_enum.has_one_or_more_variants_with_data() {
vec![]
} else {
vec![quote! {Copy}, quote! {Clone}]
};
// User derives
let mut derive_impl_ffi_bridges = vec![];
if shared_enum.derive.debug {
// We don't want to confuse the developer if one of our variants has data and Debug isn't derived,
// so we still want to derive(Debug) on the Rust side.
// TODO: push a warning if one of our variants has data?
derives.push(quote! {::std::fmt::Debug});
// We currently only allow derive(Debug) on non data carrying enums in order
// to prevent a potential memory safety issue.
// https://github.com/chinedufn/swift-bridge/pull/194#discussion_r1134386788
if !shared_enum.has_one_or_more_variants_with_data() {
// __swift_bridge__$SomeEnum$Debug
let export_name = format!("{}$Debug", shared_enum.ffi_name_string());
// __swift_bridge__SomeEnum_Debug
let fn_name = format_ident!("{}_Debug", enum_ffi_name);
derive_impl_ffi_bridges.push(quote! {
#[export_name = #export_name]
pub extern "C" fn #fn_name(this: #enum_ffi_name) -> *mut swift_bridge::string::RustString {
swift_bridge::string::RustString(format!("{:?}", this.into_rust_repr())).box_into_raw()
}
});
}
}
let vec_support = if shared_enum.has_one_or_more_variants_with_data() {
// Enums with variants that contain data are not yet supported.
quote! {}
} else {
generate_vec_of_transparent_enum_functions(&shared_enum)
};
let definition = quote! {
#[derive(#(#derives),*)]
pub enum #enum_name {
#(#enum_variants),*
}
#[repr(C)]
#[doc(hidden)]
pub enum #enum_ffi_name {
#(#enum_ffi_variants),*
}
impl #swift_bridge_path::SharedEnum for #enum_name {
type FfiRepr = #enum_ffi_name;
}
impl #enum_name {
#[doc(hidden)]
#[inline(always)]
pub fn into_ffi_repr(self) -> #enum_ffi_name {
match self {
#(#convert_rust_variants_to_ffi),*
}
}
}
impl #enum_ffi_name {
#[doc(hidden)]
#[inline(always)]
pub fn into_rust_repr(self) -> #enum_name {
match self {
#(#convert_ffi_variants_to_rust),*
}
}
}
#[repr(C)]
#[doc(hidden)]
pub struct #option_enum {
is_some: bool,
val: std::mem::MaybeUninit<#enum_ffi_name>,
}
impl #option_enum {
#[doc(hidden)]
#[inline(always)]
pub fn into_rust_repr(self) -> Option<#enum_name> {
if self.is_some {
Some(unsafe { self.val.assume_init().into_rust_repr() })
} else {
None
}
}
#[doc(hidden)]
#[inline(always)]
pub fn from_rust_repr(val: Option<#enum_name>) -> #option_enum {
if let Some(val) = val {
#option_enum {
is_some: true,
val: std::mem::MaybeUninit::new(val.into_ffi_repr())
}
} else {
#option_enum {
is_some: false,
val: std::mem::MaybeUninit::uninit()
}
}
}
}
#vec_support
#(#derive_impl_ffi_bridges),*
};
Some(definition)
}
}