Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make templated C++ types useful #349

Open
adetaylor opened this issue Apr 6, 2021 · 4 comments
Open

Make templated C++ types useful #349

adetaylor opened this issue Apr 6, 2021 · 4 comments
Labels
cpp-feature C++ feature not yet supported

Comments

@adetaylor
Copy link
Collaborator

Split off from #106. At the moment, a templated C++ type gets made into a completely opaque type called AutocxxConcrete0...n. These types don't have constructors, accessors, or methods, so are useless except as an opaque token passed from C++, through Rust, back into C++.

Options are:

@koutheir
Copy link

koutheir commented Apr 5, 2022

The LLVM C++ API defines a class called ErrorOr<T> whose semantics resemble the Rust Result struct. ErrorOr<T> is used by many functions as return value, and due to this, all those functions cannot be used through autocxx. An example of this is llvm::MemoryBuffer::getFile():

// Inside class MemoryBuffer

static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename,
        bool IsText = false,
        bool RequiresNullTerminator = true,
        bool IsVolatile = false);

I read the documentation, but I couldn't figure out the recommended way to deal with this situation. @adetaylor, can you advise on how I should fix this issue in order to use these functions?

@adetaylor
Copy link
Collaborator Author

Yeah, per this bug they're just opaque from Rust.

The workaround is to define new C++ functions like this, to let you unpack the error.

bool IsAnError(ErrorOr<std::unique_ptr<MemoryBuffer>>) {
  // ...
}

std::unique_ptr<MemoryBuffer> UnpackSuccessValue(ErrorOr<std::unique_ptr<MemoryBuffer>>) {
  // unpack and return the thing
}

Ask autocxx to generate bindings for those. Whenever you call any function which gives you the pesky ErrorOr<std::unique_ptr<T>>, you can pass it into those functions to get out some useful information.

This is not ideal, of course. That's why this bug exists.

@koutheir
Copy link

koutheir commented Apr 6, 2022

It looks like, due to the issue of ErrorOr<T>, the method llvm::MemoryBuffer::getFile() is generated as follows:

#[doc = "autocxx bindings couldn't be generated: This function or method uses a type where one of the template parameters was incomprehensible to bindgen/autocxx - probably because it uses template specialization."]
fn getFile(_uhoh: autocxx::BindingGenerationFailure) {}

How should I convince autocxx to generate that method with an opaque type (e.g., ErrorOrUniquePtrMemoryBuffer) that I could then pass to custom functions like the above IsAnError()?

@adetaylor
Copy link
Collaborator Author

Oh, I see.

Unfortunately that's currently not possible. But, I just raised #1009 as a future enhancement to make that possible. I'll try to look into it sometime in the next few weeks (no promises at all.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cpp-feature C++ feature not yet supported
Projects
None yet
Development

No branches or pull requests

2 participants