Find symbol in unknown module at compile time?

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 6 21:42:31 PST 2014


> why don't create a dummy module which only keeps list of all 
> known
> objects, and create `shared static this ()` constructors which 
> will
> just register everything in that module?

I am actually doing this already, but this only solves for 
finding information at runtime.

One or more of these are created in a module by the mixin above:

class testReflection : Reflection {
	override void register(){ ... }
	static const(ModuleDecl) info = ... ;
}

abstract class Reflection {
	void register();
}

static this() {
	foreach(m; ModuleInfo) {
		foreach(c; m.localClasses()) {
			if(c.base is Reflection.classinfo) {
				Reflection refl = cast(Reflection)c.create();
				refl.register();
			}
		}
	}
}

So as long as 'Test_Reflection' is in the module that it's 
reflecting, I can get the info I need like this:

__traits(getMember, __traits(getMember, test, "test" ~ 
"Reflection"), "info");

But, this doesn't work if the mixin is instantiated in a separate 
module.

Now when the reflection actually can be found, the entire 
reflection data hierarchy is accessible at compile time.

Either one of these could work:

Tuple(symbols) __traits(find, string name)
Tuple(alias or string) __traits(allModules)

I have minimal experience with compiler technology right now, and 
I've just recently gotten my own fork of DMD set up, but wouldn't 
the first option be as simple as looking the name up in a symbol 
table and returning it?



More information about the Digitalmars-d mailing list