forked from gtk-rs/gir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.rs
173 lines (159 loc) · 5.07 KB
/
fields.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
use crate::{
analysis::{rust_type::*, types::*},
codegen::sys::{ffi_type::ffi_type, functions::function_signature},
env::Env,
library::*,
traits::{IntoString, MaybeRefAs},
};
pub struct Fields {
/// Name of union, class, or a record that contains the fields.
pub name: String,
/// Is this external type?
pub external: bool,
/// Reason for truncating the representation, if any.
pub truncated: Option<String>,
derives_copy: bool,
/// "struct" or "union"
pub kind: &'static str,
/// specified GObject cfg condition
pub cfg_condition: Option<String>,
pub fields: Vec<FieldInfo>,
}
pub struct FieldInfo {
/// Rust field name
pub name: String,
/// Rust type name
pub typ: String,
/// Does access to this field require unsafe block?
unsafe_access: bool,
/// Include this field in Debug impl?
pub debug: bool,
}
impl Fields {
/// List of derived traits
pub fn derived_traits(&self) -> Vec<&'static str> {
let mut traits = Vec::new();
if self.derives_copy {
traits.push("Copy");
traits.push("Clone");
}
traits
}
}
impl FieldInfo {
/// Generates a string that accesses the field in the context of &self
/// receiver.
pub fn access_str(&self) -> String {
let mut s = format!("&self.{}", self.name);
if self.unsafe_access {
s = format!("unsafe {{ {s} }}");
}
s
}
}
pub fn from_record(env: &Env, record: &Record) -> Fields {
let (fields, truncated) = analyze_fields(env, false, &record.fields);
let derives_copy = truncated.is_none() && record.derives_copy(&env.library);
Fields {
name: record.c_type.clone(),
external: record.is_external(&env.library),
truncated,
derives_copy,
kind: "struct",
cfg_condition: get_gobject_cfg_condition(env, &record.name),
fields,
}
}
pub fn from_class(env: &Env, klass: &Class) -> Fields {
let (fields, truncated) = analyze_fields(env, false, &klass.fields);
let derives_copy = truncated.is_none() && klass.derives_copy(&env.library);
Fields {
name: klass.c_type.clone(),
external: klass.is_external(&env.library),
truncated,
derives_copy,
kind: "struct",
cfg_condition: get_gobject_cfg_condition(env, &klass.name),
fields,
}
}
pub fn from_union(env: &Env, union: &Union) -> Fields {
let (fields, truncated) = analyze_fields(env, true, &union.fields);
let derives_copy = truncated.is_none() && union.derives_copy(&env.library);
Fields {
name: union.c_type.as_ref().unwrap().clone(),
external: union.is_external(&env.library),
truncated,
derives_copy,
kind: "union",
cfg_condition: None,
fields,
}
}
fn analyze_fields(
env: &Env,
unsafe_access: bool,
fields: &[Field],
) -> (Vec<FieldInfo>, Option<String>) {
let mut truncated = None;
let mut infos = Vec::with_capacity(fields.len());
let mut is_bitfield = false;
for field in fields {
// See IsIncomplete for &[Field].
if is_bitfield && field.bits.is_some() {
truncated = Some(format!("field {} has incomplete type", &field.name));
break;
}
is_bitfield = field.bits.is_some();
let typ = match field_ffi_type(env, field) {
e @ Err(..) => {
truncated = Some(e.into_string());
break;
}
Ok(typ) => typ,
};
// Skip private fields from Debug impl. Ignore volatile as well,
// they are usually used as synchronization primites,
// so we wouldn't want to introduce additional reads.
let debug = !field.private && !field.is_volatile() && field.implements_debug(&env.library);
infos.push(FieldInfo {
name: field.name.clone(),
typ: typ.into_string(),
debug,
unsafe_access,
});
}
(infos, truncated)
}
fn field_ffi_type(env: &Env, field: &Field) -> Result {
if field.is_incomplete(&env.library) {
return Err(TypeError::Ignored(format!(
"field {} has incomplete type",
&field.name
)));
}
if let Some(ref c_type) = field.c_type {
ffi_type(env, field.typ, c_type)
} else if let Some(func) = env.library.type_(field.typ).maybe_ref_as::<Function>() {
let (failure, signature) = function_signature(env, func, true);
let signature = format!("Option<unsafe extern \"C\" fn{signature}>");
if failure {
Err(TypeError::Unimplemented(signature))
} else {
Ok(signature.into())
}
} else {
Err(TypeError::Ignored(format!(
"field {} has empty c:type",
&field.name
)))
}
}
fn get_gobject_cfg_condition(env: &Env, name: &str) -> Option<String> {
let full_name = format!("{}.{}", env.namespaces.main().name, name);
if let Some(obj) = env.config.objects.get(&full_name) {
obj.cfg_condition.clone()
} else {
None
}
}