Skip to content

[SUGGESTION] cppfront transpiler parameter to generate interoperability with C #612

Description

@raffaeler

My suggestion does not touch cppfront at all, but is a transpiler parameter to automatically generate extern C functions to overcome the binary compatibility issues in C++.

The goal is to allow consuming C++ pure libraries from other languages without messing with the mangled names and risking breaking changes with future versions of the C++ libraries and/or compilers.

The generation should be restricted to:

  • global or public member functions tagged by the user so that the generation is limited
  • global or public member functions taking and returning basic types
  • mapping the ctor/dtor to create/close functions taking care of allocating/freeing objects

If I understood correctly, cppfront makes the parameter direction clear using in and out. This metadata can be used to apply the patterns to correctly allocate/deallocate the memory for the parameters.

It could be also useful to define a "type transformer" that can be used by the transpiler to extend the types that can be used in the generation. For example, a std::vector<T> could be mapped to T* and length.

Activity

  1. JohelEGP commented on Aug 22, 2023

    @JohelEGP
    Contributor

    The generation should be restricted to:

    Sounds like a prime use-case for metafunctions.
    E.g.,

    algo: @c_api (r: std::span<int>) = { … }
    

    could generate the usual Cpp2 declaration, and a Cpp1 extern "C" at lower time that looks like:

    extern "C" {
    void algo(int* rp, std::size_t rs) { algo(std::span<int>{rp, rs}); }
    }
  2. raffaeler commented on Aug 22, 2023

    @raffaeler
    Author

    @JohelEGP sounds neat :)

  3. Repository owner locked and limited conversation to collaborators on Aug 30, 2023
  4. converted this issue into a discussion #624 on Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions