Runtime Type Information
Generate runtime type info. e.g.:
namespace info
- pointer to parent namespace (or NULL)
- name of namespace (or "" for global namespace (defined in runtime library))
type info
- pointer to containing namespace
- name of type
struct info (inherits type info)
- sizeof struct
- alignof struct (optional)
- array of members
datatype info (inherits type info)
- array of constructors
- extensible flag (is this useful for anything?)
constructor info
- name of constructor
- array of arguments (members)
member info
- name of member
- pointer to type
All (some?) structs and datatypes will have an additional first member pointing to their type info structure the problem with this is that they are no longer compatible with C, because:
alignment would always be at least alignof(pointer)
Yes, this is a problem, because we can't just pass ptr + sizeof(pointer) to a C functions.
This can be solved by doing something like
struct foo { // alignment=2 (x86) char a; short b; };
is compiled to:
struct foo_data { // alignment still 2 char a; short b; }; struct foo { // alignment=4 (x86) type_info const *ti; foo_data data; };
and passing foo* to an extern C function looks like:
cfun (&foo->data);
Similarly, a struct foo can be passed as
cfun (foo.data)
.
Special care must be taken with casts. We support casting to supertypes. This
should retain the
type_info
of the original struct, but if its pointee
is copied, the
type_info
should be updated to reflect the supertype.
Luckily, there is no
void*
, but
`t
exists and this also needs
special thought, because rtti is most useful for generic functions. The
problem is that
`t
can be non-pointer. Perhaps it is a good idea to forbid
non-pointers or introduce some kind of autoboxing for non-pointer types.