another cool RTInfo trick - i want in runtime

Jacob Carlborg doob at me.com
Thu Jan 16 12:13:28 PST 2014


On 2014-01-16 16:57, Adam D. Ruppe wrote:

> Brothers and sisters, I've talked to you before about doing custom
> RTInfo using __traits(compiles) tricks to look for the config module,
> but my previous ways - while they worked - sucked because the compiler
> would look for the module over and over again, slowing things down a bit.
>
> This new method achieves success on all counts. By offering a file with
> druntime to be the module up front, the compiler does no special
> searching, it finds it. It is a trivial file, so impact on compile speed
> is negligible.
>
> But then, if you offer your own file with the same module name on the
> command line, dmd looks THERE first, finds your special code, and knows
> to skip searching the import path! Even true for object.d, since this is
> all in templates with local imports; the template still needs to be
> instantiated.

Does this only work once? Say I have two libraries that use this 
technique, would that work?

This seems somewhat complicated, depending on the orders of modules 
passed to the compiler.

> Since this object.d doesn't actually store anything* in the RTInfo
> itself, there's no worry of binary incompatibilities resulting from
> separate compiliation with different rtinfo modules. It simply provides
> a hook into the types for checking.

No, not yet. As far as I know, the idea was to use RTInfo to collect and 
store type info for a precise GC.

> Using this, we can automatically check all kinds of per-project
> requirements. Don't want virtual functions in your class unless they are
> reviewed? Make your TypeCheck loop over them and look for a UDA, issuing
> a static assert if not there. Don't want references to un-owned mutable
> data? Ditto. Have a naming convention to check? Can do that too.
>
>
> I see lots of upsides, and the downsides from my last experiment with
> this I believe have been eliminated. Am I missing something? I wanna do
> a druntime pull request!

I have had a different idea for a solution to this problem for a while. 
That is, add a new manifest constant to object.d, called rtInfo. This 
will act like a UDA. Any template with the object.rtInfo UDA attached to 
itself will behave just as RTInfo does today.

module object;

enum rtInfo;

@rtInfo template RTInfo (T)
{
     enum RTInfo = null;
}

module foo.check_virtual_methods;

@rtInfo template CheckVirtualMethods (T)
{
     static if (!containsVirtualMethods!(T))
         static assert(false, "Error, contains virtual methods");

     enum CheckVirtualMethods = null;
}

An idea for storing data in TypeInfo is to create an associative array. 
The keys would be the fully qualified name of the template, in this case 
"foo.check_virtual_methods.CheckVirtualMethods". The values would be 
whatever the template evaluates to. There's one problem with this, the 
associative array would need to be built at runtime due to separate 
compilation.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list