rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

C++ features we can't handle right now.

Open emilio opened this issue 9 years ago • 8 comments

This is a list of things we can't handle correctly in bindgen, and we're not expected to be able to in a while.

Partial template specialization

We can't get the specialized types from a construct like this via libclang, rather unfortunately. We do hackily search through the ast to get the non-specialized ones though.

template<typename T, typename U>
class Foo { };

template<typename T>
class Bar {
  Foo<T, int> mFoo;
};

In the above example the ast for Bar is the following:

(ClassTemplate Bar Invalid
    (TemplateTypeParameter T Unexposed
    )
    (FieldDecl mFoo Unexposed
        (TemplateRef Foo Invalid
        )
        (TypeRef T Unexposed
        )
    )
)

And type template parameter related functions in bindgen don't give us any information.

Template unions with template parameters inside.

We can't handle this because the size of the union depends on size_of::<T>, and that's not const fn (and we can't use const fn in stable anyway). We can try to compute it and be correct if T is not the largest type in the union.

template<typename T>
union Bar {
  T mFoo;
  int mBar;
};

emilio avatar Aug 25 '16 17:08 emilio

Can bindgen handle SFINAE?

fitzgen avatar Aug 25 '16 18:08 fitzgen

@fitzgen nope, for obvious reasons, sorry, forgot to mention it.

emilio avatar Aug 25 '16 18:08 emilio

The template unions with template parameters thing should be doable once #61 lands.

emilio avatar Sep 22 '16 01:09 emilio

I'm a bit surprised bindgen can't handle this:

$ cat test.h
template <typename T>
void foo(T a);
$ bindgen --version
bindgen 0.21.2
$ bindgen test.h -- -x c++
ERROR:bindgen::ir::item: Unhandled cursor kind CXCursorKind(30): Cursor(foo kind: FunctionTemplate, loc: test.h:2:6, usr: Some("c:@FT@>1#Tfoo#t0.0#v#"))
/* automatically generated by rust-bindgen */

Is this expected? It's not any fancy specialization, just a normal templated function.

gsingh93 avatar Feb 07 '17 23:02 gsingh93

@gsingh93

We can't invoke the C++ compiler to generate new instantiations of templates, so I don't think we even try to keep track of them currently.

Perhaps we could track explicit instantiations of template functions (14.7.2 in the standard) and make bindings to those.

For example, if given

template<class T> void foo(T t) { /∗ ... ∗/ }
template void foo(char);
template void foo(int);

We could generate something like:

extern "C" {
    #[link_name = "..."]
    pub fn foo_char(char);

    #[link_name = "..."]
    pub fn foo_int(int);
}

But either way, we can't create new template instantiations since we aren't a C++ compiler and aren't creatign new object files from C++ code, so without those explicit instantiations to help us know what already exists, I don't think we can do much else.

fitzgen avatar Feb 07 '17 23:02 fitzgen

Filed https://github.com/servo/rust-bindgen/issues/492 for tracking explicit instantiations of template functions and generating bindings for them.

fitzgen avatar Feb 07 '17 23:02 fitzgen

Just hit this while trying to work on https://github.com/rust-lang-nursery/rust-bindgen/issues/492.

Here's the output, in case anyone finds it helpful: https://gist.github.com/anonymous/d20daf345e3b7512061c719e15684bb9.

Note that it includes the Unhandled cursor kind 30: Cursor(__test kind: FunctionTemplate bit tracked in https://github.com/rust-lang-nursery/rust-bindgen/issues/492, but also includes a bunch of Unhandled cursor kind 21: Cursor(__on_zero_shared kind: CXXMethod.

EDIT: also featured is Unhandled cursor kind 24: Cursor(shared_ptr<_Tp> kind: CXXConstructor.

tamird avatar Sep 13 '17 22:09 tamird

I propose closing this issue and maybe opening tracking issue listing all the other issues related to C++ features. What do you way?

pvdrz avatar Sep 15 '22 22:09 pvdrz

superseded by https://github.com/rust-lang/rust-bindgen/issues/2343

pvdrz avatar Nov 11 '22 16:11 pvdrz