Sometimes you want to retrieve a C++ method from a dynamic library using jit_dynlib_get_symbol.  Unfortunately, C++ name mangling rules differ from one system to another, making this process very error-prone.

The functions that follow try to help.  They aren't necessarily fool-proof, but they should work in the most common cases.  The only alternative is to wrap your C++ library with C functions, so that the names are predictable.

The basic idea is that you supply a description of the C++ method that you wish to access, and these functions return a number of candidate forms that you can try with jit_dynlib_get_symbol.  If one form fails, you move on and try the next form, until either symbol lookup succeeds or until all forms have been exhausted.

The following code demonstrates how to resolve a global function:

     jit_dynlib_handle_t handle;
     jit_type_t signature;
     int form = 0;
     void *address = 0;
     char *mangled;

     while((mangled = jit_mangle_global_function
     ("foo", signature, form)) != 0)
     {
     address = jit_dynlib_get_symbol(handle, mangled);
     if(address != 0)
     {
     break;
     }
     jit_free(mangled);
     ++form;
     }

     if(address)
     {
     printf("%s = 0x%lxn", mangled, (long)address);
     }
     else
     {
     printf("could not resolve foon");
     }

This mechanism typically cannot be used to obtain the entry points for inline methods.  You will need to make other arrangements to simulate the behaviour of inline methods, or recompile your dynamic C++ library in a mode that explicitly exports inlines.

C++ method names are very picky about types.  On 32-bit systems, int and long are the same size, but they are mangled to different characters.  To ensure that the correct function is picked, you should use jit_type_sys_int, jit_type_sys_long, etc instead of the platform independent types.  If you do use a platform independent type like jit_type_int, this library will try to guess which system type you mean, but the guess will most likely be wrong.

 -- Function: char * jit_mangle_global_function (const char *NAME, jit_type_t SIGNATURE, int FORM)
     Mangle the name of a global C++ function using the specified FORM. Returns NULL if out of memory, or if the form is not supported.

 -- Function: char * jit_mangle_member_function (const char *CLASS_NAME, const char *NAME, jit_type_t SIGNATURE, int FORM, int FLAGS)
     Mangle the name of a C++ member function using the specified FORM. Returns NULL if out of memory, or if the form is not supported. The following flags may be specified to modify the mangling rules:

     JIT_MANGLE_PUBLIC
          The method has public access within its containing class.

     JIT_MANGLE_PROTECTED
          The method has protected access within its containing class.

     JIT_MANGLE_PRIVATE
          The method has private access within its containing class.

     JIT_MANGLE_STATIC
          The method is static.

     JIT_MANGLE_VIRTUAL
          The method is a virtual instance method.  If neither JIT_MANGLE_STATIC nor JIT_MANGLE_VIRTUAL are supplied, then the method is assumed to be a non-virtual instance method.

     JIT_MANGLE_CONST
          The method is an instance method with the const qualifier.

     JIT_MANGLE_EXPLICIT_THIS
          The SIGNATURE includes an extra pointer parameter at the start that indicates the type of the this pointer.  This parameter won't be included in the final mangled name.

     JIT_MANGLE_IS_CTOR
          The method is a constructor.  The NAME parameter will be ignored.

     JIT_MANGLE_IS_DTOR
          The method is a destructor.  The NAME parameter will be ignored.

     JIT_MANGLE_BASE
          Fetch the "base" constructor or destructor entry point, rather than the "complete" entry point.

     The CLASS_NAME may include namespace and nested parent qualifiers by separating them with :: or ..  Class names that involve template parameters are not supported yet.