Finding class template instantiations via runtime reflection (for openmethods)

Steven Schveighoffer schveiguy at yahoo.com
Fri Sep 22 12:16:52 UTC 2017


On 9/21/17 4:32 PM, Jean-Louis Leroy wrote:
> It did not take long! Someone tried to create templatized open methods 
> and it didn't work right of the box. I expected that, but in fact there 
> may be a bit of hope. You cannot have virtual function templates in C++ 
> or in D because the layout of the vtables have to be known at compile 
> time - but openmethods creates its method tables at runtime so maybe it 
> can be made to work.
> 
> I stumbled upon a problem very quickly: it seems that classes that come 
> from class template instantiations are not registered in ModuleInfo - 
> see below,
> 
> import std.stdio;
> 
> class Foo {}
> 
> class Bar(T) : Foo
> {
>    int i;
> }
> 
> alias BarInt = Bar!int;
> 
> const barInt = new BarInt;
> 
> void main()
> {
>    foreach (mod; ModuleInfo) {
>      foreach (c; mod.localClasses) {
>        writeln(c);
>      }
>    }
> }
> 
> ....output:
> modtemp.Foo
> core.exception.RangeError
> core.exception.AssertError
> etc...
> 
> Neither 'Bar!int' nor 'BarInt' appear in 'localClasses'.
> 
> Ideas?
> 

It used to be that classes were always in ModuleInfo (and that 
ModuleInfo was always generated). That's not so much the case any more, 
as there has been a push to reduce the runtime footprint and complexity 
(especially toward -betterC). This makes traditional runtime reflection 
more difficult and sporadic.

However, I would expect that if ModuleInfo is generated for a file, and 
some classes are added, then ALL classes instantiated in the module 
should be added.

I would say it's a bug.

-Steve


More information about the Digitalmars-d mailing list