/
object.rs
372 lines (323 loc) · 10.2 KB
/
object.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
371
372
use std::ops::Deref;
use config::gobjects::GObject;
use env::Env;
use library;
use nameutil::*;
use super::*;
use super::child_properties::ChildProperties;
use super::imports::Imports;
use super::info_base::InfoBase;
use super::signatures::Signatures;
use traits::*;
#[derive(Debug, Default)]
pub struct Info {
pub base: InfoBase,
pub c_type: String,
pub c_class_type: Option<String>,
pub rust_class_type: Option<String>,
pub get_type: String,
pub is_interface: bool,
pub supertypes: Vec<general::StatusedTypeId>,
pub final_type: bool,
pub generate_trait: bool,
pub trait_name: String,
pub has_constructors: bool,
pub has_methods: bool,
pub has_functions: bool,
pub signals: Vec<signals::Info>,
pub notify_signals: Vec<signals::Info>,
pub trampolines: trampolines::Trampolines,
pub properties: Vec<properties::Property>,
pub child_properties: ChildProperties,
pub signatures: Signatures,
}
impl Info {
pub fn has_signals(&self) -> bool {
self.signals.iter().any(|s| s.trampoline_name.is_ok())
|| self.notify_signals
.iter()
.any(|s| s.trampoline_name.is_ok())
}
pub fn has_action_signals(&self) -> bool {
self.signals.iter().any(|s| s.action_emit_name.is_some())
}
}
impl Deref for Info {
type Target = InfoBase;
fn deref(&self) -> &InfoBase {
&self.base
}
}
pub fn has_known_subtypes(env: &Env, class_tid: library::TypeId) -> bool {
for child_tid in env.class_hierarchy.subtypes(class_tid) {
let child_name = child_tid.full_name(&env.library);
let status = env.config
.objects
.get(&child_name)
.map(|o| o.status)
.unwrap_or_default();
if status.normal() {
return true;
}
}
false
}
pub fn class(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<Info> {
info!("Analyzing class {}", obj.name);
let full_name = obj.name.clone();
let class_tid = match env.library.find_type(0, &full_name) {
Some(tid) => tid,
None => return None,
};
let type_ = env.type_(class_tid);
let name: String = split_namespace_name(&full_name).1.into();
let klass: &library::Class = match type_.maybe_ref() {
Some(klass) => klass,
None => return None,
};
let mut imports = Imports::with_defined(&env.library, &name);
imports.add("glib::translate::*", None);
imports.add("ffi", None);
if obj.generate_display_trait {
imports.add("std::fmt", None);
}
let supertypes = supertypes::analyze(env, class_tid, &mut imports);
let final_type = obj.final_type.unwrap_or(klass.c_class_type.is_none() && !has_known_subtypes(env, class_tid));
// We would like to generate a trait if this is not a final type as there can be
// subtypes, however this will be overridden again below if there is nothing that
// would be put into this trait.
let generate_trait = !final_type;
let trait_name = obj.trait_name
.as_ref()
.cloned()
.unwrap_or_else(|| format!("{}Ext", name));
// Sanity check the user's configuration. It's unlikely that a final type is
// wanted if there are subtypes in this very crate
if final_type && has_known_subtypes(env, class_tid) {
error!(
"Type {} marked as final although subtypes exist",
full_name
);
}
let mut trampolines =
trampolines::Trampolines::with_capacity(klass.signals.len() + klass.properties.len());
let mut signatures = Signatures::with_capacity(klass.functions.len());
let mut functions = functions::analyze(
env,
&klass.functions,
class_tid,
generate_trait,
obj,
&mut imports,
Some(&mut signatures),
Some(deps),
);
let mut specials = special_functions::extract(&mut functions);
// `copy` will duplicate an object while `clone` just adds a reference
special_functions::unhide(&mut functions, &specials, special_functions::Type::Copy);
// these are all automatically derived on objects and compare by pointer. If such functions
// exist they will provide additional functionality
for t in &[special_functions::Type::Hash,
special_functions::Type::Equal,
special_functions::Type::Compare,
special_functions::Type::ToString,
] {
special_functions::unhide(&mut functions, &specials, *t);
specials.remove(t);
}
special_functions::analyze_imports(&specials, &mut imports);
let signals = signals::analyze(
env,
&klass.signals,
class_tid,
generate_trait,
&mut trampolines,
obj,
&mut imports,
);
let (properties, notify_signals) = properties::analyze(
env,
&klass.properties,
class_tid,
generate_trait,
&mut trampolines,
obj,
&mut imports,
&signatures,
deps,
);
let (version, deprecated_version) = info_base::versions(
env,
obj,
&functions,
klass.version,
klass.deprecated_version,
);
let child_properties =
child_properties::analyze(env, obj.child_properties.as_ref(), class_tid, &mut imports);
let has_methods = functions
.iter()
.any(|f| f.kind == library::FunctionKind::Method);
let has_signals = signals.iter().any(|s| s.trampoline_name.is_ok())
|| notify_signals.iter().any(|s| s.trampoline_name.is_ok());
// There's no point in generating a trait if there are no signals, methods, properties
// and child properties: it would be empty
//
// There's also no point in generating a trait for final types: there are no possible subtypes
let generate_trait = !final_type && (has_signals || has_methods || !properties.is_empty() || !child_properties.is_empty());
if generate_trait {
imports.add("glib::object::IsA", None);
}
if obj.concurrency == library::Concurrency::SendUnique {
imports.add("glib::ObjectExt", None);
}
let rust_class_type = Some(format!("{}Class", name));
let base = InfoBase {
full_name,
type_id: class_tid,
name,
functions,
specials,
imports,
version,
deprecated_version,
cfg_condition: obj.cfg_condition.clone(),
concurrency: obj.concurrency,
};
// patch up trait methods in the symbol table
if generate_trait {
let mut symbols = env.symbols.borrow_mut();
for func in base.methods() {
if let Some(symbol) = symbols.by_c_name_mut(&func.glib_name) {
symbol.make_trait_method(&trait_name);
}
}
}
let has_constructors = !base.constructors().is_empty();
let has_functions = !base.functions().is_empty();
let info = Info {
base,
c_type: klass.c_type.clone(),
c_class_type: klass.c_class_type.clone(),
rust_class_type,
get_type: klass.glib_get_type.clone(),
is_interface: false,
supertypes,
final_type,
generate_trait,
trait_name,
has_constructors,
has_methods,
has_functions,
signals,
notify_signals,
trampolines,
properties,
child_properties,
signatures,
};
Some(info)
}
pub fn interface(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<Info> {
info!("Analyzing interface {}", obj.name);
let full_name = obj.name.clone();
let iface_tid = match env.library.find_type(0, &full_name) {
Some(tid) => tid,
None => return None,
};
let type_ = env.type_(iface_tid);
let name: String = split_namespace_name(&full_name).1.into();
let iface: &library::Interface = match type_.maybe_ref() {
Some(iface) => iface,
None => return None,
};
let mut imports = Imports::with_defined(&env.library, &name);
imports.add("glib::translate::*", None);
imports.add("ffi", None);
imports.add("glib::object::IsA", None);
if obj.generate_display_trait {
imports.add("std::fmt", None);
}
let supertypes = supertypes::analyze(env, iface_tid, &mut imports);
let trait_name = obj.trait_name
.as_ref()
.cloned()
.unwrap_or_else(|| format!("{}Ext", name));
let mut trampolines = trampolines::Trampolines::with_capacity(iface.signals.len());
let mut signatures = Signatures::with_capacity(iface.functions.len());
let functions = functions::analyze(
env,
&iface.functions,
iface_tid,
true,
obj,
&mut imports,
Some(&mut signatures),
Some(deps),
);
let signals = signals::analyze(
env,
&iface.signals,
iface_tid,
true,
&mut trampolines,
obj,
&mut imports,
);
let (properties, notify_signals) = properties::analyze(
env,
&iface.properties,
iface_tid,
true,
&mut trampolines,
obj,
&mut imports,
&signatures,
deps,
);
let (version, deprecated_version) = info_base::versions(
env,
obj,
&functions,
iface.version,
iface.deprecated_version,
);
if obj.concurrency == library::Concurrency::SendUnique {
imports.add("glib::ObjectExt", None);
}
let base = InfoBase {
full_name,
type_id: iface_tid,
name,
functions,
specials: Default::default(),
imports,
version,
deprecated_version,
cfg_condition: obj.cfg_condition.clone(),
concurrency: obj.concurrency,
};
let has_methods = !base.methods().is_empty();
let has_functions = !base.functions().is_empty();
let info = Info {
base,
c_type: iface.c_type.clone(),
c_class_type: None,
rust_class_type: None,
get_type: iface.glib_get_type.clone(),
is_interface: true,
supertypes,
final_type: false,
generate_trait: true,
trait_name,
has_methods,
has_functions,
signals,
notify_signals,
trampolines,
properties,
signatures,
..Default::default()
};
Some(info)
}