autocxx-engine 0.3.1

Safe autogenerated interop between Rust and C++
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::additional_cpp_generator::AdditionalNeed;
use crate::byvalue_checker::ByValueChecker;
use crate::known_types::KNOWN_TYPES;
use crate::TypeName;
use proc_macro2::{Span, TokenStream as TokenStream2, TokenTree};
use std::collections::HashSet;
use syn::punctuated::Punctuated;
use syn::Token;
use syn::{
    parse_quote, AngleBracketedGenericArguments, Attribute, FnArg, ForeignItem, ForeignItemFn,
    GenericArgument, Ident, Item, ItemForeignMod, ItemMod, PatType, Path, PathArguments,
    PathSegment, ReturnType, Type, TypePath, TypePtr, TypeReference,
};

#[derive(Debug)]
pub enum ConvertError {
    NoContent,
    UnsafePODType(String),
    UnknownForeignItem,
}

/// Results of a conversion.
pub(crate) struct BridgeConversion {
    pub items: Vec<Item>,
    pub additional_cpp_needs: Vec<AdditionalNeed>,
}

/// Converts the bindings generated by bindgen into a form suitable
/// for use with `cxx`.
/// Tasks current performed:
/// * Replaces certain identifiers e.g. `std_unique_ptr` with `UniquePtr`
/// * Replaces pointers with references
/// * Removes repr attributes
/// * Removes link_name attributes
/// * Adds include! directives
/// * Adds #[cxx::bridge]
/// At the moment this is almost certainly not using the best practice for parsing
/// stuff. It's multiple simple-but-yucky state machines. Can undoubtedly be
/// simplified and made less error-prone: TODO. Probably the right thing to do
/// is just manipulate the syn types directly.
pub(crate) struct BridgeConverter {
    include_list: Vec<String>,
    pod_requests: Vec<TypeName>,
    class_names_discovered: HashSet<TypeName>,
    byvalue_checker: ByValueChecker,
}

struct TypeAliasResults {
    for_extern_c: ForeignItem,
    for_bridge: Item,
    for_anywhere: Item,
}

impl<'a> BridgeConverter {
    pub fn new(include_list: Vec<String>, pod_requests: Vec<TypeName>) -> Self {
        Self {
            include_list,
            class_names_discovered: HashSet::new(),
            byvalue_checker: ByValueChecker::new(),
            pod_requests,
        }
    }

    fn find_nested_pod_types(&mut self, items: &[Item]) -> Result<(), ConvertError> {
        for item in items {
            if let Item::Struct(s) = item {
                self.byvalue_checker.ingest_struct(s);
            }
        }
        self.byvalue_checker
            .satisfy_requests(self.pod_requests.clone())
            .map_err(ConvertError::UnsafePODType)
    }

    fn generate_type_alias(&self, tyname: &TypeName, should_be_pod: bool) -> TypeAliasResults {
        let tyident = tyname.to_ident();
        let kind_item: Ident = Ident::new(
            if should_be_pod { "Trivial" } else { "Opaque" },
            Span::call_site(),
        );
        let tynamestring = tyname.to_cxx_name();
        let mut for_extern_c_ts = TokenStream2::new();
        // TODO - add #[rustfmt::skip] here until
        // https://github.com/rust-lang/rustfmt/issues/4159 is fixed.
        for_extern_c_ts.extend(
            [
                TokenTree::Ident(Ident::new("type", Span::call_site())),
                TokenTree::Ident(tyident.clone()),
                TokenTree::Punct(proc_macro2::Punct::new('=', proc_macro2::Spacing::Alone)),
                TokenTree::Ident(Ident::new("super", Span::call_site())),
                TokenTree::Punct(proc_macro2::Punct::new(':', proc_macro2::Spacing::Joint)),
                TokenTree::Punct(proc_macro2::Punct::new(':', proc_macro2::Spacing::Joint)),
                TokenTree::Ident(Ident::new("bindgen", Span::call_site())),
                TokenTree::Punct(proc_macro2::Punct::new(':', proc_macro2::Spacing::Joint)),
                TokenTree::Punct(proc_macro2::Punct::new(':', proc_macro2::Spacing::Joint)),
                TokenTree::Ident(tyident.clone()),
                TokenTree::Punct(proc_macro2::Punct::new(';', proc_macro2::Spacing::Alone)),
            ]
            .to_vec(),
        );
        TypeAliasResults {
            for_extern_c: ForeignItem::Verbatim(for_extern_c_ts),
            for_bridge: Item::Impl(parse_quote! {
                impl UniquePtr<#tyident> {}
            }),
            for_anywhere: Item::Impl(parse_quote! {
                unsafe impl cxx::ExternType for bindgen::#tyident {
                    type Id = cxx::type_id!(#tynamestring);
                    type Kind = cxx::kind::#kind_item;
                }
            }),
        }
    }

    /// Convert a TokenStream of bindgen-generated bindings to a form
    /// suitable for cxx.
    pub(crate) fn convert(
        &mut self,
        bindings: ItemMod,
        extra_inclusion: Option<&str>,
    ) -> Result<BridgeConversion, ConvertError> {
        match bindings.content {
            None => Err(ConvertError::NoContent),
            Some((brace, items)) => {
                self.find_nested_pod_types(&items)?;
                let mut all_items: Vec<Item> = Vec::new();
                let mut bindgen_mod = ItemMod {
                    attrs: bindings.attrs,
                    vis: bindings.vis,
                    ident: bindings.ident,
                    mod_token: bindings.mod_token,
                    content: Some((brace, Vec::new())),
                    semi: bindings.semi,
                };
                let mut bridge_items = Vec::new();
                let mut extern_c_mod = None;

                let mut full_include_list = self.include_list.clone();
                if let Some(extra_inclusion) = extra_inclusion {
                    full_include_list.push(extra_inclusion.to_string());
                }
                let mut extern_c_mod_items: Vec<ForeignItem> = full_include_list
                    .iter()
                    .map(|inc| {
                        ForeignItem::Macro(parse_quote! {
                            include!(#inc);
                        })
                    })
                    .collect();

                let mut additional_cpp_needs = Vec::new();
                let mut types_found = Vec::new();
                let mut bindgen_items = Vec::new();
                for item in items {
                    match item {
                        Item::ForeignMod(fm) => {
                            if extern_c_mod.is_none() {
                                // We'll use the first 'extern "C"' mod we come
                                // across for attributes, spans etc. but we'll stuff
                                // the contents of all bindgen 'extern "C"' mods into this
                                // one.

                                extern_c_mod = Some(ItemForeignMod {
                                    attrs: fm.attrs,
                                    abi: fm.abi,
                                    brace_token: fm.brace_token,
                                    items: Vec::new(),
                                });
                            }
                            extern_c_mod
                                .as_mut()
                                .unwrap()
                                .items
                                .extend(self.convert_foreign_mod_items(&types_found, fm.items)?);
                        }
                        Item::Struct(s) => {
                            let tyident = s.ident.clone();
                            let tyname = TypeName::from_ident(&tyident);
                            types_found.push(tyname.clone());
                            self.class_names_discovered.insert(tyname.clone());
                            let should_be_pod = self.byvalue_checker.is_pod(&tyname);
                            let type_alias = self.generate_type_alias(&tyname, should_be_pod);
                            bridge_items.push(type_alias.for_bridge);
                            extern_c_mod_items.push(type_alias.for_extern_c);
                            bindgen_mod
                                .content
                                .as_mut()
                                .unwrap()
                                .1
                                .push(Item::Struct(self.convert_struct(s)));
                            all_items.push(type_alias.for_anywhere);
                        }
                        Item::Enum(e) => {
                            let tyident = e.ident.clone();
                            let tyname = TypeName::from_ident(&tyident);
                            types_found.push(tyname.clone());
                            let type_alias = self.generate_type_alias(&tyname, true);
                            bridge_items.push(type_alias.for_bridge);
                            extern_c_mod_items.push(type_alias.for_extern_c);
                            bindgen_mod.content.as_mut().unwrap().1.push(Item::Enum(e));
                            all_items.push(type_alias.for_anywhere);
                        }
                        Item::Impl(i) => {
                            if let Some(ty) = self.type_to_typename(&i.self_ty) {
                                for item in i.items {
                                    match item {
                                        syn::ImplItem::Method(m) if m.sig.ident == "new" => {
                                            let constructor_args = m
                                                .sig
                                                .inputs
                                                .iter()
                                                .filter_map(|x| match x {
                                                    FnArg::Typed(ty) => {
                                                        self.type_to_typename(&ty.ty)
                                                    }
                                                    FnArg::Receiver(_) => None,
                                                })
                                                .collect::<Vec<TypeName>>();
                                            additional_cpp_needs.push(AdditionalNeed::MakeUnique(
                                                ty.clone(),
                                                constructor_args.clone(),
                                            ));
                                        }
                                        _ => {}
                                    }
                                }
                            }
                        }
                        _ => {
                            all_items.push(item);
                        }
                    }
                }
                // We will always create an extern "C" mod even if bindgen
                // didn't generate one, e.g. because it only generated types.
                // We still want cxx to know about those types.
                let mut extern_c_mod = match extern_c_mod {
                    None => ItemForeignMod {
                        attrs: Vec::new(),
                        abi: syn::Abi {
                            extern_token: Token![extern](Span::call_site()),
                            name: Some(syn::LitStr::new("C", Span::call_site())),
                        },
                        brace_token: syn::token::Brace {
                            span: Span::call_site(),
                        },
                        items: Vec::new(),
                    },
                    Some(md) => md,
                };

                extern_c_mod.items.append(&mut extern_c_mod_items);
                bridge_items.push(Item::ForeignMod(extern_c_mod));
                bindgen_mod
                    .content
                    .as_mut()
                    .unwrap()
                    .1
                    .append(&mut bindgen_items);
                all_items.push(Item::Mod(bindgen_mod));
                let mut bridge_mod: ItemMod = parse_quote! {
                    #[cxx::bridge]
                    pub mod cxxbridge {
                    }
                };
                bridge_mod
                    .content
                    .as_mut()
                    .unwrap()
                    .1
                    .append(&mut bridge_items);
                all_items.push(Item::Mod(bridge_mod));
                Ok(BridgeConversion {
                    items: all_items,
                    additional_cpp_needs,
                })
            }
        }
    }

    fn type_to_typename(&self, ty: &Type) -> Option<TypeName> {
        match ty {
            Type::Path(pn) => Some(TypeName::from_type_path(pn)),
            _ => None,
        }
    }

    fn convert_foreign_mod_items(
        &self,
        encountered_types: &[TypeName],
        foreign_mod_items: Vec<ForeignItem>,
    ) -> Result<Vec<ForeignItem>, ConvertError> {
        let mut new_items = Vec::new();

        for i in foreign_mod_items {
            match i {
                ForeignItem::Fn(f) => {
                    let maybe_foreign_item = self.convert_foreign_fn(encountered_types, f)?;
                    if let Some(foreign_item) = maybe_foreign_item {
                        new_items.push(ForeignItem::Fn(foreign_item));
                    }
                }
                _ => return Err(ConvertError::UnknownForeignItem),
            }
        }
        Ok(new_items)
    }

    fn convert_foreign_fn(
        &self,
        encountered_types: &[TypeName],
        fun: ForeignItemFn,
    ) -> Result<Option<ForeignItemFn>, ConvertError> {
        let mut s = fun.sig.clone();
        let old_name = s.ident.to_string();
        // See if it's a constructor, in which case skip it.
        // We instead pass onto cxx an alternative make_unique implementation later.
        for ty in encountered_types {
            let constructor_name = format!("{}_{}", ty, ty);
            if old_name == constructor_name {
                return Ok(None);
            }
        }
        s.output = self.convert_return_type(s.output);
        let (new_params, any_this): (Punctuated<_, _>, Vec<_>) = fun
            .sig
            .inputs
            .into_iter()
            .map(|i| self.convert_fn_arg(i))
            .unzip();
        s.inputs = new_params;
        let is_a_method = any_this.iter().any(|b| *b);
        if is_a_method {
            // bindgen generates methods with the name:
            // {class}_{method name}
            // It then generates an impl section for the Rust type
            // with the original name, but we currently discard that impl section.
            // We want to feed cxx methods with just the method name, so let's
            // strip off the class name.
            // TODO test with class names containing underscores. It should work.
            for cn in &self.class_names_discovered {
                if old_name.starts_with(&cn.0) {
                    s.ident = Ident::new(&old_name[cn.0.len() + 1..], s.ident.span());
                    break;
                }
            }
        }
        Ok(Some(ForeignItemFn {
            attrs: self.strip_attr(fun.attrs, "link_name"),
            vis: fun.vis,
            sig: s,
            semi_token: fun.semi_token,
        }))
    }

    fn strip_attr(&self, attrs: Vec<Attribute>, to_strip: &str) -> Vec<Attribute> {
        attrs
            .into_iter()
            .filter(|a| {
                let i = a.path.get_ident();
                !matches!(i, Some(i2) if *i2 == to_strip)
            })
            .collect::<Vec<Attribute>>()
    }

    /// Returns additionally a Boolean indicating whether an argument was
    /// 'this'
    fn convert_fn_arg(&self, arg: FnArg) -> (FnArg, bool) {
        match arg {
            FnArg::Typed(pt) => {
                let mut found_this = false;
                let old_pat = *pt.pat;
                let new_pat = match old_pat {
                    syn::Pat::Ident(pp) if pp.ident == "this" => {
                        found_this = true;
                        syn::Pat::Ident(syn::PatIdent {
                            attrs: pp.attrs,
                            by_ref: pp.by_ref,
                            mutability: pp.mutability,
                            subpat: pp.subpat,
                            ident: Ident::new("self", pp.ident.span()),
                        })
                    }
                    _ => old_pat,
                };
                (
                    FnArg::Typed(PatType {
                        attrs: pt.attrs,
                        pat: Box::new(new_pat),
                        colon_token: pt.colon_token,
                        ty: self.convert_boxed_type(pt.ty),
                    }),
                    found_this,
                )
            }
            _ => (arg, false),
        }
    }

    fn convert_return_type(&self, rt: ReturnType) -> ReturnType {
        match rt {
            ReturnType::Default => ReturnType::Default,
            ReturnType::Type(rarrow, typebox) => {
                ReturnType::Type(rarrow, self.convert_boxed_type(typebox))
            }
        }
    }

    fn convert_boxed_type(&self, ty: Box<Type>) -> Box<Type> {
        Box::new(self.convert_type(*ty))
    }

    fn convert_type(&self, ty: Type) -> Type {
        match ty {
            Type::Path(p) => Type::Path(self.convert_type_path(p)),
            Type::Reference(r) => Type::Reference(TypeReference {
                and_token: r.and_token,
                lifetime: r.lifetime,
                mutability: r.mutability,
                elem: self.convert_boxed_type(r.elem),
            }),
            Type::Ptr(ptr) => Type::Reference(self.convert_ptr_to_reference(ptr)),
            _ => ty,
        }
    }

    fn convert_ptr_to_reference(&self, ptr: TypePtr) -> TypeReference {
        TypeReference {
            and_token: Token![&](Span::call_site()),
            lifetime: None,
            mutability: ptr.mutability,
            elem: self.convert_boxed_type(ptr.elem),
        }
    }

    fn convert_type_path(&self, typ: TypePath) -> TypePath {
        let p = typ.path;
        let new_p = Path {
            leading_colon: p.leading_colon,
            segments: p
                .segments
                .into_iter()
                .map(|s| {
                    let old_ident = TypeName::from_ident(&s.ident);
                    let args = match s.arguments {
                        PathArguments::AngleBracketed(ab) => {
                            PathArguments::AngleBracketed(AngleBracketedGenericArguments {
                                colon2_token: ab.colon2_token,
                                lt_token: ab.lt_token,
                                gt_token: ab.gt_token,
                                args: self.convert_punctuated(ab.args),
                            })
                        }
                        _ => s.arguments,
                    };
                    let ident = match crate::known_types::KNOWN_TYPES
                        .get(&old_ident)
                        .and_then(|x| x.cxx_replacement.as_ref())
                    {
                        None => s.ident,
                        Some(replacement) => replacement.to_ident(),
                    };
                    PathSegment {
                        ident,
                        arguments: args,
                    }
                })
                .collect(),
        };
        TypePath {
            qself: typ.qself,
            path: new_p,
        }
    }

    fn convert_punctuated<P>(
        &self,
        pun: Punctuated<GenericArgument, P>,
    ) -> Punctuated<GenericArgument, P>
    where
        P: Default,
    {
        let mut new_pun = Punctuated::new();
        for arg in pun.into_iter() {
            new_pun.push(match arg {
                GenericArgument::Type(t) => GenericArgument::Type(self.convert_type(t)),
                _ => arg,
            });
        }
        new_pun
    }

    /// Renames std_string within structs to CxxString, etc. This should be done
    /// by bindgen because of the PRELUDE we give it, but it doesn't appear to
    /// replace types within structs, so we do it.
    /// This may run the risk of altering the size/structure/ABI of the struct,
    /// but that's OK, because this is only ever applied to opaque types which
    /// will only be passed by UniquePtr/reference within Rust, never by value.
    fn convert_struct(&self, mut strct: syn::ItemStruct) -> syn::ItemStruct {
        for f in &mut strct.fields {
            self.convert_struct_field_type(&mut f.ty);
        }
        strct
    }

    fn convert_struct_field_type(&self, ty: &mut Type) {
        match ty {
            Type::Path(ty) => {
                // O(n*m) but m is small.
                for (type_name, type_details) in KNOWN_TYPES.iter() {
                    if ty.path.is_ident(&type_name.to_string()) {
                        if let Some(replacement) = &type_details.cxx_replacement {
                            let replacement = replacement.to_ident();
                            ty.path = parse_quote! {
                                cxx:: #replacement
                            };
                        }
                    }
                }
            }
            _ => {}
        }
    }
}