www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Generating headers with -H

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
This question may actually belong in .learn.

What's exactly eliminated from the header generated with -H? I tried it 
just now and was surprised to see that the generated .di file includes 
the function bodies of regular (non-template) functions and methods.

I was under the impression that generated headers exclude function 
bodies, and found documentation to support that viewpoint:

http://www.digitalmars.com/d/2.0/dmd-windows.html#interface_files

says:

============
A D interface file contains only what an import of the module needs, 
rather than the whole implementation of that module.

The advantages of using a D interface file for imports rather than a D 
source file are:

     * D interface files are often significantly smaller and much faster 
to process than the corresponding D source file.
     * They can be used to hide the source code, for example, one can 
ship an object code library along with D interface files rather than the 
complete source code.
============

This strongly suggests that function bodies are eliminated. But what I'm 
seeing is that pretty much all function bodies are kept (exception: 
static this() functions are not), and only comments are removed.

So what's -H really doing?


Andrei
Nov 02 2009
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 02 Nov 2009 11:53:12 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail erdani.org> wrote:

 This question may actually belong in .learn.

 What's exactly eliminated from the header generated with -H? I tried it  
 just now and was surprised to see that the generated .di file includes  
 the function bodies of regular (non-template) functions and methods.

 I was under the impression that generated headers exclude function  
 bodies, and found documentation to support that viewpoint:

 http://www.digitalmars.com/d/2.0/dmd-windows.html#interface_files

 says:

 ============
 A D interface file contains only what an import of the module needs,  
 rather than the whole implementation of that module.

 The advantages of using a D interface file for imports rather than a D  
 source file are:

      * D interface files are often significantly smaller and much faster  
 to process than the corresponding D source file.
      * They can be used to hide the source code, for example, one can  
 ship an object code library along with D interface files rather than the  
 complete source code.
 ============

 This strongly suggests that function bodies are eliminated. But what I'm  
 seeing is that pretty much all function bodies are kept (exception:  
 static this() functions are not), and only comments are removed.

 So what's -H really doing?

It removes whitespace and comments. My understanding of why it does not remove implementations is for CTFE and inlining. Somehow I got it in my head that a significantly large enough function would have it's body removed, but I can't remember why I think that... Note that the above snippit from the docs suggests reasons for using d interface files, *NOT* reasons for using dmd -H :) -Steve
Nov 02 2009
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Steven Schveighoffer wrote:
 On Mon, 02 Nov 2009 11:53:12 -0500, Andrei Alexandrescu 
 <SeeWebsiteForEmail erdani.org> wrote:
 
 This question may actually belong in .learn.

 What's exactly eliminated from the header generated with -H? I tried 
 it just now and was surprised to see that the generated .di file 
 includes the function bodies of regular (non-template) functions and 
 methods.

 I was under the impression that generated headers exclude function 
 bodies, and found documentation to support that viewpoint:

 http://www.digitalmars.com/d/2.0/dmd-windows.html#interface_files

 says:

 ============
 A D interface file contains only what an import of the module needs, 
 rather than the whole implementation of that module.

 The advantages of using a D interface file for imports rather than a D 
 source file are:

      * D interface files are often significantly smaller and much 
 faster to process than the corresponding D source file.
      * They can be used to hide the source code, for example, one can 
 ship an object code library along with D interface files rather than 
 the complete source code.
 ============

 This strongly suggests that function bodies are eliminated. But what 
 I'm seeing is that pretty much all function bodies are kept 
 (exception: static this() functions are not), and only comments are 
 removed.

 So what's -H really doing?

It removes whitespace and comments. My understanding of why it does not remove implementations is for CTFE and inlining. Somehow I got it in my head that a significantly large enough function would have it's body removed, but I can't remember why I think that...

You're right. I just added a loop to a function, and its body disappeared from the .di file. Thanks! The CTFE-ability of functions seems to play no role in they being kept around, however.
 Note that the above snippit from the docs suggests reasons for using d 
 interface files, *NOT* reasons for using dmd -H :)

Yeah, I'd agree if the link didn't come from the same page's explanation of -H. I think the doc should be clearer about the fact that the compiler has the freedom to keep whichever function bodies it finds fit for inlining. Andrei
Nov 02 2009
prev sibling next sibling parent Frank Benoit <keinfarbton googlemail.com> writes:
I think there should be also something added in .di generation:

http://d.puremagic.com/issues/show_bug.cgi?id=2795
Nov 02 2009
prev sibling parent reply grauzone <none example.net> writes:
Andrei Alexandrescu wrote:
 This question may actually belong in .learn.
 
 What's exactly eliminated from the header generated with -H? I tried it 
 just now and was surprised to see that the generated .di file includes 
 the function bodies of regular (non-template) functions and methods.
 
 I was under the impression that generated headers exclude function 
 bodies, and found documentation to support that viewpoint:
 
 http://www.digitalmars.com/d/2.0/dmd-windows.html#interface_files
 
 says:
 
 ============
 A D interface file contains only what an import of the module needs, 
 rather than the whole implementation of that module.
 
 The advantages of using a D interface file for imports rather than a D 
 source file are:
 
     * D interface files are often significantly smaller and much faster 
 to process than the corresponding D source file.
     * They can be used to hide the source code, for example, one can 
 ship an object code library along with D interface files rather than the 
 complete source code.
 ============
 
 This strongly suggests that function bodies are eliminated. But what I'm 
 seeing is that pretty much all function bodies are kept (exception: 
 static this() functions are not), and only comments are removed.
 
 So what's -H really doing?

Quoting from the same link: "[...] they are not part of the D language. They are a feature of the compiler, and serve only as an optimization of the build process." A compiler specific hack that speeds up the build process. Especially notice how it includes inlineable functions by design, which make it relatively useless as "library headers".
 
 Andrei

Nov 02 2009
parent "Denis Koroskin" <2korden gmail.com> writes:
On Mon, 02 Nov 2009 22:48:59 +0300, grauzone <none example.net> wrote:

 Andrei Alexandrescu wrote:
 This question may actually belong in .learn.
  What's exactly eliminated from the header generated with -H? I tried  
 it just now and was surprised to see that the generated .di file  
 includes the function bodies of regular (non-template) functions and  
 methods.
  I was under the impression that generated headers exclude function  
 bodies, and found documentation to support that viewpoint:
  http://www.digitalmars.com/d/2.0/dmd-windows.html#interface_files
  says:
  ============
 A D interface file contains only what an import of the module needs,  
 rather than the whole implementation of that module.
  The advantages of using a D interface file for imports rather than a D  
 source file are:
      * D interface files are often significantly smaller and much  
 faster to process than the corresponding D source file.
     * They can be used to hide the source code, for example, one can  
 ship an object code library along with D interface files rather than  
 the complete source code.
 ============
  This strongly suggests that function bodies are eliminated. But what  
 I'm seeing is that pretty much all function bodies are kept (exception:  
 static this() functions are not), and only comments are removed.
  So what's -H really doing?

Quoting from the same link: "[...] they are not part of the D language. They are a feature of the compiler, and serve only as an optimization of the build process." A compiler specific hack that speeds up the build process. Especially notice how it includes inlineable functions by design, which make it relatively useless as "library headers".
  Andrei


.d and .di files are meant to be interchangeable without any impact on produced binary. I do think that -H-strip-all (including private methods etc) would be nice, though.
Nov 02 2009