[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 :)

void main()
{
    import std.stdio;
    auto x = typeid(int);
    writefln("%s, %s", x.init.length, x.init.ptr);
}

output:

0, null

Doing some research, I found this tidbit in history:

https://issues.dlang.org/show_bug.cgi?id=2990 <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.

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.

-Steve

> 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 [1], you'll see init() returns null.  Then if you look at the TypeInfo_i class for integers [2], 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?
> 
> [1] 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>
> [2] 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>
>  
> Mike
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/dmd-internals/attachments/20150611/44c81f03/attachment-0001.html>


More information about the dmd-internals mailing list