forked from EPashkin/gir
/
bounds.rs
370 lines (345 loc) · 12.5 KB
/
bounds.rs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use crate::{
analysis::{
function_parameters::{async_param_to_remove, CParameter},
functions::{find_function, find_index_to_ignore, finish_function_name},
imports::Imports,
out_parameters::use_function_return_for_result,
rust_type::{bounds_rust_type, rust_type, rust_type_with_scope},
},
consts::TYPE_PARAMETERS_START,
env::Env,
library::{
Class, Concurrency, Function, Fundamental, Nullable, ParameterDirection, Type, TypeId,
},
traits::IntoString,
};
use std::{collections::vec_deque::VecDeque, slice::Iter};
#[derive(Clone, Eq, Debug, PartialEq)]
pub enum BoundType {
NoWrapper,
// lifetime
IsA(Option<char>),
// lifetime <- shouldn't be used but just in case...
AsRef(Option<char>),
}
impl BoundType {
pub fn need_isa(&self) -> bool {
match *self {
BoundType::IsA(_) => true,
_ => false,
}
}
}
#[derive(Clone, Eq, Debug, PartialEq)]
pub struct Bound {
pub bound_type: BoundType,
pub parameter_name: String,
pub alias: char,
pub type_str: String,
pub info_for_next_type: bool,
pub callback_modified: bool,
}
#[derive(Clone, Debug)]
pub struct Bounds {
unused: VecDeque<char>,
used: Vec<Bound>,
unused_lifetimes: VecDeque<char>,
lifetimes: Vec<char>,
}
impl Default for Bounds {
fn default() -> Bounds {
Bounds {
unused: (TYPE_PARAMETERS_START as u8..)
.take_while(|x| *x <= b'Z')
.map(|x| x as char)
.collect(),
used: Vec::new(),
unused_lifetimes: "abcdefg".chars().collect(),
lifetimes: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct CallbackInfo {
pub callback_type: String,
pub success_parameters: String,
pub error_parameters: String,
pub bound_name: char,
}
impl Bounds {
pub fn add_for_parameter(
&mut self,
env: &Env,
func: &Function,
par: &CParameter,
r#async: bool,
concurrency: Concurrency,
) -> (Option<String>, Option<CallbackInfo>) {
let type_name = bounds_rust_type(env, par.typ);
if (r#async && async_param_to_remove(&par.name)) || type_name.is_err() {
return (None, None);
}
let mut type_string = type_name.into_string();
let mut callback_info = None;
let mut ret = None;
let mut need_is_into_check = false;
if !par.instance_parameter && par.direction != ParameterDirection::Out {
if let Some(bound_type) = Bounds::type_for(env, par.typ, par.nullable) {
ret = Some(Bounds::get_to_glib_extra(&bound_type));
if r#async && (par.name == "callback" || par.name.ends_with("_callback")) {
let func_name = func.c_identifier.as_ref().unwrap();
let finish_func_name = finish_function_name(func_name);
if let Some(function) = find_function(env, &finish_func_name) {
let mut out_parameters = find_out_parameters(env, function);
if use_function_return_for_result(env, function.ret.typ) {
out_parameters
.insert(0, rust_type(env, function.ret.typ).into_string());
}
let parameters = format_out_parameters(&out_parameters);
let error_type = find_error_type(env, function);
type_string = format!(
"FnOnce(Result<{}, {}>) + Send + 'static",
parameters, error_type
);
let bound_name = *self.unused.front().unwrap();
callback_info = Some(CallbackInfo {
callback_type: type_string.clone(),
success_parameters: parameters,
error_parameters: error_type,
bound_name,
});
}
} else if par.c_type == "GDestroyNotify" || env.library.type_(par.typ).is_function()
{
need_is_into_check = par.c_type != "GDestroyNotify";
if let Type::Function(_) = env.library.type_(par.typ) {
type_string = rust_type_with_scope(env, par.typ, par.scope, concurrency)
.into_string();
let bound_name = *self.unused.front().unwrap();
callback_info = Some(CallbackInfo {
callback_type: type_string.clone(),
success_parameters: String::new(),
error_parameters: String::new(),
bound_name,
});
}
}
if (!need_is_into_check || !*par.nullable)
&& par.c_type != "GDestroyNotify"
&& !self.add_parameter(&par.name, &type_string, bound_type, r#async)
{
panic!(
"Too many type constraints for {}",
func.c_identifier.as_ref().unwrap()
)
}
}
} else if par.instance_parameter {
if let Some(bound_type) = Bounds::type_for(env, par.typ, par.nullable) {
ret = Some(Bounds::get_to_glib_extra(&bound_type));
}
}
(ret, callback_info)
}
pub fn type_for(env: &Env, type_id: TypeId, nullable: Nullable) -> Option<BoundType> {
use self::BoundType::*;
match *env.library.type_(type_id) {
Type::Fundamental(Fundamental::Filename) => Some(AsRef(None)),
Type::Fundamental(Fundamental::OsString) => Some(AsRef(None)),
Type::Fundamental(Fundamental::Utf8) if *nullable => None,
Type::Class(Class { final_type, .. }) => {
if final_type {
None
} else {
Some(IsA(None))
}
}
Type::Interface(..) => Some(IsA(None)),
Type::List(_) | Type::SList(_) | Type::CArray(_) => None,
Type::Fundamental(_) if *nullable => None,
Type::Function(_) => Some(NoWrapper),
_ => None,
}
}
fn get_to_glib_extra(bound_type: &BoundType) -> String {
use self::BoundType::*;
match *bound_type {
AsRef(_) => ".as_ref()".to_owned(),
IsA(_) => ".as_ref()".to_owned(),
_ => String::new(),
}
}
pub fn add_parameter(
&mut self,
name: &str,
type_str: &str,
bound_type: BoundType,
r#async: bool,
) -> bool {
if r#async && name == "callback" {
if let Some(alias) = self.unused.pop_front() {
self.used.push(Bound {
bound_type: BoundType::NoWrapper,
parameter_name: name.to_owned(),
alias,
type_str: type_str.to_string(),
info_for_next_type: false,
callback_modified: false,
});
return true;
}
return false;
}
if self.used.iter().any(|n| n.parameter_name == name) {
return false;
}
let type_str = type_str.to_owned();
if let Some(alias) = self.unused.pop_front() {
self.used.push(Bound {
bound_type,
parameter_name: name.to_owned(),
alias,
type_str: type_str.to_owned(),
info_for_next_type: false,
callback_modified: false,
});
true
} else {
false
}
}
pub fn get_parameter_alias_info(&self, name: &str) -> Option<(char, BoundType)> {
self.used
.iter()
.find(move |n| {
if n.parameter_name == name {
!n.info_for_next_type
} else {
false
}
})
.map(|t| (t.alias, t.bound_type.clone()))
}
pub fn get_base_alias(&self, alias: char) -> Option<char> {
if alias == TYPE_PARAMETERS_START {
return None;
}
let prev_alias = ((alias as u8) - 1) as char;
self.used
.iter()
.find(move |n| n.alias == prev_alias)
.and_then(|b| {
if b.info_for_next_type {
Some(b.alias)
} else {
None
}
})
}
pub fn update_imports(&self, imports: &mut Imports) {
//TODO: import with versions
use self::BoundType::*;
for used in &self.used {
match used.bound_type {
NoWrapper => (),
IsA(_) => imports.add("glib::object::IsA"),
AsRef(_) => imports.add_used_type(&used.type_str),
}
}
}
pub fn is_empty(&self) -> bool {
self.used.is_empty()
}
pub fn iter(&self) -> Iter<'_, Bound> {
self.used.iter()
}
pub fn iter_lifetimes(&self) -> Iter<'_, char> {
self.lifetimes.iter()
}
}
#[derive(Clone, Debug)]
pub struct PropertyBound {
pub alias: char,
pub type_str: String,
}
impl PropertyBound {
pub fn get(env: &Env, type_id: TypeId) -> Option<PropertyBound> {
let type_ = env.type_(type_id);
if type_.is_final_type() {
return None;
}
Some(PropertyBound {
alias: TYPE_PARAMETERS_START,
type_str: bounds_rust_type(env, type_id).into_string(),
})
}
}
fn find_out_parameters(env: &Env, function: &Function) -> Vec<String> {
let index_to_ignore = find_index_to_ignore(&function.parameters);
function
.parameters
.iter()
.enumerate()
.filter(|&(index, param)| {
Some(index) != index_to_ignore
&& param.direction == ParameterDirection::Out
&& param.name != "error"
})
.map(|(_, param)| rust_type(env, param.typ).into_string())
.collect()
}
fn format_out_parameters(parameters: &[String]) -> String {
if parameters.len() == 1 {
parameters[0].to_string()
} else {
format!("({})", parameters.join(", "))
}
}
fn find_error_type(env: &Env, function: &Function) -> String {
let error_param = function
.parameters
.iter()
.find(|param| param.direction == ParameterDirection::Out && param.name == "error")
.expect("error type");
if let Type::Record(ref record) = *env.type_(error_param.typ) {
return record.name.clone();
}
panic!("cannot find error type")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_new_all() {
let mut bounds: Bounds = Default::default();
let typ = BoundType::IsA(None);
assert_eq!(bounds.add_parameter("a", "", typ.clone(), false), true);
// Don't add second time
assert_eq!(bounds.add_parameter("a", "", typ.clone(), false), false);
assert_eq!(bounds.add_parameter("b", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("c", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("d", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("e", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("f", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("g", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("h", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("h", "", typ.clone(), false), false);
assert_eq!(bounds.add_parameter("i", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("j", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("k", "", typ.clone(), false), true);
assert_eq!(bounds.add_parameter("l", "", typ, false), false);
}
#[test]
fn get_parameter_alias_info() {
let mut bounds: Bounds = Default::default();
let typ = BoundType::IsA(None);
bounds.add_parameter("a", "", typ.clone(), false);
bounds.add_parameter("b", "", typ.clone(), false);
assert_eq!(
bounds.get_parameter_alias_info("a"),
Some(('P', typ.clone()))
);
assert_eq!(bounds.get_parameter_alias_info("b"), Some(('Q', typ)));
assert_eq!(bounds.get_parameter_alias_info("c"), None);
}
}