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
use registry::{Registry, Ns};
use std::io;
#[allow(missing_copy_implementations)]
pub struct DebugStructGenerator;
impl super::Generator for DebugStructGenerator {
fn write<W>(&self, registry: &Registry, ns: Ns, dest: &mut W) -> io::Result<()> where W: io::Write {
try!(write_header(dest));
try!(write_type_aliases(&ns, dest));
try!(write_enums(registry, dest));
try!(write_fnptr_struct_def(dest));
try!(write_panicking_fns(&ns, dest));
try!(write_struct(registry, &ns, dest));
try!(write_impl(registry, &ns, dest));
Ok(())
}
}
fn write_header<W>(dest: &mut W) -> io::Result<()> where W: io::Write {
writeln!(dest, r#"
mod __gl_imports {{
extern crate gl_common;
extern crate libc;
pub use std::mem;
pub use std::marker::Send;
}}
"#)
}
fn write_type_aliases<W>(ns: &Ns, dest: &mut W) -> io::Result<()> where W: io::Write {
try!(writeln!(dest, r#"
pub mod types {{
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(missing_copy_implementations)]
"#));
try!(super::gen_type_aliases(ns, dest));
writeln!(dest, "}}")
}
fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write {
for e in registry.enum_iter() {
try!(super::gen_enum_item(e, "types::", dest));
}
Ok(())
}
fn write_fnptr_struct_def<W>(dest: &mut W) -> io::Result<()> where W: io::Write {
writeln!(dest, "
#[allow(dead_code)]
#[allow(missing_copy_implementations)]
pub struct FnPtr {{
/// The function pointer that will be used when calling the function.
f: *const __gl_imports::libc::c_void,
/// True if the pointer points to a real function, false if points to a `panic!` fn.
is_loaded: bool,
}}
impl FnPtr {{
/// Creates a `FnPtr` from a load attempt.
fn new(ptr: *const __gl_imports::libc::c_void) -> FnPtr {{
if ptr.is_null() {{
FnPtr {{
f: missing_fn_panic as *const __gl_imports::libc::c_void,
is_loaded: false
}}
}} else {{
FnPtr {{ f: ptr, is_loaded: true }}
}}
}}
/// Returns `true` if the function has been successfully loaded.
///
/// If it returns `false`, calling the corresponding function will fail.
#[inline]
#[allow(dead_code)]
pub fn is_loaded(&self) -> bool {{
self.is_loaded
}}
}}
")
}
fn write_panicking_fns<W>(ns: &Ns, dest: &mut W) -> io::Result<()> where W: io::Write {
writeln!(dest,
"#[inline(never)]
fn missing_fn_panic() -> ! {{
panic!(\"{ns} function was not loaded\")
}}",
ns = ns
)
}
fn write_struct<W>(registry: &Registry, ns: &Ns, dest: &mut W) -> io::Result<()> where W: io::Write {
try!(writeln!(dest, "
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(dead_code)]
pub struct {ns} {{",
ns = ns.fmt_struct_name()
));
for c in registry.cmd_iter() {
if let Some(v) = registry.aliases.get(&c.proto.ident) {
try!(writeln!(dest, "/// Fallbacks: {}", v.connect(", ")));
}
try!(writeln!(dest, "pub {name}: FnPtr,", name = c.proto.ident));
}
writeln!(dest, "}}")
}
fn write_impl<W>(registry: &Registry, ns: &Ns, dest: &mut W) -> io::Result<()> where W: io::Write {
try!(writeln!(dest,
"impl {ns} {{
/// Load each OpenGL symbol using a custom load function. This allows for the
/// use of functions like `glfwGetProcAddress` or `SDL_GL_GetProcAddress`.
///
/// ~~~ignore
/// let gl = Gl::load_with(|s| glfw.get_proc_address(s));
/// ~~~
#[allow(dead_code)]
#[allow(unused_variables)]
pub fn load_with<F>(mut loadfn: F) -> {ns} where F: FnMut(&str) -> *const __gl_imports::libc::c_void {{
let mut metaloadfn = |symbol: &str, symbols: &[&str]| {{
let mut ptr = loadfn(symbol);
if ptr.is_null() {{
for &sym in symbols.iter() {{
ptr = loadfn(sym);
if !ptr.is_null() {{ break; }}
}}
}}
ptr
}};
{ns} {{",
ns = ns.fmt_struct_name()
));
for c in registry.cmd_iter() {
try!(writeln!(dest,
"{name}: FnPtr::new(metaloadfn(\"{symbol}\", &[{fallbacks}])),",
name = c.proto.ident,
symbol = super::gen_symbol_name(ns, &c.proto.ident),
fallbacks = match registry.aliases.get(&c.proto.ident) {
Some(fbs) => {
fbs.iter()
.map(|name| format!("\"{}\"", super::gen_symbol_name(ns, &name)))
.collect::<Vec<_>>().connect(", ")
},
None => format!(""),
},
))
}
try!(writeln!(dest,
"}}
}}
/// Load each OpenGL symbol using a custom load function.
///
/// ~~~ignore
/// let gl = Gl::load(&glfw);
/// ~~~
#[allow(dead_code)]
#[allow(unused_variables)]
pub fn load<T: __gl_imports::gl_common::GlFunctionsSource>(loader: &T) -> {ns} {{
{ns}::load_with(|name| loader.get_proc_addr(name))
}}",
ns = ns.fmt_struct_name()
));
for c in registry.cmd_iter() {
let idents = super::gen_parameters(c, true, false);
let typed_params = super::gen_parameters(c, false, true);
let println = format!("println!(\"[OpenGL] {}({})\" {});",
c.proto.ident,
(0 .. idents.len()).map(|_| "{:?}".to_string()).collect::<Vec<_>>().connect(", "),
idents.iter().zip(typed_params.iter())
.map(|(name, ty)| {
if ty.contains("GLDEBUGPROC") {
format!(", \"<callback>\"")
} else {
format!(", {}", name)
}
}).collect::<Vec<_>>().concat());
try!(writeln!(dest,
"#[allow(non_snake_case)] #[allow(unused_variables)] #[allow(dead_code)]
#[inline] pub unsafe fn {name}(&self, {params}) -> {return_suffix} {{ \
{println}
__gl_imports::mem::transmute::<_, extern \"system\" fn({typed_params}) -> {return_suffix}>\
(self.{name}.f)({idents}) \
}}",
name = c.proto.ident,
params = super::gen_parameters(c, true, true).connect(", "),
typed_params = typed_params.connect(", "),
return_suffix = super::gen_return_type(c),
idents = idents.connect(", "),
println = println
))
}
writeln!(dest,
"}}
unsafe impl __gl_imports::Send for {ns} {{}}",
ns = ns.fmt_struct_name()
)
}