[cfe-dev] Reflection
David Chisnall
csdavec at swan.ac.uk
Tue Dec 14 06:52:54 PST 2010
On 13 Dec 2010, at 18:37, Stuart Carnie wrote:
> I imagine some of the things you would need are:
> • A runtime, similar to Objective-C's, which needs some of the following capabilities
> • collection of metadata classes which describe methods, types, etc
> • APIs to read over data sections to extract metadata and generate aforementioned classes
> • Public APIs to query for this metadata, create instances of classes via name, etc
> • Changes to compiler
> • Generation of metadata into special data section
> • Generation of entry points / APIs to compiled libraries, executables to allow you to do things like create instances of classes / types by name, etc
>
It's worth noting that C++ already has some of this via RTTI, although not all of it is public. The classes declared in <typeinfo> give you some information about an object via public interfaces (e.g. what its superclasses are and so on). They also include a name field which contains an encoding of the field types.
Unlike Objective-C, this encoding is defined by the ABI, rather than by the language, although you can use the typeid operator to get a type info object for a specific type and then use dynamic_cast<> to see which subclass of std::type_info it is. The (BSD-licensed) libelf-tc library in the elftoolchain project has code for parsing these encodings for the old-GNU, new-GNU (Itanium), and ARM ABIs, so it wouldn't be too much effort to create something that gives you more useful introspection metadata.
This information does not, as far as I am aware, give you type encodings for methods, so that would need to be added, which would modify the ABI somewhat (although not necessarily in an incompatible way).
One question though: When you say reflection, do you actually mean reflection, or do you just mean introspection? For example, true reflection would allow you to add methods to a C++ class, which would require modifying the vtable. If you're only replacing methods, this is quite simple (although, again, ABI-dependent), but adding methods would be much harder. Creating new types at run time would also be relatively difficult. Simply extending C++ RTTI to provide useful introspection would be a lot easier.
David
More information about the cfe-dev
mailing list