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
use registry::{Enum, Registry, Cmd, Ns};
use std::io;
mod ty;
pub mod debug_struct_gen;
pub mod global_gen;
pub mod static_gen;
pub mod struct_gen;
pub mod static_struct_gen;
pub trait Generator {
fn write<W>(&self, registry: &Registry, ns: Ns, dest: &mut W) -> io::Result<()> where W: io::Write;
}
pub fn gen_enum_item<W>(enm: &Enum, types_prefix: &str, dest: &mut W) -> io::Result<()> where W: io::Write {
let ident = if (enm.ident.chars().next().unwrap()).is_numeric() {
format!("_{}", enm.ident)
} else {
enm.ident.clone()
};
let val_regexed = {
if enm.value.starts_with("((") && enm.value.ends_with(")") {
let separator = enm.value.chars().skip(2).position(|c| c == ')').unwrap();
Some((&enm.value[2 .. separator + 2], enm.value[separator + 3 ..].trim_matches(')')))
} else {
None
}
};
let ty = {
if let Some((ty, _)) = val_regexed {
format!("{}{}", types_prefix, ty)
} else if enm.value.starts_with("\"") {
"&'static str".to_string()
} else {
match &ident[..] {
"TRUE" | "FALSE" => format!("{}GLboolean", types_prefix),
_ => match enm.ty {
Some(ref s) if &s[..] == "ull" => format!("{}GLuint64", types_prefix),
_ => format!("{}GLenum", types_prefix)
}
}
}
};
let value = {
if let Some((ty, val)) = val_regexed {
format!("{} as {}{}", val, types_prefix, ty)
} else {
enm.value.clone()
}
};
writeln!(dest, "\
#[allow(dead_code)]
#[allow(non_upper_case_globals)]
pub const {}: {} = {}; \
", ident, ty, value)
}
pub fn gen_type_aliases<W>(namespace: &Ns, dest: &mut W) -> io::Result<()> where W: io::Write {
match *namespace {
Ns::Gl | Ns::Gles1 | Ns::Gles2 => {
try!(ty::build_gl_aliases(dest));
}
Ns::Glx => {
try!(ty::build_gl_aliases(dest));
try!(ty::build_x_aliases(dest));
try!(ty::build_glx_aliases(dest));
}
Ns::Wgl => {
try!(ty::build_gl_aliases(dest));
try!(ty::build_win_aliases(dest));
try!(ty::build_wgl_aliases(dest));
}
Ns::Egl => {
try!(ty::build_gl_aliases(dest));
try!(ty::build_egl_aliases(dest));
}
}
Ok(())
}
pub fn gen_parameters(cmd: &Cmd, with_idents: bool, with_types: bool) -> Vec<String> {
cmd.params.iter()
.map(|binding| {
let ident = match &binding.ident[..] {
"in" => "in_",
"ref" => "ref_",
"type" => "type_",
ident => ident,
};
let ty = ty::to_rust_ty(&binding.ty[..]);
if with_idents && with_types {
format!("{}: {}", ident, ty)
} else if with_types {
format!("{}", ty)
} else if with_idents {
format!("{}", ident)
} else {
panic!()
}
})
.collect()
}
pub fn gen_return_type(cmd: &Cmd) -> String {
let ty = ty::to_rust_ty(&cmd.proto.ty);
if ty == "__gl_imports::libc::c_void" {
return "()".to_string();
}
ty.to_string()
}
pub fn gen_symbol_name(ns: &Ns, cmd: &str) -> String {
(match *ns {
Ns::Gl | Ns::Gles1 | Ns::Gles2 => "gl",
Ns::Glx => "glX",
Ns::Wgl => "wgl",
Ns::Egl => "egl",
}).to_string() + cmd
}