Skip to content

Commit ff3d6a9

Browse files
WalterBrightdlang-bot
authored andcommitted
document ImportC to D source translation
1 parent 16e1720 commit ff3d6a9

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

spec/importc.dd

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,51 @@ $(H2 $(LNAME2 wrapping, Wrapping C Code))
837837
$(P Then, `import file_ic;` instead of `import file;`, and use `S_t` when $(CCODE struct S) is desired.)
838838

839839

840+
$(H2 $(LNAME2 ctod, Converting C Code to D Code))
841+
842+
$(P Sometimes its desirable to go further than importing C code, to actually do a C source to
843+
D source conversion. Reasons include:)
844+
845+
$(UL
846+
$(LI Migrating a C project to a D project.)
847+
$(LI Equivalent D code can compile much faster, due to not needing a preprocessor, etc.)
848+
$(LI Tweaking the D code to add attributes for memory safety, purity, etc.)
849+
$(LI Eliminating the need for C-isms in the D part of the project.)
850+
)
851+
852+
$(P This can be done with the D compiler by using the $(TT -Hf) switch:)
853+
854+
$(CONSOLE
855+
dmd -c mycode.c -Hf=mycode.di
856+
)
857+
858+
$(P which will convert the C source code in $(TT mycode.c) to D source code in $(TT mycode.di).
859+
If the $(TT -inline) switch is also used, it will emit the C function bodies as well, instead
860+
of just the function prototypes.)
861+
862+
$(H3 Impedance Mismatch)
863+
864+
$(P A precise mapping of C semantics, with all its oddities, to D source code is not
865+
always practical. ImportC uses C semantics in its semantic analysis to get much closer
866+
to exact C semantics than is expressible in D source code. Hence, the translation to
867+
D source code will be less than perfect. For example:)
868+
869+
$(CCODE
870+
int S;
871+
struct S { int a, b; };
872+
int foo(struct S s)
873+
{
874+
return S + s.a;
875+
}
876+
)
877+
878+
$(P will work fine in ImportC, because the `int S` and the `struct S` are in different
879+
symbol tables. But in the generated D code, both symbols would be in the same symbol table, and will collide.
880+
Such D source code translated from C will need to be adjusted by the user.)
881+
882+
$(P Nevertheless, reports from the field are that this conversion capability is a huge
883+
timesaver for users who need to deal with existing C code.)
884+
840885
$(H2 $(LNAME2 warnings, Warnings))
841886

842887
$(P Many suspicious C constructs normally cause warnings to be emitted by default by

0 commit comments

Comments
 (0)