Skip to content

Commit

Permalink
Merge pull request #10801 from MoonlightSentinel/headers
Browse files Browse the repository at this point in the history
Add C++ header generation
  • Loading branch information
wilzbach authored Feb 21, 2020
2 parents 5b540c3 + d0d5f08 commit 44112eb
Show file tree
Hide file tree
Showing 19 changed files with 2,901 additions and 1 deletion.
104 changes: 104 additions & 0 deletions changelog/headers.dd
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.
2 changes: 1 addition & 1 deletion src/build.d
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ auto sourceFiles()
cli.d clone.d compiler.d complex.d cond.d constfold.d cppmangle.d cppmanglewin.d ctfeexpr.d
ctorflow.d dcast.d dclass.d declaration.d delegatize.d denum.d dimport.d dinifile.d
dinterpret.d dmacro.d dmangle.d dmodule.d doc.d dscope.d dstruct.d dsymbol.d dsymbolsem.d
dtemplate.d dversion.d env.d escape.d expression.d expressionsem.d func.d hdrgen.d impcnvtab.d
dtemplate.d dtoh.d dversion.d env.d escape.d expression.d expressionsem.d func.d hdrgen.d impcnvtab.d
imphint.d init.d initsem.d inline.d inlinecost.d intrange.d json.d lambdacomp.d lib.d libelf.d
libmach.d libmscoff.d libomf.d link.d mars.d mtype.d nogc.d nspace.d ob.d objc.d opover.d optimize.d
parse.d parsetimevisitor.d permissivevisitor.d printast.d safe.d sapply.d scanelf.d scanmach.d
Expand Down
9 changes: 9 additions & 0 deletions src/dmd/cli.d
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ dmd -cov -unittest myprog.d
Option("Hf=<filename>",
"write 'header' file to filename"
),
Option("HC",
"generate C++ 'header' file"
),
Option("HCd=<directory>",
"write C++ 'header' file to directory"
),
Option("HCf=<filename>",
"write C++ 'header' file to filename"
),
Option("-help",
"print help and exit"
),
Expand Down
Loading

0 comments on commit 44112eb

Please sign in to comment.