www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Support for C header files in D

reply "Lionello Lunesu" <lio remove.lunesu.com> writes:
Hi again,

This has been mentioned before but I can't find any final word on the 
matter:

<legalese>
Seeing how D is ABI compatible with C and also the huge amount of useful C 
libraries in existence; also noting the amount of time needed to translate C 
header files, and keeping them up-to-date as new versions come out; finally 
observing that the creator of D has a vast experience in writing C 
compilers, and the fact that the D compiler and C compiler of the 
aformentioned share some program code,
</legalese>

Why not enable dmd to "import" C header files directly?

L.

PS. sorry about the legalese. Had to write some official docs for a 
residents permit, and I'm starting the enjoy the language : )
Mar 20 2006
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Lionello Lunesu wrote:
 Hi again,
 
 This has been mentioned before but I can't find any final word on the 
 matter:
 
 <legalese>
 Seeing how D is ABI compatible with C and also the huge amount of useful C 
 libraries in existence; also noting the amount of time needed to translate C 
 header files, and keeping them up-to-date as new versions come out; finally 
 observing that the creator of D has a vast experience in writing C 
 compilers, and the fact that the D compiler and C compiler of the 
 aformentioned share some program code,
 </legalese>
 
 Why not enable dmd to "import" C header files directly?

Because this would happen: 1. People create D projects that will only compile with DMD, because they rely on a legacy C API with .h files not yet translated into D modules. 2. Writers of other D compilers will feel compelled to build in a C compiler so that they can compile such projects. 3. It will become a de facto standard that D supports C header files. 4. By making D fully source-compatible with C, it would become bloated once again with all of C's archaisms and complexities that D was designed to eliminate. And so things would come full circle, destroying the ease of implementation issue that is one of the basic design goals of D. The Internet domain name system is suffering a similar fate in this day and age. Once upon a time, there was a flat namespace in which every machine on the net had a unique name. The hierarchical domain name system we have now came along to replace this, with both the grouping of entities into .com, .org, etc. along with hundreds of country codes and the ability to create subdomains to (theoretically) arbitrary depth. Nowadays, marketing types throw a lot of money down the drain (along with the safety of their customers) trying to turn it into a flat namespace once again. http://domains.dan.info/structure/intro.html We can do better at keeping D true to its design goals. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Mar 20 2006
parent "Lionello Lunesu" <lio remove.lunesu.com> writes:
Thanks, that's a good point you mention there. People will indeed expect it. 
If you allow DMD to "import" header files, then that'll be something you're 
stuck with forever.

However, a C header file could be converted into a D header file, I suppose. 
I guess that should be possible in a manner similar to the way dmd creates D 
header files from D modules.

L.

Note that I'm not talking about C++ header files. That would indeed be too 
complicated, and useless, since there's no standard C++ ABI to support. 
(right?) 
Mar 20 2006
prev sibling parent reply Don Clugston <dac nospam.com.au> writes:
I don't think it's possible, even in theory, to convert C to D 
automatically. #define is a total nightmare. Macros have to be converted
to inline functions. Some #defines need to become constants.

Also, C doesn't define the size of basic types. Etc.

Incidentally, "Converting C to D" recommends running the preprocessor
on the code first. This doesn't really work. I've seen a great many 
header files where #defines were used, where enums should have been used 
instead. Sometimes this makes you lose most of the information from the 
file.

BUT...
what would be *very* useful would be something that turned a 
preprocessed C file into D.

I think we need to write a preprocessor which:
* goes through the .H file, recording every #define, and every use that 
is made of it.
* Based on this usage data, attempt to determine the meaning of each 
#define. (eg, if it is ever #undef-ed, after it's #defined, it is not a 
constant, it should be expanded instead). And also determine whether 
#ifdef's should become version() {} statements, or just evaluated.
* If there are any ambiguous constructions, we report them now and 
abort. User must provide an additional file to resolve ambiguities.
* Armed with this information, we then preprocess the file, Every 
#define becomes a const, is replaced with an inline function, or is 
expanded, depending on how it is used.

Writing a C pre-processor does not seem too difficult (is that correct, 
Walter?) Maybe there's even source code for one lying around.
(boost.wave is available, but it seems unnecessarily complicated).

Lionello Lunesu wrote:
 Hi again,
 
 This has been mentioned before but I can't find any final word on the 
 matter:
 
 <legalese>
 Seeing how D is ABI compatible with C and also the huge amount of useful C 
 libraries in existence; also noting the amount of time needed to translate C 
 header files, and keeping them up-to-date as new versions come out; finally 
 observing that the creator of D has a vast experience in writing C 
 compilers, and the fact that the D compiler and C compiler of the 
 aformentioned share some program code,
 </legalese>
 
 Why not enable dmd to "import" C header files directly?
 
 L.
 
 PS. sorry about the legalese. Had to write some official docs for a 
 residents permit, and I'm starting the enjoy the language : )
 
 

Mar 20 2006
next sibling parent "Lionello Lunesu" <lio remove.lunesu.com> writes:
That's actually kind-of what I meant. A tool to turn a C (header) file into 
a .di, D header file. Maybe walter can put the .di generation code from dmd 
into dmc? ;)

L. 
Mar 20 2006
prev sibling parent reply John Reimer <John_member pathlink.com> writes:
In article <dvmit2$34d$1 digitaldaemon.com>, Don Clugston says...

<snip>

Writing a C pre-processor does not seem too difficult (is that correct, 
Walter?) Maybe there's even source code for one lying around.
(boost.wave is available, but it seems unnecessarily complicated).

There are good preprocessors out there. Beyond using the ones integrated in dmc and gcc, there is mcpp ( http://www.m17n.org/mcpp/index_eng.html ). I like mcpp. I've used it for C -> D conversion projects successfully. The preprocessor junk is by far the biggest challenge in the conversion process and using mcpp seems to make short work of the procedure as long as the correct command line definitions are declared. I used mcpp as one of the steps in converting the large complicated freetype header array (which uses macros so extensively that it makes the source hard to follow) into consumable text for my python scripts. These custom scripts then output the appropriate D source in a format compatible with my dynamic loader engine. I ended up touching up the resulting D source with some manual changes, but the whole conversion could probably have been automated completely. I know Tom S., aka h3r3tic, has also done similar procedures with C -> D header conversion, although I think he has used the integrated compiler preprocessor for the job. mcpp is opensource, and I imagine it could be integrated into a bigger D -> C header project without too much difficulty. -JJR
Mar 20 2006
parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
John Reimer wrote:
 I know Tom S., aka h3r3tic, has also done similar procedures with C -> D header
 conversion, although I think he has used the integrated compiler preprocessor
 for the job.

Yeah, I used GCC with -save-temps, then some regexping with gVim or Python. Works for me :) -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O !M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y ------END GEEK CODE BLOCK------ Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Mar 20 2006