Mixin C

Tim tim.dlang at t-online.de
Fri Mar 8 17:06:21 UTC 2024


On Friday, 8 March 2024 at 03:23:12 UTC, Paul Backus wrote:
> What if instead of importing C files like D modules, we could 
> write bits of C code directly in the middle of our D code, like 
> we do with inline ASM?

Are multiple mixin(C) blocks evaluated in the same context? 
Symbols and macros from one mixin(C) block could then be used in 
another mixin(C) block, like in this example:
```D
mixin(C) {
#include <stdio.h>
};
void main()
{
     mixin(C) {
         printf("test\n");
     }
}
```
The preprocessor call for the second block would need to know all 
macros from the first call.

Can code in mixin(C) statements access local variables from D? 
How would name conflicts be resolved when an identifier exists 
both in the current module and a C header file? In the following 
example `BUFSIZ` is both a local variable and a macro from a C 
header:
```D
void main()
{
     int BUFSIZ = 5;
     mixin(C) {
         #include <stdio.h>
         printf("BUFSIZ = %d\n", BUFSIZ);
     }
}
```

Are variables declared in mixin(C) statements interpreted as 
global or local variables?
```D
void main()
{
     mixin(C) {
         #include <stdio.h>
         fprintf(stderr, "test\n");
     }
}
```
The header declares variable `stderr`. If this is now a local 
variable, because the header is included inside a function, it 
could cause problems. Maybe this could be solved by treating C 
variables marked with `extern` as globals.



More information about the dip.ideas mailing list