[dmd-internals] How does TypeInfo.init work?
Steven Schveighoffer via dmd-internals
dmd-internals at puremagic.com
Thu Jun 11 20:31:19 PDT 2015
And in fact, that is what it does :)
auto x = typeid(int);
writefln("%s, %s", x.init.length, x.init.ptr);
Doing some research, I found this tidbit in history:
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.
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.
> On Jun 11, 2015, at 7:48 PM, Mike Franklin <slavo5150 at yahoo.com> wrote:
> Ok, but if you look at the TypeInfo class in object.d , you'll see init() returns null. Then if you look at the TypeInfo_i class for integers , it doesn't override init(). I, therefore, suspect some compiler special treatment. So, how (and where in the source code) does an integer get it's 32-bit 0 initial value?
>  https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L289 <https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L289>
>  https://github.com/D-Programming-Language/druntime/blob/master/src/rt/typeinfo/ti_int.d#L18 <https://github.com/D-Programming-Language/druntime/blob/master/src/rt/typeinfo/ti_int.d#L18>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the dmd-internals