summaryrefslogtreecommitdiff
path: root/codegen/command/functions.rs
blob: 6d1a3d3c6d8356a66378dec7cd1296291f84eb6b (plain)
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
use std::error::Error;
use std::str::FromStr;

use opengl_registry::command::Command;
use proc_macro2::{LexError, TokenStream};
use quote::{format_ident, quote};

use crate::codegen::formatting::{clean_param_name, fix_type};

pub fn create_command_functions(
    commands: &[Command],
) -> Result<Vec<TokenStream>, Box<dyn Error>>
{
    commands.iter().map(|command| {
        let command_name_str = command.prototype().name();

        let command_name = format_ident!("{}", command.prototype().name());

        let command_name_prefixless = format_ident!("{}", command.prototype()
            .name()
            .strip_prefix("gl")
            .unwrap_or_else(|| command.prototype().name()));

        let command_args = command
            .parameters()
            .iter()
            .map(|param| {
                let param_name = format_ident!("{}", clean_param_name(param.name()));

                let param_type = TokenStream::from_str(&fix_type(param.get_type()))?;

                Ok::<_, LexError>(quote! { #param_name: #param_type })
            }).collect::<Result<Vec<_>, _>>()?;

        let command_return_type = TokenStream::from_str(&fix_type(command.prototype().return_type()))?;

        let command_arg_types = command
            .parameters()
            .iter()
            .map(|param| TokenStream::from_str(&fix_type(param.get_type())))
            .collect::<Result<Vec<_>, _>>()?;

        let command_arg_names = command
            .parameters()
            .iter()
            .map(|param| format_ident!("{}", clean_param_name(param.name())))
            .collect::<Vec<_>>();

        Ok(quote! {
            #[doc = #command_name_str]
            #[allow(non_snake_case, clippy::too_many_arguments)]
            pub unsafe fn #command_name_prefixless(#(#command_args),*) -> #command_return_type {
                type GLFunc =
                    unsafe extern "C" fn(#(#command_arg_types),*) -> #command_return_type;

                let gl_func = std::mem::transmute::<_, GLFunc>(functions::#command_name);

                gl_func(#(#command_arg_names),*)
            }
        })
    }).collect()
}