-
-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
132 additions
and
9 deletions.
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,45 @@ | ||
C files can now include a module statement | ||
|
||
Similar to the `__import` extension, the `__module` keyword brings D's module declaration to C. | ||
|
||
It's particularly useful when you want to import multiple C files with the same name | ||
(e.g. hello/utils.c and world/utils.c), since both have to be imported with `import utils` when | ||
they are listed on the command line, resulting in conflicts. | ||
|
||
Now you can do: | ||
|
||
hello/utils.c: | ||
|
||
```C | ||
|
||
#if __IMPORTC__ | ||
__module hello.utils; | ||
#endif | ||
|
||
int sqr(int x) { return x * x; } | ||
``` | ||
|
||
world/utils.c: | ||
```C | ||
|
||
#if __IMPORTC__ | ||
__module world.utils; | ||
#endif | ||
|
||
int max(int a, int b) { return a > b ? a : b; } | ||
``` | ||
|
||
app.d: | ||
```D | ||
import hello.utils; | ||
import world.utils; | ||
|
||
static assert(sqr(3) == 9); | ||
static assert(max(3, 5) == 5); | ||
``` | ||
|
||
A __module declaration can appear anywhere in the top level scope. | ||
When there are multiple, the first one will be used. | ||
Therefore, every `#include` containing a __module declaration should come after the file's own module declaration, | ||
or it will be overwritten. | ||
When you always put the __module declaration at the very top like in D, there won't be such problems. |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
EXTRA_SOURCES: imports/cpkg/cmodule.c | ||
*/ | ||
|
||
import imports.cpkg.cmodule; | ||
|
||
static assert(sqr(3) == 9); | ||
|
||
void main() {} |
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,13 @@ | ||
// D module declaration | ||
|
||
#if __IMPORTC__ | ||
|
||
__module imports.cpkg.cmodule; | ||
|
||
// Only the first module statement is used, | ||
// subsequent __module declarations are assumed to come from #included other files | ||
__module some.header; | ||
|
||
#endif | ||
|
||
int sqr(int i) { return i * i; } |
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,31 @@ | ||
/** | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/cmodule_malformed.c(15): Error: identifier expected following `module` | ||
fail_compilation/cmodule_malformed.c(15): Error: no type for declarator before `"a"` | ||
fail_compilation/cmodule_malformed.c(21): Error: no type-specifier for struct member | ||
fail_compilation/cmodule_malformed.c(21): Error: identifier or `(` expected | ||
fail_compilation/cmodule_malformed.c(21): Error: expected identifier for declarator | ||
fail_compilation/cmodule_malformed.c(26): Error: found `__module` instead of statement | ||
--- | ||
*/ | ||
|
||
#if __IMPORTC__ | ||
|
||
__module "a"; | ||
|
||
typedef struct S | ||
{ | ||
int x; | ||
|
||
__module b; | ||
} S; | ||
|
||
void main(void) | ||
{ | ||
__module c.d; | ||
} | ||
|
||
__module e; | ||
|
||
#endif |