Runtime reflection idea

Adam D. Ruppe destructionator at gmail.com
Thu May 30 07:59:52 PDT 2013


The compiler instantiates a template called RTInfo!T for every 
custom type T that is made available through the TypeInfo class 
(.classinfo, typeid()).

The first idea with this was to do precise GC, but it can do more 
than just that. Currently, it is totally unused in druntime. I 
propose we put it to use to allow some runtime reflection.


First, here's a taste of what it can look like (or whatever else, 
I did name/value strings since that's easy to write for an 
example and access at runtime, even without phobos, but there's 
no reason we couldn't try something else, maybe a class or 
something would be good):


@CustomTypeInfoExtension("name", "value")
@CustomTypeInfoExtension("name2", "value2")
class Test { }

int main(string[] args) {
         auto rtInfo = typeid(Test).rtInfo();

         import std.stdio;
         foreach(ci; rtInfo.customInfo) {
                 writeln(ci.name, " = ", ci.value);
         }
}

// prints:
// name = value
// name2 = value2



Here's some example code I wrote up as a proof of concept in my 
custom druntime:

struct MoreTypeInfo {
     hash_t hash;
     string stringOf;
   /* whatever else we want, this is extensible by druntime */

     // and finally the custom info
     CustomTypeInfoExtension[] customInfo;
}

struct CustomTypeInfoExtension {
    // we can do this however too, I just do name/value pairs
    // as something simple to get us started
         string name;
         string value;
         string meta;
}

// fetch the data from a type using UDA capabilities
CustomTypeInfoExtension[] getCustomInfo(T)() {
        CustomTypeInfoExtension[] ext;
        foreach(attr; __traits(getAttributes, T))
               static if(is(typeof(attr) == 
CustomTypeInfoExtension))
                      ext ~= attr;
         return ext;
}

// and the thing dmd calls:
template RTInfo(T) {
         __gshared static immutable minfo =
             // the first few args are just to show druntime still
             // has special access... and the last one is to fetch
             // the custom stuff
             MoreTypeInfo(typehash!T, T.stringof, getCustomInfo!T);
         enum RTInfo = &minfo;
}






Since the template is already instantiated by the compiler - this 
exists today and works if you wanna modify your object.d in 
druntime - we don't have to do like a mixin registerModule to 
parse this UDA. Of course, you still have that option and it 
rocks, this just gives a simple runtime thing we can use out of 
the box....

and ModuleInfo even has a list of all classes, and all modules in 
a program, so we could go pretty nuts with runtime stuff here.



What do you all think? It is a pretty minor change to druntime to 
enable this kind of thing, it wouldn't get in the way of anything 
else - druntime can still use RTInfo for its own thing when the 
time comes without a problem -  and it seems to me like it could 
be easily useful and convenient for some tasks.


More information about the Digitalmars-d mailing list