| 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 StaticStructGenerator; |
| 20 |
|
| 21 |
impl super::Generator for StaticStructGenerator { |
| 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_struct(registry, dest)); |
| 30 |
try!(write_impl(registry, dest)); |
| 31 |
try!(write_fns(registry, dest)); |
| 32 |
Ok(()) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/// Creates a `__gl_imports` module which contains all the external symbols that we need for the |
| 37 |
/// bindings. |
| 38 |
fn write_header<W>(dest: &mut W) -> io::Result<()> |
| 39 |
where |
| 40 |
W: io::Write, |
| 41 |
{ |
| 42 |
writeln!( |
| 43 |
dest, |
| 44 |
r#" |
| 45 |
mod __gl_imports {{ |
| 46 |
pub use std::mem; |
| 47 |
pub use std::os::raw; |
| 48 |
}} |
| 49 |
"# |
| 50 |
) |
| 51 |
} |
| 52 |
|
| 53 |
/// Creates a `types` module which contains all the type aliases. |
| 54 |
/// |
| 55 |
/// See also `generators::gen_types`. |
| 56 |
fn write_type_aliases<W>(registry: &Registry, dest: &mut W) -> io::Result<()> |
| 57 |
where |
| 58 |
W: io::Write, |
| 59 |
{ |
| 60 |
try!(writeln!( |
| 61 |
dest, |
| 62 |
r#" |
| 63 |
pub mod types {{ |
| 64 |
#![allow(non_camel_case_types, non_snake_case, dead_code, missing_copy_implementations)] |
| 65 |
"# |
| 66 |
)); |
| 67 |
|
| 68 |
try!(super::gen_types(registry.api, dest)); |
| 69 |
|
| 70 |
writeln!(dest, "}}") |
| 71 |
} |
| 72 |
|
| 73 |
/// Creates all the `<enum>` elements at the root of the bindings. |
| 74 |
fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()> |
| 75 |
where |
| 76 |
W: io::Write, |
| 77 |
{ |
| 78 |
for enm in ®istry.enums { |
| 79 |
try!(super::gen_enum_item(enm, "types::", dest)); |
| 80 |
} |
| 81 |
|
| 82 |
Ok(()) |
| 83 |
} |
| 84 |
|
| 85 |
/// Creates a stub structure. |
| 86 |
/// |
| 87 |
/// The name of the struct corresponds to the namespace. |
| 88 |
fn write_struct<W>(registry: &Registry, dest: &mut W) -> io::Result<()> |
| 89 |
where |
| 90 |
W: io::Write, |
| 91 |
{ |
| 92 |
writeln!( |
| 93 |
dest, |
| 94 |
" |
| 95 |
#[allow(non_camel_case_types, non_snake_case, dead_code)] |
| 96 |
#[derive(Copy, Clone)] |
| 97 |
pub struct {api};", |
| 98 |
api = super::gen_struct_name(registry.api), |
| 99 |
) |
| 100 |
} |
| 101 |
|
| 102 |
/// Creates the `impl` of the structure created by `write_struct`. |
| 103 |
fn write_impl<W>(registry: &Registry, dest: &mut W) -> io::Result<()> |
| 104 |
where |
| 105 |
W: io::Write, |
| 106 |
{ |
| 107 |
try!(writeln!(dest, |
| 108 |
"impl {api} {{ |
| 109 |
/// Stub function. |
| 110 |
#[allow(dead_code)] |
| 111 |
pub fn load_with<F>(mut _loadfn: F) -> {api} where F: FnMut(&'static str) -> *const __gl_imports::raw::c_void {{ |
| 112 |
{api} |
| 113 |
}}", |
| 114 |
api = super::gen_struct_name(registry.api), |
| 115 |
)); |
| 116 |
|
| 117 |
for cmd in ®istry.cmds { |
| 118 |
try!(writeln!( |
| 119 |
dest, |
| 120 |
"#[allow(non_snake_case)] |
| 121 |
// #[allow(unused_variables)] |
| 122 |
#[allow(dead_code)] |
| 123 |
#[inline] |
| 124 |
pub unsafe fn {name}(&self, {typed_params}) -> {return_suffix} {{ |
| 125 |
{name}({idents}) |
| 126 |
}}", |
| 127 |
name = cmd.proto.ident, |
| 128 |
typed_params = super::gen_parameters(cmd, true, true).join(", "), |
| 129 |
return_suffix = cmd.proto.ty, |
| 130 |
idents = super::gen_parameters(cmd, true, false).join(", "), |
| 131 |
)); |
| 132 |
} |
| 133 |
|
| 134 |
writeln!(dest, "}}") |
| 135 |
} |
| 136 |
|
| 137 |
/// io::Writes all functions corresponding to the GL bindings. |
| 138 |
/// |
| 139 |
/// These are foreign functions, they don't have any content. |
| 140 |
fn write_fns<W>(registry: &Registry, dest: &mut W) -> io::Result<()> |
| 141 |
where |
| 142 |
W: io::Write, |
| 143 |
{ |
| 144 |
try!(writeln!( |
| 145 |
dest, |
| 146 |
" |
| 147 |
#[allow(non_snake_case)] |
| 148 |
#[allow(unused_variables)] |
| 149 |
#[allow(dead_code)] |
| 150 |
extern \"system\" {{" |
| 151 |
)); |
| 152 |
|
| 153 |
for cmd in ®istry.cmds { |
| 154 |
try!(writeln!( |
| 155 |
dest, |
| 156 |
"#[link_name=\"{symbol}\"] fn {name}({params}) -> {return_suffix};", |
| 157 |
symbol = super::gen_symbol_name(registry.api, &cmd.proto.ident), |
| 158 |
name = cmd.proto.ident, |
| 159 |
params = super::gen_parameters(cmd, true, true).join(", "), |
| 160 |
return_suffix = cmd.proto.ty, |
| 161 |
)); |
| 162 |
} |
| 163 |
|
| 164 |
writeln!(dest, "}}") |
| 165 |
} |