Debian Codegraph @

third_party/rust/gl_generator/generators/static_gen.rs

raw
1 // Copyright 2015 Brendan Zabarauskas and the gl-rs developers
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use registry::Registry;
16 use std::io;
17
18 #[allow(missing_copy_implementations)]
19 pub struct StaticGenerator;
20
21 impl super::Generator for StaticGenerator {
22 fn write<W>(&self, registry: &Registry, dest: &mut W) -> io::Result<()>
23 where
24 W: io::Write,
25 {
26 try!(write_header(dest));
27 try!(write_type_aliases(registry, dest));
28 try!(write_enums(registry, dest));
29 try!(write_fns(registry, dest));
30 Ok(())
31 }
32 }
33
34 /// Creates a `__gl_imports` module which contains all the external symbols that we need for the
35 /// bindings.
36 fn write_header<W>(dest: &mut W) -> io::Result<()>
37 where
38 W: io::Write,
39 {
40 writeln!(
41 dest,
42 r#"
43 mod __gl_imports {{
44 pub use std::mem;
45 pub use std::os::raw;
46 }}
47 "#
48 )
49 }
50
51 /// Creates a `types` module which contains all the type aliases.
52 ///
53 /// See also `generators::gen_types`.
54 fn write_type_aliases<W>(registry: &Registry, dest: &mut W) -> io::Result<()>
55 where
56 W: io::Write,
57 {
58 try!(writeln!(
59 dest,
60 r#"
61 pub mod types {{
62 #![allow(non_camel_case_types, non_snake_case, dead_code, missing_copy_implementations)]
63 "#
64 ));
65
66 try!(super::gen_types(registry.api, dest));
67
68 writeln!(
69 dest,
70 "
71 }}
72 "
73 )
74 }
75
76 /// Creates all the `<enum>` elements at the root of the bindings.
77 fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()>
78 where
79 W: io::Write,
80 {
81 for enm in &registry.enums {
82 try!(super::gen_enum_item(enm, "types::", dest));
83 }
84
85 Ok(())
86 }
87
88 /// io::Writes all functions corresponding to the GL bindings.
89 ///
90 /// These are foreign functions, they don't have any content.
91 fn write_fns<W>(registry: &Registry, dest: &mut W) -> io::Result<()>
92 where
93 W: io::Write,
94 {
95 try!(writeln!(
96 dest,
97 "
98 #[allow(non_snake_case, unused_variables, dead_code)]
99 extern \"system\" {{"
100 ));
101
102 for cmd in &registry.cmds {
103 try!(writeln!(
104 dest,
105 "#[link_name=\"{symbol}\"]
106 pub fn {name}({params}) -> {return_suffix};",
107 symbol = super::gen_symbol_name(registry.api, &cmd.proto.ident),
108 name = cmd.proto.ident,
109 params = super::gen_parameters(cmd, true, true).join(", "),
110 return_suffix = cmd.proto.ty,
111 ));
112 }
113
114 writeln!(dest, "}}")
115 }