This repository has been archived by the owner on Sep 23, 2019. It is now read-only.
forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_interface.dd
601 lines (454 loc) · 11.8 KB
/
cpp_interface.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
Ddoc
$(SPEC_S Interfacing to C++,
$(P
While D is fully capable of
$(DDLINK interfaceToC, Interfacing to C, interfacing to C),
its ability to interface to C++ is much more limited.
There are three ways to do it:
)
$(OL
$(LI Use C++'s ability to create a C interface, and then
use D's ability to
$(DDLINK interfaceToC, Interfacing to C, interface with C)
to access that interface.
)
$(LI Use C++'s ability to create a COM interface, and then
use D's ability to
$(DPLLINK COM.html, interface with COM)
to access that interface.
)
$(LI Use the limited ability described here to connect
directly to C++ functions and classes.
)
)
$(H2 The General Idea)
$(P Being 100% compatible with C++ means more or less adding
a fully functional C++ compiler front end to D.
Anecdotal evidence suggests that writing such is a minimum
of a 10 man-year project, essentially making a D compiler
with such capability unimplementable.
Other languages looking to hook up to C++ face the same
problem, and the solutions have been:
)
$(OL
$(LI Support the COM interface (but that only works for Windows).)
$(LI Laboriously construct a C wrapper around
the C++ code.)
$(LI Use an automated tool such as SWIG to construct a
C wrapper.)
$(LI Reimplement the C++ code in the other language.)
$(LI Give up.)
)
$(P D takes a pragmatic approach that assumes a couple
modest accommodations can solve a significant chunk of
the problem:
)
$(UL
$(LI matching C++ name mangling conventions)
$(LI matching C++ function calling conventions)
$(LI matching C++ virtual function table layout for single inheritance)
)
$(H2 Calling C++ Global Functions From D)
$(P Given a C++ function in a C++ source file:)
$(CPPLISTING
#include $(LT)iostream$(GT)
using namespace std;
int foo(int i, int j, int k) {
cout << "i = " << i << endl;
cout << "j = " << j << endl;
cout << "k = " << k << endl;
return 7;
}
)
$(P In the corresponding D code, $(CODE foo)
is declared as having C++ linkage and function calling conventions:
)
------
extern (C++) int foo(int i, int j, int k);
------
$(P and then it can be called within the D code:)
------
extern (C++) int foo(int i, int j, int k);
void main() {
foo(1,2,3);
}
------
$(P Compiling the two files, the first with a C++ compiler,
the second with a D compiler, linking them together,
and then running it yields:)
$(CONSOLE
i = 1
j = 2
k = 3
)
$(P There are several things going on here:)
$(UL
$(LI D understands how C++ function names are "mangled" and the
correct C++ function call/return sequence.)
$(LI Because modules are not part of C++, each function with
C++ linkage must be globally unique within the program.)
$(LI There are no $(D __cdecl), $(D __far), $(D __stdcall), $(D __declspec), or other
such nonstandard C++ extensions in D.)
$(LI There are no volatile type modifiers in D.)
$(LI Strings are not 0 terminated in D. See "Data Type Compatibility"
for more information about this. However, string literals in D are
0 terminated.)
)
$(P C++ functions that reside in namespaces cannot be
direcly called from D.
)
$(H2 Calling Global D Functions From C++)
$(P To make a D function accessible from C++, give it
C++ linkage:)
---
import std.stdio;
extern (C++) int foo(int i, int j, int k) {
writefln("i = %s", i);
writefln("j = %s", j);
writefln("k = %s", k);
return 1;
}
extern (C++) void bar();
void main() {
bar();
}
---
$(P The C++ end looks like:)
$(CPPLISTING
int foo(int i, int j, int k);
void bar() {
foo(6, 7, 8);
}
)
$(P Compiling, linking, and running produces the output:)
$(CONSOLE
i = 6
j = 7
k = 8
)
$(H2 Classes)
$(P D classes are singly rooted by Object, and have an
incompatible layout from C++ classes.
D interfaces, however, are very similar to C++ single
inheritance class heirarchies.
So, a D interface with the attribute of $(CODE extern (C++))
will have a virtual function pointer table (vtbl[]) that
exactly matches C++'s.
A regular D interface has a vtbl[] that differs in that
the first entry in the vtbl[] is a pointer to D's RTTI info,
whereas in C++ the first entry points to the first virtual
function.
)
$(H2 Calling C++ Virtual Functions From D)
$(P Given C++ source code defining a class like:)
$(CPPLISTING
#include $(LT)iostream$(GT)
using namespace std;
class D {
public:
virtual int bar(int i, int j, int k)
{
cout << "i = " << i << endl;
cout << "j = " << j << endl;
cout << "k = " << k << endl;
return 8;
}
};
D *getD() {
D *d = new D();
return d;
}
)
$(P We can get at it from D code like:)
---
extern (C++) {
interface D {
int bar(int i, int j, int k);
}
D getD();
}
void main() {
D d = getD();
d.bar(9,10,11);
}
---
$(H2 Calling D Virtual Functions From C++)
$(P Given D code like:)
---
extern (C++) int callE(E);
extern (C++) interface E {
int bar(int i, int j, int k);
}
class F : E {
extern (C++) int bar(int i, int j, int k)
{
writefln("i = ", i);
writefln("j = ", j);
writefln("k = ", k);
return 8;
}
}
void main() {
F f = new F();
callE(f);
}
---
$(P The C++ code to access it looks like:)
$(CPPLISTING
class E {
public:
virtual int bar(int i, int j, int k);
};
int callE(E *e) {
return e->bar(11,12,13);
}
)
$(P Note:)
$(UL
$(LI non-virtual functions, and static member functions,
cannot be accessed.)
$(LI class fields can only be accessed via virtual getter
and setter methods.)
)
$(H2 Function Overloading)
$(P C++ and D follow different rules for function overloading.
D source code, even when calling $(CODE extern (C++)) functions,
will still follow D overloading rules.
)
$(H2 Storage Allocation)
$(P C++ code explicitly manages memory with calls to
$(CODE ::operator new()) and $(CODE ::operator delete()).
D allocates memory using the D garbage collector,
so no explicit delete's are necessary.
D's new and delete are not compatible with C++'s
$(CODE ::operator new) and $(CODE::operator delete).
Attempting to allocate memory with C++ $(CODE ::operator new)
and deallocate it with D's $(CODE delete), or vice versa, will
result in miserable failure.
)
$(P D can still explicitly allocate memory using std.c.stdlib.malloc()
and std.c.stdlib.free(), these are useful for connecting to C++
functions that expect malloc'd buffers, etc.
)
$(P If pointers to D garbage collector allocated memory are passed to
C++ functions, it's critical to ensure that that memory will not
be collected by the garbage collector before the C++ function is
done with it. This is accomplished by:
)
$(UL
$(LI Making a copy of the data using std.c.stdlib.malloc() and passing
the copy instead.)
$(LI Leaving a pointer to it on the stack (as a parameter or
automatic variable), as the garbage collector will scan the stack.)
$(LI Leaving a pointer to it in the static data segment, as the
garbage collector will scan the static data segment.)
$(LI Registering the pointer with the garbage collector with the
std.gc.addRoot() or std.gc.addRange() calls.)
)
$(P An interior pointer to the allocated memory block is sufficient
to let the GC
know the object is in use; i.e. it is not necessary to maintain
a pointer to the beginning of the allocated memory.
)
$(P The garbage collector does not scan the stacks of threads not
created by the D Thread interface. Nor does it scan the data
segments of other DLL's, etc.
)
$(H2 Data Type Compatibility)
$(TABLE2 D And C Type Equivalence,
$(THEAD D type, C type)
$(TROW
$(ARGS $(B void)),
$(ARGS $(B void))
)
$(TROW
$(ARGS $(B byte)),
$(ARGS $(B signed char))
)
$(TROW
$(ARGS $(B ubyte)),
$(ARGS $(B unsigned char))
)
$(TROW
$(ARGS $(B char)),
$(ARGS $(B char) (chars are unsigned in D))
)
$(TROW
$(ARGS $(B wchar)),
$(ARGS $(D wchar_t) (when $(D sizeof(wchar_t)) is 2))
)
$(TROW
$(ARGS $(B dchar)),
$(ARGS $(D wchar_t) (when $(D sizeof(wchar_t)) is 4))
)
$(TROW
$(ARGS $(B short)),
$(ARGS $(B short))
)
$(TROW
$(ARGS $(B ushort)),
$(ARGS $(B unsigned short))
)
$(TROW
$(ARGS $(B int)),
$(ARGS $(B int))
)
$(TROW
$(ARGS $(B uint)),
$(ARGS $(B unsigned))
)
$(TROW
$(ARGS $(B long)),
$(ARGS $(B long long))
)
$(TROW
$(ARGS $(B ulong)),
$(ARGS $(B unsigned long long))
)
$(TROW
$(ARGS $(B float)),
$(ARGS $(B float))
)
$(TROW
$(ARGS $(B double)),
$(ARGS $(B double))
)
$(TROW
$(ARGS $(B real)),
$(ARGS $(B long double))
)
$(TROW
$(ARGS $(B ifloat)),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(B idouble)),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(B ireal)),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(B cfloat)),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(B cdouble)),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(B creal)),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(B struct)),
$(ARGS $(B struct))
)
$(TROW
$(ARGS $(B union)),
$(ARGS $(B union))
)
$(TROW
$(ARGS $(B enum)),
$(ARGS $(B enum))
)
$(TROW
$(ARGS $(B class)),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(I type)$(B *)),
$(ARGS $(I type) $(B *))
)
$(TROW
$(ARGS no equivalent),
$(ARGS $(I type) $(CODE_AMP))
)
$(TROW
$(ARGS $(I type)$(B [)$(I dim)$(B ])),
$(ARGS $(I type)$(B [)$(I dim)$(B ]))
)
$(TROW
$(ARGS $(I type)$(B [)$(I dim)$(B ]*)),
$(ARGS $(I type)$(B (*)[)$(I dim)$(B ]))
)
$(TROW
$(ARGS $(I type)$(B [])),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(I type)$(B [)$(I type)$(B ])),
$(ARGS no equivalent)
)
$(TROW
$(ARGS $(I type) $(B function)$(B $(LPAREN))$(I parameters)$(B $(RPAREN))),
$(ARGS $(I type)$(B (*))$(B $(LPAREN))$(I parameters)$(B $(RPAREN)))
)
$(TROW
$(ARGS $(I type) $(B delegate)$(B $(LPAREN))$(I parameters)$(B $(RPAREN))),
$(ARGS no equivalent)
)
)
$(P These equivalents hold for most 32 bit C++ compilers.
The C++ standard
does not pin down the sizes of the types, so some care is needed.
)
$(H2 Structs and Unions)
$(P D structs and unions are analogous to C's.
)
$(P C code often adjusts the alignment and packing of struct members
with a command line switch or with various implementation specific
$(HASH)pragma's. D supports explicit alignment attributes that correspond
to the C compiler's rules. Check what alignment the C code is using,
and explicitly set it for the D struct declaration.
)
$(P D does not support bit fields. If needed, they can be emulated
with shift and mask operations.
$(DPLLINK htod.html, htod) will convert bit fields to inline functions that
do the right shift and masks.
)
$(H2 Object Construction and Destruction)
$(P Similarly to storage allocation and deallocation, objects
constructed in D code should be destructed in D,
and objects constructed
in C++ should be destructed in C++ code.
)
$(H2 Special Member Functions)
$(P D cannot call C++ special member functions, and vice versa.
These include constructors, destructors, conversion operators,
operator overloading, and allocators.
)
$(H2 Runtime Type Identification)
$(P D runtime type identification
uses completely different techniques than C++.
The two are incompatible.)
$(H2 C++ Class Objects by Value)
$(P D can access POD (Plain Old Data) C++ structs, and it can
access C++ class virtual functions by reference.
It cannot access C++ classes by value.
)
$(H2 C++ Templates)
$(P D templates have little in common with C++ templates,
and it is very unlikely that any sort of reasonable method
could be found to express C++ templates in a link-compatible
way with D.
)
$(P This means that the C++ STL, and C++ Boost, likely will
never be accessible from D.
)
$(H2 Exception Handling)
$(P D and C++ exception handling are completely different.
Throwing exceptions across the boundaries between D
and C++ code will likely not work.
)
$(H2 Future Developments)
$(P How the upcoming C++0x standard will affect this is not
known.)
$(P Over time, more aspects of the C++ ABI may be accessible
directly from D.)
)
Macros:
TITLE=Interfacing to C++
WIKI=InterfaceToCPP
CATEGORY_SPEC=$0