summaryrefslogtreecommitdiff
path: root/src/codegen/helpers.rs
blob: 5136d7877dadce0161aa5241ba99eea486efc51d (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//! Helpers for code generation that don't need macro expansion.

use ir::layout::Layout;
use quote;

pub mod attributes {
    use quote;

    pub fn repr(which: &str) -> quote::Tokens {
        let which = quote::Ident::new(which);
        quote! {
            #[repr( #which )]
        }
    }

    pub fn repr_list(which_ones: &[&str]) -> quote::Tokens {
        let which_ones = which_ones.iter().cloned().map(quote::Ident::new);
        quote! {
            #[repr( #( #which_ones ),* )]
        }
    }

    pub fn derives(which_ones: &[&str]) -> quote::Tokens {
        let which_ones = which_ones.iter().cloned().map(quote::Ident::new);
        quote! {
            #[derive( #( #which_ones ),* )]
        }
    }

    pub fn inline() -> quote::Tokens {
        quote! {
            #[inline]
        }
    }

    pub fn doc(comment: String) -> quote::Tokens {
        // Doc comments are already preprocessed into nice `///` formats by the
        // time they get here. Just make sure that we have newlines around it so
        // that nothing else gets wrapped into the comment.
        let mut tokens = quote! {};
        tokens.append("\n");
        tokens.append(comment);
        tokens.append("\n");
        tokens
    }

    pub fn link_name(name: &str) -> quote::Tokens {
        quote! {
            #[link_name = #name]
        }
    }
}

/// Generates a proper type for a field or type with a given `Layout`, that is,
/// a type with the correct size and alignment restrictions.
pub fn blob(layout: Layout) -> quote::Tokens {
    let opaque = layout.opaque();

    // FIXME(emilio, #412): We fall back to byte alignment, but there are
    // some things that legitimately are more than 8-byte aligned.
    //
    // Eventually we should be able to `unwrap` here, but...
    let ty_name = match opaque.known_rust_type_for_array() {
        Some(ty) => ty,
        None => {
            warn!("Found unknown alignment on code generation!");
            "u8"
        }
    };

    let ty_name = quote::Ident::new(ty_name);

    let data_len = opaque.array_size().unwrap_or(layout.size);

    if data_len == 1 {
        quote! {
            #ty_name
        }
    } else {
        quote! {
            [ #ty_name ; #data_len ]
        }
    }
}

pub mod ast_ty {
    use ir::context::BindgenContext;
    use ir::function::FunctionSig;
    use ir::ty::FloatKind;
    use quote;

    pub fn raw_type(ctx: &BindgenContext, name: &str) -> quote::Tokens {
        let ident = ctx.rust_ident_raw(name);
        match ctx.options().ctypes_prefix {
            Some(ref prefix) => {
                let prefix = ctx.rust_ident_raw(prefix.as_str());
                quote! {
                    #prefix::#ident
                }
            }
            None => quote! {
                ::std::os::raw::#ident
            },
        }
    }

    pub fn float_kind_rust_type(
        ctx: &BindgenContext,
        fk: FloatKind,
    ) -> quote::Tokens {
        // TODO: we probably should just take the type layout into
        // account?
        //
        // Also, maybe this one shouldn't be the default?
        //
        // FIXME: `c_longdouble` doesn't seem to be defined in some
        // systems, so we use `c_double` directly.
        match (fk, ctx.options().convert_floats) {
            (FloatKind::Float, true) => quote! { f32 },
            (FloatKind::Double, true) |
            (FloatKind::LongDouble, true) => quote! { f64 },
            (FloatKind::Float, false) => raw_type(ctx, "c_float"),
            (FloatKind::Double, false) |
            (FloatKind::LongDouble, false) => raw_type(ctx, "c_double"),
            (FloatKind::Float128, _) => quote! { [u8; 16] },
        }
    }

    pub fn int_expr(val: i64) -> quote::Tokens {
        // Don't use quote! { #val } because that adds the type suffix.
        let mut tokens = quote! {};
        tokens.append(val.to_string());
        tokens
    }

    pub fn uint_expr(val: u64) -> quote::Tokens {
        // Don't use quote! { #val } because that adds the type suffix.
        let mut tokens = quote! {};
        tokens.append(val.to_string());
        tokens
    }

    pub fn byte_array_expr(bytes: &[u8]) -> quote::Tokens {
        let mut bytes: Vec<_> = bytes.iter().cloned().collect();
        bytes.push(0);
        quote! {
            #bytes
        }
    }

    pub fn cstr_expr(mut string: String) -> quote::Tokens {
        string.push('\0');
        let b = quote::ByteStr(&string);
        quote! {
            #b
        }
    }

    pub fn float_expr(
        ctx: &BindgenContext,
        f: f64,
    ) -> Result<quote::Tokens, ()> {
        if f.is_finite() {
            let mut string = f.to_string();

            // So it gets properly recognised as a floating point constant.
            if !string.contains('.') {
                string.push('.');
            }

            let mut tokens = quote! {};
            tokens.append(string);
            return Ok(tokens);
        }

        let prefix = ctx.trait_prefix();

        if f.is_nan() {
            return Ok(quote! {
                ::#prefix::f64::NAN
            });
        }

        if f.is_infinite() {
            return Ok(if f.is_sign_positive() {
                quote! {
                    ::#prefix::f64::INFINITY
                }
            } else {
                quote! {
                    ::#prefix::f64::NEG_INFINITY
                }
            });
        }

        warn!("Unknown non-finite float number: {:?}", f);
        return Err(());
    }

    pub fn arguments_from_signature(
        signature: &FunctionSig,
        ctx: &BindgenContext,
    ) -> Vec<quote::Tokens> {
        let mut unnamed_arguments = 0;
        signature
            .argument_types()
            .iter()
            .map(|&(ref name, _ty)| {
                match *name {
                    Some(ref name) => {
                        let name = ctx.rust_ident(name);
                        quote! { #name }
                    }
                    None => {
                        unnamed_arguments += 1;
                        let name = ctx.rust_ident(format!("arg{}", unnamed_arguments));
                        quote! { #name }
                    }
                }
            })
            .collect()
    }
}