www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - .di files have implementation??

reply "Nick Sabalausky" <a a.a> writes:
I think I've misunderstood something about .di files:

$ cat testDI.d
private int foo(){ return 1; }
$ dmd -H -c testDI.d
$ cat testDI.di
// D import file generated from 'testDI.d'
private int foo()
{
return 1;
}

Why does the .di file include the body of foo? In fact, why does it have foo 
at all? I thought the point of .di files was they just have the public 
declarations, not the bodies and not the private stuff. Well, except for 
templates, obviously, but this isn't a template.

What's going on? What am I misunderstanding?
Oct 22 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
It's because the function is small enough to be inlineable, you can't
inline it if you've only got the prototype in the header file. So it's
optimizations at play.
Oct 22 2011
parent "Nick Sabalausky" <a a.a> writes:
"Andrej Mitrovic" <andrej.mitrovich gmail.com> wrote in message 
news:mailman.331.1319338165.24802.digitalmars-d-learn puremagic.com...
 It's because the function is small enough to be inlineable, you can't
 inline it if you've only got the prototype in the header file. So it's
 optimizations at play.

I hadn't expected that to happen without specifying -inline. But I guess this way makes sence since it lets the user app decide whether or not to inline.
Oct 22 2011
prev sibling next sibling parent Andrew Wiley <wiley.andrew.j gmail.com> writes:
--000e0cd4b0d63bbd4804afee7827
Content-Type: text/plain; charset=ISO-8859-1

On Sat, Oct 22, 2011 at 9:49 PM, Andrej Mitrovic <andrej.mitrovich gmail.com
 wrote:

 It's because the function is small enough to be inlineable, you can't
 inline it if you've only got the prototype in the header file. So it's
 optimizations at play.

Interestingly, it looks like GNU ld should be able to inline functions as part of link time optimizations, which would make this unnecessary on platforms where DMD uses ld. Probably not worth looking into right now, but if di files become more used in the future, this sort of thing could probably go away (except on Windows, but the linker situation there is probably one of the most problematic parts of the D toolchain). --000e0cd4b0d63bbd4804afee7827 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sat, Oct 22, 2011 at 9:49 PM, Andrej Mitrovic= <span dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitrovich gmail.com">andrej= .mitrovich gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_qu= ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex= ;"> It&#39;s because the function is small enough to be inlineable, you can&#39= ;t<br> inline it if you&#39;ve only got the prototype in the header file. So it&#3= 9;s<br> optimizations at play.<br> </blockquote></div><br><div>Interestingly, it looks like GNU ld should be a= ble to inline functions as part of link time optimizations, which would mak= e this unnecessary on platforms where DMD uses ld. Probably not worth looki= ng into right now, but if di files become more used in the future, this sor= t of thing could probably go away (except on Windows, but the linker situat= ion there is probably one of the most problematic parts of the D toolchain)= .</div> --000e0cd4b0d63bbd4804afee7827--
Oct 22 2011
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
It could also be considered a little bit dangerous. E.g. you could
forget to regenerate header files after compilation and potentially
leave the user code with an old function body.
Oct 22 2011
parent "Nick Sabalausky" <a a.a> writes:
"Andrej Mitrovic" <andrej.mitrovich gmail.com> wrote in message 
news:mailman.333.1319338819.24802.digitalmars-d-learn puremagic.com...
 It could also be considered a little bit dangerous. E.g. you could
 forget to regenerate header files after compilation and potentially
 leave the user code with an old function body.

...Or specify different compilation options (like version identifiers) when generating the lib/di versus when building your app. Which could leave a chimera of a module, a module built partially with one configuration and partially with another.
Oct 22 2011