Recent

Author Topic: C/C++ Interop, RTTI From C/C++  (Read 2892 times)

guest60499

  • Guest
C/C++ Interop, RTTI From C/C++
« on: August 02, 2018, 06:00:00 pm »
Hello,

Has this ever been considered or has any work ever been done on it?

The main usecase I can see is taking an existing C++ codebase and making it usable with FPC.

However I do also have some interest in using the LCL from C++. I see other people benefiting from this as well; the LCL pops up from time to time on programming forums as the best cross platform toolkit. (It is.)

Cheers,
    guest

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7625
Re: C/C++ Interop, RTTI From C/C++
« Reply #1 on: August 02, 2018, 06:15:52 pm »
Well, the first problem is that there is no "C++" abi to interface. Every C++ compiler has its own rules wrt mangling and other details.

The easiest way to do this is to also have a Free C++ compiler and keep it strictly aligned to FPC. (more or less like Embacadero does with C++ builder and Delphi)


guest60499

  • Guest
Re: C/C++ Interop, RTTI From C/C++
« Reply #2 on: August 03, 2018, 07:13:32 pm »
Well, the first problem is that there is no "C++" abi to interface. Every C++ compiler has its own rules wrt mangling and other details.
Right, this is what I was hoping someone had a solution to. It looks like Rust provides a library for demangling LLVM names. I'm not sure how good an idea this ultimately is.

The easiest way to do this is to also have a Free C++ compiler and keep it strictly aligned to FPC. (more or less like Embacadero does with C++ builder and Delphi)
Well, if that is indeed the best solution I do not see it happening very soon.

Easier might be to provide a linkage block compatible with FPC and its RTTI. This was at one point done for Java via GCJ and 'extern "Java"'.

Another might be exposing "class" information in C linkage blocks. This is why I had been asking about COM/GObject, though I think FreePascal could do far better than either.

Hopefully it can be seen that these two are kind of related.


What about using FPC code from C/C++ without shoving everything through a dynamic library?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7625
Re: C/C++ Interop, RTTI From C/C++
« Reply #3 on: August 03, 2018, 07:59:39 pm »
Well, the first problem is that there is no "C++" abi to interface. Every C++ compiler has its own rules wrt mangling and other details.
Right, this is what I was hoping someone had a solution to. It looks like Rust provides a library for demangling LLVM names. I'm not sure how good an idea this ultimately is.

All hypothetically feasible, but difficult in practice.

The easiest way to do this is to also have a Free C++ compiler and keep it strictly aligned to FPC. (more or less like Embacadero does with C++ builder and Delphi)
Well, if that is indeed the best solution I do not see it happening very soon.

Easier might be to provide a linkage block compatible with FPC and its RTTI. This was at one point done for Java via GCJ and 'extern "Java"'.
[/quote]

What is a "linkage block" ? 

Quote
Another might be exposing "class" information in C linkage blocks. This is why I had been asking about COM/GObject, though I think FreePascal could do far better than either.


I don't know what C linkage blocks are. You can write a C library that can parse FPC rtti. 

COM and afaik GOBject doesn't work on RTTI level. You call functions to get info, and you can rewire them to whatever your implementation is.

Quote
Hopefully it can be seen that these two are kind of related.

Well, COM is more interprocess and about interoperability. GObject is just a library for languages that don't have a proper object model. Just start playing with it. As said you might be able to wrap a IDispatch interface around a gobject base class, and then be able to use it dynamically.

Quote
What about using FPC code from C/C++ without shoving everything through a dynamic library?

You can try. But in practice that is again through a procedural interface only.

A lot is easy to do for isolated cases. It is hard to do something that is universal wrt various targets, various languages and not reducing any  of the languages involved to the lowest common denominator (read: sacrifice either C++ standard compliance or FPC Delphi compatibility)

There are few people working on this, so I guess it is just waiting for sb interested to get into it.

guest60499

  • Guest
Re: C/C++ Interop, RTTI From C/C++
« Reply #4 on: August 03, 2018, 08:35:00 pm »
Sorry, a linkage block looks like:
Code: C  [Select]
  1. extern "C" {
  2.  
  3. void exported_function(void);
  4.  
  5. }

The only valid string is typically "C" but GCC has supported one nontraditional one, "Java," which created virtual method tables that would interop. with GCC's now defunct Java compiler.

I only now remembered it, it may be the best way to go from C++ -> FPC.


Is there any code available that shows parsing the RTTI from C? Ideally I'm wanting to get everything but COM working (I don't care too much for Windows, and it already half-works) but for one person it is definitely a multiyear project.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7625
Re: C/C++ Interop, RTTI From C/C++
« Reply #5 on: August 03, 2018, 09:27:18 pm »
Sorry, a linkage block looks like:
Code: C  [Select]
  1. extern "C" {
  2.  
  3. void exported_function(void);
  4.  
  5. }

The only valid string is typically "C" but GCC has supported one nontraditional one, "Java," which created virtual method tables that would interop. with GCC's now defunct Java compiler.

This is just a syntax detail of how two aligned compilers specify things. Just like we have cdecl and C-like packing options.

The problem is not the syntax, but fully aligning those two compilers on the binary level. Which, as said is typically only done for compilers from the same organization. As you demonstrate that gcc is not compatible to general Java, but only its own Java.

And then with two compilers that were not designed to have common ground.

Quote
Is there any code available that shows parsing the RTTI from C?

No. And RTTI is not 100% stable in time either, and is occasionally (but not often) expanded when new features are necessary.

Quote
Ideally I'm wanting to get everything but COM working (I don't care too much for Windows, and it already half-works) but for one person it is definitely a multiyear project.

COM is the only somewhat mature option that already exists. And I don't see anything else happening anytime soon.

PascalDragon

  • Hero Member
  • *****
  • Posts: 726
  • Compiler Developer
Re: C/C++ Interop, RTTI From C/C++
« Reply #6 on: August 03, 2018, 10:00:41 pm »
Quote
Ideally I'm wanting to get everything but COM working (I don't care too much for Windows, and it already half-works) but for one person it is definitely a multiyear project.

COM is the only somewhat mature option that already exists. And I don't see anything else happening anytime soon.
Well, one could try to use the spiritual predecessor of COM: SOM. Originally developed by IBM for OS/2 there exists an open source implementation of it, though it would need an Object Pascal emitter.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7625
Re: C/C++ Interop, RTTI From C/C++
« Reply #7 on: August 03, 2018, 10:44:25 pm »
Well, one could try to use the spiritual predecessor of COM: SOM. Originally developed by IBM for OS/2 there exists an open source implementation of it, though it would need an Object Pascal emitter.

You could do an idispatch implementation for it.

guest60499

  • Guest
Re: C/C++ Interop, RTTI From C/C++
« Reply #8 on: August 04, 2018, 04:44:33 pm »
This is just a syntax detail of how two aligned compilers specify things. Just like we have cdecl and C-like packing options.

The problem is not the syntax, but fully aligning those two compilers on the binary level. Which, as said is typically only done for compilers from the same organization. As you demonstrate that gcc is not compatible to general Java, but only its own Java.

And then with two compilers that were not designed to have common ground.
Extant syntax in C++ is a good thing because it means most of GCC can be reused. Java I think is a special case, because there is no native vanilla Java code to target. While only C is implemented the point of the feature is what is being discussed now.

No. And RTTI is not 100% stable in time either, and is occasionally (but not often) expanded when new features are necessary.
Is there no chance of a stable interface being agreed upon seeing as there might be a use for one? As long as things aren't removed the stability requirement could be met fairly easily.


COM is the only somewhat mature option that already exists. And I don't see anything else happening anytime soon.
Well, one could try to use the spiritual predecessor of COM: SOM. Originally developed by IBM for OS/2 there exists an open source implementation of it, though it would need an Object Pascal emitter.
Not a horrible idea. How does this compare in complexity to IDispatch?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7625
Re: C/C++ Interop, RTTI From C/C++
« Reply #9 on: August 04, 2018, 10:40:42 pm »
Extant syntax in C++ is a good thing because it means most of GCC can be reused.

As said it is the getting the pascal compatible implementation of those declarations into GCC that is the problem. And to use them from other pieces of code.

Quote
Java I think is a special case, because there is no native vanilla Java code to target. While only C is implemented the point of the feature is what is being discussed now.

I talked about Java/GCJ because the case sounded analoguous to what Embarcadero does with Delphi and BCB builder. Only for compilers maintained within the same "house"

Quote
Is there no chance of a stable interface being agreed upon seeing as there might be a use for one? As long as things aren't removed the stability requirement could be met fairly easily.

It is at it is. Backwards compatibility is important, also for Pascal codebases, but there are no guarantees.


sash

  • Sr. Member
  • ****
  • Posts: 289
Re: C/C++ Interop, RTTI From C/C++
« Reply #10 on: August 05, 2018, 01:26:57 am »
What about using FPC code from C/C++ without shoving everything through a dynamic library?

FPC code links together with plain C (gcc)  as a static library, or even as object file {$linklib} {$link}.
But of course you cannot omit external declarations (.h/interface).
Lazarus 2.0.6 FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 19.10 XFCE

avra

  • Hero Member
  • *****
  • Posts: 1736
    • Additional info
Re: C/C++ Interop, RTTI From C/C++
« Reply #11 on: August 05, 2018, 12:07:24 pm »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

PascalDragon

  • Hero Member
  • *****
  • Posts: 726
  • Compiler Developer
Re: C/C++ Interop, RTTI From C/C++
« Reply #12 on: August 07, 2018, 12:23:30 pm »
Quote
Is there no chance of a stable interface being agreed upon seeing as there might be a use for one? As long as things aren't removed the stability requirement could be met fairly easily.

It is at it is. Backwards compatibility is important, also for Pascal codebases, but there are no guarantees.
Even then we're only really caring about source compatibility here, not binary compatibility...