forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htomodule.dd
434 lines (337 loc) · 7.96 KB
/
htomodule.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
Ddoc
$(D_S Converting C $(D .h) Files to D Modules,
While D cannot directly compile C source code, it can easily
interface to C code, be linked with C object files, and call
C functions in DLLs.
The interface to C code is normally found in C $(D .h) files.
So, the trick to connecting with C code is in converting C
$(D .h) files to D modules.
This turns out to be difficult to do mechanically since
inevitably some human judgement must be applied.
This is a guide to doing such conversions.
$(H4 Preprocessor)
$(D .h) files can sometimes be a bewildering morass of layers of
macros, $(D #include) files, $(D #ifdef)'s, etc. D doesn't
include a text preprocessor like the C preprocessor,
so the first step is to remove the need for
it by taking the preprocessed output. For DMC (the Digital
Mars C/C++ compiler), the command:
$(CONSOLE
<a href="http://www.digitalmars.com/ctg/sc.html">dmc</a> <a href="http://www.digitalmars.com/ctg/sc.html#dashc">-c</a> program.h <a href="http://www.digitalmars.com/ctg/sc.html#dashe">-e</a> <a href="http://www.digitalmars.com/ctg/sc.html#dashl">-l</a>
)
will create a file $(D program.lst) which is the source file after
all text preprocessing.
$(P Remove all the $(D #if), $(D #ifdef), $(D #include),
etc. statements.)
$(H4 Linkage)
Generally, surround the entire module with:
---------------------------
extern (C)
{
/* ...file contents... */
}
---------------------------
to give it C linkage.
$(H4 Types)
$(P A little global search and replace will take care of renaming
the C types to D types. The following table shows a typical mapping
for 32 bit C code:)
$(TABLE1
<caption>Mapping C type to D type</caption>
<tr>
<th>C type
<th>D type
<tr>
<td>long double
<td>real
<tr>
<td>unsigned long long
<td>ulong
<tr>
<td>long long
<td>long
<tr>
<td>unsigned long
<td>uint
<tr>
<td>long
<td>int
<tr>
<td>unsigned
<td>uint
<tr>
<td>unsigned short
<td>ushort
<tr>
<td>signed char
<td>byte
<tr>
<td>unsigned char
<td>ubyte
<tr>
<td>wchar_t
<td>wchar or dchar
<tr>
<td>bool
<td>bool, byte, int
<tr>
<td>size_t
<td>size_t
<tr>
<td>ptrdiff_t
<td>ptrdiff_t
)
$(H4 NULL)
$(D NULL) and $(D ((void*)0)) should be replaced
with $(D null).
$(H4 Numeric Literals)
Any $(SINGLEQUOTE L) or $(SINGLEQUOTE l) numeric literal suffixes should be removed,
as a C $(D long) is (usually) the same size as a D $(D int).
Similarly, $(SINGLEQUOTE LL) suffixes should be replaced with a
single $(SINGLEQUOTE L).
Any $(SINGLEQUOTE u) suffix will work the same in D.
$(H4 String Literals)
In most cases, any $(SINGLEQUOTE L) prefix to a string can just be dropped,
as D will implicitly convert strings to wide characters if
necessary. However, one can also replace:
$(CCODE
L"string"
)
with:
---------------------------
"string"w // for 16 bit wide characters
"string"d // for 32 bit wide characters
---------------------------
$(H4 Macros)
Lists of macros like:
$(CCODE
#define FOO 1
#define BAR 2
#define ABC 3
#define DEF 40
)
can be replaced with:
---------------------------
enum
{ FOO = 1,
BAR = 2,
ABC = 3,
DEF = 40
}
---------------------------
or with:
---------------------------
const int FOO = 1;
const int BAR = 2;
const int ABC = 3;
const int DEF = 40;
---------------------------
Function style macros, such as:
$(CCODE
#define MAX(a,b) ((a) < (b) ? (b) : (a))
)
can be replaced with functions:
---------------------------
int MAX(int a, int b) { return (a < b) ? b : a; }
---------------------------
<!-- Thanks to Jarrett Billingsley for the following tip -->
The functions, however, won't work if they appear inside static
initializers that must be evaluated at compile time rather than
runtime. To do it at compile time, a template can be used:
$(CCODE
#define GT_DEPTH_SHIFT (0)
#define GT_SIZE_SHIFT (8)
#define GT_SCHEME_SHIFT (24)
#define GT_DEPTH_MASK (0xffU << GT_DEPTH_SHIFT)
#define GT_TEXT ((0x01) << GT_SCHEME_SHIFT)
/* Macro that constructs a graphtype */
#define GT_CONSTRUCT(depth,scheme,size) \
((depth) | (scheme) | ((size) << GT_SIZE_SHIFT))
/* Common graphtypes */
#define GT_TEXT16 GT_CONSTRUCT(4, GT_TEXT, 16)
)
The corresponding D version would be:
---------------------------
const uint GT_DEPTH_SHIFT = 0;
const uint GT_SIZE_SHIFT = 8;
const uint GT_SCHEME_SHIFT = 24;
const uint GT_DEPTH_MASK = 0xffU << GT_DEPTH_SHIFT;
const uint GT_TEXT = 0x01 << GT_SCHEME_SHIFT;
// Template that constructs a graphtype
template GT_CONSTRUCT(uint depth, uint scheme, uint size)
{
// notice the name of the const is the same as that of the template
const uint GT_CONSTRUCT = (depth | scheme | (size << GT_SIZE_SHIFT));
}
// Common graphtypes
const uint GT_TEXT16 = GT_CONSTRUCT!(4, GT_TEXT, 16);
---------------------------
$(H4 Declaration Lists)
D doesn't allow declaration lists to change the type.
Hence:
$(CCODE
int *p, q, t[3], *s;
)
should be written as:
---------------------------
int* p, s;
int q;
int[3] t;
---------------------------
$(H4 Void Parameter Lists)
Functions that take no parameters:
$(CCODE
int foo(void);
)
are in D:
---------------------------
int foo();
---------------------------
$(H4 Extern Global C Variables)
Whenever a global variable is declared in D, it is also defined.
But if it's also defined by the C object file being linked in,
there will be a multiple definition error. To fix this problem,
use the extern storage class.
For example, given a C header file named
$(D foo.h):
$(CCODE
struct Foo { };
struct Foo bar;
)
It can be replaced with the D modules, $(D foo.d):
---------------------------
struct Foo { }
extern (C)
{
extern Foo bar;
}
---------------------------
$(H4 Typedef)
$(D alias) is the D equivalent to the C $(D typedef):
$(CCODE
typedef int foo;
)
becomes:
---------------------------
alias int foo;
---------------------------
$(H4 Structs)
Replace declarations like:
$(CCODE
typedef struct Foo
{ int a;
int b;
} Foo, *pFoo, *lpFoo;
)
with:
---------------------------
struct Foo
{ int a;
int b;
}
alias Foo* pFoo, lpFoo;
---------------------------
$(H4 Struct Member Alignment)
A good D implementation by default will align struct members the
same way as the C compiler it was designed to work with. But
if the $(D .h) file has some $(D #pragma)'s to control alignment, they
can be duplicated with the D $(D align) attribute:
$(CCODE
#pragma pack(1)
struct Foo
{
int a;
int b;
};
#pragma pack()
)
becomes:
---------------------------
struct Foo
{
align (1):
int a;
int b;
}
---------------------------
$(H4 Nested Structs)
$(CCODE
struct Foo
{
int a;
struct Bar
{
int c;
} bar;
};
struct Abc
{
int a;
struct
{
int c;
} bar;
};
)
becomes:
---------------------------
struct Foo
{
int a;
struct Bar
{
int c;
}
Bar bar;
}
struct Abc
{
int a;
struct
{
int c;
}
}
---------------------------
$(H4 $(D __cdecl), $(D __pascal), $(D __stdcall))
$(CCODE
int __cdecl x;
int __cdecl foo(int a);
int __pascal bar(int b);
int __stdcall abc(int c);
)
becomes:
---------------------------
extern (C) int x;
extern (C) int foo(int a);
extern (Pascal) int bar(int b);
extern (Windows) int abc(int c);
---------------------------
$(H4 $(D __declspec(dllimport)))
$(CCODE
__declspec(dllimport) int __stdcall foo(int a);
)
becomes:
---------------------------
export extern (Windows) int foo(int a);
---------------------------
$(H4 $(D __fastcall))
Unfortunately, D doesn't support the $(D __fastcall) convention.
Therefore, a shim will be needed, either written in C:
$(CCODE
int __fastcall foo(int a);
int myfoo(int a)
{
return foo(int a);
}
)
and compiled with a C compiler that supports $(D __fastcall) and
linked in, or compile the above, disassemble it with
<a href="http://www.digitalmars.com/ctg/obj2asm.html">obj2asm</a>
and insert it in a D $(D myfoo) shim with
<a href="iasm.html">inline assembler</a>.
)
Macros:
TITLE=Converting C .h Files to D Modules
WIKI=HToModule
CATEGORY_HOWTOS=$0