www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - how to get typeid of extern(C++) classes?

reply Timothee Cour <thelastmammoth gmail.com> writes:
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)
Feb 15 2018
next sibling parent timotheecour <timothee.cour2 gmail.com> writes:
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()` ?
Feb 16 2018
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Feb 16 2018
prev sibling next sibling parent reply Meta <jared771 gmail.com> writes:
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.
Feb 16 2018
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Feb 16 2018
prev sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
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.
Feb 16 2018