[dmd-internals] How does TypeInfo.init work?

Mike Franklin via dmd-internals dmd-internals at puremagic.com
Fri Jun 12 05:25:13 PDT 2015


(I hope this formatting comes out right)

>And in fact, that is what it does :)>
>void main()
>{
>    import std.stdio;
>    auto x = typeid(int);
>    writefln("%s, %s", x.init.length, x.init.ptr);
>}
>
>output:
>
>0, null


Actually, I think it is only half right.  Shouldn't x.init.length return 4?

>Doing some research, I found this tidbit in history:
>
>https://issues.dlang.org/show_bug.cgi?id=2990
>
>The only basic types that have init() defined are the ones that have non-zero initializers. I suppose we have 3 options:
>
>1. Revise the documentation so it says “if this returns null with zero length, it means the type is initialized with all zeros, and use tsize to get the size of the type”
>2. Add the appropriate overrides to all the druntime types.
>3. We could make TypeInfo.init return (cast(void *)null)[0..tsize] by default, defaulting all inits to a sane value.
>
>My personal preference is for 3, since it adds no new functions, and will fix any omissions that we haven’t discovered or that would be added in the future.


I'm inclined to agree with you.  I think I'll put this on my todo list for pull request when the time is right.  I think now is not it.  Perhaps after 2.068 is released.

>Clearly, though, this identifies how unused that function really is :)
>
>I recall now, the compiler only sets up static data inside the typeinfo for things like classes and structs, and the TypeInfo_Struct init() function reads that when determining what to return.
>
>Everything else is defined in druntime.

Thanks Steve,  You've been very helpful.  Things are starting to come together for me now.



More information about the dmd-internals mailing list