Support for C header files in D

Don Clugston dac at nospam.com.au
Mon Mar 20 07:48:49 PST 2006


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



More information about the Digitalmars-d mailing list