dmd icon indicating copy to clipboard operation
dmd copied to clipboard

add support for Object.RTTypeid template

Open WalterBright opened this issue 4 years ago • 10 comments

Currently, instances of TypeInfo are generated by a complex and tedious process inside the compiler. This change experiments with using a template in druntime, Object.RTTypeid instead, to generate it. The compiler will know nothing about the contents of TypeInfo, leaving people free to innovate with the definition of RTTypeid. (All that code in the compiler will be removed once RTTypeid is successful.)

This is analogous to the successful use of Object.RTinfo to generate info for the garbage collector. The implementation of RTTypeid is very similar for that for RTinfo, which can be found in Semantic3Visitor in semantic3.d

This PR makes it so that if Object.RTTypeid is not there, it falls back to the original compiler implementation of TypeInfo.

WalterBright avatar Jul 26 '20 07:07 WalterBright

Thanks for your pull request, @WalterBright!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#11459"

dlang-bot avatar Jul 26 '20 07:07 dlang-bot

Currently, instances of TypeInfo are generated by a complex and tedious process inside the compiler. This change experiments with using a template in druntime, Object.RTTypeid instead, to generate it. The compiler will know nothing about the contents of TypeInfo, leaving people free to innovate with the definition of RTTypeid. (All that code in the compiler will be removed once RTTypeid is successful.)

This is analogous to the successful use of Object.RTinfo to generate info for the garbage collector. The implementation of RTTypeid is very similar for that for RTinfo, which can be found in Semantic3Visitor in semantic3.d

This PR makes it so that if Object.RTTypeid is not there, it falls back to the original compiler implementation of TypeInfo.

So the call to toObjFile is planned to be removed?

ibuclaw avatar Jul 26 '20 07:07 ibuclaw

So the call to toObjFile is planned to be removed?

??

WalterBright avatar Jul 26 '20 08:07 WalterBright

See also: https://github.com/dlang/druntime/pull/3172. Need to fix the associative arrays first.

andralex avatar Jul 27 '20 03:07 andralex

Doesn't this completely obsolete RTInfo? It is currently just accessible through typeinfo and if typeinfo is itself a template, whatever we want from RTInfo can simply be done in here.

adamdruppe avatar Jul 27 '20 14:07 adamdruppe

@adamdruppe maybe they can be merged in the future.

WalterBright avatar Jul 29 '20 08:07 WalterBright

I modified it so if RTTypeid doesn't return a variable, it'll just use the old builtin method. This is so a partial implementation of RTTypeid can be developed.

WalterBright avatar Jul 29 '20 08:07 WalterBright

@WalterBright Thanks, that didn't work with:

template RTTypeid(T)
if (is(T == int))
{
    ...
}

I get a bunch of errors such as:

Error: cannot interpret RTTypeid!(const(char[])) at compile time
Error: cannot interpret RTTypeid!(char[]) at compile time
src/core/demangle.d(2337): Error: template instance core.demangle.mangle!(nothrow @nogc string(string, scope string delegate(string) nothrow @nogc, bool)) error instantiating

They suggest RTTypeid is instantiated with a bunch of types, not just int.

andralex avatar Jul 29 '20 20:07 andralex

Even simpler to eliminate all possibilities of error:

struct neverusedbyanyone {}
template RTTypeid(T)
if (is(T == neverusedbyanyone))
{
}

causes the same errors

andralex avatar Jul 29 '20 20:07 andralex

Oh, even simpler:

template RTTypeid(T)
if (false)
{
}

This also causes errors. That's the simplest baseline.

andralex avatar Jul 29 '20 20:07 andralex