| 1 |
Ddoc |
|---|
| 2 |
|
|---|
| 3 |
$(D_S htod, |
|---|
| 4 |
|
|---|
| 5 |
$(P While D is binary compatible with C code, it cannot |
|---|
| 6 |
compile C code nor C header files. In order for |
|---|
| 7 |
D to link with C code, the C declarations residing |
|---|
| 8 |
in C header files need to be converted to a D |
|---|
| 9 |
module. $(B htod) is a migration tool to aid in |
|---|
| 10 |
convering C header files. |
|---|
| 11 |
) |
|---|
| 12 |
$(P $(B htod) is built from the front end of the Digital |
|---|
| 13 |
Mars C and C++ compiler. It works just like a C or |
|---|
| 14 |
C++ compiler except that its output is a D module |
|---|
| 15 |
rather than object code. |
|---|
| 16 |
) |
|---|
| 17 |
$(P The macro $(B __HTOD__) is predefined and set to $(B 1), |
|---|
| 18 |
which is handy for improving C header files to give better |
|---|
| 19 |
D output. |
|---|
| 20 |
) |
|---|
| 21 |
|
|---|
| 22 |
<h3>Download</h3> |
|---|
| 23 |
|
|---|
| 24 |
$(P $(LINK2 http://ftp.digitalmars.com/htod.zip, htod) |
|---|
| 25 |
) |
|---|
| 26 |
|
|---|
| 27 |
<h3>Usage</h3> |
|---|
| 28 |
|
|---|
| 29 |
$(GRAMMAR |
|---|
| 30 |
$(B htod) $(I cheader.h) [$(I dimport.d)] [$(B -cpp)] [$(B -hc)] [$(B -hi)] [$(B -hs)] [$(B -ht)] { $(I C compiler switches) } |
|---|
| 31 |
) |
|---|
| 32 |
|
|---|
| 33 |
$(P where:) |
|---|
| 34 |
|
|---|
| 35 |
$(DL |
|---|
| 36 |
|
|---|
| 37 |
$(DT $(I cheader.h) |
|---|
| 38 |
$(DD C or C++ header input file |
|---|
| 39 |
) |
|---|
| 40 |
) |
|---|
| 41 |
|
|---|
| 42 |
$(DT $(I dimport.d) |
|---|
| 43 |
$(DD D source code output file (defaults to $(I cheader).d) |
|---|
| 44 |
) |
|---|
| 45 |
) |
|---|
| 46 |
|
|---|
| 47 |
$(DT $(B -cpp) |
|---|
| 48 |
$(DD Indicates a C++ header file |
|---|
| 49 |
) |
|---|
| 50 |
) |
|---|
| 51 |
|
|---|
| 52 |
$(DT $(B -hc) |
|---|
| 53 |
$(DD By default, $(B htod) will insert the C and C++ declarations |
|---|
| 54 |
in a file into the output file prefixed by $(TT //C ). |
|---|
| 55 |
$(B -hc) will suppress this. |
|---|
| 56 |
Use only if you're confident that $(B htod) is generating the |
|---|
| 57 |
correct output file (such as if the header file was modified with |
|---|
| 58 |
$(B __HTOD__)). |
|---|
| 59 |
) |
|---|
| 60 |
) |
|---|
| 61 |
|
|---|
| 62 |
$(DT $(B -hi) |
|---|
| 63 |
$(DD By default, $(B htod) will represent a $(TT #include "file") with |
|---|
| 64 |
a corresponding $(B import) statement. The $(B -hi) will cause the |
|---|
| 65 |
declarations in the included file to be converted to D declarations as |
|---|
| 66 |
well. The declarations in all included files are parsed regardless. |
|---|
| 67 |
$(B -hi) is handy when replacing an entire hierarchy of include files |
|---|
| 68 |
with a single D import. |
|---|
| 69 |
System includes like $(TT #include <file>) are not affected |
|---|
| 70 |
by $(B -hi). |
|---|
| 71 |
See also $(B -hs). |
|---|
| 72 |
) |
|---|
| 73 |
) |
|---|
| 74 |
|
|---|
| 75 |
$(DT $(B -ht) |
|---|
| 76 |
$(DD By default, $(B htod) will write types using typedef names as |
|---|
| 77 |
using those names. $(B -ht) will cause the underlying types to be |
|---|
| 78 |
used instead. This is very useful for "drilling down" layers of |
|---|
| 79 |
macros, typedefs, and #includes to find out the underlying type. |
|---|
| 80 |
) |
|---|
| 81 |
) |
|---|
| 82 |
|
|---|
| 83 |
$(DT $(B -hs) |
|---|
| 84 |
$(DD Works just like $(B -hi), except that system includes are |
|---|
| 85 |
migrated as well. |
|---|
| 86 |
) |
|---|
| 87 |
) |
|---|
| 88 |
|
|---|
| 89 |
$(DT $(I C compiler switches) |
|---|
| 90 |
$(DD C or C++ compiler switches, such as $(B -D) and $(B -I) as documented |
|---|
| 91 |
for $(LINK2 http://www.digitalmars.com/ctg/dmc.html, dmc). |
|---|
| 92 |
) |
|---|
| 93 |
) |
|---|
| 94 |
|
|---|
| 95 |
) |
|---|
| 96 |
|
|---|
| 97 |
<h3>Example</h3> |
|---|
| 98 |
|
|---|
| 99 |
$(P The C test.h file:) |
|---|
| 100 |
|
|---|
| 101 |
$(CCODE |
|---|
| 102 |
unsigned u; |
|---|
| 103 |
#define MYINT int |
|---|
| 104 |
void bar(int x, long y, long long z); |
|---|
| 105 |
) |
|---|
| 106 |
|
|---|
| 107 |
$(P Translated with:) |
|---|
| 108 |
|
|---|
| 109 |
$(CONSOLE |
|---|
| 110 |
htod test.h |
|---|
| 111 |
) |
|---|
| 112 |
|
|---|
| 113 |
$(P Produces the file test.d:) |
|---|
| 114 |
|
|---|
| 115 |
--- |
|---|
| 116 |
/* Converted to D from test.h by htod */ |
|---|
| 117 |
module test; |
|---|
| 118 |
//C unsigned u; |
|---|
| 119 |
extern (C): |
|---|
| 120 |
uint u; |
|---|
| 121 |
//C #define MYINT int |
|---|
| 122 |
//C void bar(int x, long y, long long z); |
|---|
| 123 |
alias int MYINT; |
|---|
| 124 |
void bar(int x, int y, long z); |
|---|
| 125 |
--- |
|---|
| 126 |
|
|---|
| 127 |
$(P The C declarations are prefixed by the string $(TT "//C ").) |
|---|
| 128 |
|
|---|
| 129 |
<h3>Type Mappings</h3> |
|---|
| 130 |
|
|---|
| 131 |
$(P C types are mapped as follows. These mappings are correct |
|---|
| 132 |
for Digital Mars C/C++, but may not be correct for your |
|---|
| 133 |
C compiler. D basic types have fixed sizes, while C basic |
|---|
| 134 |
type sizes are implementation defined. |
|---|
| 135 |
) |
|---|
| 136 |
|
|---|
| 137 |
$(TABLE1 |
|---|
| 138 |
<caption>Mapping C to D types</caption> |
|---|
| 139 |
$(TR $(TH C type) $(TH D type)) |
|---|
| 140 |
$(TR $(TD void) $(TD void)) |
|---|
| 141 |
$(TR $(TD _Bool) $(TD bool)) |
|---|
| 142 |
$(TR $(TD wchar_t) $(TD wchar)) |
|---|
| 143 |
$(TR $(TD char) $(TD char)) |
|---|
| 144 |
$(TR $(TD signed char) $(TD byte)) |
|---|
| 145 |
$(TR $(TD unsigned char) $(TD ubyte)) |
|---|
| 146 |
$(TR $(TD short) $(TD short)) |
|---|
| 147 |
$(TR $(TD unsigned short) $(TD ushort)) |
|---|
| 148 |
$(TR $(TD int) $(TD int)) |
|---|
| 149 |
$(TR $(TD unsigned) $(TD uint)) |
|---|
| 150 |
$(TR $(TD long) $(TD int)) |
|---|
| 151 |
$(TR $(TD unsigned long) $(TD uint)) |
|---|
| 152 |
$(TR $(TD long long) $(TD long)) |
|---|
| 153 |
$(TR $(TD unsigned long long) $(TD ulong)) |
|---|
| 154 |
$(TR $(TD float) $(TD float)) |
|---|
| 155 |
$(TR $(TD double) $(TD double)) |
|---|
| 156 |
$(TR $(TD long double) $(TD real)) |
|---|
| 157 |
$(TR $(TD _Imaginary float) $(TD ifloat)) |
|---|
| 158 |
$(TR $(TD _Imaginary double) $(TD idouble)) |
|---|
| 159 |
$(TR $(TD _Imaginary long double) $(TD ireal)) |
|---|
| 160 |
$(TR $(TD _Complex float) $(TD cfloat)) |
|---|
| 161 |
$(TR $(TD _Complex double) $(TD cdouble)) |
|---|
| 162 |
$(TR $(TD _Complex long double) $(TD creal)) |
|---|
| 163 |
) |
|---|
| 164 |
|
|---|
| 165 |
<h3>Limitations</h3> |
|---|
| 166 |
|
|---|
| 167 |
$(P There is no one to one correspondence of C declarations |
|---|
| 168 |
to D declarations. A review of the D module output will |
|---|
| 169 |
be necessary to ensure the right decisions are made. |
|---|
| 170 |
Furthermore: |
|---|
| 171 |
) |
|---|
| 172 |
|
|---|
| 173 |
$(OL |
|---|
| 174 |
$(LI Whereever |
|---|
| 175 |
practical, C headers should be written using $(B typedef)'s and |
|---|
| 176 |
$(B enum)'s rather than macros. |
|---|
| 177 |
$(B htod) will attempt to convert simple macro $(B #define)'s |
|---|
| 178 |
to $(B alias) and $(B const) declarations. |
|---|
| 179 |
Even so, macros are fully expanded before further analysis.) |
|---|
| 180 |
|
|---|
| 181 |
$(LI No attempt is made to convert C conditional compilation |
|---|
| 182 |
into D $(B version) or $(B static if) declarations.) |
|---|
| 183 |
|
|---|
| 184 |
$(LI No output is generated for false conditional compilation |
|---|
| 185 |
sections.) |
|---|
| 186 |
|
|---|
| 187 |
$(LI $(B htod) converts declarations only, it does not convert |
|---|
| 188 |
C code.) |
|---|
| 189 |
|
|---|
| 190 |
$(LI Declarations with C++ linkage cannot be converted. |
|---|
| 191 |
A C interface must be made for any C++ code.) |
|---|
| 192 |
|
|---|
| 193 |
$(LI C language extensions present in the C .h file |
|---|
| 194 |
may not be recognized.) |
|---|
| 195 |
|
|---|
| 196 |
$(LI Pragmas are not translated.) |
|---|
| 197 |
|
|---|
| 198 |
$(LI The tag names are assumed to not collide with |
|---|
| 199 |
names in the regular name space.) |
|---|
| 200 |
|
|---|
| 201 |
$(LI Any character data that is not ASCII will need |
|---|
| 202 |
to be converted as necessary to UTF.) |
|---|
| 203 |
|
|---|
| 204 |
$(LI The C $(B char) type is assumed to map to the |
|---|
| 205 |
D $(B char) type. However, these should be examined individually |
|---|
| 206 |
to see if they should instead be translated to $(B byte) or |
|---|
| 207 |
$(B ubyte) types. Whether the C $(B char) type is signed or |
|---|
| 208 |
unsigned is implementation defined. The D $(B char) type |
|---|
| 209 |
is unsigned.) |
|---|
| 210 |
|
|---|
| 211 |
$(LI Named C enum members are not inserted into the surrounding |
|---|
| 212 |
scope as they are in C.) |
|---|
| 213 |
|
|---|
| 214 |
$(LI D modules are each in their own name space, but C |
|---|
| 215 |
header files are all in the same global name space. This means |
|---|
| 216 |
that D references to names defined in other modules may |
|---|
| 217 |
need to be qualified.) |
|---|
| 218 |
) |
|---|
| 219 |
|
|---|
| 220 |
<h3>Bugs</h3> |
|---|
| 221 |
|
|---|
| 222 |
$(OL |
|---|
| 223 |
$(LI Anything other than the default struct member |
|---|
| 224 |
alignment is not accounted for.) |
|---|
| 225 |
|
|---|
| 226 |
$(LI No Linux version.) |
|---|
| 227 |
) |
|---|
| 228 |
|
|---|
| 229 |
) |
|---|
| 230 |
|
|---|
| 231 |
Macros: |
|---|
| 232 |
TITLE=htod |
|---|
| 233 |
WIKI=htod |
|---|