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
//! Functions for actually parsing the source file.
use crate::common::{Lang, Outputs};
use crate::Error;
use unwrap::unwrap;
pub fn parse_usetree(usetree: &syn::UseTree) -> Vec<String> {
let mut modules = Vec::new();
if let syn::UseTree::Path(ref path) = usetree {
modules.push(path.ident.to_owned().to_string());
for x in parse_usetree(&*path.tree) {
modules.push(x);
}
};
if let syn::UseTree::Name(ref name) = usetree {
modules.push(name.ident.to_owned().to_string());
};
if let syn::UseTree::Glob(ref _glob) = usetree {
return modules;
};
modules
}
/// Returns a list of FFI submodules imported in a top-level module
pub fn imported_mods(import: &syn::ItemUse) -> Option<Vec<String>> {
// If it's not visible it can't be called from C.
if let syn::Visibility::Inherited = import.vis {
None
} else {
let mut segments = parse_usetree(&import.tree);
if segments[0] == "crate" {
segments.remove(0);
}
if &segments[0] == "ffi" {
Some(segments)
} else {
None
}
}
}
/// The manager of bindgen and entry point when the crate is the module.
///
/// Iterates through all items in the module and dispatches to correct methods, then pulls all
/// the results together into a header.
pub fn parse_file<L: Lang>(
lang: &mut L,
module: &syn::File,
mod_path: &[String],
outputs: &mut Outputs,
) -> Result<(), Vec<Error>> {
let mut errors = vec![];
for item in module.items.to_owned() {
// If it's not visible it can't be called from C.
match item {
syn::Item::Mod(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Const(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Type(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Enum(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Fn(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Struct(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
_ => {}
}
// Dispatch to correct method.
let res = match item {
syn::Item::Mod(ref item) => {
parse_mod(lang, item, mod_path, outputs)?;
Ok(())
}
syn::Item::Const(ref item) => {
lang.parse_const(item, mod_path, outputs)?;
Ok(())
}
syn::Item::Type(ref item) => {
lang.parse_ty(item, mod_path, outputs)?;
Ok(())
}
syn::Item::Enum(ref item) => {
lang.parse_enum(item, mod_path, outputs)?;
Ok(())
}
syn::Item::Fn(ref item) => {
lang.parse_fn(item, mod_path, outputs)?;
Ok(())
}
syn::Item::Struct(ref item) => {
lang.parse_struct(&item, mod_path, outputs)?;
Ok(())
}
_ => Ok(()),
};
// Display any non-fatal errors, fatal errors are handled at cause.
if let Err(error) = res {
errors.push(error)
}
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
pub fn parse_mod<L: Lang>(
lang: &mut L,
module: &syn::ItemMod,
mod_path: &[String],
outputs: &mut Outputs,
) -> Result<(), Vec<Error>> {
let mut errors = vec![];
if module.to_owned().content.is_some() {
for item in unwrap!(module.to_owned().content).1 {
// If it's not visible it can't be called from C.
match item {
syn::Item::Mod(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Const(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Type(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Enum(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Fn(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
syn::Item::Struct(ref item) => {
if let syn::Visibility::Inherited = item.vis {
continue;
}
if let syn::Visibility::Crate(_) = item.vis {
continue;
}
}
_ => {}
}
// Dispatch to correct method.
let res = match item {
syn::Item::Mod(ref item) => {
parse_mod(lang, item, mod_path, outputs)?;
Ok(())
}
syn::Item::Const(ref item) => {
lang.parse_const(item, mod_path, outputs)?;
Ok(())
}
syn::Item::Type(ref item) => {
lang.parse_ty(item, mod_path, outputs)?;
Ok(())
}
syn::Item::Enum(ref item) => {
lang.parse_enum(item, mod_path, outputs)?;
Ok(())
}
syn::Item::Fn(ref item) => {
lang.parse_fn(item, mod_path, outputs)?;
Ok(())
}
syn::Item::Struct(ref item) => {
lang.parse_struct(&item, mod_path, outputs)?;
Ok(())
}
_ => Ok(()),
};
// Display any non-fatal errors, fatal errors are handled at cause.
if let Err(error) = res {
errors.push(error)
}
}
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}