Thread overview
how to get typeid of extern(C++) classes?
Feb 16, 2018
Timothee Cour
Feb 16, 2018
timotheecour
Feb 17, 2018
Meta
Feb 17, 2018
Jonathan M Davis
Feb 17, 2018
Stefan Koch
February 15, 2018
is there a way to get typeid of extern(C++) classes (eg for ones in
dmd/astbase.d but not limited to that) ?
C++ exposes it via typeid so in theory all the info is there ;
I would need it at least for debugging (eg if RTTI is not enabled for
all compilers or in release mode that's fine so long there's a
documented way to get it for debugging)

a lot of extern(C++) classes in dmd use hacks like enum values to get their type but it's unreliable and doesn't work for all AST classes.

at least having a way to expose typeid(instance).name() would be a start

also, that could be used to fix the bug I posted here:
https://forum.dlang.org/post/mailman.3138.1517949584.9493.digitalmars-d@puremagic.com
cast overly permissive with extern(C++ ) classes; ( ie that `cast(A)
b` doesn't care that b is of dynamic type A)
February 16, 2018
On Friday, 16 February 2018 at 00:42:02 UTC, Timothee Cour wrote:
> is there a way to get typeid of extern(C++) classes (eg for ones in
> dmd/astbase.d but not limited to that) ?

as a workaround, could the compiler insert (eg, depending on a version(insert_typeid)) a virtual method in each extern(C++) class eg:
`const(char)* __typeid()` ?
February 16, 2018
On 2/15/18 7:42 PM, Timothee Cour wrote:
> is there a way to get typeid of extern(C++) classes (eg for ones in
> dmd/astbase.d but not limited to that) ?
> C++ exposes it via typeid so in theory all the info is there ;
> I would need it at least for debugging (eg if RTTI is not enabled for
> all compilers or in release mode that's fine so long there's a
> documented way to get it for debugging)
> 
> a lot of extern(C++) classes in dmd use hacks like enum values to get
> their type but it's unreliable and doesn't work for all AST classes.
> 
> at least having a way to expose typeid(instance).name() would be a start
> 
> also, that could be used to fix the bug I posted here:
> https://forum.dlang.org/post/mailman.3138.1517949584.9493.digitalmars-d@puremagic.com
> cast overly permissive with extern(C++ ) classes; ( ie that `cast(A)
> b` doesn't care that b is of dynamic type A)
> 

typeid always returns a D TypeInfo object, which is a D-specific animal.

I don't think we can do this for C++ classes.

For D classes in particular, the typeid is a runtime type, gleaned from the embedded vtable. For structs, it is a compile-time defined reference. Neither of these things would exist for C++ classes.

-Steve
February 17, 2018
On Friday, 16 February 2018 at 00:42:02 UTC, Timothee Cour wrote:
> C++ exposes it via typeid so in theory all the info is there ;

It's been awhile since I've written any C++ code, but as I remember it, this type of type info is not available unless you enable it with a (C++) compiler switch.
February 16, 2018
On Saturday, February 17, 2018 00:23:01 Meta via Digitalmars-d wrote:
> On Friday, 16 February 2018 at 00:42:02 UTC, Timothee Cour wrote:
> > C++ exposes it via typeid so in theory all the info is there ;
>
> It's been awhile since I've written any C++ code, but as I remember it, this type of type info is not available unless you enable it with a (C++) compiler switch.

I think that all that's required is #including <typeinfo>. Certainly, I've never used a compiler switch to get at it, but I have no clue what <typeinfo> really does to make things work beyond making certain symbols available. So, I don't know how easy it is to make it work from D code.

- Jonathan M Davis

February 17, 2018
On Friday, 16 February 2018 at 00:42:02 UTC, Timothee Cour wrote:
> is there a way to get typeid of extern(C++) classes (eg for ones in
> dmd/astbase.d but not limited to that) ?
> C++ exposes it via typeid so in theory all the info is there ;
> I would need it at least for debugging (eg if RTTI is not enabled for
> all compilers or in release mode that's fine so long there's a
> documented way to get it for debugging)
>
> [...]

look at the asttypename.d in the dmd source.
it can give you a sting which represnts the ast-type-name.