-
-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10801 from MoonlightSentinel/headers
Add C++ header generation
- Loading branch information
Showing
19 changed files
with
2,901 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
Added experimental `C++` header generation from `extern(C|C++)` declarations | ||
|
||
DMD can now write C++ header files which contain bindings for declarations | ||
in existing D files that were marked as `extern(C)` or `extern(C++)`. | ||
|
||
This feature is available via the following command line switches: | ||
- `-HC`: Write the generated headers to standard output | ||
- `-HCf=<file>`: Write the generated headers to file | ||
- `-HCd=<directory>`: Write the generated headers to file (not implemented yet) | ||
|
||
Consider the following example: | ||
|
||
``` | ||
module a; | ||
|
||
extern(C) int foo(int a) { ... } | ||
extern(C++) void bar() { ... } | ||
void ignored() { ... } | ||
``` | ||
``` | ||
module b; | ||
|
||
extern (C++) struct S | ||
{ | ||
string name; | ||
this (string name) { ... } | ||
bool bar() { ... } | ||
} | ||
``` | ||
``` | ||
module c; | ||
|
||
import a, b; | ||
|
||
extern (C++) class C | ||
{ | ||
S[] s; | ||
this () {} | ||
bool bar() { ...} | ||
} | ||
|
||
``` | ||
|
||
Compiling these modules with `dmd -c -o- -HC a.d b.d c.d` will generate the following header: | ||
|
||
``` | ||
#pragma once | ||
|
||
// Automatically generated by dmd -HC | ||
|
||
#include <assert.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define _d_void void | ||
#define _d_bool bool | ||
#define _d_byte signed char | ||
#define _d_ubyte unsigned char | ||
#define _d_short short | ||
#define _d_ushort unsigned short | ||
#define _d_int int | ||
#define _d_uint unsigned | ||
#define _d_long long long | ||
#define _d_ulong unsigned long long | ||
#define _d_float float | ||
#define _d_double double | ||
#define _d_real long double | ||
#define _d_char char | ||
#define _d_wchar wchar_t | ||
#define _d_dchar unsigned | ||
typedef _d_long d_int64; | ||
#define _d_null NULL | ||
|
||
// Parsing module a | ||
// Parsing module b | ||
// Parsing module c | ||
struct S; | ||
extern "C" _d_int foo(_d_int a); | ||
|
||
extern _d_void bar(); | ||
|
||
// ignoring function a.ignored because of linkage | ||
struct S | ||
{ | ||
DArray< _d_char > name; | ||
S(DArray< _d_char > name); | ||
_d_bool bar(); | ||
S() : name() {} | ||
}; | ||
|
||
class C | ||
{ | ||
public: | ||
DArray< S > s; | ||
C(); | ||
virtual _d_bool bar(); | ||
}; | ||
``` | ||
|
||
Refer to the $(LINK2 $(ROOT_DIR)/spec/cpp_interface.html, documentation) for further information regarding C++ interoperation. | ||
|
||
Note that this feature is considered experimental and might not work correctly sometimes. | ||
Please open an issue in the $(LINK2 https://issues.dlang.org, bug tracker) if you encounter a bug while using this feature. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.