summaryrefslogtreecommitdiffstats
path: root/vendor/windows-bindgen/src/rust/constants.rs
blob: 672dbcc8afa50fdcd9a8a6c3da41bc56fb39e93b (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
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
use super::*;

pub fn writer(writer: &Writer, def: Field) -> TokenStream {
    let name = to_ident(writer.reader.field_name(def));
    let ty = writer.reader.field_type(def, None).to_const_type();
    let cfg = field_cfg(writer.reader, def);
    let doc = writer.cfg_doc(&cfg);
    let features = writer.cfg_features(&cfg);

    if let Some(constant) = writer.reader.field_constant(def) {
        let constant_type = writer.reader.constant_type(constant);

        if ty == constant_type {
            if ty == Type::String {
                let crate_name = writer.crate_name();
                if field_is_ansi(writer.reader, def) {
                    let value = writer.value(&writer.reader.constant_value(constant));
                    quote! {
                        #doc
                        #features
                        pub const #name: #crate_name PCSTR = #crate_name s!(#value);
                    }
                } else {
                    let value = writer.value(&writer.reader.constant_value(constant));
                    quote! {
                        #doc
                        #features
                        pub const #name: #crate_name PCWSTR = #crate_name w!(#value);
                    }
                }
            } else {
                let value = writer.typed_value(&writer.reader.constant_value(constant));
                quote! {
                    #doc
                    #features
                    pub const #name: #value;
                }
            }
        } else {
            let kind = writer.type_default_name(&ty);
            let value = writer.value(&writer.reader.constant_value(constant));
            let underlying_type = type_underlying_type(writer.reader, &ty);

            let value = if underlying_type == constant_type {
                value
            } else if writer.std && underlying_type == Type::ISize {
                quote! { ::core::ptr::invalid_mut(#value as _) }
            } else {
                quote! { #value as _ }
            };

            if !writer.sys && type_has_replacement(writer.reader, &ty) {
                quote! {
                    #doc
                    #features
                    pub const #name: #kind = #kind(#value);
                }
            } else {
                quote! {
                    #doc
                    #features
                    pub const #name: #kind = #value;
                }
            }
        }
    } else if let Some(guid) = field_guid(writer.reader, def) {
        let value = writer.guid(&guid);
        let guid = writer.type_name(&Type::GUID);
        quote! {
            #doc
            pub const #name: #guid = #value;
        }
    } else if let Some(value) = initializer(writer, def) {
        let kind = writer.type_default_name(&ty);

        quote! {
            #doc
            #features
            pub const #name: #kind = #kind { #value };
        }
    } else {
        quote! {}
    }
}

fn initializer(writer: &Writer, def: Field) -> Option<TokenStream> {
    let Some(value) = constant(writer, def) else {
        return None;
    };

    let mut input = value.as_str();

    let Type::TypeDef(def, _) = writer.reader.field_type(def, None) else {
        unimplemented!();
    };

    let mut result = quote! {};

    for field in writer.reader.type_def_fields(def) {
        let (value, rest) = field_initializer(writer, field, input);
        input = rest;
        result.combine(&value);
    }

    Some(result)
}

fn field_initializer<'a>(writer: &Writer, field: Field, input: &'a str) -> (TokenStream, &'a str) {
    let name = to_ident(writer.reader.field_name(field));

    match writer.reader.field_type(field, None) {
        Type::GUID => {
            let (literals, rest) = read_literal_array(input, 11);
            let value = writer.guid(&GUID::from_string_args(&literals));
            (quote! { #name: #value, }, rest)
        }
        Type::Win32Array(_, len) => {
            let (literals, rest) = read_literal_array(input, len);
            let literals = literals.iter().map(|literal| TokenStream::from(*literal));
            (quote! { #name: [#(#literals,)*], }, rest)
        }
        _ => {
            let (literal, rest) = read_literal(input);
            let literal: TokenStream = literal.into();
            (quote! { #name: #literal, }, rest)
        }
    }
}

fn constant(writer: &Writer, def: Field) -> Option<String> {
    writer.reader.find_attribute(def, "ConstantAttribute").map(|attribute| {
        let args = writer.reader.attribute_args(attribute);
        match &args[0].1 {
            Value::String(value) => value.clone(),
            rest => unimplemented!("{rest:?}"),
        }
    })
}

fn read_literal(input: &str) -> (&str, &str) {
    let mut start = None;
    let mut end = 0;

    for (pos, c) in input.bytes().enumerate() {
        if start.is_none() {
            if c != b' ' && c != b',' {
                start = Some(pos);
            }
        } else if c == b' ' || c == b',' || c == b'}' {
            break;
        }
        end += 1;
    }

    let Some(start) = start else {
        unimplemented!();
    };

    (&input[start..end], &input[end..])
}

fn read_token(input: &str, token: u8) -> &str {
    for (pos, c) in input.bytes().enumerate() {
        if c == token {
            return &input[pos + 1..];
        } else if c != b' ' && c != b',' {
            break;
        }
    }

    panic!("`{}` expected", token.escape_ascii());
}

fn read_literal_array(input: &str, len: usize) -> (Vec<&str>, &str) {
    let mut input = read_token(input, b'{');
    let mut result = vec![];

    for _ in 0..len {
        let (literal, rest) = read_literal(input);
        result.push(literal);
        input = rest;
    }

    (result, read_token(input, b'}'))
}